$(document).ready(function(){
	$('.homepageslides').cycle({
		fx: 'fade',
		speed: 1000,
		timeout: 3000,
		random: 1
  	});
  	
	$('.newslettersubscr').example('Enter Email');



	//$('.productcatbrowser ul li').hover(function(){
	//	$(this).children('a').children('img').slideDown('slow');
	//	$(this).children('a').children('img').parent('a').css({'border': '1px solid #F2DA38', 'border-top':'none', 'margin-bottom' : '1em'});
		//({height: '190px' });
	//}, function(){
	//	$(this).children('a').children('img').slideUp('fast');
	//	$(this).children('a').children('img').parent('a').css({'border':'none', 'margin-bottom' : '0'});
	//});
	$('[name=search_form] .button').click(function(){
		//alert('blah');
		var id_categ=0;
		var categ_label="";
		var id_brand=0;
		var brand_label="";
		var keywords="";
		if ($("[name=categ_brands[0]]").val()){
			id_categ=$("[name=categ_brands[0]]").val();
			if (id_categ>0){
				categ_label=$("[name=categ_brands[0]] option[value='"+id_categ+"']").text();
				categ_label=categ_label.replace(/ /g, "-");
			}
		}
		if ($("[name=categ_brands[1]]").val()){
			id_brand=$("[name=categ_brands[1]]").val();
			if (id_brand>0){
				brand_label=$("[name=categ_brands[1]] option[value='"+id_brand+"']").text();
				brand_label=brand_label.replace(/ /g, "-");
			}
		}
		//alert($("[name=search_form] [name=keywords]").val());
		if ($("[name=search_form] [name=keywords]").val()){
			keywords=$("[name=search_form] [name=keywords]").val();
		}		
		
		if ($("[name=search_form] [name=sidebar_keywords]").val()){
			keywords=$("[name=search_form] [name=sidebar_keywords]").val();
		}		

		window.location="http://www.amare.com.au/search/"+id_categ+"/"+id_brand+"/"+categ_label+"/"+brand_label+"/keywords/"+keywords;
	});
		
	$('[name=search_form] input').bind('keypress', function(e) {
		 var code = (e.keyCode ? e.keyCode : e.which);
		 if(code == 13) { //Enter keycode
			$('[name=search_form] .button').click();
			return false;
		 }	
	});


	$('.compareproduct').click(function(){
		element_id=$(this).attr('id');
		id_product=element_id.replace("id_product_", "");
		action="remove";
		if ($(this).is(':checked')){
			action="add";
		}
		$.get("compare_products_action.php?id_product="+id_product+"&action="+action, function(data){
			  //alert(data);
			  compareButton=data;
			  if (compareButton=="1"){
				  $('#compareproducts').attr('class','button'); 
				  $('#compareproducts').removeAttr("disabled");  
			  }else{
				  $('#compareproducts').attr('class','button disabled');
				  $('#compareproducts').attr('disabled',true );   
			  }
			  //$("#compare_test").html(data);
		});		
	});
	$('#compareproductsreset').click(function(){
		action="resetAll";
		 $('.compareproduct').attr('checked', false);
		$.get("compare_products_action.php?action="+action, function(data){
			  compareButton=data;
			  if (compareButton=="1"){
				  $('#compareproducts').attr('class','button'); 
				  $('#compareproducts').removeAttr("disabled");  
			  }else{
				  //alert('disabled');
				  //$('#compareproducts').attr('disabled',true); 
				  $('#compareproducts').attr('class','button disabled'); 
				  $('#compareproducts').attr('disabled',true); 
			  }
			  //$("#compare_test").html(data);
		});		
	});	
	
	$('.howtoorder dt').live('click',function() {
		if ($(this).next('dd').hasClass('selected')) { 
			$(this).next('dd').slideUp('fast').removeClass('selected');
		} else {
			$('dd').slideUp('fast');
			$(this).next('dd').slideDown('slow').addClass('selected');
		}
		return false;
	});

	thisPage = location.href.substring((location.href.lastIndexOf("/"))+1);
	if (thisPage.indexOf("#")>0){
		thisPage = thisPage.substring(0,thisPage.indexOf("#"));				  
	}
	if (thisPage.indexOf("?")>0){
		thisPage = thisPage.substring(0,thisPage.indexOf("?"));				  
	}
	if (thisPage==""){
		thisPage='index.php';
	}
	if (thisPage=="results.php"){
		thisPage='browse-catalogue.php';
	}	
	if (thisPage=="advanced-product-search.php"){
		thisPage='browse-catalogue.php';
	}
	if (thisPage=="compare-products.php"){
		thisPage='browse-catalogue.php';
	}	
	$("#nav li a[href='"+thisPage+"']'").each(function() {
		$(this).addClass('current');
	});	
	
});

String.prototype.trim = function() {
                return this.replace(/^\s+|\s+$/g,"");
}


function validate_login(){
    var username=document.getElementById("username").value.trim();
    var password=document.getElementById("password").value.trim();

    var message='The following errors have been found-\n\n';
    var flag=true;
    if(username == ""  ){
        flag=false;
        message=message+"\t- Username must be provided\n"
    }
    if(password == ""  ){
        flag=false;
        message=message+"\t- Password must be provided\n"
    }
    
    if (!flag){
            alert(message);
     }
        return flag;
}

function submit_form() {
    document.loginForm.submit();
}

function similarProducts(id_categ,id_brand,s_page){	
	$.get("similar_products.php?id_categ="+id_categ+"&id_brand="+id_brand+"&s_page="+s_page, function(data){
	  $("#similarproducts").html(data);
	});
}
