// Product detail thumbnails + lightbox



$(function(){



		function isTouchDevice(){
				try{
					document.createEvent("TouchEvent");
					return true;
				}catch(e){
					return false;
				}
			}
			function touchScroll(manuf_text_wrap){
				if(isTouchDevice()){ //if touch events exist...
					var el=document.getElementById(id);
					var scrollStartPos=0;

					document.getElementById(id).addEventListener("touchstart", function(event) {
						scrollStartPos=this.scrollTop+event.touches[0].pageY;
						event.preventDefault();
					},false);

					document.getElementById(id).addEventListener("touchmove", function(event) {
						this.scrollTop=scrollStartPos-event.touches[0].pageY;
						event.preventDefault();
					},false);
				}
			}





	$('.prod_tn').each(function(i){

		$(this).bind('mouseover', function(){

			$('.prod_img_wrap li').removeClass('first').css('display', 'none');



			$('#prod_img-'+i).css('display', 'block');

		});

	});

	/* */

	var lbOptions = {

		//overlayBgColor: '#5e2400',

		overlayBgColor: '#000',

		imageBtnClose: '/media/img/com/lightbox/lightbox-btn-close-2.gif'

	};

	$('.prod_med_img').lightBox(lbOptions);

	$('.prod_tn a').lightBox(lbOptions);



	encrypt_email();

	ajax_init();

});



function ajax_init() {

	update_cart();

}



// Scroll artist + designer list



function scroll_manuf_menu(current) {

	var boxsize = $("#designers_wrap").height();

	var listsize = $("#designers").height();

	var pos = parseInt( $("#designers").css("margin-top"));

	var line_h =  ($("#designers a:first").outerHeight());

	//var line_h = 14;



	var max_offset = listsize - boxsize //Maximum allowed offset - place a minus before



	//Set initial offset (to place current item on top)

	var init_offset = line_h * current;

	if (init_offset > max_offset) {

		init_offset = max_offset;

	};

	//*DEBUG*/alert( "boxsize: "+boxsize+"; listsize: "+listsize+"; pos: "+pos+"; line height: "+line_h+"; offset: "+init_offset);



	if (listsize > boxsize) {

		//Tries to place current item on top initiallly

		if ( init_offset > 0) {

			$("#designers").css({marginTop: -init_offset});

			$("#des_arrow_up").css({visibility: "visible"});

		}



		$("#des_arrow_dn").css({visibility: "visible"});

		$("#des_arrow_up").click(function(){

			$("#des_arrow_dn").css({visibility: "visible"});

			if (-boxsize > pos) {

				goal = pos + boxsize;

			} else {

				goal = 0;

				$("#des_arrow_up").css({visibility: "hidden"});

			}

			$("#designers").animate({ marginTop: goal }, "slow");

			pos = goal;

		});



		$("#des_arrow_dn").click(function(){

			$("#des_arrow_up").css({visibility: "visible"});

			if (2 * boxsize < listsize + pos) {

				goal = pos - boxsize;

				//alert("boxsize: "+boxsize+"; pos: "+pos+"; boxsize: "+boxsize+"; goal: "+goal);

			} else {

				goal = -max_offset;

				$("#des_arrow_dn").css({visibility: "hidden"});

				//alert("boxsize: "+boxsize+"; pos: "+pos+"; listsize: "+listsize+"; goal: "+goal);

			}

			$("#designers").animate({ marginTop: goal }, "medium", 'linear' );

			pos = goal;

		});



	}

}



function do_scroll_list() {

	//Scrolling homepage list

	lh = $("#sl1").height(); //list height

	ch = $("#scroll_list_wrap").height(); //container height;

	v = 0.025; //real speed

	//v = 0.1; //test speed





	//$("#sl1").css('marginTop', ch+100);

	$("#sl1").animate(

		{marginTop: 0},

		ch/v,

		"linear"

	);

	scroll_list(lh,v)



};



function scroll_list(lh,v) {

	$("#sl1").css('marginTop', 0);

	$("#sl1").animate(

		{marginTop: -lh},

		lh/v,

		"linear",

		function () {scroll_list(lh,v);}

	);

};



function home_ss() {

	$('#home_ss').fadeIn(4000);

	//*

	$('#home_ss').innerfade({

		animationtype: 'fade',

		speed: 2000,

		timeout: 4000,

		type: 'sequence'

	});

	/* */

};



function inject(url, target) {

	$(target).load(url);

}



function encrypt_email() {

	$('a.email').each( function() {

		var emailriddlerarray=[105,110,102,111,64,116,119,101,110,116,105,101,116,104,46,110,101,116]

	var encryptedemail_id60='' //variable to contain encrypted email

	for (var i=0; i<emailriddlerarray.length; i++)

		encryptedemail_id60+=String.fromCharCode(emailriddlerarray[i])



	$(this).attr( 'href', 'mailto:' + encryptedemail_id60 );

	});

}



function update_cart() {

	if( $('#ajax_cart').length >0 ) {

		$('#ajax_cart').load(
			'/store/orders/show',
			null,
			update_total
		);

	}



}



function update_total() {

	//*

	/* UNCOMMENT */

	/* /store/orders/total */

	$('#total').load('/store/orders/total', function(){

		if($('#total').html()){

		//if(true) {

			$('#checkout_btn').removeClass( 'inactive' ).

			addClass('link1').

			attr('href', '/store/orders/checkout');

			//ajax_init();

		}

	});

	/* */

	/*

	$.ajax({

		url: '/store/orders/total',

		success: function(data) {

			$('#total').html(data);

			//ajax_init();

		}

	});

	/* */

}

