/**
 * 
 */

var imgIndexCurrent  = 1; // starting image index
var carouselTopIndex = 1; 
var showCount        = 6; // number of images to show
var isHidden         = false;
var fadeMargin       = 300;
var showHomePageBackground = false;

jQuery(document).ready(function() {
	
    //Home Page Background
	if (showHomePageBackground) {
		jQuery("#wrapper").fadeOut('fast', function (){
			jQuery("#wrapper").css("background-image", "url(/images/background.jpg)");						
		});		
		jQuery("#wrapper").fadeIn('slow');
	}
	
	jQuery('#mycarousel').jcarousel({
        vertical: true,
        scroll: 6
    });
	
	
	// set the fade point for the indexProductList	
	var fadePointDiv = jQuery("#wrapper").offset();
	var fadePoint = fadePointDiv.left + fadeMargin;
	
	
   jQuery().mousemove(function(e)
   	{
			if(parseInt(e.pageX) > fadePoint)
			{
				if (!isHidden) {
					jQuery("li[jcarouselindex=" + imgIndexCurrent + "]").css("border", "2px solid #fff")
					jQuery('li img.listingProductImage').fadeOut('slow', function(){
						jQuery(".jcarousel-next").fadeOut('fast');
						jQuery(".jcarousel-prev").fadeOut('fast');
					});
					isHidden = true;
				}	
			}
			else
			{
				if (isHidden) {
					
					jQuery("li[jcarouselindex=" + imgIndexCurrent + "]").css("border", "2px solid #000");
					jQuery('li img.listingProductImage').fadeIn('slow', function(){
						jQuery(".jcarousel-next").fadeIn('fast');
						jQuery(".jcarousel-prev").fadeIn('fast');
					});
					isHidden = false;
				}
			}
		}
	)
   
   
});


function showProduct( image,imageTitle,imgIndex, productID)
	{
		imgOldIndex     =  imgIndexCurrent;
		imgIndexCurrent = imgIndex;  //
		
		//load product description		
		$.get("ajax.php",{ action: "description", id:productID}, function(data)
		{
			jQuery("#descriptionBox").slideUp("slow", function(){
				jQuery("#descriptionBox").html(data);	
				jQuery("#descriptionBox").slideDown("slow");		
			});	
		
		});
		
		//add image to div#mainPhotohre
		if(jQuery('#mainPhoto img').length>0 ) //first image
		{
					jQuery('#mainPhoto').html(image);
		}
		else
		{
				jQuery('#mainPhoto').html(image);
		}

		// hightlight selected current image
		jQuery("li[jcarouselindex=" + imgOldIndex + "]").css("border", "2px solid #fff");
		if (!isHidden) {
			jQuery("li[jcarouselindex=" + imgIndex + "]").css("border", "2px solid #000");
		}
		imgIndexCurrent = imgIndex;
		
		//Update the footer navigation				
		jQuery('.pageCounter').html(imgIndex + " of " + jQuery(".jcarousel-item").length);
		
		// Upadate the BUY button;
		//index.php?main_page=product_info&cPath=2_3&products_id=3
		var buyLink = '<a href="/site/index.php?main_page=product_info&cPath='+getParameterByName('cPath') +'&products_id='+productID+'"><span>Purchase</span></a>';
		jQuery('#buyLink').html(buyLink);
		
		
	}

	
	
function prevImage()
	{	
		if (imgIndexCurrent - 1 > 0) {
			if (imgIndexCurrent == carouselTopIndex)
			{
				jQuery('.jcarousel-prev').click();
				carouselTopIndex -= showCount;
				if (carouselTopIndex<=0){carouselTopIndex = 1}
			}
		jQuery("li[jcarouselindex=" + (imgIndexCurrent - 1) + "] img").click();	
		}
	}
	
function nextImage()
	{	
		var imageCount = jQuery(".jcarousel-item").length;
		if (imgIndexCurrent + 1 <= imageCount) {
			if(imgIndexCurrent + 1 == carouselTopIndex + showCount)
			{
				jQuery('.jcarousel-next').click();
				carouselTopIndex += showCount;
				if (carouselTopIndex + showCount > imageCount)
				{
					carouselTopIndex = imageCount -showCount + 1;
				}
			}				
			jQuery("li[jcarouselindex=" + (imgIndexCurrent + 1) + "] img").click();
		}
	}
	
function displayVars()
{
	var text = "";
	text += "imageCount:" +  jQuery(".jcarousel-item").length + "<br>";
	text += "imgIndexCurrent:" +  imgIndexCurrent + "<br>";
	text += "carouselTopIndex:" +  carouselTopIndex + "<br>";
	jQuery("#mainMenu").html(text);
	
}

// use jquery add the background photo to the opening page.
function homePageBackground()
{
		
	showHomePageBackground = true;	
}

function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

