
/**

 * jQuery.ScrollTo - Easy element scrolling using jQuery.

 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com

 * Dual licensed under MIT and GPL.

 * Date: 5/25/2009

 * @author Ariel Flesler

 * @version 1.4.2

 *

 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html

 */

;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);

//Easing plugin
jQuery.easing['jswing']=jQuery.easing['swing'];jQuery.extend(jQuery.easing,{def:'easeOutQuad',swing:function(x,t,b,c,d){return jQuery.easing[jQuery.easing.def](x,t,b,c,d);},easeInQuad:function(x,t,b,c,d){return c*(t/=d)*t+b;},easeOutQuad:function(x,t,b,c,d){return-c*(t/=d)*(t-2)+b;},easeInOutQuad:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t+b;return-c/2*((--t)*(t-2)-1)+b;},easeInCubic:function(x,t,b,c,d){return c*(t/=d)*t*t+b;},easeOutCubic:function(x,t,b,c,d){return c*((t=t/d-1)*t*t+1)+b;},easeInOutCubic:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t+b;return c/2*((t-=2)*t*t+2)+b;},easeInQuart:function(x,t,b,c,d){return c*(t/=d)*t*t*t+b;},easeOutQuart:function(x,t,b,c,d){return-c*((t=t/d-1)*t*t*t-1)+b;},easeInOutQuart:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t*t+b;return-c/2*((t-=2)*t*t*t-2)+b;},easeInQuint:function(x,t,b,c,d){return c*(t/=d)*t*t*t*t+b;},easeOutQuint:function(x,t,b,c,d){return c*((t=t/d-1)*t*t*t*t+1)+b;},easeInOutQuint:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t*t*t+b;return c/2*((t-=2)*t*t*t*t+2)+b;},easeInSine:function(x,t,b,c,d){return-c*Math.cos(t/d*(Math.PI/2))+c+b;},easeOutSine:function(x,t,b,c,d){return c*Math.sin(t/d*(Math.PI/2))+b;},easeInOutSine:function(x,t,b,c,d){return-c/2*(Math.cos(Math.PI*t/d)-1)+b;},easeInExpo:function(x,t,b,c,d){return(t==0)?b:c*Math.pow(2,10*(t/d-1))+b;},easeOutExpo:function(x,t,b,c,d){return(t==d)?b+c:c*(-Math.pow(2,-10*t/d)+1)+b;},easeInOutExpo:function(x,t,b,c,d){if(t==0)return b;if(t==d)return b+c;if((t/=d/2)<1)return c/2*Math.pow(2,10*(t-1))+b;return c/2*(-Math.pow(2,-10*--t)+2)+b;},easeInCirc:function(x,t,b,c,d){return-c*(Math.sqrt(1-(t/=d)*t)-1)+b;},easeOutCirc:function(x,t,b,c,d){return c*Math.sqrt(1-(t=t/d-1)*t)+b;},easeInOutCirc:function(x,t,b,c,d){if((t/=d/2)<1)return-c/2*(Math.sqrt(1-t*t)-1)+b;return c/2*(Math.sqrt(1-(t-=2)*t)+1)+b;},easeInElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d)==1)return b+c;if(!p)p=d*.3;if(a<Math.abs(c)){a=c;var s=p/4;}
else var s=p/(2*Math.PI)*Math.asin(c/a);return-(a*Math.pow(2,10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p))+b;},easeOutElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d)==1)return b+c;if(!p)p=d*.3;if(a<Math.abs(c)){a=c;var s=p/4;}
else var s=p/(2*Math.PI)*Math.asin(c/a);return a*Math.pow(2,-10*t)*Math.sin((t*d-s)*(2*Math.PI)/p)+c+b;},easeInOutElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d/2)==2)return b+c;if(!p)p=d*(.3*1.5);if(a<Math.abs(c)){a=c;var s=p/4;}
else var s=p/(2*Math.PI)*Math.asin(c/a);if(t<1)return-.5*(a*Math.pow(2,10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p))+b;return a*Math.pow(2,-10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p)*.5+c+b;},easeInBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;return c*(t/=d)*t*((s+1)*t-s)+b;},easeOutBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;return c*((t=t/d-1)*t*((s+1)*t+s)+1)+b;},easeInOutBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;if((t/=d/2)<1)return c/2*(t*t*(((s*=(1.525))+1)*t-s))+b;return c/2*((t-=2)*t*(((s*=(1.525))+1)*t+s)+2)+b;},easeInBounce:function(x,t,b,c,d){return c-jQuery.easing.easeOutBounce(x,d-t,0,c,d)+b;},easeOutBounce:function(x,t,b,c,d){if((t/=d)<(1/2.75)){return c*(7.5625*t*t)+b;}else if(t<(2/2.75)){return c*(7.5625*(t-=(1.5/2.75))*t+.75)+b;}else if(t<(2.5/2.75)){return c*(7.5625*(t-=(2.25/2.75))*t+.9375)+b;}else{return c*(7.5625*(t-=(2.625/2.75))*t+.984375)+b;}},easeInOutBounce:function(x,t,b,c,d){if(t<d/2)return jQuery.easing.easeInBounce(x,t*2,0,c,d)*.5+b;return jQuery.easing.easeOutBounce(x,t*2-d,0,c,d)*.5+c*.5+b;}});


$(document).ready(
    function ()
    {   
        $(".siteNavLi").find("div > ul a:last").addClass("viewAll");
        
        $(".siteNavLi").hover(
            function() {
                this.canShow=true;   
                var show=showFunctionBuilder($(this),this);
                setTimeout(show,250);
                clearTimeout(this.closeTimer); 
            },
            function(){
                this.canShow=false; 
                var close=closeFunctionBuilder(this);
                if(navigator.userAgent.indexOf("Safari")>0)  this.closeTimer = setTimeout(close,200);// only for safari
                else this.closeTimer = setTimeout(close,50);
            }	    
       );
       $('#search').keypress(
            function(event)
            {
                if(document.activeElement && document.activeElement.name!="searchImageButton" && event.keyCode==13)
                {
                    return false; 
                } 
                else if(event.keyCode==13)
                {
                     searchProduct();
                    return false; 
                }  
            } 
       ); 
    }
); 

//adjust the width of menu tabs
function adjustTabWidth(){
    var menuWidth = 0;
    var minWidth = 968;//the min width of top navigation
    var maxWidth = 968;// the max width of top navigation
    $(".siteNavLi").each(function(i){
       menuWidth += $(this).width();//get the real width of top menu
    }); 

    var avgPaddingGap = Math.floor((minWidth - menuWidth) / $(".siteNavLi").length);//get the avarage space
    var newMenuPaddingLeft = Math.floor(avgPaddingGap/2) + parseInt($(".siteNavLi").find(".menuTitle").parent().css("padding-left").replace("px",""));//get new left padding
    var newMenuPaddingRight = Math.ceil(avgPaddingGap/2) + parseInt($(".siteNavLi").find(".menuTitle").parent().css("padding-right").replace("px",""));//get new right padding
    
    if (newMenuPaddingLeft>=0 && newMenuPaddingRight>=0){
        //if the free space is enough to be assign to each menu
        $(".siteNavLi").find(".menuTitle").parent().css({"padding-left":newMenuPaddingLeft + "px", "padding-right":newMenuPaddingRight + "px"});//reset the padding of each menu to fix the width of top navigation
        restGap = Math.floor(minWidth - $(".siteNavRightBg").width());//assign the rest of space into the last menu
        $(".siteNavLi:last").find(".menuTitle").parent().css({"padding-left":newMenuPaddingLeft + Math.floor(restGap/2) + "px", "padding-right":newMenuPaddingRight + Math.ceil(restGap/2) + "px"});
    }
    else{
        //increase the width of top navigation to max 1500px
        $("#siteNav").css("width",menuWidth<maxWidth?menuWidth + "px":maxWidth + "px");
    }
    initBlueArrow();
}

//show the children menu
function showChildren(obj,objSelected)
{
   if(objSelected.canShow)
   { 
        var secNavHeight=obj.find(".secNavContent > ul").height() > obj.find(".secNavContent > dl").height() ? obj.find(".secNavContent > ul").height():(obj.find(".secNavContent > dl").height());
        obj.find(".siteSecondNav").height(secNavHeight+40);
        obj.find(".siteNavIframe").height(secNavHeight);

        obj.find(".siteSecondNav").css("visibility","visible");
        obj.find(".siteNavIframe").css("visibility","visible");
        obj.find(".secNavContent").css("visibility","visible");
        obj.find(".siteNavLink").addClass("selected");
        showBlueArrow(obj);
    } 
}    

function closeChildren(objSelected)
{
    $(objSelected).find(".siteSecondNav").css("visibility","hidden");    
    $(objSelected).find(".siteNavIframe").css("visibility","hidden");  
    $(objSelected).find(".secNavContent").css("visibility","hidden");
    $(objSelected).find(".siteNavLink").removeClass("selected");
    hideBlueArrow($(objSelected));
}

function showFunctionBuilder(obj, objSelected)
{
    return function(){
       showChildren(obj,objSelected);
   } 
} 

function closeFunctionBuilder(objSelected)
{
    return function(){
       closeChildren(objSelected);
   } 
} 

function searchInputPress(event)
{
    if(event.keyCode==13)
    {        
        searchProductByKeyword();
    } 
}

function searchProductByKeyword()
{
    var keyword = $('#keyword').val();  
    if(keyword=="")
    {
        return false;
    }        
    var url="/product/searchresults.aspx?freetext="+encodeURIComponent(keyword)+"&queryfrom=search";
    window.location.href = url;
    //alert(url);
}

function SetSearchTextFocus()
{
    $('#keyword').focus();
}

function initBlueArrow(){
    $(".siteNavLinkArrow").each(function(i){
       $(this).css("width",$(this).parent().width() + "px");
    });
}
function showBlueArrow(obj){
    $(".siteNavLinkArrow").css("visibility","hidden");
    obj.find(".siteNavLinkArrow").css("visibility","visible");
}
function hideBlueArrow(obj){
    obj.find(".siteNavLinkArrow").css("visibility","hidden");
    $(".siteNavLinkSelected").find(".siteNavLinkArrow").css("visibility","visible");
}

