var catlistTimeout;
var prodlistTimeout;
EZFinder = Class.create()

EZFinder.prototype = {
	initialize:function() {
		$('EZFinderCategoryName').value='Catégories'
		$('EZFinderCategoryValue').value='Catégories'
		$('EZFinderProductName').value='Produits'
		$('EZFinderProductValue').value='Produits'
		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='Produits'
			$('EZFinderProductValue').value='Produits'
			$('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(
				'/fr_ca/global/ajax/ajaxResponse/easyFinderAjaxResponse.php', 
				{
					method: 'get',
					parameters: 'categoryId='+elId+'&currentTime='+currentTime.getTime(),
					onLoading: EZFinder.onLoadingHandler,
					onFailure: EZFinder.onFailureHandler,
					onComplete: EZFinder.onCompleteHandler
				});	
		} else {
			$('EZFinderCategoryName').value = 'Catégories';
			$('EZFinderCategoryValue').value = 'Catégories';
			$('productDropDown').innerHTML = '';
			$('EZFinderProductName').value='Produits';
			$('EZFinderProductValue').value='Produits';
			$('easyFinderProdContainer').style.cursor='default';
		}
		$('categoryDropDown').style.display='none';
	},
	onFailureHandler:function() {
		clearTimeout(ajaxTimer);
		$('productDropDown').innerHTML = '<div><table cellpadding="0" cellspacing="0" border="0" width="250" summary="tableau récapitulatif" >';
		$('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 = 'Produits';
			$('EZFinderProductName').value = 'Produits'
		}
		$('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'
		try {$('dumpBox').innerHTML = responseBack.responseText.strip().escapeHTML();} catch(err){}

	},
		
	submitEasyFinder:function() {
		var myArray = new Array();
		myArray['fondsdeteint'] = "foundation";
		myArray['poudres'] = "powder";
		myArray['dissimulateurs'] = "concealer";
		myArray['mascaras'] = "mascara";
		myArray['fardsàpaupières'] = "eyeshadow";
		myArray['crayonstraceurs'] = "eyeliner";
		myArray['rougesàlèvres'] = "lipcolor";
		myArray['brillantsàlèvres'] = "lipgloss";
		myArray['crayonsàlèvres'] = "lipliner";
		myArray['vernisàongles'] = "nailcolor";
		myArray['outils'] = "tools";	
		if ($F('EZFinderCategoryValue')!='Catégories') {
			if ($F('EZFinderProductValue')!='Produits') {
				document.location='/fr_ca/products/product.php?productId='+$F('EZFinderProductValue');
			} else {
				var categoryVal = $F('EZFinderCategoryValue').toLowerCase();				
				categoryVal = categoryVal.split(' ').join('');			
				document.location='/fr_ca/products/'+myArray[categoryVal];
			}
		} else {
			alert('You must first select at least a category.')			
		}
	}
}

var EZFinder = new EZFinder;


