	Event.observe(window, 'load', filter)
	Event.observe(window, 'load', changeCurrency)
	Event.observe(window, 'load', filterBrand)
	Event.observe(window, 'load', function(){
										   
										   if($('previewcos')){getCart()}
										   
										   $$('div#nav h2').each(
												function(e){
													
													Event.observe(e, 'click', function(){
														e.next().toggle()
													})
												}
											)
										   
										   })
	
	var website = "http://www.homemarket.ro/";
	
	
	function mmm(){
		if($('listing').style.width != "760px"){
			$('toolbox').style.display = "none";
			$('listing').style.width = "760px";
		} else {
			$('toolbox').style.display = "";
			$('listing').style.width = "580px";	
		}
	}
	
	function substituie(val){
		if(val !=0 ){
			$$('select.substitutie').each( function(e){
				var aa = e.value.split("_")
				if(aa[0] = val){
					e.value = val + "_" + aa[1] + "_" + aa[2]
					
				}
			})
		} else {
			alert('Va rugam sa alegeti o valoare valida');
			return false;
		}
	}
	
	function filterBrand(){
		if($('brands')){
			Event.observe('brands', 'change', function(){
				 var values = $('brands').value.split("_")
				 if(values != 0){
					window.location = website + "/index.php?c=" + values[0] + "&b=" + values[1]
				 } else {
				 	return false;			 	
				 }
				}
			)	
		}
	}	

	function newaddress(adresa){
		if($(adresa).value != 0){
			window.location = website + '/comanda.php?adresa=' + $(adresa).value;
		} else {
			return null
		}
	}
	
	function filter(){
		if($('sort')){
			Event.observe('sort', 'change', function(){
				 var values = $('sort').value.split("_")
				 if(values != 0){
					window.location = website + "/index.php?" + values[0] + "&f=" + values[1]
				 } else {
				 	return false;			 	
				 }
				}
			)	
		}
	}
	
	function changeCurrency(){
		if($('moneda')){
			Event.observe('moneda', 'change', function(){
				var values = $('moneda').value.split("_");
				 if(values != 0){				
					window.location = website + "/index.php?" + values[0] + "&m=" + values[1]
				 } else {
				 	return false;			 	
				 }
				}
			)
		}
	}
	
	function makeCant(item, action){
		var cantitate = parseInt($(item).value);
		
		if(isNaN(cantitate)) {	cantitate = 0;	}
		
		var produs = item.split("_");
		
		if(action == 'add'){
			var valoare = cantitate+1;			
			updateCart(produs[1], valoare);
			
		} else {
			if($(item).value >= 1){
				var valoare = cantitate-1;
				updateCart(produs[1], valoare);
			} else {
				var valoare = 0;
				updateCart(produs[1], 0);
			}
		}
		if($(item)){
			$(item).value = valoare
		}
	}
	
	function getCart(){
		var url = website + '/cos.php';
		
		var myAjax = new Ajax.Updater(
			'previewcos', 
			url, 
			{
				method: 'get',
				onLoading: loadCart()
			});
	}
	
	function updateCart(produs, cantitate){
		var url = website + '/cos.php';
		var pars = 'produs=' + produs + '&cantitate=' + cantitate;
		
		var myAjax = new Ajax.Updater(
			'previewcos', 
			url, 
			{
				method: 'get', 
				parameters: pars,
				onLoading: loadCart()
			});
	}
	
	function loadCart(){
		$('previewcos').innerHTML = "<p class='loading'><img src='" + website +"/assets/loader.gif' alt='loading' /></p>";
	}
	
	function addComment(produs, cos, comentariu){
		var url = website + '/actions/handler.comanda.php';
		var pars = 'produs=' + produs + '&cos=' + cos + '&comentariu=' + comentariu + '&action=commentprodus';		
		var myAjax = new Ajax.Updater(
			'done', 
			url, 
			{
				method: 'post', 
				parameters: pars,
				onComplete: function(){
					$('tiny-over').hide();
					$('tiny-tbox').hide();

				}
			}); 
	}
	
	function addInList(val){
		var tot = val.split("_")
		
		var user 		= tot[3];
		var cod 		= tot[1];
		var lista		= tot[2];	
		
		var url = website + '/actions/handler.produse.php';
		
		var pars = "cod=" + cod + "&list=" + lista + "&user=" + user + "&action=listamica"; 

		var myAjax = new Ajax.Updater(
			val, 
			url, 
			{
				method: 'post', 
				parameters: pars
			
			}); 
	}