
Ajax.Responders.register({
	onException: function(aj, ex) {
		try {
			log("Exception Occured\nError name: " + ex.name + "\nError message: " + ex.message, true);
			log(ex);
			alert("Exception Occured\nError name: " + ex.name + "\nError message: " + ex.message);
			//log(ex);
		} catch(e) {}
		throw ex;
	}
});

var LibraryCategory = Class.create({
	CLASSDEF: {
		name: 'LibraryCategory'
	},
	
	initialize: function(library, id, name, parent, options) {
		this.library = library;
		this.id = id;
		this.uId = library.getNextId(); //we need a unique id because the same cat can appear twice..
		this.name = name;
		this.parent = parent;
		this.children = new MapList(this);
		this.expanded = (options.expand == true);
		this.hasChildren = options.hc;
		this.assetCount = options.ac == null? 0 : options.ac;
		this.directAssetCount = options.dc == null? 0 : options.dc;
		this.parentCount = options.pc;
		this.nonDecCount = options.dl == null? 0 : options.dl;
		this.images = new MapList(this);
		this.selected = false;
		this.childrenLoadingCleared = false;
		
		library.registerCategory(this);
		this.buildElement();
		if(options.c != null) {
			
			for(var i=0; i < options.c.length; i++) {
				var catOpt = options.c[i];
				this.addCat(catOpt.id, catOpt.n, catOpt.o);
			}
		} else {
			this.childrenLoaded = false;
		}
	},
	
	addCat: function(id, name, options) {
		log("LibraryCategory.addCat(" + name + ")");
		this.addCatObject(new LibraryCategory(this.library, id, name, this, options));
	},
	
	//remove the "loading..." li..
	clearCatLoadingMessage: function() {
		if(!this.childrenLoadingCleared) {
			this.getChildrenListElement().innerHTML = "";
			this.childrenLoadingCleared = true;
		}
	},
	
	addCatObject: function(cat) {
		if(!this.childrenLoaded) {
			this.childrenLoadCalled = true;
			this.childrenLoaded = true;
		}
		if(!this.hasChildren) { //we have added children thru the interface...
			this.hasChildren = true;
			this.checkExpander();
		}
		if(this.children.byId[cat.id] != null) {
			return this.children.byId[cat.id]; 
		}
		return this.children.add(cat);
	},
	
	//check if we should be showing the expander...
	checkExpander: function() {
		if(this.expanderEl != null) {
			if(this.hasChildren) {
				//this.expanderEl.style.visibility="visible";
				this.expanderEl.className="open";
			} else {
				//this.expanderEl.style.visibility="hidden";
				//this.childrenEl.style.display="none";
        if(this.childrenEl!=null) { //this is needed to catch when the server thinks the cat has children but it actually doesnt 
          this.childrenEl.hide();
        }
				this.expanderEl.className="null";
			}
		}
	},
	
	//called from move/delete.. check if has_children still applies...assumes the element has already been removed..
	removeCatObject: function(cat) {
		this.changeImageCount(0-cat.assetCount, false); //deduct asset count 
		this.children.remove(cat.id);
		if(this.children.list.size() == 0) {
			this.hasChildren = false;
			this.checkExpander();
		}
	},
	
	buildElement: function() {
		log("buildElement: this.expanded=" + this.expanded);
		var style = this.expanded ? '' : ' style="display:none;"';
		
		//var expandElClass = this.expanded ? 'icon cat_expander_expanded' : 'icon cat_expander_shrunk';
		var expandElClass = this.expanded ? 'close' : 'open';
    var visHtml = this.isVisible() ? '' : ' style="display:none;"';
		//var expandStyle = this.hasChildren ? '' : ' style="visibility: hidden"';
		expandElClass = this.hasChildren ? expandElClass : ' null';
		
		/*var html = '<li id="cat_' + this.uId + '" class="cat_node"><img src="../../images/admin/current.gif" /><span class="' + expandElClass + '"' + expandStyle + ' id="cat_e_' + this.uId + '">&nbsp;</span><span class="cat_label" id="cat_n_' + this.uId + '">' + this.getCaption() + '</span><ul class="cat_children"' + style + ' id="cat_c_' + this.uId + '"><li><span class="cat_label">Loading...</span></li></ul></li>';*/
		
		var html = '<li id="cat_' + this.uId + '" class="cat_node"' + visHtml + '><span><a href="Javascript://" class="' + expandElClass + '" id="cat_e_' + this.uId + '">&nbsp;</a><a href="Javascript://" class="link" id="cat_n_' + this.uId + '">' + this.getCaption() + '</a></span><ul class="cat_children"' + style + ' id="cat_c_' + this.uId + '" style="padding-left: 20px;"><li><span class="link">Loading...</span></li></ul></li>';
		
		this.parent.clearCatLoadingMessage();
		new Insertion.Bottom(this.parent.getChildrenListElement(), html);
		this.liEl = $('cat_' + this.uId);
		this.expanderEl = $('cat_e_' + this.uId);
		this.nameEl = $('cat_n_' + this.uId);
		this.childrenEl = $('cat_c_' + this.uId);
		
		var self = this;
		this.nameEl.onclick = function() {
			log("LibraryCategory.nameEl.onclick");
			self.select();
      return false;
		};
		
		this.expanderEl.onclick = function() {
			self.toggleExpand();
      return false;
		};
		
		this.afterBuildElement();
	},
	
	afterBuildElement: function() {},
	
	getCaption: function() {
		var assetCount = this.assetCount == null ? '' : ' (' + this.assetCount + ')';
		return this.name + assetCount;
	},
	
	getChildrenListElement: function() {
		return this.childrenEl;
	},
	
	select: function() {
		if(this.library.selectCategory(this)) {
			//this.nameEl.className = "link ";
			this.nameEl.parentNode.className="selected";
			if(this.library.autoExpandCats && !this.expanded && this.hasChildren) {
				log("toggleExpand from select");
				this.toggleExpand();
			}
			this.getImages(1);
      this.selected = true;
		}
	},
	
	deSelect: function() {
		if(this.nameEl != null) {
			this.nameEl.className = "link";
			this.nameEl.parentNode.className=null;
		}
    this.selected = false;
	},
	
	toggleExpand: function() {
		//log("toggleExpand");
		if(this.expanderEl.className=="null") return false;
		if(this.expanded) {
			this.expanded = false;
			this.childrenEl.style.display = "none";
			//this.expanderEl.className = "icon cat_expander_shrunk";
			this.expanderEl.className="open";
		} else {
			if(!this.childrenLoadCalled) {
				this.childrenLoadCalled = true; //we dont want to call this twice if toggled quickly...
				this.loadChildCategories();
			}
			this.childrenEl.style.display = "";
			//this.expanderEl.className = "icon cat_expander_expanded";
			this.expanderEl.className="close";
			this.expanded = true;
		}
	},
	
	//get a list of children (loading from server if needed)
	getCategories: function(callback) {
		if(!this.childrenLoadCalled) {
			this.childrenLoadCalled = true; //we dont want to call this twice if toggled quickly...
			this.loadChildCategories(function() {
				callback(this.children);
			}.bind(this));
		}
	},
	
	loadChildCategories: function(callback) {
		callback = callback == null ? function() {} : callback
		var ajax = new Ajax.Request("/shared/library/get_children_categories?id=" + this.id + "&uid=" + this.uId + "&process=" + this.library.process, {asynchronous:true, evalScripts:true, method:'get', onComplete: callback});
	},
	
	getImagesUrl: function(page, keywords) {
		var id = this.id == null ? "0" : this.id;
		
		if (keywords != null) {
		  return "/shared/library/search_images?process=" + this.library.process + "&page=" + page + "&keywords=" + keywords;
		} else {
		  return "/shared/library/get_images?id=" + id + "&uid=" + this.uId + "&process=" + this.library.process + "&page=" + page;
		}
	},
	
	getImages: function(page, keywords) {
		log("getImages", true);
		resetImageQueue(); //stop downloading any images from another category...
		this.currentPage = page;
		var url = this.getImagesUrl(page, keywords);
		var searchIndicator = null;
		this.keywords = keywords;
		if (keywords != null) searchIndicator = $("search_box_indicator_" + this.library.process);
		var key = null;
		var ajax = new Ajax.Request(url, {asynchronous:true, evalScripts:true, method:'get', 
			onComplete: function() {
			  if (keywords != null) searchIndicator.style.display = "none";
				asyncFinish(key);
			}
		});
		if (keywords != null) searchIndicator.style.display = "";
		key = asyncStart("decoration_list_container_div_" + this.library.process);
	},
  
  refreshImages: function(onlyIfSelected) {
    if(onlyIfSelected != true || this.selected) {
      this.getImages(this.currentPage);
    }
  },
	
	paginate: function(page) {
		if(page == -1) {
			page = this.currentPage - 1;
		} else if(page == -2) {
			page = this.currentPage + 1;
		}
		this.getImages(page, this.keywords);
	},
	
	//called before addImage to (re)set the list
	initImageList: function(pages) {
		this.images = new MapList(this);
		this.pages = pages;
		this.decorationImageListHtml = "";
		
		if(this.pages.length > 1) {
			var paginatorHtml = '';
			
			if(this.currentPage > 1) {
				paginatorHtml = '<a class="prev_page" rel="prev" href="#" onclick="decorationLibrary.paginate(-1); return false;">' + ml('&laquo; Previous') + '</a>';
			} else {
				paginatorHtml = '<span class="disabled prev_page">' + ml('&laquo; Previous') + '</span>';
			}

			for(var i=0; i < pages.length; i++) {
				var page = pages[i];
				if(page == -1) {
					paginatorHtml += '<span class="disabled">...</span>';
				} else if(page == this.currentPage) {
					paginatorHtml += '<span class="current">' + page + '</span>';
				} else {
					paginatorHtml += '<a href="#" onclick="decorationLibrary.paginate(' + page + '); return false;">' + page + '</a>';
				}
			}

			var maxPage = pages[pages.length-1];
			if(this.currentPage < maxPage) {
				paginatorHtml += '<a class="next_page" rel="next" href="#" onclick="decorationLibrary.paginate(-2); return false;">' + ml('Next &raquo;') + '</a>';
			} else {
				paginatorHtml += '<span class="disabled next_page">' + ml('Next &raquo;') + '</span>';
			}
			
			this.library.paginatorPagesElement.innerHTML = paginatorHtml;
			this.library.paginatorContainerElement.style.display = "none";
		} else {
			this.library.paginatorContainerElement.style.display = "none";
		}
	},
	
	//called by server response for each image
	//doInstant= dont batch it (called after adding new image to server)
	addImage: function(options, doInstant) {
		var img = new DecorationLibraryImage(this, options);
		this.images.add(img);
		
		var html = this.getImageHtml(img);
		if(doInstant == true) {
			new Insertion.Bottom(this.library.imageListElement, html);
			img.loadImage();
		} else {
			this.decorationImageListHtml += html;
		}
		return img;
	},
  
  getImageHtml: function(img) {
    var dims = img.scale(50);
		var m_top=(50-dims[1])/2
    var html = '<li id="dec_lib_li_' + img.uId + '" class="product">' +
		  '<div class="d_l_container">'+
        '<img src="/images/trans.gif" width="' + dims[0] + '" height="' + dims[1] + '" style="margin-top: '+m_top+'px;" id="dec_lib_img_' + img.uId + '" />'+
      '</div>'+
      '<span>&nbsp;</span>'+
    '</li>';
    return html;
  },
	
	//when a user has changed an image...
	updateImage: function(options) {
		var img = new DecorationLibraryImage(this, options);
		var oldImage = this.images.byId[img.id];
		img.uId = oldImage.uId;
		this.images.replace(img);
		img.loadImage();
		return img;
	},
	
	//called once all the addImage() calls are done...
	allImagesAdded: function() {
		
		this.library.imageListElement.innerHTML = this.decorationImageListHtml;
		for(var i=0; i < this.images.list.size(); i++) {
			var img = this.images.list[i];
			img.loadImage();
		}
		if(this.pages.length > 1) {
			this.library.paginatorContainerElement.style.display = "";
		}
		
		var noResult = $("decoration_list_no_result_" + this.library.process);
		if (this.images.list.size() == 0) {
		  if (noResult != null) {
		    noResult.style.display = "";
		  } else {
		    var dlContainer = null;
		    if (this.library.process == 0) {
		      dlContainer = $("decoration_list_container");
		    } else {
		      dlContainer = $("decoration_list_container_" + this.library.process);
		    }
		    new Insertion.Top(dlContainer, "<p id='decoration_list_no_result_" + this.library.process + "'>No results</p>");
		  }
		} else {
		  if (noResult != null) {
		    noResult.style.display = "none";
		  }
		}
	},
	
	//to track selected image
	selectImage: function(image) {
		if(this.selectedImage != null) {
			if(this.library.imageSelectionCallback != null) { //notify container we are deselecting the image 
				if(!this.library.imageSelectionCallback(false, this.selectedImage)) { //if returns false we cant deselect the image
					return false;
				}
			}
			this.selectedImage.deSelect();
		}
		this.selectedImage = image;
		if(this.selectedImage != null) {
			if(this.library.imageSelectionCallback != null) {
				this.library.imageSelectionCallback(true, image);
			}
		}
		return true;
	},
	
	changeImageCount: function(delta, direct) {
		//get all instances of the category...
		this.library.processCategory(this.id, function(cat) {
				cat.assetCount += delta;
				if(direct) {
					cat.directAssetCount += delta;
				}
				cat.setCaption();
		});
		if(this.parent!=null) {
			log(this.parent);
			this.parent.changeImageCount(delta, false);
		}
	},
	
	setCaption: function() {
		if(this.nameEl != null) {
			this.nameEl.innerHTML = this.name + " (" + this.assetCount + ")";
		}
	},
	
	//called when deleting/moving cat
	removeElement: function() {
		this.beforeRemoveElement();
		this.liEl.parentNode.removeChild(this.liEl);
		this.liEl = null;
		this.expanderEl = null;
		this.nameEl = null;
		this.childrenEl = null;
		this.library.deRegisterCategory(this);
	},
  
  isVisible: function() {
    return true;
  },
  
  toggleVisible: function(visible) {
    if(visible) {
      $("cat_" + this.uId).show();
    } else {
      $("cat_" + this.uId).hide();
    }
  },
	
	beforeRemoveElement: function() {}
});

var RootCategory = Class.create({
	CLASSDEF: {
		name: 'RootCategory',
		parent: LibraryCategory
	},
	
	getCaption: function() {
		return this.name;
	},
	
	changeImageCount: function(delta, direct) {
		//do nothing
	}
});

var UserCategory = Class.create({
	CLASSDEF: {
		name: 'UserCategory',
		parent: RootCategory
	},
	
	initialize: function(library, name, options) {
    this.forProduct = options.for_product;
    this.hidden = options.hidden;
    this.forDigitize = (options.forDigitize == true);
		UserCategory.parentClass.constructor().call(this, library, "user_images", name, library, options);
	},
	
	getImagesUrl: function(page) {
		return "/shared/library/get_user_images?process=" + this.library.process + "&page=" + page + "&uid=" + this.uId + "&for_product=" + this.forProduct + "&for_digitize=" + this.forDigitize;
	},
  
  isVisible: function() {
    return (this.hidden != true);
  }
});

var StoreRootCategory = Class.create({
	CLASSDEF: {
		name: 'StoreRootCategory',
		parent: RootCategory
	},
	
	initialize: function(library, name, options) {
		StoreRootCategory.parentClass.constructor().call(this, library, "store_images", name, library, options);
	},
	
	getImagesUrl: function(page) {
		return "/shared/library/get_store_images?process=" + this.library.process + "&page=" + page + "&uid=" + this.uId;
	}
});

var StoreCategory = Class.create({
	CLASSDEF: {
		name: 'StoreCategory',
		parent: LibraryCategory
	},
	
	getImagesUrl: function(page) {
		return "/shared/library/get_store_images?cat=" + this.id + "&process=" + this.library.process + "&page=" + page + "&uid=" + this.uId;
	}
});

//used when the library co-exists with other categories (designer)
var DecorationLibraryRoot = Class.create({
	CLASSDEF: {
		name: 'DecorationLibraryRoot',
		parent: RootCategory
	},

	initialize: function(library, name, options) {
		DecorationLibraryRoot.parentClass.constructor().call(this, library, 0, name, library, options); 
	},
	
	getImagesUrl: function(page) {
		var id = this.id == null ? "0" : this.id;
		return "/shared/library/get_images?id=0&uid=" + this.uId + "&process=" + this.library.process + "&page=" + page;
	}
});

var DecorationLibrary = Class.create({
	CLASSDEF: {
		name: 'DecorationLibrary',
		parent: RootCategory
	},
	
	initialize: function(categoryListElement, imageListElement, paginatorContainerElement, paginatorPagesElement, displayRoot, process, manager, searchElement) {
		this.displayRoot = displayRoot;
		this.id = 0;
		this.uId = (manager == null)? 0 : manager.getNextId();
		this.manager = manager; //when used from designer.. handles multiple processes
		this.name = "Root";
		this.hasChildren = false; //this will stop the expander from showing...
		this.childrenLoaded = true; //stop check of expand info....
		//this.expanded = true;
		this.children = new MapList(this);
		this.categoryListElement = categoryListElement;
		this.imageListElement = imageListElement;
		this.paginatorContainerElement = paginatorContainerElement;
		this.paginatorPagesElement = paginatorPagesElement;
		this.searchElement = searchElement;
		
		this.library = this;
		this.categories = {};
		this.categoriesById = {}; //handle multiple instances of same cat....
		this.nextId = -1;
		this.process = process;
		this.categories[0] = this;
		this.categoriesById[0] = [this];
		this.imageSelectionCallback = null;
		this.imageUploadedCallback = null;
		this.categorySelectionCallback = null;
		this.doubleClickImageCallback = null;
		this.autoExpandCats = false; //when a cat is selected expand it
		
		this.childrenLoadCalled = true;
		
		
		if(displayRoot) {
			this.parent = new DecorationLibraryParent(this.categoryListElement);
			this.buildElement();
			this.getChildrenListElement().innerHTML = ""; //remove the Loading....
			this.select();
		}
	},
	
	getNextId: function() {
		if(this.manager != null) {
			return this.manager.getNextId();
		} else {
			return this.nextId--;
		}
	},
	
	getChildrenListElement: function() {
		if(this.displayRoot) {
			return this.childrenEl;
		} else {
			return this.categoryListElement;
		}
	},
	
	//track currenctly selected category
	selectCategory: function(libraryCategory) {
		
		if(this.selectedCat != null) {
			if(this.categorySelectionCallback != null) {
				if(!this.categorySelectionCallback(false, this.selectedCat)) {
						return false;
				}
			}
			this.selectedCat.deSelect();
		}
		this.selectedCat = libraryCategory;
		if(this.categorySelectionCallback != null) {
			this.categorySelectionCallback(true, libraryCategory);
		}
		return true;
	},
	
	//allow us to lookup a cat directly
	registerCategory: function(category) {
		this.categories[category.uId] = category;
		if(this.categoriesById[category.id] == null) {
			this.categoriesById[category.id] = [];
		}
		this.categoriesById[category.id].push(category);
	},
	
	deRegisterCategory: function(category) {
		var cats = this.categoriesById[category.id];
		var catIdx = -1;
		for(var i=0; i < cats.length; i++) {
			if(cats[i].uId == category.uId) {
				catIdx = i;
				break;
			}
		}
		if(catIdx != -1) {
			this.categoriesById[category.id].splice(catIdx,1);
		} else {
			log("ERROR: unable to deRegisterCategory");
		}
	},
	
	//run a callback on each instance of a category
	processCategory: function(id, callback) {
		var cats = this.categoriesById[id];
		//make a copy in case the array gets changed from the callback
		var cats2 = [];
		for(var i=0; i < cats.length; i++) {
			cats2.push(cats[i]);
		}
		for(var i=0; i < cats2.length; i++) {
			var cat = cats2[i];
			callback(cat);
		}
	},
	
	search: function() {
	  if(this.selectedCat != null) {
			if(this.categorySelectionCallback != null) {
				if(!this.categorySelectionCallback(false, this.selectedCat)) {
						return false;
				}
			}
			this.selectedCat.deSelect();
		}
		this.selectedCat = this;
		this.getImages(1, this.searchElement.value);
	},
	
	paginate: function(page) {
		if(this.selectedCat == this) {
			DecorationLibrary.parentClass.method("paginate").call(this, page);
		} else {
			this.selectedCat.paginate(page);
		}
	},
  //after making a change to an image it may need processing... lets track that... 
  startTrackingProcessing: function(imageId, isNew, catUid, inDesigner) {
    this.processTrackingImageId = imageId;
    this.processTrackingIsNew = isNew;
    this.processTrackingCatUid = catUid;
    this.failureCount = 0;
    this.processTrackingInDesigner = (inDesigner == true)? 1 : 0;
    this.updateTrackedProcess(imageId, "Processing....", 0);
    popup("process_status");
  },
  
  updateTrackedProcess: function(imageId, caption, percent) {
    
    if(imageId == this.processTrackingImageId) {
      var totalWidth = parseInt($("process_status_bar").style.width);
      var barWidth = totalWidth * percent / 100;
      $("process_status_indicator").style.width = parseInt(barWidth) + "px";
      $("process_status_message").innerHTML = caption;
      var self = this;
      if(percent >= 100) {
        window.setTimeout( function() {
            self.finishTrackingProcess(true);
        }, 200);
      } else {
        window.setTimeout( function() {
          var ajax = new Ajax.Request("/shared/library/processing_status?image_id=" + imageId + "&ts=" + new Date().getTime() , {asynchronous:true, evalScripts:true, method:'get', 
            onFailure: function() {
              self.failureCount ++;
              if(self.failureCount > 10) {
                self.finishTrackingProcess(false);
              } else {
                self.updateTrackedProcess(imageId, caption, percent);
              }
            }
          });
        }, 500);
      }
    }
  },
  
  finishTrackingProcess: function(allOk) {
    if(!allOk) {
      alert("A Server error occured while tracking file processing.");
      closePopup("process_status");
    } else {
      var ajax = new Ajax.Request("/shared/library/get_details?id=" + this.processTrackingImageId + "&is_new=" + this.processTrackingIsNew + "&category_uid=" + this.processTrackingCatUid +  "&must_be_ok=1&ts=" + new Date().getTime()+ "&in_designer=" + this.processTrackingInDesigner, {asynchronous:true, evalScripts:true, method:'get', 
        onComplete: function() {
          closePopup("process_status");
        }
      });
    }
  }	
});

//placeholder class to hold a container el when root is shown
var DecorationLibraryParent = Class.create({
	CLASSDEF: {
		name: 'DecorationLibraryParent'
	},
	
	initialize: function(listElement) {
		this.listElement = listElement;
	},
	
	getChildrenListElement: function() {
		return this.listElement;
	},
	
	clearCatLoadingMessage: function() {
	}
});


var DecorationLibraryImage = Class.create({
	CLASSDEF: {
		name: 'DecorationLibraryImage'
	},
	
	initialize: function(libraryCategory, options) {
		this.libraryCategory = libraryCategory;
		this.id = options.id;
		this.uId = libraryCategory.library.getNextId(); //we need a unique id because the same cat can appear twice in designer
    
    this.urlOptions = {
       bURL: options.bURL,
       tURL: options.tURL,
       sURL: options.sURL,
       url: options.url
    };
    this.baseUrl = options.bURL; //base url
    
		this.thumbUrl = this.baseUrl + "/" + options.tURL;
		this.url = this.baseUrl + "/" + options.url;
    this.smallUrl = this.baseUrl + "/" + options.sURL;
		this.width = options.width;
		this.height = options.height;
		this.aspectRatio = parseFloat(this.width) / parseFloat(this.height);
		this.type = options.type;
		this.usesTransparency = options.trans;
		this.name = options.name;
		this.colors = options.c;
		this.colorWays = options.cw;
		this.defaultColorway = options.dcw;
		this.threadCount = options.tc;
		this.processes = options.p;
		this.ownerType = options.ot;
		this.imageType = options.it;
		this.percentOpaque = options.po;
		this.defaultColorCount = options.dcc;
		this.metaData = options.meta;
		this.decorationLibraryId = options.dl;
    this.digitizedProcessId = options.dp;
    this.digitizedState = options.dps;
    this.hasColorsAlt = options.hc;
	},
	
	//return width/height of thumb that fits in square of side 
	scale: function(side) {
		
		if(this.aspectRatio > 1) { //width is greater...
			side = (this.width < side) ? this.width : side;
			return [side, parseInt(parseFloat(side) / this.aspectRatio)];
		} else {
			side = (this.height < side) ? this.height : side;
			return [parseInt(parseFloat(side) * this.aspectRatio), side];
		}
	},
	
	loadImage: function() {
		log("loadImage");
		var self =this;
		if(this.el == null) {
			this.el = $("dec_lib_img_" + this.uId);
			this.liEl = $("dec_lib_li_" + this.uId);
			
			this.liEl.onmousemove = function() {
				if(this.liEl.className != "alt") {
					//this.liEl.className = "product over";
				}
			}.bind(this);
			this.liEl.onmouseleave = function() {
				if(this.liEl.className != "alt") {
					//this.liEl.className = "product";
				}
			}.bind(this);
			this.liEl.onclick = function(el) {
				self.select(el);
        return false;
			};
			this.liEl.ondblclick = function(el) {
				if(self.libraryCategory.library.doubleClickImageCallback != null) {
					self.libraryCategory.selectImage(null); //not tracking selected image.. make sure state reflects this
					self.libraryCategory.library.doubleClickImageCallback(self);
				} else {
					self.select(el);
				}
        return false;
			};
			
		}
		var dims = this.scale(50);
		
		queueImageLoading(this.el,this.thumbUrl,dims[0],dims[1], function() {self.el.alt = "Double Click To Add To Design"});
	},
	
	select: function(el) {
    if(el != null) {
      Event.stop(el);
    }
		if(this.libraryCategory.selectImage(this)) {
			//this.liEl.className = "product alt";
      this.selected=true;
		}
	},
	
	deSelect: function() {
		//this.liEl.className = "product";
    this.selected=false;
	},
	
	uploadFinished: function() {
		var result = {action:"select"};
		if(this.libraryCategory.library.imageUploadedCallback != null) {
			this.libraryCategory.library.imageUploadedCallback(this);
		} else {
			this.select();
		}
	},
	
	allowsProcess: function(processId) {
		for(var i=0; i < this.processes.length; i++) {
			if(this.processes[i] == processId) {
				return true;
			}
		}
		return false;
	},
	
	hasColors: function() {
		//if((this.colors == null || this.colors.length == 0)&&(this.defaultColorCount ==0)) {
		//	return false;
		//}
		//return true;
		return this.hasColorsAlt;
	},
	
	//called after delete image
	remove: function() {
    if(this.selected) {
      this.libraryCategory.selectImage(null);
    }
		this.liEl.parentNode.removeChild(this.liEl);
		this.libraryCategory.images.remove(this.id);
    
	},
	
	//get info a hash to init Asset
	assetOptions: function() {
		return {
			id: this.id,
      bURL: this.urlOptions.bURL,
      tURL: this.urlOptions.tURL,
      sURL: this.urlOptions.sURL,
      url: this.urlOptions.url,
			width: this.width,
			height: this.height,
			type: this.type,
			trans: this.usesTransparency,
			name: this.name,
			c: this.colors,
			cw: this.colorWays,
			tc: this.threadCount,
			p: this.processes,
			ot: this.ownerType,
			it: this.imageType,
			po: this.percentOpaque,
			dcc: this.defaultColorCount,
			meta: this.metaData,
			dl: this.decorationLibraryId,
      dp: this.digitizedProcessId,
      dps: this.digitizedState,
      hc: this.hasColorsAlt
		};
	}
});


