$(document).ready(function() { 

	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}
	
	
	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 0, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 0, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
	
	$("ul#topNav li .sub").css({'opacity':'0'});
	$("ul#topNav li").hoverIntent(config);

});

function popup(url){
    window.open(url,'popup','width=350,height=150,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function ConfirmDelete(varURL, name){
    if (confirm("Are you sure you want to delete " + name + "?")){
        window.open(varURL, "_top");
        return true;
    }else{
        return false;
    }
}

function toggleHide(obj){
    var el = document.getElementById(obj);
    if(el.style.display == "block"){
        el.style.display = "none";
    }else{
        el.style.display = "block";
    }
}


function change_lang(lang){ 
    var the_url = window.location.href;
    if(the_url.match("lang=")){
        the_url = the_url.replace("&lang=en","");
        the_url = the_url.replace("&lang=ga","");
    }
    if(the_url.indexOf("?") < 1){
        the_url = the_url + "?lang=" + lang;
    }else{
        the_url = the_url + "&lang=" + lang;
    }
    window.location.href = the_url
}

function setDisplay(num){

    $("#display").load("ajax.php?action=set_display&display="+num, null, function(response, status) {
        if (status == "error") {
            errorMsg();
        }else{
            successMsg();
        }
    });

}


		
function showCart(){
    $("#side_cart").load("ajax.php?action=show_cart");
    $("#more_products").load("ajax.php?action=show_cart");
    $("#main_display").load("ajax.php?action=show_cart");
		
}		
function addToCart(id,quantity)
{
    $("#addHolder").hide();
    $("#viewMyList").hide();
    $("#preloader").fadeIn('fast');
	$("#shoppingList").animate({opacity: 0.4}, 0);
    $("#hiddenDiv").load("ajax.php?action=add_to_cart&subproduct_id="+id+"&quantity="+quantity, null, function(response, status) {
        if (status == "error") {
            errorMsg();
        }else{
            successMsg();
        }
    });
		
//$("#added_product").fadeOut("slow");
//alert("here");
/*$("#side_cart").load("ajax.php?action=add_to_cart&product_id="+id+"&quantity="+quantity, null, function(){
        $("#added_product").fadeOut(2500);
    });
    $("#added_product").fadeOut("slow");*/
}
function errorMsg(){

    //$("#shoppingList").html("My Shopping List");
    showAddProdErrAlert();
    //$("#added_product_error").show();
    //$("#added_product_error").fadeOut(2000, onAddAlertComplete);
    hidePreloader();
}
function successMsg(){
	
    //$("#shoppingList").html("My Shopping List");
    showAddProdAlert();
    //$("#shoppingList").css("background-color","#dfffc4");
    //$("#shoppingList").stop().animate({backgroundColor: '#edecea'}, 2500);
	$("#shoppingList").stop(true, true).animate({opacity: 1}, 300);
    hidePreloader();
}

function hidePreloader()
{
    $("#preloader").hide();
}

function showAddProdErrAlert()
{
    $('#added_product_error').fadeIn('fast', hideAddProdAlert);
}

function showAddProdAlert()
{
    $('#added_product').fadeIn('fast', hideAddProdAlert);
}

function hideAddProdAlert()
{
    $(this).delay(3000).fadeOut('fast', onAddAlertComplete);
}

function onAddAlertComplete(){
    $("#addHolder").fadeIn('fast');
    $("#viewMyList").fadeIn('fast');
    $("#add_to_cart_small").css("display","block");
    //$("#shoppingList").animate({backgroundColor: "#edecea"},3000);
    $("#shoppingList").animate({opacity: 1}, 300);
}

function removeFromCart(id,sub_id,remove,pic){
    
    $("#side_cart").load("ajax.php?action=remove_from_cart&product_id="+id+"&subproduct_id="+sub_id, null, null);
/*
    $("#verify_products").load("ajax.php?action=remove_from_cart&product_id="+id+"&subproduct_id="+sub_id+"&otherarea=true", null, function(){
        $("#removed").fadeOut(2500);
    });
    $("#more_products").load("ajax.php?action=remove_from_cart&product_id="+id+"&subproduct_id="+sub_id+"&mainarea=true", null, function(){
        $("#removed").fadeOut(2500);
    });*/
}
function emptyCart(){
    $("#side_cart").load("ajax.php?action=empty_cart");
}		
    	
    	
function clipBasket(){
    if($("#clip_basket").css("position") == "fixed"){
        $("#clip_basket").css("position","static");
        $("#clip").attr("src","images/pin_up.gif");
    }else{
        $("#clip_basket").css("position","fixed");
        $("#clip").attr("src","images/pin_down.gif");
    }
}		
		
		
		
function useBillingAsDelivery(){
    if($("#use_billing_as_delivery:checked").val() == 1){
        $("#delivery_firstname").val($("#billing_firstname").val());
        $("#delivery_surname").val($("#billing_surname").val());
        $("#delivery_address1").val($("#billing_address1").val());
        //$("#delivery_address2").val($("#billing_address2").val());
        //$("#delivery_address3").val($("#billing_address3").val());
        //$("#delivery_postcode").val($("#billing_postcode").val());
        //$("#delivery_county").val($("#billing_county").val());
        $("#delivery_country").val($("#billing_country").val());
        $("#delivery_phone1").val($("#billing_phone1").val());
        $("#delivery_phone2").val($("#billing_phone2").val());
        $("#delivery_email").val($("#billing_email").val());
    }else{
        $("#delivery_firstname").val("");
        $("#delivery_surname").val("");
        $("#delivery_address1").val("");
        //$("#delivery_address2").val("");
        //$("#delivery_address3").val("");
        //$("#delivery_postcode").val("");
        //$("#delivery_county").val("");
        $("#delivery_country").val("Ireland");
        $("#delivery_phone1").val("");
        $("#delivery_phone2").val("");
        $("#delivery_email").val("");
    }
}

function alphaInit() {
        $('#page.alpha').keyup(function() {
            if (this.value.match(/[^a-zA-Z0-9_]/g)) {
                this.value = this.value.replace(/[^a-zA-Z0-9_]/g, '');
            }
        });
}

