// JavaScript Document

// EDIT THESE VALUES IF REQUIRED
var alertText = 'Are you sure you wish to perform this action' + "\n"; //DEFAULT TEXT DISPLAYED ON CONFIRM BUTTONS/LINKS WHEN NO ALT/TITLE
var newWindowLink = true; //OPEN EXTERNAL LINKS BY DEFAULT IN A NEW WINDOW (TRUE/FALSE)?


var woms = new Array(); //CREATE ARRAY OF FUNCTIONS TO LAUNCH ONLOAD

//ONLOAD MANAGER
function womOn(){
  window.onload = womGo;
}

function womGo(){
  for(var i = 0;i < woms.length;i++)
    eval(woms[i]);
}

function womAdd(func){
  woms[woms.length] = func;
}

function CreateBookmarkLink() {

	title = document.title;
	url = window.location.href;
	
	if (window.sidebar) { 
		window.sidebar.addPanel(title, url,"");
	} else if(window.external) {
		window.external.AddFavorite( url, title); 
	} else if(window.opera && window.print) {
		return true;
	}
	
}

function selectText() {

	if(!document.getElementsByTagName('input')) return false; //MAKE SURE TO GET INPUTS
	
	var inputs = document.getElementsByTagName('input');
	
	for(var i=0; i<inputs.length; i++) {
	
		inputs[i].onfocus = function() {
		
			this.select();
		
		}
	
	}
	
	

}

function objToggle(obj) { 

	var obj = document.getElementById(obj);

	if(obj.style.display == "block") { 
		obj.style.display = "none";
	} else { 
		obj.style.display = "block";
	}

}


function Products ()
{
	
	$(".zoom-description").show();
	$(".zoom-description .content").hide();
	$(".zoom-description .show-content").click(
		function ()
		{
			$(".zoom-description .content").slideToggle();
			return false;
		});
	
	$("#presentation_box").click(
		function ()
		{
			var checkState = $(this).attr("checked");
			var product_price = $("#product_price").val();
			var product_price_pres_box = $("#product_price_pres_box").val();
			if(checkState) {
				$("#display-price").html(product_price);
			} else {
				$("#display-price").html(product_price_pres_box);
			}
		});
	
	// Switch to zoom.
	$("#zoom-image").click(
		function ()
		{
		
			var zoom_view_port = $("#zoom-view-port");
			var current_image = $("#product-image");
			var large_image_src = $(this).attr("href");
			
			$(current_image).attr("alt", "");
			$(current_image).attr("title", "");
			
			$(zoom_view_port).unbind();
			$(zoom_view_port).css("cursor", "default");
			
			$(current_image).fadeOut(350,
				function ()
				{
					
					$(zoom_view_port).css("height", $(current_image).height()+"px");
					$(zoom_view_port).css("width", $(current_image).width()+"px");
					$(zoom_view_port).css("background-image", "url('images/ajax-loader.gif')");
					$(zoom_view_port).css("background-position", "center");
					$(zoom_view_port).css("background-repeat", "no-repeat");
					
					// Get large image.
					var largeImage = $("<img>");
					$(largeImage).attr("src", large_image_src);
					$(largeImage).load(
						function ()
						{
							$(zoom_view_port).css("background", "none");
							$(largeImage).fadeIn();
							$(zoom_view_port).append($(largeImage));
							$(largeImage).fadeIn(350)
										 .css("position", "absolute")
										 .css("top", "0")
										 .css("left", "0");
							
							$(zoom_view_port).bind(
								"mousemove",
								function (e)
								{
									
									$(this).css("cursor", "crosshair");
		
									var normalWidth = $(this).width();
									var normalHeight = $(this).height();
									var largeWidth = $(largeImage).width();
									var largeHeight = $(largeImage).height();
									
									// Mouse position on the normal image.
									var mousePosX =  $(this).offset().left - e.pageX;
									var mousePosY =  $(this).offset().top - e.pageY;
									
									// Get multiplyer.
									var xMultiplyer = (largeWidth-normalWidth) / normalWidth;
									var yMultiplyer = (largeHeight-normalHeight) / normalHeight;
									
									var leftPos = ((mousePosX * xMultiplyer))+"px";
									var topPos = ((mousePosY * yMultiplyer))+"px";
									
									$(largeImage).css("left", leftPos);
									$(largeImage).css("top", topPos);
									
								});
						});
					
				});
			
			$(".zoom-description").fadeOut("slow");
			$(this).parent().hide();
			
			return false;
		
		});
	
	$(".thumbnails li a").click(
		function ()
		{
		
			var new_image = $(this).attr("href");
			var zoom_view_port = $("#zoom-view-port");
			var thumb_image = $(this).children("img").attr("src");
			var large_image = thumb_image.replace("/thumbs/", "/large/");
			$(zoom_view_port).empty();
			$(zoom_view_port).css("height", "0px");
			$(zoom_view_port).css("width", "0px");
			$("#product-image").fadeOut(350,
				function ()
				{
					$("#product-image").attr("src", new_image).fadeIn(350);
				});
			
			$("#zoom-image").attr("href", large_image);
			$(".zoom-description .content").hide();
			$(".zoom-description").fadeIn("slow");
			$("#zoom-image").parent().show();
			
			return false;
		
		});
	
	$(".featured-product .featured-box").hide();
	$(".featured-product .featured-box:first").show();
	
}

function featuredRotate ()
{
	var wait_time = 8000; // miliseconds (1s = 1000ms)
	var currItem = 0;
	var numItems = $(".featured-product .featured-box").length;
	if(numItems > 1) {
		window.setInterval(doRotate, wait_time);
	}
	function doRotate()
	{
		$(".featured-product .featured-box:eq("+currItem+")").fadeOut(2000);
		if(currItem < (numItems-1)) {
			currItem++
		} else {
			currItem = 0;
		}
		$(".featured-product .featured-box:eq("+currItem+")").fadeIn(1000);
	}
}



$(document).ready(
				  
	function()
	{
		featuredRotate();
		Products();
	
	}
	
);

