var catlistTimeout;
var prodlistTimeout;
EZFinder = Class.create()

EZFinder.prototype = {
	initialize:function() {
		$('EZFinderCategoryName').value='Category'
		$('EZFinderCategoryValue').value='Category'
		$('EZFinderProductName').value='Product'
		$('EZFinderProductValue').value='Product'
		this.catDropDown = $('categoryDropDown').getElementsByClassName('dropDownOption')
		for (i=0;i<this.catDropDown.length;i++) {
			this.catDropDown[i].onclick = this.getProdList.bindAsEventListener(this.catDropDown[i],this.catDropDown[i].id)
			this.catDropDown[i].onmouseover = this.onCatList.bindAsEventListener(this.catDropDown[i],this.catDropDown[i].id)
			this.catDropDown[i].onmouseout = this.offCatList.bindAsEventListener(this.catDropDown[i],this.catDropDown[i].id)
		}
		 
	},
	
	getProdList:function(el, elId) {
		if (elId!='defaultCat') {
			$('EZFinderProductName').value='Product'
			$('EZFinderProductValue').value='Product'
			$('EZFinderCategoryValue').value = $(elId).innerHTML
			$('EZFinderCategoryName').value = $(elId).innerHTML.replace('&amp;', '&')
			$(elId).style.backgroundColor='#99CCFF'
			currentTime = new Date()
			ajaxSetTimeOut("EZFinder.onFailureHandler()")
			var goAjax = new Ajax.Request(
				'/global/ajax/ajaxResponse/easyFinderAjaxResponse.jsp', 
				{
					method: 'get',
					parameters: 'categoryId='+elId+'&currentTime='+currentTime.getTime(),
					onLoading: EZFinder.onLoadingHandler,
					onFailure: EZFinder.onFailureHandler,
					onComplete: EZFinder.onCompleteHandler
				});	
		} else {
			$('EZFinderCategoryName').value = 'Category';
			$('EZFinderCategoryValue').value = 'Category';
			$('productDropDown').innerHTML = '';
			$('EZFinderProductName').value='Product';
			$('EZFinderProductValue').value='Product';
			$('easyFinderProdContainer').style.cursor='default';
		}
		$('categoryDropDown').style.display='none';
	},
	onFailureHandler:function() {
		clearTimeout(ajaxTimer);
		$('productDropDown').innerHTML = '<div><table cellpadding="0" cellspacing="0" border="0" width="250">';
		$('productDropDown').innerHTML += '<div id="defaultProd" class="dropDownOption" style="padding: 2px;border:1px solid #cccccc;color:#ff0000;background-color:#ffffff;">Connection error, please try again later.</div>';
		$('productDropDown').innerHTML += '</td></tr></table></div>';
	},
	selectProduct:function(elId) {
		if (elId != 'defaultProd') {
			$('EZFinderProductValue').value = elId;
			$('EZFinderProductName').value = $(elId).innerHTML.replace('&amp;', '&');
		} else {
			$('EZFinderProductValue').value = 'Product';
			$('EZFinderProductName').value = 'Product'
		}
		$('productDropDown').style.display = 'none';
		EZFinder.submitEasyFinder();
	},
	
	onCatList:function(el, elId) {
		clearTimeout(catlistTimeout);
		$('ezMenu').value=1;
		$('ezSubMenu').value=1;
		if ($('productDropDown').style.display!='none') $('productDropDown').style.display='none';
		$(elId).style.backgroundColor='#F0F0F0'
		$('categoryDropDown').style.display='block';
	},
	
	offCatList:function(el, elId) {
		$('ezMenu').value=0;
		$('ezSubMenu').value=0;
		$(elId).style.backgroundColor='#FFFFFF';
		catlistTimeout = setTimeout(function() {
			if ($("ezSubMenu").value == 0 && $("ezMenu").value == 0) {
				$("categoryDropDown").style.display="none";
			}
		}, 150)		
	},
	
	onProdList:function(el, elId) {
		clearTimeout(prodlistTimeout);
		$('ezMenu').value=1;
		$('ezSubMenu').value=1;
		if ($('categoryDropDown').style.display!='none') $('categoryDropDown').style.display='none';
		$(elId).style.backgroundColor='#F0F0F0'
		$('productDropDown').style.display='block';
	},
	
	offProdList:function(el, elId) {
		$('ezMenu').value=0;
		$('ezSubMenu').value=0;
		$(elId).style.backgroundColor='#FFFFFF';
		prodlistTimeout = setTimeout(function() {
			if ($("ezSubMenu").value == 0 && $("ezMenu").value == 0) {
				$("productDropDown").style.display="none";
			}
		}, 150)		
	},
	
	onCompleteHandler:function(responseBack) {
		clearTimeout(ajaxTimer);
		$('productDropDown').innerHTML = responseBack.responseText.strip();
		$('easyFinderProdContainer').style.cursor='pointer';
		pageTracker._trackPageview('/action/easyFinder/');
		try {$('dumpBox').innerHTML = responseBack.responseText.strip().escapeHTML();} catch(err){}

	},
	
	submitEasyFinder:function() {
		if ($F('EZFinderCategoryValue')!='Category') {
			if ($F('EZFinderProductValue')!='Product') {
				document.location='/products/product.jsp?productId='+$F('EZFinderProductValue');
			} else {
				var categoryVal = $F('EZFinderCategoryValue').toLowerCase();
				if (categoryVal.indexOf('nail') != -1) {
					document.location='/beauty_products/nailcolor';
				}
				else if (categoryVal.indexOf('blush') != -1) {
					document.location='/beauty_products/blush/';
				}
				else {				
					document.location='/beauty_products/'+categoryVal+'/';
				}
			}
		} else {
			alert('You must first select at least a category.')			
		}
	}
}

var EZFinder = new EZFinder;

