
var showOverlay = function(){
	
	var isOverlay = $("div#overlay").length;
	if (isOverlay > 0) {
		$("div#overlay").css("display","block");
		return false;
	}
	
	var overlayHTML = "<div id='overlay' class='transparentBg'></div>";
	
	if($("#overlay").length == 0)
		$("body").append(overlayHTML);
	
	$("#overlay").width($('body').width());
	$("#overlay").height($('body').height());
	
	$("#overlay").click(function(){
		hideBox();
		hideOverlay();
	});
	
}

var hideOverlay = function(){
	$("div#overlay").fadeOut("normal",function(){
									$(this).remove();									$("div.jqZoomWindow").remove();
							});
}

var showBox = function(){
	
	var root = 'http://ayakawa.com/';
	
	var isBox = $("div#box").length;
	if (isBox > 0) {
		$("div#box").css("display","block");
		return false;
	}
	
	var boxHTML = "<div id='box'><img src='"+root+"images/close.png' id='box_close_btn' onclick='hideBox();hideOverlay();'><div id='box_container'></div></div>";
	$("body").append(boxHTML);
	
	documentW = $(document).width();
	boxLeft   = (documentW/2)-300;
	$("div#box").css('left',boxLeft+'px');
}

var hideBox = function(){
	$("#box").fadeOut("normal",function(){
									$(this).remove();									$("div.jqZoomWindow").remove();
							});
}

var loadInBox = function(url){
	
	url = url;
	
	showOverlay();
	showBox();
	
	$("div#box > div#box_container").html("<div class='center' style='margin:50px;'>Loading...</div>");
	
	$.ajax({
	   type: "POST",
	   data: 'tpl_mode=ajax',
	   url: url,
	   success: function(msg){
			$("#box > div#box_container").html(msg);
	   }
	});
	
	return false;
	
}

var postToBox = function(objForm,fnCallBack){
	
	$("div#box > div#box_container").html("<div class='center' style='margin:50px;'>Loading...</div>");
	
	var thisURL = objForm.attr('action');
	var thisURL = (thisURL.indexOf('?'))? thisURL+'&__mode=ajax' : thisURL+'?__mode=ajax' ;
	
	$.ajax({
	   type: "POST",
	   data: objForm.serialize(),
	   url: thisURL,
	   success: function(msg){
		   	fnCallBack(msg)
	   }
	});
	
	return false;
	
}

var addAndContinue = function(formID){
	
	var obForm = $("#"+formID);
	
	postToBox(obForm,function(msg){
			if (msg != 'err') {
				/*msg = (msg <= 0)? 0 : (msg/1) ;
				strTopPrice = $("#cart_total").html().replace('$','');
				intTopPrice = strTopPrice/1;
				alert(intTopPrice);
				newPrice    = intTopPrice+(msg/1);
				alert(newPrice);*/
				$("#cart_total").html(msg).addClass('red');
				$("div#box > div#box_container").html("<h1>The product is added to your bascket.</h1>");
			}
		});
	
}
var addAndGoToCart = function(formID){
	
	var obForm = $("#"+formID);
	
	postToBox(obForm,function(msg){
			if (msg != 'err') {
				window.location = 'http://ayakawa.com/index.php?_g=co&_a=cart';
			}
		});
}

var loadImage = function(imgURL,originalURL){
	
	var htmlImage = "<a href='"+originalURL+"' class=\"jqzoom\">";
	htmlImage	 += "<img src='"+imgURL+"' align=\"middle\" id=\"MainProdImage\" />";
	htmlImage	 += "</a>";
	
	$("div#fullImageContainer").children("a").remove();
	$("div#fullImageContainer").html(htmlImage);
	
	$(".jqzoom").jqzoom({
		zoomWidth: 350,
		zoomHeight: 350,
		title: false
	});
	
	return false;
	
}

var slideshow_init = function(){
	
	if($("div#slideshow").length > 0)
		setInterval('slideshow_rotate()',5000);
	
}

var slideshow_rotate = function(){
	
	var objSlideshow = $("div#slideshow");
	var thisImg = objSlideshow.children("img:visible");
	var nextImg = thisImg.next("img:hidden");
	if(!nextImg || nextImg.length == 0){
		nextImg = objSlideshow.children("img:eq(0)");
	}
	
	thisImg.fadeOut("normal",function(){
		objSlideshow.children("img").css("display","none");
		nextImg.fadeIn("normal");
	});
}

$(document).ready(function(){
	
	/*$(".colorDropDown").hover(function(){
		$(this).children("div:eq(1)").css('display','block');
	},function(){
		$(this).children("div:eq(1)").css('display','none');
	});*/
	
	$("a.sidebarHead").click(function(){
		
		var elUL = $(this).parents('h3').next("ul.list");
		
		if(elUL.children("li").eq(0).is(":hidden")){
			
			//$(this).removeClass("right").addClass("down");
			elUL.children("li").show();
			
		}else{
			
			//$(this).removeClass("down").addClass("right");
			elUL.children("li").hide();
			
		}
		
	});
	
	slideshow_init();
	
});
