$(document).ready(function () {

    $('.ShippingAndTaxEstimator').attr("style", "text-align:right");

});


$(document).ready(function(){
	//Newsletter Input Box

	$('#filterbox_newsletter').focus(function(){
		if(this.value =="join our noozletter"){
			this.value="";
		}
	});
	$('#filterbox_newsletter').blur(function(){
		if(this.value ==""){
			this.value="join our noozletter";
		}
	});	///////////////////////


	$.historyInit(pageload);
						   		
		// set onlick event for buttons
		$("a[rel='history']").click(function(){
			// 
			var hash = $(this).text() ;
			hash = hash.replace(/^.*#/, '');
		
			// moves to a new page. 
			// pageload is called at once. 
			$.historyLoad(hash);
			return false;
		});
	
		$("a[rel='history_nxt']").click(function(){
			// 
			var hash = location.href
			hash = hash.replace(/^.*#/, '');
			
			if(hash.length > 2){
				hash = 2
			}else{
				hash = parseInt(hash)+1
				hash.toString()
			}
			//alert(hash + "<<>>" + MPage)
			//alert(hash)
			// moves to a new page. 
			// pageload is called at once. 
			
			if(hash < MPage+1){
			$.historyLoad(hash);
			
			}
			return false;
		});

		$("a[rel='history_prv']").click(function(){
			// 
			var hash = location.href
			hash = hash.replace(/^.*#/, '');
			
			if(hash.length > 2){
				hash = 2
			}else{
				hash = parseInt(hash)-1
				hash.toString()
			}
			//alert(hash + "<<>>" + MPage)
			//alert(hash)
			// moves to a new page. 
			// pageload is called at once. 
			
			if(hash != 0){
			$.historyLoad(hash);
			
			}
			return false;
		});

						   
		$(".viewerPagin div a").eq(1).css("font-weight","bold");				   
	$(window).bind('load', function() {
		var preload = new Array();
		$(".pImg").each(function() {
			s = $(this).attr("src").replace(/\.(.+)$/i, "_on.$1");
			preload.push(s)
		});
		var img = document.createElement('img');
		$(img).bind('load', function() {
			if(preload[0]) {
				this.src = preload.shift();
			}
		}).trigger('load');
	});

	var navHoverCongfig = {    
		sensitivity: 100, // number = sensitivity threshold (must be 1 or higher)    
		interval: 100, // number = milliseconds for onMouseOver polling interval    
		over: navMoveUp, // function = onMouseOver callback (REQUIRED)    
		timeout: 0, // number = milliseconds delay before onMouseOut    
		out: navMoveDown // function = onMouseOut callback (REQUIRED)    
	};

	$(".sub_nav li a").hoverIntent( navHoverCongfig );

	function navMoveUp(){
		$(this).animate({marginTop:"0px"},300);
	}

	function navMoveDown(){
		$(this).animate({marginTop:"5px"},300)
	}

	var thumbHoverCongfig = {    
		sensitivity: 100, // number = sensitivity threshold (must be 1 or higher)    
		interval: 100, // number = milliseconds for onMouseOver polling interval    
		over: moveUp, // function = onMouseOver callback (REQUIRED)    
		timeout: 0, // number = milliseconds delay before onMouseOut    
		out: moveDown // function = onMouseOut callback (REQUIRED)    
	};

	$(".thumbContainer > ul > li").hoverIntent( thumbHoverCongfig );

	function moveUp(){
		$(this).find("img:not(.loader)").animate({"width": "+=10px","height": "+=10px", marginLeft: "9px"},500)
		$(this).find("span").animate({marginTop:"0"},500)
	}

	function moveDown(){
		$(this).find("img:not(.loader)").animate({"width": "-=10px","height": "-=10px", marginLeft: "19px"},500)
		$(this).find("span").animate({marginTop:"0px"},500)
	}
	
	/** Search Box **/
	$('#filterbox').focus(function(){
		if(this.value =="search products"){
			this.value="";
		}
	});
	$('#filterbox').blur(function(){
		if(this.value ==""){
			this.value="search products";
		}
	});
	/** ----------- **/
});

var CPage = 0;
var MPage = 0;

function moveThumb(num){

	if(CPage != num){
		
		var target = num * -684
		$(".thumbContainer").animate({"opacity": ".5"},300, function(){
			$(this).animate({"left": target},1000, function(){
				$(this).animate({"opacity": "1"},300)
			})
		})
		$(".viewerPagin div a").css("font-weight","normal")
		$(".viewerPagin div a").eq(num + 1).css("font-weight","bold")
	}
	CPage = num;
}

function movePre(){
	if(CPage != 0){
		moveThumb(CPage - 1);
		
	}
}

function moveNxt(){
	if(CPage != MPage-1){
		
		moveThumb(CPage + 1);
	}
}

function showThumb(){
	$(".contentBox").animate({ opacity:".2", height:"600px"},300,function(){
		$(this).animate({ marginLeft:"0"},1000,function(){
			$(this).animate({ opacity:"1"},300)
			$(".thumbViewer").animate({ opacity:"1"},300)
		})
	})
}

function showProduct(target,s,ss,p){
    $(target).prev().fadeIn(300)
	 $(".productBox").load("pInfo.aspx?id="+s+"_"+ss+"_"+p+" #productHTML",function(){
	 $(target).prev().fadeOut(300)
	    if($(".productGalleryBox").height() > $(".productDesBox").height()){
	 	    var conH = $(".productGalleryBox").height()
	    }else{
		     var conH = $(".productDesBox").height()
	    }
	    $(".contentBox").animate({ opacity:".2"},300,function(){
		    $(".thumbViewer").animate({ opacity:".2"},300)
		    $(this).animate({ marginLeft:"-907px"},1000,function(){
			    $(this).animate({ opacity:"1"},300,function(){
				    $(".contentBox").animate({ height:conH+"px"},300)
    				
			    })
		    })
	    })
	});
}

function showLarge(num){
$(".ImgBox > img").fadeOut(300,function(){
     $(".ImgBox > img").attr("src","img/products/" + num)
    	$.preload( '.ImgBox > img', {
				onFinish:finish,
				placeholder:'img/ajax-loader.gif',
				notFound:'http://aflesler.webs.com/img/notfound.jpg',
				threshold:1 //'1' is the default, how many at a time, to load.
		});
})
}	

function finish(){
$(".ImgBox > img").fadeIn(300)
}

var after = false;
/*
	// PageLoad function
	// This function is called when:
	// 1. after calling $.historyInit();
	// 2. after calling $.historyLoad();
	// 3. after pushing "Go Back" button of a browser
	function pageload(hash) {
		// hash doesn't contain the first # character.
		if(hash) {
			// restore ajax loaded state
			//alert("dddd")
		} else {
			// start page
			//alert("1111")
			if(after){
			showThumb()
			}else{
			after = true
			}
		}
	}
	
	$(document).ready(function(){
		// Initialize history plugin.
		// The callback is called at once by present location.hash. 
		$.history.init(pageload);
		
		// set onlick event for buttons
		$("a[@rel='history']").click(function(){
			// 
			var hash = this.href;
		
			hash = hash.replace(/^.*#/, '');
			// moves to a new page. 
			// pageload is called at once. 
			$.history.load(hash);
			return false;
		});
	});
*/
function showProductImage(image){
    //$(".ImgBox").find("img").attr("src","images/product/large/" + image);
    MagicZoom.update($(".ImgBox").find("a").get(0), "images/product/large/" + image, "images/product/medium/" + image);
    MagicZoom.update($(".ImgBox").find("img").get(0), "images/product/large/" + image, "images/product/large/" + image);
    MagicZoom.update($(".ImgBox").find("MagicZoomBigImageCont").get(0), "images/product/large/" + image, "images/product/medium/" + image);

    MagicZoom.refresh();
    MagicZoom.start(document.getElementById('zoom'));
}
function pageload(hash) {
		// hash doesn't contain the first # character.
		if(hash) {
			// restore ajax loaded state
		
			hash = parseInt(hash)-1
			hash.toString()
			moveThumb(hash)
			//$("#load").load(hash + ".html");
		} else {
			// start page
			
			//$("#load").empty();
		}
	}


///////////////////////////////////////////////////////
//                                                   //
//  The following is for the product option changes  //
//                                                   //
///////////////////////////////////////////////////////
        $(document).ready(function(){
            $("div.addToCartForm").css("margin-top","0px");
            $("#Size, #Color").hide();
//          $("#Size").each(function(){
//              $("option", this).each(function() {
//                  //alert($(this).val());// your normalizing script here
//                  if ($(this).text() == 'Size')
//                      $("#SizeList").append("Select size<br />");
//                  else
//                      $("#SizeList").append("<input type='radio' name='sizeRadio' value='" + $(this).val() + "' onclick='setSize(this);' />&nbsp;" + $(this).text() + "<br />");
//              })
//              $("#SizeList").show();
//          });
//          $("#Color").each(function(){
//              $("option", this).each(function() {
//                  //alert($(this).val());// your normalizing script here
//                  if ($(this).text() == 'Color')
//                      $("#ColorList").append("Select color<br />");
//                  else
//                      $("#ColorList").append("<input type='radio' name='colorRadio' value='" + $(this).val() + "' onclick='setColor(this);' />&nbsp;" + $(this).text() + "<br />");
//              });
//              $("#ColorList").show();
//          });
        })
        function setSize(ctl) {
            var selectedValue = ctl.value;
            $("#Size").each(function(){
                $("option", this).each(function() {
                    if ($(this).val() == selectedValue) {
                        $(this).attr("selected","selected");
                    }
                })
            });
        }
        function setColor(ctl) {
            var selectedValue = ctl.value;
            $("#Color").each(function(){
                $("option", this).each(function() {
                    if ($(this).val() == selectedValue) {
                        $(this).attr("selected","selected");
                    }
                })
            });
        }

function openCoupon(){
		$("#useCouponCode1").slideDown(500)
	}
function showOverlay(){
	
	$("#overlayBG").animate({"opacity":".7"},1)
	var bTop = ($("#overlayBG").height() - $("#overlayContent").height()) / 2 
	var bLeft =($("#overlayBG").width() - $("#overlayContent").width()) / 2 
	$("#overlayContent").animate({"left":bLeft+"px","top":30+"px"},1)
$("#overlayBG").fadeIn(500,function(){
			$("#overlayContent").slideDown(500)			 
						 })	
}
function hideOverlay(){

	$("#overlayContent").slideUp(500,function(){
			$("#overlayBG").fadeOut(500)			 
						 })	
}

function showOverlay2(){
	
	$("#overlayBG").animate({"opacity":".7"},1)
	var bTop = ($("#overlayBG").height() - $("#overlayContent2").height()) / 2 
	var bLeft =($("#overlayBG").width() - $("#overlayContent2").width()) / 2 
	$("#overlayContent2").animate({"left":bLeft+"px","top":bTop+"px"},1)
$("#overlayBG").fadeIn(500,function(){
			$("#overlayContent2").slideDown(500)	
			
						 })	
}
function hideOverlay2(){

	$("#overlayContent2").slideUp(500,function(){
			$("#overlayBG").fadeOut(500)			 
						 })
}
function showOverlay3() {

    $("#overlayBG3").animate({ "opacity": ".7" }, 1)
    var bTop = ($("#overlayBG3").height() - $("#overlayContent3").height()) / 2
    var bLeft = ($("#overlayBG3").width() - $("#overlayContent3").width()) / 2
    $("#overlayContent3").animate({ "left": bLeft + "px", "top": 30 + "px" }, 1)
    $("#overlayBG3").fadeIn(500, function () {
        $("#overlayContent3").slideDown(500)
    })
}
function hideOverlay3() {

    $("#overlayContent3").slideUp(500, function () {
        $("#overlayBG3").fadeOut(500)
    })
}