//Global base shipping, used as javascript quirks. 
var baseShipping = 0.0;
$(document).ready(function() {
	if(typeof(price)!='undefined') {
		$('.input_field').val(1);
		//$('#total').html(calculateTotal(1));
		baseShipping = parseFloat($("input[@name='shippingdest']").val());
		shoppingCart();
	}
	$('.btn').click(function() {
		shoppingCart();
	});
	$('.input_field').change(function() {
		shoppingCart();
	});
	$("input[@name='shippingdest']").click(function() {
		//Does not update on click, need to pass it the value
		baseShipping = parseFloat($(this).val());
		shoppingCart();
	});
	$(".overseasinput").click(function() {
		$(".overseastr").removeClass("shippinglight");
		$(".austr").addClass("shippinglight");
	});
	$(".ausinput").click(function() {
		$(".austr").removeClass("shippinglight");
		$(".overseastr").addClass("shippinglight");
	});
	$(".btn").hover(function() {
		$(this).addClass("btnhover");
	},function() {
		$(this).removeClass("btnhover");	
	});
	
	//IE 6 FIX
	if (jQuery.browser.msie) {
	    if(parseInt(jQuery.browser.version) < 7) {
	    	$.ifixpng(iefiximagepath);
	    	$('img[@src$=.png]').ifixpng(); 
	    	$('#header h1,.btn').ifixpng(); 
	  		$("#menu li").hover(function(){
	  			$(this).children("ul").css("display","block");
	  			$(this).children("a").css("background-color","#FFF");
	  		},function() {
	  			$(this).children("ul").css("display","none");
	  			$(this).children("a").css("background","transparent");
	  		});
	  		$("#menu li.selected").hover(function(){
	  			$(this).children("a").css("color","#647075");
	  		},function() {
	  			$(this).children("a").css("color","#FFF");
	  		});
	  		$("#menu li ul li").hover(function() {
	  			$(this).children("a").css("background-color","#cdd2d4");
	  			$(this).css("background-color","#cdd2d4");
	  			//$(this).parent().parent().children("a").css("background-color","#FFF");
	  		}, function() {
	  			$(this).children("a").css("background-color","FFFFFF");
	  			$(this).css("background-color","#FFFFFF");
	  			//$(this).parent().parent().children("a").css("background","transparent");
	  		});
	  		$("#menu li ul li a").css("padding-right","2px");
	    }
	}
	 
	//Gallery
	$(".thumb").click(function() {
		$(".selected").removeClass("selected");
		$(this).addClass("selected");
		var id=$(this).attr('id').substring(5);
		$("#fullsizecontainer").html($("#fullsize"+id).html());
	});
	
	//Form Add comment validation
	$("#commentform").validate();
});

function isInteger(s) { 
	var i;
	for (i = 0; i < s.length; i++)	{
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	return true;
}

function calculateShipping(qty) {
	qty = parseInt(qty);
	var shipping = 0.0;
	if(qty > 1)
		shipping = baseShipping+((qty-1)*(baseShipping*0.5));
	else
		shipping = baseShipping;
		
	return shipping.toFixed(2)
}

function calculateTotal(qty,shipping) {
	qty = parseInt(qty);
	var total = (qty*price)+parseFloat(shipping);
	return total.toFixed(2);
}

function shoppingCart() {
	var quantity = $('.input_field').val();
	if(!isInteger(quantity)) {
		quantity = 1;
		$('.input_field').val(quantity);
	}
	
	qty = parseFloat(quantity);
	if(qty == 0) {
		quantity = 1;
		$('.input_field').val(1);
	}	
	var shipping = calculateShipping(quantity);
	var total = calculateTotal(quantity,shipping);
	
	$('#total').html('$'+total);
	//Set all the information to the paypal button
	$('input[@name=quantity]').val(quantity);	
	$('input[@name=shipping]').val(shipping);
	$('input[@name=amount]').val(price);	
}

/*** ROTATION ELEMENT **/
jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (typeof callback == 'function')  
            callback();  
    }); 
}; 

var rotationElements= new Object();

function checkRotationId(id) {
	if (rotationElements[id]==undefined) {
		rotationElements[id]= new Object();
		rotationElements[id].elements= new Array();
		rotationElements[id].current= 0;
		rotationElements[id].firstCall= true;
	}
}
function getRotationElements(id) {
	return rotationElements[id].elements;
}
function addRotationElement(id,data) {
	checkRotationId(id);
	rotationElements[id].elements.push(data);
}
function setRotationElements(id,array) {
	rotationElements[id].elements = array;
}
function getRotationCurrent(id) {
	return rotationElements[id].current;
}
function setRotationCurrent(id,pos) {
	return rotationElements[id].current= pos;
}
function getRotationFirstCall(id) {
	return rotationElements[id].firstCall;
}
function setRotationFirstCall(id,setting) {
	return rotationElements[id].firstCall= setting;
}

function showCurrentRotationElement(id) {
	checkRotationId(id);
	var elements= getRotationElements(id);
	var container = document.getElementById(id);
	if (container) {
		container.innerHTML=elements[getRotationCurrent(id)];
	}
}
function nextRotationElement(id) {
	var elements= getRotationElements(id);
	var pos= getRotationCurrent(id)+1;
	if(pos >= elements.length) pos = 0;
	setRotationCurrent(id,pos)
	showCurrentRotationElement(id);
}
function prevRotationElement(id) {
	var elements= getRotationElements(id);
	var pos= getRotationCurrent(id)-1;
	if(pos < 0) pos = elements.length-1;
	setRotationCurrent(id,pos)
	showCurrentRotationElement(id);
}
function shuffle ( myArray ) {
	var i = myArray.length;
	if ( i == 0 ) return false;
	while ( --i ) {
	    var j = Math.floor( Math.random() * ( i + 1 ) );
	    var tempi = myArray[i];
	    var tempj = myArray[j];
	    myArray[i] = tempj;
	    myArray[j] = tempi;
   }
   return myArray;
}
function autoRotationElement(id) {
	if(getRotationFirstCall(id)) {
		setRotationFirstCall(id,false);
		setRotationElements(id,shuffle(getRotationElements(id)));
	}
	else 
		$('#'+id).css('opacity',0);
	nextRotationElement(id);	
	$('#'+id).fadeTo('slow',1);
	setTimeout("autoRotationElement('"+id+"')", 13000); /*4000*/
}