
var imageWidth = 0;
var timer = null;
var timer2 = null;
var timer3 = null;
var cartOpen = 0;
var scrollTop = 0;
var scrollStep = 0;

function addToCart() {
	var prod_id = document.getElementById("prod_id").value;
	var opt0 = document.getElementById("option0");
	if(opt0 != null) {
		var option0 = opt0.value;
		var option_select0 = trim(document.getElementById(option0).value);
	}
	var opt1 = document.getElementById("option1");
	if(opt1 != null) {
		var option1 = opt1.value;
		var option_select1 = trim(document.getElementById(option1).value);				
	}
	var opt2 = document.getElementById("option2");
	if(opt2 != null) {
		var option2 = opt2.value;
		var option_select2 = trim(document.getElementById(option2).value);				
	}
	
	// use ajax to add to cart
	var poststring = "id=" + prod_id;
	if(option_select0 != null) {
		poststring = poststring + "&option0=" +  option0 + "&" + option0 + "=" + option_select0;	
	}
	if(option_select1 != null) {
		poststring = poststring + "&option1=" + option1 + "&" + option1 + "=" + option_select1;	
	}
	if(option_select2 != null) {
		poststring = poststring + "&option2=" +  option2 + "&" + option2 + "=" + option_select2;	
	}
	var xmlhttp;
	if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
	  	xmlhttp=new XMLHttpRequest();
	} else {// code for IE6, IE5
	  	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			// get ajax return to update the minicart
			document.getElementById("cart-contents").innerHTML=xmlhttp.responseText;
			document.getElementById("cartLabel").style.display="block";
			document.getElementById("cartValue").innerHTML = document.getElementById("cartValue").innerHTML * 1 + 1;
			if(document.getElementById("build").value == "true") {
				document.getElementById("cart-link").href = "http://pearlsforgirls.com.au/index.php?p=Cart";
			} else {
				document.getElementById("cart-link").href = "http://pearlsforgirls.com.au/Cart";
			}
			moveProductImage();
	    }
	}
	xmlhttp.open("POST","http://pearlsforgirls.com.au/addToCart.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send(poststring);
	
	return false;
}
	
var currentScroll = 0;
var scrollPosition = new Array();
scrollPosition[-8] = -3516;
scrollPosition[-7] = -3223;
scrollPosition[-6] = -2930;
scrollPosition[-5] = -2637;
scrollPosition[-4] = -2344;
scrollPosition[-3] = -2051;
scrollPosition[-2] = -1758;
scrollPosition[-1] = -1465;
scrollPosition[0] = -1172;
scrollPosition[1] = -879;
scrollPosition[2] = -586;
scrollPosition[3] = -293;
scrollPosition[4] = 0;
scrollPosition[5] = 293;
scrollPosition[6] = 586;
scrollPosition[7] = 879;
scrollPosition[8] = 1172;
scrollPosition[9] = 1465;
scrollPosition[10] = 1758;
scrollPosition[11] = 2051;

var num_new;
var newScrollPosition = new Array();
newScrollPosition[-1] = 0;
newScrollPosition[0] = -240;
newScrollPosition[1] = -480;
newScrollPosition[2] = -720;
var nextPos = 0;

$(document).ready(function(){
	$('.shorten').truncate({maxLength: 230});
	var view_link = "News/" + $("#view_link").val();
	$('.blog').truncate({maxLength: 230, more: '...Read More', link: view_link});
	$("#cart-contents").css({opacity:0, marginTop: "-30px"});
	$("#leftScroll").css({opacity:0});
	$("#rightScroll").css({opacity:0});	
	$("#scroller").css({left: scrollPosition[0] + "px"});
	$("#magnifier").css({display:"none"});
	$("#magnified").css({opacity:0, display:"none"});
	
	$("#view-cart").mouseover(function(){
		//openCart();
	  	showCartContents();
  	}).mouseout(function(){
	  	hideCartContents();
  	}); 
	$('#prodImage').bind('mouseenter', function() {
		$("#magnifier").css({display:"block"});
		showLargeImage();
	}).bind('mouseleave', function() {
		$("#magnifier").css({display:"none"});
		hideLargeImage();
	});  	
	$('#outerMenu').bind('mouseenter', function() {
		showScrollers();
	}).bind('mouseleave', function() {
		hideScrollers();
	});  	
  	$("#leftScroll").click(function(){
	  	moveGraphicsMenu('right');
  	}).mouseover(function(){
	  	$(this).css('cursor', 'pointer');
  	});  	
  	$("#rightScroll").click(function(){
	  	moveGraphicsMenu('left');
  	}).mouseover(function(){
	  	$(this).css('cursor', 'pointer');
  	});  	
	timer = $.timer(function() {
		autoScroll();
    });
	timer.set({ time : 5000, autostart : true });
	
   $("#prodImage").mousemove(function(e){
	   followMouse(e.pageX, e.pageY);
   }); 	
   
   num_new = $("#num_new").val();
   if(num_new > 0) {
	   // start pos = 240;
	   $("#new_scroller").css({left: newScrollPosition[0] + "px"});
		timer3 = $.timer(function() {
			newScroll();
	    });
		timer3.set({ time : 3000, autostart : true });
		$('#new_frame').bind('mouseenter', function() {
			timer3.pause();
		}).bind('mouseleave', function() {
			timer3.play();
		});  	
   }
});

function followMouse(mouse_X, mouse_Y) {

	var image = document.getElementById("prodImage");
		
	var imageLeft = findPosX(image);
	var imageTop = findPosY(image);
	
	var xcoord = mouse_X - imageLeft - 100;
	var ycoord = mouse_Y - imageTop - 60;	
	var mousex = mouse_X - imageLeft;
	var mousey = mouse_Y - imageTop;	
	
	if(xcoord < 20) xcoord = 20;
	if(ycoord < 60) ycoord = 60;

	$("#magnifier").css({left: xcoord + "px", top: ycoord + "px"});
	
	var mainImageWidth = image.offsetWidth;
	//var mainImageWidth = $("#prodImage").width();
	var mainImageHeight = image.offsetHeight;
	//var mainImageWidth = $("#prodImage").height();
	var imageWidth = $("#magnifiedImg").width();
	var imageHeight = $("#magnifiedImg").height();

	var xConversion = imageWidth/mainImageWidth;
	var yConversion = imageHeight/mainImageHeight;
	var leftmargin = (mousex * xConversion - 350) * -1;
	if(leftmargin > 0) leftmargin = 0;
	if(leftmargin < (imageWidth - 400) * -1) leftmargin = (imageWidth - 400) * -1;
	var topmargin = (mousey * yConversion - 350) * -1;
	if(topmargin > 0) topmargin = 0;
	if(topmargin < (imageHeight - 400) * -1) topmargin = (imageHeight - 400) * -1;
	$("#magnifiedImg").css({margin: topmargin + "px 0px 0px " + leftmargin + "px"});	
	
	//$("#currentPrice2").html(imageLeft +' - '+ imageTop);
			
}


function newScroll() {
	nextPos++;
	if(nextPos >= (num_new - 1)) {
		nextPos = 0;
		$("#new_scroller").css({left: newScrollPosition[-1] + "px"});				
	} 
	$("#new_scroller").animate({left: newScrollPosition[nextPos] + "px"},500);		
}

function autoScroll() {
	moveGraphicsMenu('left');
}

function moveGraphicsMenu(direction) {
	if(direction == 'right') {
		if(currentScroll == 4) {
			$("#scroller").css({left: scrollPosition[-4] + "px"});
			currentScroll = -4;
		}
		if(currentScroll < 5) {
			currentScroll++;	
		}
	} else if(direction == 'left') {
		// reset for continual scrolling
		if(currentScroll == -8) {
			$("#scroller").css({left: scrollPosition[0] + "px"});
			currentScroll = 0;
		}
		if(currentScroll > -9) {
			currentScroll--;
		} 
	}
	$("#scroller").animate({left: scrollPosition[currentScroll] + "px"},500);			
}

function showScrollers() {
	timer.pause();
	$("#leftScroll").animate({opacity:1},500);	
	$("#rightScroll").animate({opacity:1},500);
}
	
function hideScrollers() {
	timer.play();
	$("#leftScroll").animate({opacity:0},500);
	$("#rightScroll").animate({opacity:0},500);
}

function showCartContents() {
	$("#cart-contents").animate({opacity:1, marginTop: "0px"},500);
}
	
function hideCartContents() {
	$("#cart-contents").animate({opacity:0, marginTop: "-30px"},500);
}

function delayHideCartContents() {
	$("#cart-contents").animate({opacity:1, marginTop: "0px"},500);
	$("#cart-contents").animate({opacity:0, marginTop: "-30px"},500);
}

function showHideCartContents() {
	$("#cart-contents").animate({opacity:1, marginTop: "0px"}, 500, 'linear', function(){ delayHideCartContents(); });
}

function moveProductImage() {
	var image = document.getElementById("hiddenImage");	
 	imageWidth = image.offsetWidth;
 	if(window.pageYOffset > 0) {
	 	scrollTop = window.pageYOffset;
 	} else if(document.documentElement.scrollTop > 0) {
	 	scrollTop = document.documentElement.scrollTop;
 	} else if(document.body.scrollTop > 0) {
	 	scrollTop = document.body.scrollTop;
 	} 
 	if(scrollTop > 0) {
 		scrollStep = scrollTop / 6; 
	}
 	moveWindow();
	$("#hiddenImage").animate({opacity:0.2, left: "760px", top: "-240px", width: "60px"}, 500, 'linear', function() { resetImage(); });		
}

function moveWindow() {
	if(!cartOpen) {
		openCart();
	}
	if(scrollTop > 0) {
		scrollTop = scrollTop - scrollStep;
 		window.scrollTo(0,scrollTop);
 		timer2 = setTimeout("moveWindow()", 50);
	} else {
		clearTimeout(timer2);
	}
}

function openCart() {
	$("#view-cart").attr('src', "http://pearlsforgirls.com.au/images/cart-full.png");
	cartOpen = 1;
}

function resetImage() {
	
	$("#hiddenImage").css({width: imageWidth + "px", opacity: 0.8, left: "20px", top: "60px"});

	showHideCartContents();
}

function showLargeImage() {
	$("#magnified").css({display:"block"});

	// fade magnified in 
	$("#magnified").animate({opacity: 1},500);
}

function hideLargeImage() {
	// fade magnified out 
	//$("#magnifier").animate({opacity: 0},500);
	$("#magnified").animate({opacity: 0},500, function() { 
		$("#magnified").css({display:"none"});
	});
}

var newimg;
var newalt;

function swapImage(img, alt) {
	newimg = img;
	newalt = alt;
	// need to change src, alt and title of prodImage, hiddenImage and magnifiedImg
	// fade out old image
	fadeSwapImage();
}

function fadeSwapImage() {
	// hide hidden image
	$("#hiddenImage").css({opacity:0});
	// fade out main image
	$("#prodImage").animate({opacity: 0.1},500, function() { 
		// when faded, swap src
		$("#prodImage").attr('src', "http://pearlsforgirls.com.au/images/products/" + newimg);
		$("#hiddenImage").attr('src', "http://pearlsforgirls.com.au/images/products/" + newimg);
		$("#magnifiedImg").attr('src', "http://pearlsforgirls.com.au/images/products/lg_" + newimg);
		$("#prodImage").attr('alt', newalt);
		$("#prodImage").attr('title', newalt);
		// fade it back in
		$("#prodImage").animate({opacity: 1},500, function() { 
			// when complete show the hidden image again
			$("#hiddenImage").css({opacity:0.8});
		});
	});
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x)
		curleft += obj.x;
	
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (obj.y)
		curtop += obj.y;

	return curtop;
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
