$(document).ready(function(){
	$(".nav").click(function(){
		if($(this).next().is(":visible")) {
		  $(this).next().slideUp("slow"); 
		  return false;
		} else {
		  $(this).next().slideDown("slow");
		  return false;
		};
	});
});

/* Simplecart
 * ----------
 */

function calc_shipping() {
	var total = simpleCart.total;
	if(total == 0) { $('.shipping').html('0.00'); } else {
	$('.shipping').load('/calcs/shipping/'+(simpleCart.total + simpleCart.total * simpleCart.taxRate));

	}
}

function add(name,price,qty) {
	$('#added').html('Added to cart: <br /><b>'+qty+' x '+name+'</b><br /> &nbsp; @ &pound;'+price+'');
	simpleCart.add('name='+name,'price='+price,'quantity='+qty);
	$('#added').show('100', function() {
		setTimeout("$('#added').hide('100')", 3000);
	});
	calc_shipping();
}

function val(type,attr) {
	return $('[name='+type+'-'+attr+']').attr('value');
}

function update(name) { //Fetch a price when someone enters a value in the box
		var me = $('[name='+name+']');
		var elem = me.parent().parent();
		type = elem.attr('id').toLowerCase();
		me.after('<img src="http://cdn.bramptonframing.com/img/loading/loading19.gif" alt="Refreshing price..." class="loading" />');
		
	if(type == "moulding-chop") {
				type = "chop";
				var opts = "qty="+val(type,'qty')+"&width="+val(type,'width')+"&height="+val(type,'height')+"&glass="+$('[name='+type+'-glass]').attr('checked')+"&backing="+$('[name='+type+'-backing]').attr('checked')+"&fixings="+$('[name='+type+'-fixings]').attr('checked');
				

	} else if(type == "bespoke-frame") {
				type = "frame";
				var opts = "qty="+val(type,'qty')+"&width="+val(type,'width')+"&height="+val(type,'height')+"&glass="+$('[name='+type+'-glass]').attr('checked')+"&backing="+$('[name='+type+'-backing]').attr('checked')+"&fixings="+$('[name='+type+'-fixings]').attr('checked');
		
			} else if(type == "std") {
				type = "std";
				var opts = "qty="+val(type,'qty')+"&width="+val(type,'width')+"&height="+val(type,'height')+"&glass="+$('[name='+type+'-glass]').attr('checked')+"&backing="+$('[name='+type+'-backing]').attr('checked')+"&fixings="+$('[name='+type+'-fixings]').attr('checked');
		
		
			} else {
				var opts = "qty="+me.attr('value');
			}
			$.ajax({
				type: "POST",
				url: "/picture_frame_mouldings/ajax_calc/"+type+"/190400000",
				data: opts,
				dataType: "json",
				success: function(data){
					elem.find('.cart').html(data.msg);
					elem.find('.cart_button').html(data.cart);
					$('.loading').remove();
					if(type == "frame" | type == "chop" | type == "std") {
						elem.find('.glass_price').html(data.glass);
						elem.find('.backing_price').html(data.backing);
						elem.find('.fixing_price').html(data.fixings);
						if(oldright != data.right) {
							elem.find('.right').fadeOut('fast', function() {
								$(this).html(data.right);
								$(this).fadeIn('fast');
								oldright = data.right;
							});
						};
					}
				}
			});
	}
	
var type;
var oldright;

$(document).ready(function(){

	if (typeof(simpleCart) != 'undefined')
		{
		simpleCart.email = "sales@bramptonframing.com";
		simpleCart.currency = GBP;
		simpleCart.taxRate = 0.2;
		simpleCart.cartHeaders = ["Quantity_noHeader", "Name_noHeader", "Price_noHeader", "Remove_noHeader" ];

		
		calc_shipping();
		$('.itemRemove a').click(function() { calc_shipping(); })
		$('.tab_content').each(function() { //Build the tab list dynamically
			//This makes sure that the page still look vaguely respectable when Javascript is disabled
			$('.tabs').append('<li><a href="#' + $(this).attr('id') + '">' + $(this).attr('id').replace(/-/gi, " ") + '</a></li>');	
		});
		
		$(".tab_content").hide(); //Hide all content
		$("ul.tabs li:first").addClass("active").show(); //Activate first tab
		$(".tab_content:first").show(); //Show first tab content

		$("ul.tabs li").click(function() { //Tab click handler
			$("ul.tabs li").removeClass("active"); //Remove any "active" class
			$(this).addClass("active"); //Add "active" class to selected tab
			$(".tab_content").hide(); //Hide all tab content

			var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
			$(activeTab).fadeIn(); //Fade in the active ID content
			return false;
		});
		
		$('.qty').show();  //Show the quantity boxes when the page loads, so that people without Javascript don't see bits that don't do anything
		$('.cart-footer').show(); //Only show the shopping cart bits if JS is enabled, too.
		$('.cart-footer').click(function() { //Handle the cart popup menu
			if($('.cart-popup-click').html() == 'Show Cart') { //The cart menu is closed
				$('#cart-popup').show('fast'); //So fade it in
				$('.cart-popup-click').html('Hide Cart'); //and set the text to close it again
			} else { //The cart menu is open already
				$('#cart-popup').fadeOut('fast'); //so fade it out
				$('.cart-popup-click').html('Show Cart'); //and set the text to open it
			};
			return false; //make sure that clicking the hyperlink doesn't do anything crazy to the page
		});
				
		$('.qty_box').keyup(function() { update($(this).attr('name')) });		
		$('.qty_chk').change(function() { update($(this).attr('name')); });
		$('.input_box').keyup(function() { update($(this).attr('name')); });
		}

	});


