$(document).ready(function(){
	
	var DocumentHeight = $(document).height();
	var DocumentHeight = DocumentHeight+70;

	$("<div id='modalLayer'></div>").css({"height":DocumentHeight+"px","opacity":0.5}).insertAfter("#outerwrapper").hide();
});


$(function(){
	$("form#addToCart").ajaxForm({
	
        dataType: 'json',
    	url: 'http://www.cabcononline.co.uk/ajax/add_to_cart.php',
        success:addToCart  
        
	});
});
 
function addToCart(data)
{
	if (data.type == "cart")
	{
		$("#cartContainer").load("http://www.cabcononline.co.uk/ajax/cart_data.php",function(){
			showMessage('<p>Your product has been added to your cart</p>');
		});
	}
	else if (data.type == "favourites")
	{
		showMessage('<p>Your product has been added to favourites list</p>');

	}
	else {
		var message = '<p class="emphasis">There are errors with the submission of your request</p>';
		for (var key in data) 
		{
			if (key == 'repeat')
			{
				message = "<p>"+data[key]+"</p>";
			}
			else
			{
				message = message + "<p>"+data[key]+"</p>";
			}
			
		}
		showMessage(message);
	}
}

function showMessage(message)
{
	var Message = $("<div class='message'>"+message+"</div>");
	Message.insertAfter("#wrapper").hide();
	var MessageWidth = Message.outerWidth();
	var MessageHeight = Message.outerHeight();
			
	var WindowWidth = $(window).width();
	var DiffWidth = (WindowWidth - MessageWidth)/2;
	var windowHeight = $(window).height();
	var documentHeight = $(document).height();
	
	$("#modalLayer").bgiframe().show();
	Message.css({"top":"200px","left":DiffWidth+"px"}).
	hide().
	fadeIn("slow").
	animate({opacity: 0.0}, 1500,function(){
		($(this).remove());
		($("#modalLayer").fadeOut("slow"));
	});
	
}


$(document).ready(function() { 
    $('ul.sf-menu').superfish({ 
    
    delay:       500,                            // one second delay on mouseout 
    animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
    speed:       'fast',                          // faster animation speed 
    autoArrows:  false,                      // disable generation of arrow mark-up 
    dropShadows: false   // disable drop shadows 
}); 
}); 

$(document).ready(function(){
	$("ul.indexCats li:eq(2), ul.indexCats li:eq(5),ul.indexCats li:eq(8)").css({"margin-right":"0px"})
});

/*
$(document).ready(function(){
	$(".cart ul li:last").css({"margin-right":"0px"})
	
});
*/

$(document).ready(function(){
	$("ul.catListing li:eq(3),ul.catListing li:eq(7),ul.catListing li:eq(11),ul.catListing li:eq(15),ul.catListing li:eq(19),ul.catListing li:eq(23),ul.catListing li:eq(27)").css({"margin-right":"0px"})
});


$(document).ready(function(){
	$(".homeBottom img:last").css({"margin-right":"0px"});
});

/*
$(document).ready(function() {
	var searchLabel = $(".search label").remove().text();
	$("#searchInput").addClass("placeholder").val(searchLabel).focus(function (){
		if (this.value == searchLabel)
		{
			$(this).removeClass("placeholder").val("");
		}
	}).blur(function(){
		if (this.value == '')
		{
			$(this).addClass("placeholder").val(searchLabel);
		}
	});
	
	$(".search").submit(function(){
		if ($("#search").val() == searchLabel)
		{
			$("#search").val("");
		}
	});
});
*/

$(document).ready(function(){
	$("form:not(.search) input:not(:submit) ").focus(function(){
		$(this).addClass("inputFocus");
	}).
	blur(function(){
		$(this).removeClass("inputFocus");
	}).
	keyup(function(){
		$(this).removeClass("inputFocus");
	});
});

$(document).ready(function(){
	var Errors = $(".errors");
	Errors.hide();
	Errors.slideDown("slow");
});

var fnGetPrice = function(o_select){ 
	
	$(o_select).parents('div').find('select').each(function()
        {
        
        	if (this.value.length == 0){return false;}
        });
        	
        var v_url = 'http://www.cabcononline.co.uk/ajax/getOptionPrices.php';
        var rowID = $(o_select).parents('div.productListing').attr('id');  
        rowID = rowID.replace(/item_/,"");
    	var v_qrystr = 'p_id=' + rowID +'&';
    	    
        $(o_select).parents('div.productListing').find('select.do').each(function()
        {
			v_qrystr = v_qrystr  + $(this).attr('name') + '=' + $(this).val() + '&';
			
		});
		
		$.ajax({
  			type: "GET", 
  	 		url: v_url,
  			data: v_qrystr,
 	  		success: function(msg)
 	  		{
    		 $("div.price_"+rowID).html(msg);
 	  		}
		});
		
}


$(document).ready(function(){
	 $('div.productListing').each(function(v_index){                           
	 	$(this).find('select.do').change(function(){
			fnGetPrice(this);
		});    
	 });
});

$(document).ready(function(){
	$("fieldset").each(function(){
		var heading = $("legend",this).remove().text();
		$("<h3></h3>").text(heading).prependTo(this);
	});
});

/*
var fnGetPrice = function(o_select){ 
        $(o_select).parents('dd').find('select').each(function()
        {
            if (this.value.length == 0){return false;}
        });
        	
        var v_url = 'http://www.cabcononline.co.uk/ajax/getOptionPrices.php';
        var rowID = $(o_select).parents('dl').attr('id');  
    	var v_qrystr = 'p_id=' + rowID +'&';
    	    
        $(o_select).parents('dl').find('select.do').each(function()
        {
			v_qrystr = v_qrystr  + $(this).attr('name') + '=' + $(this).val() + '&';
			
		});
		
		$.ajax({
  			type: "GET", 
  	 		url: v_url,
  			data: v_qrystr,
 	  		success: function(msg)
 	  		{
    		 $("div.price_"+rowID).html(msg);
 	  		}
		});
		
}


$(document).ready(function(){
	 $('dl.productListing').each(function(v_index) {                            
		$(this).find('select.do').change(function(){
			fnGetPrice(this);
		});    
	 });
});

/*
       


/*

$(function(){
	$("input.btn, a.inputStyle").css({"border":"none","background":"none"}).wrap("<div class='inner'><div class='demo'></div></div>");
	$('.demo').corner("round 6px");
	$('.demo').corner("round 6px").parent().css('padding', '3px').corner("round 8px")
		
});
*/
 
$(document).ready(function(){
	$("input.btn, a.inputStyle").hover(function(){
		$(this).addClass("hover");
	},
	function() {
		$(this).removeClass("hover");	
	});
});

$(document).ready(function(){
	$("input.btn, a.inputStyle").focus(function(){
		$(this).addClass("hover");
	});
});



$(document).ready(function() {
   	$('#sameAsBilling').click(function() {
    if ($("#sameAsBilling").is(":checked")) 
    {
   		$(".billingtext").text("Deselect");
    	$('#shipping_fields input').each(function(i) { 
   		$(this).val( $('#billing_fields input').eq(i).val() );
    	});
 		var bcounty = $("select#billing_county").val();
    	$("select#shipping_county").val(bcounty);                    
    } 
    else 
    {
  	 
  	 	$(".billingtext").text("Same as billing");
    	$('#shipping_fields input').each(function(i) { 
     	$(this).val(""); 
    	});
    	$("select#shipping_county").val("");
    }
    });
});


$(document).ready(function() {
	$("select.deliveryCost").change(function() {
		if ((this).value == '')
		{
			$('.revisedTotals').load('ajax/getDeliveryCost.php',{'value':(this).value},function() {
				$("td.delCost").text("");
			});
		}
		else
		{
			$('.revisedTotals').load('ajax/getDeliveryCost.php',{'value':(this).value});
		}
	});
});


$(document).ready(function() {
	$("table.orderItems, table.addressBook, table.cartTable").each(function() {
		$("tr:last",this).css({"border":"none"});
	});
});

$(document).ready(function(){
	$("#billing_fields label, ").each(function(){
		var Original = $(this).text();
		var New = Original.replace(/Billing/,"");
		New = New.replace(/[*]/,"");
		$(this).text(New);
		$(this).append("<span class=\"req\">*</span>");
	});
});

$(document).ready(function(){
	$("#shipping_fields label, ").each(function(){
		var Original = $(this).text();
		var New = Original.replace(/Shipping/,"");
		New = New.replace(/[*]/,"");
		$(this).text(New);
		$(this).append("<span class=\"req\">*</span>");
	});
});

$(document).ready(function(){
	function throwalert(message)
	{
		
		var Message = $("<div class='inlinemessage'>"+message+"</div>");
		Message.insertAfter("#wrapper").hide();
		var MessageWidth = Message.width();
		var Padding = Message.css("padding-left").replace(/px/,"");
		var TotalMessagwWidth = MessageWidth + (Padding * 2);
		var WindowWidth = $(window).width();	
		var DiffWidth = (WindowWidth - TotalMessagwWidth)/2;
					
		$("#modalLayer").bgiframe().show();
		Message.css({"left":DiffWidth+"px"}).
		hide().
		fadeIn("slow").
		animate({opacity: 0.0}, 1500,function(){
			($(this).remove());
			($("#modalLayer").fadeOut("slow"));
		});	
	}
	
	$("form.createOrder :submit").click(function() {
		if ($("select.deliveryCost").val() == '')
		{

			throwalert("Can you enter your delivery method please");
			return false;
		}
		if ($("select.paymentMethod").val() == '')
		{
			throwalert("Can you enter your payment method please");
			return false;
		}
		return true;
	});
});

$(function(){
	$(".printSummary").colorbox({width:"820px", height:"160%"});
	/*
	$("a.print").live("click",function (){
		$(".orderSummary").print();
		return false;
	});
	*/
	
});

$(function(){
	$(".showContent").colorbox({width:"800px", height:"70%"});
});


$(document).ready(function(){
	$("table.information tr:last td").css({"padding":"10px 0 10px 0"})
});

$(document).ready(edit_order_init);
function edit_order_init()
{
	$("a.editOrder").click(edit_order);
}
function edit_order()
{
	var id  = getUrlVars(this.href)["id"];
	$.getJSON('http://www.cabcononline.co.uk/ajax/editOrder.php?ajax=true&id='+id,returntocart);
	return false;
}
function returntocart()
{
	location.href="http://www.cabcononline.co.uk/cart.html";
}

$(document).ready(delete_order_init);
function delete_order_init()
{
	$("a.deleteOrder").click(delete_order);
}
function delete_order()
{
	var id  = getUrlVars(this.href)["id"];
	var user_id = getUrlVars(this.href)["user_id"];		
	this.id = 'delete_link_'+id;
	$.getJSON('http://www.cabcononline.co.uk/ajax/deleteOrder.php?ajax=true&id='+id+'&user_id='+user_id,remove_row);
	return false;
}
function remove_row(data)
{	
	if(data.empty == "1")
	{
		$("table").remove()
		$("p.ordersmessage").text("You have yet to place any orders with us");
	}
	else
	{
		$("#delete_link_"+data.id)
		.closest("tr")
		.remove()
		$("table tr:last").css({"border-bottom":"1px solid #bd0b0f"});
	}
}


$(document).ready(delete_address_init);
function delete_address_init()
{
	$("a.deleteAddress").click(delete_address);
}
function delete_address()
{
	var id = getUrlVars(this.href)["delete"];
	var user_id = getUrlVars(this.href)["user_id"];		
	this.id = 'delete_link_'+id;
		$.getJSON('http://www.cabcononline.co.uk/ajax/deleteAddress.php?ajax=true&id='+id+'&user_id='+user_id,remove_address);
	return false;
}
function remove_address(data)
{
	if (data.empty == "0")
	{
		var form = $("form.addressBook");
		$("table").remove()
		$("input.selectAsMainAddress").remove()
		$("<p>You have no delivery addresses set up - please add one now</p>").prependTo(form);
	}
	if (data.empty == "1")
	{
		$("#delete_link_"+data.id)
		.closest("tr")
		.remove()
		$("table tr:last").css({"border-bottom":"1px solid #bd0b0f"});
		
		if (data.priority == "1")
		{
		 if ($("form.addressBook input").not(":checked")) 
		 {
		 	$("form.addressBook input:first").attr("checked","checked");
		 }
		}
	}
}


function getUrlVars(url)
{
 
	var vars = [], hash;
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
    
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

$(document).ready(function(){
	$("a[rel='imageGallery']").colorbox();
});

$(document).ready(function() {
  $('#loading').ajaxStart(function() {
    $(this).show();
  }).ajaxStop(function() {
    $(this).hide();
  });
});


    
