simpleCart.checkoutTo = PayPal;
simpleCart.email = "rob@amberleyautos.com.au";
simpleCart.currency = AUD;
simpleCart.cartHeaders = [ "Name", "Size", "Decrement", "Quantity" , "Increment", "Total" ];

$(document).ready(function(){
	colorBox();
	dispCart();
	khAlert();
	$("#tabs").organicTabs();
	imgRotator();
	stock_sort('ul#stock_vic');
	stock_sort('ul#stock_nsw');
	stock_sort('ul#stock_tas');
	stock_sort('ul#stock_sa');
	stock_sort('ul#stock_qld');
	stock_sort('ul#stock_wa');
	$("#stocknav,.btt").localScroll({
		hash: true,
		duration: 300,
		offset: {top:-35}
	});
});

function stock_sort(list) {
	var mylist = $(list);
	var listitems = mylist.children('li').get();
	listitems.sort(function(a, b) {
		var compA = $(a).find('h4').text().toUpperCase();
		var compB = $(b).find('h4').text().toUpperCase();
		return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
	})
	$.each(listitems, function(idx, itm) { mylist.append(itm); });
}

function khAlert() {
		$('.cartadd').click(function() {
			$('#itemadded').stop(true,true).fadeIn(200).delay(800).fadeOut(800);
		});
}

function dispCart() {
		$('.ieffch').css("display","inline");
		$('.nojs').css("display","none");
}

function colorBox() {
	$('a[rel^="lightbox"]').colorbox({
		maxWidth:"85%",
		maxHeight:"85%",
		scalePhotos:true,
		transition:"fade"
	});

	$('.viewcart').colorbox({
		width: "706px",
		inline: true,
		href:"#cart",
		opacity: 0.70
	});
}

function imgRotator() {
	//Show the paging and activate its first link
	$(".img_paging").show();
	$(".img_paging a:first").addClass("active");

	//Get size of the image, how many images there are, then determin the size of the image reel.
	var imageWidth = $(".img_rotator").width();
	var imageSum = $(".img_reel img").size();
	var imageReelWidth = imageWidth * imageSum;

	//Adjust the image reel to its new size
	$(".img_reel").css({'width' : imageReelWidth});

	//Paging  and Slider Function
	rotate = function(){
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

		$(".img_paging a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

		//Slider Animation
		$(".img_reel").animate({
			left: -image_reelPosition
		}, 500 );
	}; 

	//Rotation  and Timing Event
	rotateSwitch = function(){
		play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
			$active = $('.img_paging a.active').next(); //Move to the next paging
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.img_paging a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 5000); //Timer speed in milliseconds (7 seconds)
	};

	rotateSwitch(); //Run function on launch

	//On Hover
	$(".img_reel a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation timer
	});	

	//On Click
	$(".img_paging a").click(function() {
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation timer
		return false; //Prevent browser jump to link anchor
	});
}
