$("document").ready(function(){

	$("a.submit").click(function(e){
	 e.preventDefault();
	 $(this).parents("form").submit();
	});

	$("div.gallery:gt(1)").slideUp();

	$(".gallery_thumb .img_black a.image").click(function(event){
		event.preventDefault();
		var imgurl = $(this).attr("href");
		$("#prev_div ").fadeOut();
		$("#prev_div #prev_img").load(function(){
		  $("#prev_div").fadeIn(900);
		}).attr("src", imgurl);
	});

	$("a.retail_image").click(function(event){
		event.preventDefault();
		var imgurl = $(this).attr("href");
		$("#prev_div ").fadeOut();
		$("#prev_div #prev_img").load(function(){
		  $("#prev_div").fadeIn(900);
		}).attr("src", imgurl);
	});

	$("div#prev_div").click(function(event){
	 $("#prev_div").fadeOut();
	});

	$("a.gallery_header").click(function(e){
	 e.preventDefault();
	 $(this).siblings("div.gallery").slideToggle(500);
	});

	$("a.order_btn").click(function(e){
	 e.preventDefault();
	 imgid = $(this).attr("id");
	 var url = $(this).parents("div.gallery_thumb").children("div.img_black").children("a.image").attr("href");
	 $("div#AddToOrder div.content").hide();
	 $("div#AddToOrder").fadeIn(function(){
	  $("li#step1 img#preview").load(function(){
	   $("div#AddToOrder ul.steps li").show(600);
	   $("li#step2 img#size_preview").attr("src", url);
	   $("li#step3 img#size_preview").attr("src", url);
	   $("div#AddToOrder div.content").fadeIn();
	  }).attr("src", url);
	 });
	});

	$("div#AddToOrder a#cancel").click(function(e){
	 e.preventDefault();
	 $("div#AddToOrder").fadeOut();
	});

	$("div#AddToOrder a#next").click(function(e){
	 e.preventDefault();
	 $("div#AddToOrder ul.steps li:visible:first").hide(600);
	});

	$("div#AddToOrder a#back").click(function(e){
	 e.preventDefault();
	 $("div#AddToOrder ul.steps li:hidden:last").show(600);
	});

	$("li#step1 input#qty").blur(function(){
	 $("li#step2 span#qty").html($(this).val());
	 $("li#step3 span#qty").html($(this).val());
	 var priceEach = parseFloat($("li#step2 input[name=size]:checked").val());
	 var subtotal = priceEach * parseFloat($("li#step1 input#qty").val());
	 $("li#step2 h3#subtotal span").html(number_format(subtotal,2,".",","));
	});
	$("li#step2 input[name=size]:first").attr("checked", "checked");

	$("li#step2 input[name=size]").click(function(){
 	 var priceEach = parseFloat($("li#step2 input[name=size]:checked").val());
	 var subtotal = priceEach * parseFloat($("li#step1 input#qty").val());
	 $("li#step3 span#size").html($(this).attr("title"));
	 $("li#step2 h3#subtotal span").html(number_format(subtotal,2,".",","));
	 $("li#step3 h3#subtotal span").html(number_format(subtotal,2,".",","));
	 $("li#step2 img#size_preview").css({width: ((priceEach*1.5)+130)+"px"});
	 $("li#step3 img#size_preview").css({width: ((priceEach*1.5)+130)+"px"});
	});

 	var priceEach = parseFloat($("li#step2 input[name=size]:checked").val());
	var subtotal = priceEach * parseFloat($("li#step1 input#qty").val());
	$("li#step2 h3#subtotal span").html(number_format(subtotal,2,".",","));
	$("li#step3 h3#subtotal span").html(number_format(subtotal,2,".",","));

	$("div#AddToOrder a#checkout").click(function(e){
	 e.preventDefault();
	 var vqty = $("li#step1 input#qty").val();
	 var vsize = $("li#step2 input[name=size]:checked").attr("id");

	 $("div#AddToOrder div.content").fadeOut();
	 $.post("index.php?page=gallery", { action : "AddToOrder", qty : vqty, image_id : imgid, size : vsize }, function() {
	  $("div.left_col").load("index.php?page=gallery div.left_col", function(){
	   window.location.href = "index.php?page=order";
	  });
	  $("li#step2 input[name=size]").removeAttr("checked");
	  $("li#step2 input[name=size]:first").attr("checked", "checked");
	  $("li#step1 input#qty").val("1");
	 });
	});

	$("div#AddToOrder a#continue").click(function(e){
	 e.preventDefault();
	 var vqty = $("li#step1 input#qty").val();
	 var vsize = $("li#step2 input[name=size]:checked").attr("id");
	 $("div#AddToOrder div.content").fadeOut();
	 $.post("index.php?page=gallery", { action : "AddToOrder", qty : vqty, image_id : imgid, size : vsize }, function() {
	  var url = $("input#current_url").val() + " div.left_col";
	  $("div.left_col").load(url, function(){
	   $("div#AddToOrder").fadeOut();
	  });
	  $("li#step2 input[name=size]").removeAttr("checked");
	  $("li#step3 span#qty").html("1");
	  $("li#step3 span#size").html($("li#step2 input[name=size]:first").attr("title"));
	  $("li#step2 input[name=size]:first").attr("checked", "checked");
	  $("li#step1 input#qty").val("1");
	 });
	});
	
});

var imgid;

function number_format(number, decimals, dec_point, thousands_sep) {
    // Formats a number with grouped thousands  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/number_format    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // +    revised by: Luke Smith (http://lucassmith.name)
    // +     bugfix by: Diogo Resende
    // +     bugfix by: Rival    // +      input by: Kheang Hok Chin (http://www.distantia.ca/)
    // +   improved by: davook
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Jay Klehr
    // +   improved by: Brett Zamir (http://brett-zamir.me)    // +      input by: Amir Habibi (http://www.residence-mixte.com/)
    // +     bugfix by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Theriault
    // *     example 1: number_format(1234.56);
    // *     returns 1: '1,235'    // *     example 2: number_format(1234.56, 2, ',', ' ');
    // *     returns 2: '1 234,56'
    // *     example 3: number_format(1234.5678, 2, '.', '');
    // *     returns 3: '1234.57'
    // *     example 4: number_format(67, 2, ',', '.');    // *     returns 4: '67,00'
    // *     example 5: number_format(1000);
    // *     returns 5: '1,000'
    // *     example 6: number_format(67.311, 2);
    // *     returns 6: '67.31'    // *     example 7: number_format(1000.55, 1);
    // *     returns 7: '1,000.6'
    // *     example 8: number_format(67000, 5, ',', '.');
    // *     returns 8: '67.000,00000'
    // *     example 9: number_format(0.9, 0);    // *     returns 9: '1'
    // *    example 10: number_format('1.20', 2);
    // *    returns 10: '1.20'
    // *    example 11: number_format('1.20', 4);
    // *    returns 11: '1.2000'    // *    example 12: number_format('1.2000', 3);
    // *    returns 12: '1.200'
    var n = !isFinite(+number) ? 0 : +number, 
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }    return s.join(dec);
}
function PreloadImg(imgurl)
{
//div_obj = document.getElementById("prev_div");
//document.getElementById("prev_img").src = imgurl;
}

function ShowATOdiv( imageid )
{
var divObj = document.getElementById('AddToOrder');
var btnObj = document.getElementById('button' + imageid);
document.getElementById('image_id').value = imageid;
divObj.style.display = 'block';
setTimeout("positionATO('AddToOrder')", 10);
}

function HideATOdiv()
{
var divObj = document.getElementById('AddToOrder');
divObj.style.display = 'none';
}

function highlite(id, color)
{
	//document.getElementById(id).style.background  = "#ffffff";
	//document.getElementById(id).style.background  = "#ffffff";
	//document.getElementById(id).style.background  = "#ffffff";
	
	if (color == 1)
		{ color = '#aedeee';}
	else
		{ color = '#ffffff'; }
	element = document.getElementById(id);
	
	if (element.style.background != '#9999FF')
		{ element.style.background = color; }
}

function select(id, value)
{
	element = document.getElementById(id);
	
	//document.getElementById("size_sml").style.background  = "#ffffff";
	//document.getElementById("size_med").style.background  = "#ffffff";
	//document.getElementById("size_lrg").style.background  = "#ffffff";
	element.style.background = '#9999ff'; 
	
	txtbox = document.getElementById("size");
	txtbox.value=value;
}
	
function SubmitATO()
{
//Add some validation in here somewhere
if (document.getElementById("size").value != '')
	{ document.addImageForm.submit(); }
else
	{ alert('Please select a size...'); }
}

function validateOrderForm()
{
dosubmit = true;
	if (document.getElementById('name').value == '')
		{ dosubmit = false; document.getElementById('name').style.backgroundColor = "#efefef"; }
	if (document.getElementById('phone').value == '')
		{ dosubmit = false; document.getElementById('phone').style.backgroundColor = "#efefef"; }
	if (document.getElementById('email').value == '')
		{ dosubmit = false; document.getElementById('email').style.backgroundColor = "#efefef"; }
	if (document.getElementById('post').value == '')
		{ dosubmit = false; document.getElementById('post').style.backgroundColor = "#efefef"; }
	if (dosubmit == false)
		{ 
			alert('Some errors were detected. Please check that you have filled in each field correctly'); 
		}
	else
	{
		document.getElementById('orderdetailsform').submit();
	}
}

function resetValidation()
{
document.getElementById('name').style.backgroundColor = '#ffffff';
document.getElementById('phone').style.backgroundColor = '#ffffff';
document.getElementById('email').style.backgroundColor = '#ffffff';
document.getElementById('post').style.backgroundColor = '#ffffff';
}

function checkPasswordForm()
{
var newpasswd = document.getElementById('newpasswd');
var confirmpasswd = document.getElementById('confirm');
if (newpasswd.value == confirmpasswd.value)
	{
	document.passwordform.submit();
	}
else
	{
	alert('Your new passwords do not match, please re-enter your confirmation password');
	confirmpasswd.value = '';
	}
}

function swapimg (id, url)
{
imgobj = document.getElementById(id);
imgobj.src = url;
}

function positionIt(divid) {
	if( document.getElementById ) {
		// Get a reference to divTest and measure its width and height.
		var div = document.getElementById(divid);
		var divWidth = div.offsetWidth ? div.offsetWidth : div.style.width ? parseInt( div.style.width ) : 0;
		var divHeight = div.offsetHeight ? div.offsetHeight :  div.style.height ? parseInt( div.style.height ) : 0;
		var vpHeight = getViewportHeight();
		var centerpos = (vpHeight - divHeight) / 2;
		// Calculating setX and setX so the div will be centered in the viewport.
		var setX = 450 - 337.5;//900/2 - divWidth/2;
		var setY = window.scrollY + centerpos;
		// If setX or setY have become smaller than 0, make them 0.
		//if( setX < 0 ) setX = 0;
		//if( setY < 0 ) setY = 0;
		
		// Position the div in the center of the page and make it visible
		
		//div.style.left = setX + "px";
		//div.style.top = setY + "px";
	}
}

function getViewportHeight() {
var x,y;
if (self.innerHeight) // all except Explorer
{
	x = self.innerWidth;
	y = self.innerHeight;
}
else if (document.documentElement && document.documentElement.clientHeight)
	// Explorer 6 Strict Mode
{
	x = document.documentElement.clientWidth;
	y = document.documentElement.clientHeight;
}
else if (document.body) // other Explorers
{
	x = document.body.clientWidth;
	y = document.body.clientHeight;
}
return y;
}

function positionATO(divid) {
	if( document.getElementById ) {
		// Get a reference to divTest and measure its width and height.
		var div = document.getElementById(divid);
		var divWidth = div.offsetWidth ? div.offsetWidth : div.style.width ? parseInt( div.style.width ) : 0;
		var divHeight = div.offsetHeight ? div.offsetHeight :  div.style.height ? parseInt( div.style.height ) : 0;
		
		var vpHeight = getViewportHeight();
		var vpWidth = getViewportWidth();
		var centerpos = (vpHeight - divHeight) / 2;
		// Calculating setX and setX so the div will be centered in the viewport.
		var setX = 450 - 160;//900/2 - divWidth/2;
		var setY = window.scrollY + centerpos;
		// If setX or setY have become smaller than 0, make them 0.
		//if( setX < 0 ) setX = 0;
		//if( setY < 0 ) setY = 0;
		
		// Position the div in the center of the page and make it visible
		
		div.style.left = setX + "px";
		div.style.top = setY + "px";
	}
};

function getScrollTop() {

var ScrollTop = document.body.scrollTop;

if (ScrollTop == 0)
{

    if (window.pageYOffset)

        ScrollTop = window.pageYOffset;

    else

        ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;

}
return ScrollTop;
}

