/* Section 1: Variables for gallery */
var number_of_gallery_pictures=0;
var gallery_picture_width=67;
var gallery_pictures_margin=0;
var gallery_pictures_maximum_margin=0;

var position_count=0;
var max_position_count=0;
var direction=-1;
var animation=null;

function delayGalleryAnimation() {
	clearInterval(animation);
	setTimeout("startGalleryAnimation()",2000);
} //end function startGalleryAnimation()

function startGalleryAnimation() {
	clearInterval(animation);
	animation=setInterval("adjustGallery()",25);
} //end function startGalleryAnimation()

function startFastGalleryAnimation() {
	clearInterval(animation);
	animation=setInterval("fastAdjustGallery()",25);
} //end function startFastGalleryAnimation()

function adjustGallery() {
	gallery_pictures_margin=gallery_pictures_margin+(direction*2);
	if (direction==-1&&gallery_pictures_margin<0) {
		gallery_pictures_margin=0; direction=1;
		delayGalleryAnimation();
    } //end if (gallery_pictures_margin<0)
	if (direction==1&&gallery_pictures_margin>gallery_pictures_maximum_margin) {
		gallery_pictures_margin=gallery_pictures_maximum_margin; direction=-1;
		delayGalleryAnimation();
	} //end if (gallery_pictures_margin>gallery_pictures_maximum_margin)
	$('#gallery ul').css({ marginLeft: (gallery_pictures_margin*-1) });
} //end function adjustGallery()

function fastAdjustGallery() {
	gallery_pictures_margin=gallery_pictures_margin+(direction*5);
	if (gallery_pictures_margin<0) { gallery_pictures_margin=0; }
	if (gallery_pictures_margin>gallery_pictures_maximum_margin) { gallery_pictures_margin=gallery_pictures_maximum_margin; }
	$('#gallery ul').css({ marginLeft: (gallery_pictures_margin*-1) });
} //end function adjustGallery()

//if the document is ready, apply all functions
$(document).ready(function() {
	//loop through all links and set external ones to target="_blank"
	$('a[rel*=external]').attr("target","_blank");
	$('a[rel*=external]').attr("title","(opens in new window)");
	$('a[rel*=nofollow]').attr("target","_blank");
	$('a[rel*=nofollow]').attr("title","(opens in new window)");
	//apply lightbox to links
	//$('a[rel*=lightbox]').lightBox();

    // Gallery
	number_of_gallery_pictures=$('#gallery ul li').length;
	if (number_of_gallery_pictures>6) {
		gallery_pictures_maximum_margin=(number_of_gallery_pictures-6)*gallery_picture_width;

		$("#gallery_previous").hover( function() {
			direction=-1;
			startFastGalleryAnimation();
		}, function() {
			startGalleryAnimation();
		}); //end $("#gallery_previous").hover(

		$("#gallery_next").hover( function() {
			direction=1;
			startFastGalleryAnimation();
		}, function() {
			startGalleryAnimation();
		}); //end $("#gallery_next").hover( function()

		$("#gallery ul li").hover( function() {
			clearInterval(animation);
		}, function() {
			startGalleryAnimation();
		}); //end $("#gallery ul li").hover( function()

		startGalleryAnimation();
	} //end if (number_of_gallery_pictures>6)

	//autosubmission for shopping basket updates
	$("table#shopping_basket_table select[name*=quantity]").bind("change", function() { this.form.submit(); }); //change quantity

	//checkout delivery same as billing
	$("#delivery_same").click( function() { if ($(this).attr('checked')) { $("#delivery_details").slideUp(200); } else { $("#delivery_details").slideDown(200); } });
    if ($("#delivery_same").attr('checked')) { $("#delivery_details").css({display:'none', visibility:'visible'}); }

	//autosubmission for frame preview
	$("form#frame_selection select").bind("change", function() { this.form.submit(); }); //change door or frame
});
