

function ObjectManager() {
	this.avURL = null;
	this.objectName = null;
	this.attributes = null;
	this.columnAttrs = null;
	this.viewAttr = null;
	this.selectedRow = null;
	this.isSearchable = true;
	this.paginator = null;
	this.entityViewer = null;
}

ObjectManager.prototype = {
   
    initialize: function(url, objectName, attributes, columnAttrs, viewAttr) {
	
	  this.avURL = url;
	  this.objectName = objectName;
	  this.attributes = attributes;	  
	  this.columnAttrs = columnAttrs;
	  this.viewAttr = viewAttr;
	
	  if (viewAttr != null) {
	    this.initDetailDialog();
	  }
    },
    
    
    setSearchable: function (isSearchable) {
    	this.isSearchable = isSearchable;
    },
    
    setPaginator: function (paginator) {
  		this.paginator = paginator;
    },
		
    setEntityViewer: function(viewerFunc) {
    	this.entityViewer = viewerFunc;
    	this.initDetailDialog();
    },
    
    getProductPaginator: function(categoryId, pageSize) {
    	
    	  this.paginator = new Paginator();
    	  $j(document).ready(function() {

    		  product.search("category="+categoryId);
    			
    			paginator.initialize(null,pageSize);

    			product.setPaginator(paginator); 
    			

    		});

          return this.paginator;

    },

    getPressPaginator: function(contextPath, pageSize) {
    	
  	  this.paginator = new Paginator();
  	  
  	  $j(document).ready(function() {
  		
  		  product.search();
  			
  		  paginator.initialize(null,pageSize);
          paginator.setContextPath(contextPath);
  		  product.setPaginator(paginator); 
  			

  		});

        return this.paginator;

  },
    
    getId : function(id) {
       return this.objectName+"-"+id;	
    },
    
	search : function(filter) {
    	
    	if (!this.isSearchable) {
    		return;
    	}
    	
	    var thisObject = this;
	    
	    var url = this.avURL+"?action=get&object="+this.objectName;
	    
	    if (filter != null) {
	    	url = url + "&" + filter;
	    }
	    
		jQuery.getJSON(url, function(objects) {
			
			if (objects['ERROR'] && objects['ERROR'] != 'undefined') {
				alert(objects);
			} else {				
				//alert(objects);				
				if (thisObject.paginator != null) {
				  thisObject.paginator.setEntities(objects);
				  return;
				}
				
				removeListItems(thisObject.objectName+"ListT");

				var userListT = document.getElementById(thisObject.objectName+"ListT");
				var bodyElem = document.createElement("tbody");
				bodyElem.setAttribute("id", thisObject.objectName+"ListTB");

				userListT.appendChild(bodyElem);

				if (objects != null) {
					
				  for (i = 0; i < objects.length; i++) {					  
					var rowElem = document.createElement("tr");
					
					rowElem.setAttribute("id", thisObject.getId(objects[i]['id']));					
					rowElem.className = "form-row";
					rowElem.setAttribute("onclick",thisObject.objectName+".selectRow(this);");
					//FOR IE
					rowElem.onclick = function() {thisObject.selectRow(this)};
					
					for (j=0; j<thisObject.columnAttrs.length; j++) {
					  var attr = thisObject.columnAttrs[j];
						
					  var data = null; 
					  if (thisObject.viewAttr != null && attr == thisObject.viewAttr) {
					      
						  data = "<a href='javascript:void(0);' onclick='"+thisObject.objectName+".view("
						        	+ objects[i]['id'] + ");'>" + objects[i][attr] + "</a>";
						  
					  }
					  else if (attr == "image") {
						  data = "<a href='../"+objects[i][attr]+"'><img src='../img/wcrown.jpg'/></a>";
					  }
					  else {
						  data = objects[i][attr];
					  }
					  createElement(rowElem, "td", data);					  
					}
					
					if (thisObject.objectName == "product") {
				    	data = "<a href='javascript:void(0);' onclick='displayPrice("+objects[i]['id']+")'>Price</a>";
				    	createElement(rowElem, "td", data);
				    }
					
					var link = "<a href='javascript:void(0);' onclick='"+thisObject.objectName+".remove("
							+ objects[i]['id'] + ");'>Delete</a>";
					createElement(rowElem, "td", link);
					bodyElem.appendChild(rowElem);
				  }

				}
			}			    
		});
	},
	
	selectRow : function(tableRow) {	
		
		if (this.selectedRow != null && tableRow == this.selectedRow) {
			return;
		}
		
	  	tableRow.className = "form-row form-row-selected";
	  	
	  	if (this.selectedRow != null) {
	  		this.selectedRow.className = "";
	  	}	
	  	this.selectedRow = tableRow;
	  	
	},
	
	save : function() {
		
		var objectForm = document.forms[this.objectName+"Form"];
        
		if (!this.isValid(objectForm)) {
			// return;
		}

		var map = {
	
		};
		
		id = objectForm['id'].value;
		if (id && id != "") {
		   map["id"] = id;
		}
		
		for (j=0; j<this.attributes.length; j++) {
			var attr = this.attributes[j];
			
			map[attr] = objectForm[attr].value;
		}
		
		var thisObject = this;
		
		jQuery.post(this.avURL+"?action=asave&object="+this.objectName,
						map,
						function(result) {
			               
			               if (result && typeof (result) == "string" && result.indexOf("{\"ERROR\"",0) >= 0) {
			            	  alert(result);
			              }	 
			              else {	
			            	  if (thisObject.isSearchable) {
			            	    thisObject.refresh();
			            	  }
			            	  else {
			            		thisObject.setEntityId(result);  
			            	  }
			              }
						});
	},

	
	refresh : function() {
	  	this.hideDetailDialog();
	  	this.search();
		
	},
	
	remove : function(objectId) {
		
		if (!confirm("Do you really want to delete?")) { 
           return;
    	}		 
		
		var thisObject = this;
		jQuery.get(this.avURL+"?action=delete&object="+this.objectName+"&id="+objectId,function(result) {
			
			if (result['ERROR'] &&result['ERROR'] != 'undefined') {
				alert(result);
			}
			else {	
			  thisObject.search();
            }
		});
	},
	
	isValid : function(pressForm) {
	},
	
	add : function() {		
	  this.clearForm();	
   	  this.openDetailDialog();
	},
	
	view : function(objectId) {
		
	  this.openDetailDialog();
	  
	  var thisObject = this;
	  
	  jQuery.getJSON(this.avURL+"?action=get&object="+this.objectName+"&id="+objectId, function(object) {
		  if (object['ERROR'] && object['ERROR'] != 'undefined') {
				alert(object);
			} else {
				if (thisObject.entityViewer == null) {
				  thisObject.populateForm(object);
				}
				else {
					$j(function() {
	    				thisObject.entityViewer(object);
	    			});	
				}
			}
	  });
	  
	},
	
	setEntityId : function(id) {
		var objectForm = document.forms[this.objectName+"Form"];
		
		objectForm['id'].value =id;
	},
	
	populateForm : function(object) {
		var objectForm = document.forms[this.objectName+"Form"];
		objectForm['id'].value = object['id'];
		
		for (j=0; j<this.attributes.length; j++) {
			var attr = this.attributes[j];
			
			if (attr != "image") {
			
			  if (attr == 'state') {
				  for (i=0; i<objectForm[attr].options.length; i++) {

					  if (object[attr] == objectForm[attr].options[i].value) {
						  objectForm[attr].options[i].selected = 'selected';
					  }
				  }
			  }
			  else {	
   		        objectForm[attr].value = object[attr];
			  }
			}
		}
	},
	
	clearForm : function() {
		var objectForm = document.forms[this.objectName+"Form"];
		
		for (attr in this.attributes) {
			objectForm[attr].value = "";
		}
	},
	
	initDetailDialog : function() {
		$j("#"+this.objectName+"Detail").dialog({
		    bgiframe: true,
		    autoOpen: false,
		    width:600,
		    modal: true});
	},
	
	openDetailDialog : function() {
		$j("#"+this.objectName+"Detail").dialog('open');
	},
	
	hideDetailDialog : function() {
		$j("#"+this.objectName+"Detail").dialog('close');
	}
	
}

function getPaginator(entities, contextPath, pageSize) {
	var paginator = new Paginator();
	paginator.initialize(entities,pageSize);
    paginator.setContextPath(contextPath);
    paginator.setDetailURL("pressd.php", "image");
    return paginator;
}

function createArray(str, delim, imgDir) {
	
	var strs = str.split(delim);
	var entities = new Array();
	
	for (i=0; i<strs.length; i++) {
		var image = new Array();
		if (strs[i] != "") {
			
		  if (imgDir)	{
			  image['image'] = imgDir+"/"+strs[i];
		  }
		  else {
 	        image['image'] = strs[i];
		  }
	      entities[i] = image;
		} 
    }
	
	return entities;
}

