jQuery.noConflict();

Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5), 10) == 6;
Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5), 10) == 7;
Prototype.Browser.IE8 = Prototype.Browser.IE && !Prototype.Browser.IE6 && !Prototype.Browser.IE7;
Prototype.Browser.Chrome = !!navigator.userAgent.match('Chrome');
var OSName="Unknown";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="Mac";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

function scrollToTop(){
    return Effect.ScrollTo('top', {duration:.5, transition: Effect.Transitions.sinoidal});
}

//-- Main Nav
function toggleMenu(el, over, second) {
    if (!second && !over) {
        setTimeout(function(){toggleMenu(el, 0, 1);}, 100);
        el.setAttribute('sameAttribute', 1);
        return;
    } else if (!over && el.getAttribute('sameAttribute') == 0)
    return;
    el.setAttribute('sameAttribute', 0);

    if (over)
        Element.addClassName(el, 'over');
    else {
        Element.addClassName(el, 'hold');
        Element.removeClassName(el, 'over');
        setTimeout(function(){Element.removeClassName(el, 'hold');}, 300);
    }
}


//-- On LOAD
document.observe('dom:loaded', function(event) {
    //-- mini-top-cart: hide "Subtotal"
    if($$('#minicart_totals td')[0])
        $$('#minicart_totals td')[0].hide();
    
    //-- mini-top-cart: show only first <tr>
    var setTr = 0;
    if($$('#minicart_totals tr'))
        $$('#minicart_totals tr').each(function(el){
            if(setTr) $(el).hide();
            else setTr = 1;
        });
    
    if(Prototype.Browser.Gecko){
        $$("input").each(function(el){
            el = $(el);
            if(el.readAttribute('type')=='text'){
                var h = (el.clientHeight=='')?18:el.clientHeight;
                var p = Math.ceil(h/6);
                el.setStyle({paddingTop:p+'px', height:(h-p)+'px'});
            }
        });
    }

    //-- navigation
    var nav = $('nav');
    // $$('#nav .level0 .inner-span', '#nav .level0 .outerspan').invoke('setStyle', {padding:'0px'});
    var navWidth = nav.getWidth();
    var navLinks = [];
    var i = 0;
    var totalNavWidth = 1000;
    var totalLinkWidth = 0;
    var navLinkMargin = 2;
    var navLinkPadding = 1;

    nav.childElements().each(function(el){
        var el = $(el);
        var links = el.getElementsBySelector('a');
        
        if(Prototype.Browser.IE6) links[0].setStyle({width:'0px'}); 
        
        var linkWidth = links[0].getWidth();
        navLinks[i] = {element:links[0], w:linkWidth};
        totalLinkWidth += linkWidth;
        i++;
    });
    
    var navLinkCount = navLinks.length;
/*
    var freeSpace = totalNavWidth-totalLinkWidth;
    var extraWidth = Math.floor((freeSpace/navLinkCount)-navLinkMargin-navLinkPadding);
    
    for(i=0;i<navLinkCount;i++){
        var newWidth = navLinks[i].w + extraWidth;
        navLinks[i].element.setStyle({width:newWidth+'px', marginRight:navLinkMargin+'px', paddingLeft:navLinkPadding+'px'});
        navLinks[i].element.down(1).setStyle({paddingRight:navLinkPadding+'px'});
    }
*/

    var chromeOnWindows = Prototype.Browser.Chrome && (OSName == "Unknown" || OSName == "Windows");
    
    if(chromeOnWindows) {
        $$('#nav .level0 .outer-span').invoke('setStyle', {paddingLeft:'25px'});
        $$('#nav .level0 .inner-span').invoke('setStyle', {paddingRight:'26px'});
    }


    var veryLastEl = navLinks[navLinkCount-1].element;
    var secondLastEl = navLinks[navLinkCount-2].element;
    var thirdLastEl = navLinks[navLinkCount-3].element;
    veryLastEl.w = veryLastEl.getWidth() + navLinkPadding;
    secondLastEl.w = secondLastEl.getWidth() + navLinkPadding;
    thirdLastEl.w = thirdLastEl.getWidth() + navLinkPadding;
    
    // var ulWidth = 0;
    var ulWidth = (!Prototype.Browser.IE6) ? 333 : 328;
    var veryLastUl,
        secondLastUl,
        thirdLastUl,
        veryLastOut = 1,
        secondLastOut = 1,
        thirdLastOut = 1,
        veryLastOver = 0,
        secondLastOver = 0,
        thirdLastOver = 0;
    
    if(veryLastUl = veryLastEl.next()) {
        var veryLastUlWidth = (Prototype.Browser.IE6 || Prototype.Browser.IE7) ? '480' : veryLastUl.getWidth();
        var extraWidth = (Prototype.Browser.IE6 || Prototype.Browser.IE7) ? 1 : 0;
        if(chromeOnWindows) {
            extraWidth = 0;
        }
        var lastGoLeft = ((veryLastUlWidth - veryLastEl.w) > 0) ? '-' + (veryLastUlWidth - veryLastEl.w + extraWidth) + 'px' : '0px';
        
        jQuery(veryLastEl).mouseenter(function(){
            veryLastUl.setStyle({left: lastGoLeft});
            veryLastOut = 0;
        }).mouseleave(function(){
            setTimeout(function(){
                if(veryLastOut || !veryLastOver) {
                    veryLastUl.setStyle({left:'-10000px'});
                };
            }, 100);
        });
        jQuery(veryLastUl).mouseenter(function(){
            veryLastOver = 1;
            veryLastOut = 0; 
        }).mouseleave(function(){
            veryLastOver = 0;
            veryLastOut = 1;
            setTimeout(function(){
                if(veryLastOut) { veryLastUl.setStyle({left:'-10000px'}); }
            }, 100);
        });
    }
    
    if(secondLastUl = secondLastEl.next()) {
        var secondLastUlWidth = (Prototype.Browser.IE6 || Prototype.Browser.IE7) ? '480' : secondLastUl.getWidth();
        var extraWidth = (Prototype.Browser.Gecko) ? 20 : 21;
        if(Prototype.Browser.IE) {
            extraWidth = (Prototype.Browser.IE8) ? 9 : 4;
        }
        if(chromeOnWindows) {
            extraWidth = 75;
        }
        var secondGoLeft = ((secondLastUlWidth - veryLastEl.w - secondLastEl.w) > 0) ? '-' + (secondLastUlWidth - veryLastEl.w - secondLastEl.w + extraWidth) + 'px' : '0px';


        jQuery(secondLastEl).mouseenter(function(){
            secondLastUl.setStyle({left: secondGoLeft});
            secondLastOut = 0;
        }).mouseleave(function(){
            setTimeout(function(){
                if(secondLastOut || !secondLastOver) {
                    secondLastUl.setStyle({left:'-10000px'});
                };
            }, 100);
        });
        jQuery(secondLastUl).mouseenter(function(){
            secondLastOver = 1;
            secondLastOut = 0; 
        }).mouseleave(function(){
            secondLastOver = 0;
            secondLastOut = 1;
            setTimeout(function(){
                if(secondLastOut) { secondLastUl.setStyle({left:'-10000px'}); }
            }, 100);
        });
    }
    
    if(thirdLastUl = thirdLastEl.next()) {
        var thirdGoLeft = ((ulWidth - veryLastEl.w - secondLastEl.w - thirdLastEl.w) > 0) ? '-' + (ulWidth - veryLastEl.w - secondLastEl.w - thirdLastEl.w) + 'px' : '0px';
        jQuery(thirdLastEl).mouseenter(function(){
            thirdLastUl.setStyle({left: thirdGoLeft});
            thirdLastOut = 0;
        }).mouseleave(function(){
            setTimeout(function(){
                if(thirdLastOut || !thirdLastOver) {
                    thirdLastUl.setStyle({left:'-10000px'});
                };
            }, 100);
        });
        jQuery(thirdLastUl).mouseenter(function(){
            thirdLastOver = 1;
            thirdLastOut = 0; 
        }).mouseleave(function(){
            thirdLastOver = 0;
            thirdLastOut = 1;
            setTimeout(function(){
                if(thirdLastOut) { thirdLastUl.setStyle({left:'-10000px'}); }
            }, 100);
        });
    }

    $$('ul.level0').each(function(el){
        var links = $(el).getElementsBySelector('a');
        links.first().setStyle({paddingTop:'7px'});
        links.last().setStyle({paddingBottom:'7px'});
    });

    $$('ul.level1').each(function(el){
        var links = $(el).getElementsBySelector('a');
        links.last().setStyle({paddingBottom:'7px'});
    });
    //-- end navigation --//


    //-- external links
    $$('a[rel="external"]').each(function(link) {
        if(link.readAttribute('href') != '' && link.readAttribute('href') != '#') {
            link.writeAttribute('target', '_blank');
        }
    });
    
    //-- IE6 hover on buttons
    if(Prototype.Browser.IE6) {
        $$('button.button').each(function(el){
            var el = $(el);
            if(!el.hasClassName('disabled')) {
                el.observe('mouseover', function(){
                    var spans = this.getElementsBySelector('span'); //.invoke('removeClassName', 'over');
                    spans[0].setStyle({backgroundPosition:'0 -30px'});
                    spans[1].setStyle({backgroundPosition:'100% -30px'});
                }).observe('mouseout', function(){
                    var spans = this.getElementsBySelector('span'); //.invoke('addClassName', 'over');
                    spans[0].setStyle({backgroundPosition:'0 0'});
                    spans[1].setStyle({backgroundPosition:'100% 0'});
                }).observe('mousedown', function(){
                    var spans = this.getElementsBySelector('span'); //.invoke('addClassName', 'over');
                    spans[0].setStyle({backgroundPosition:'0 100%'});
                    spans[1].setStyle({backgroundPosition:'100% 100%'});
                });
            }
        });
        $$('button.button-dark').each(function(el){
            var el = $(el);
           el.observe('mouseover', function(){
               var spans = this.getElementsBySelector('span'); //.invoke('addClassName', 'over');
               spans[0].setStyle({backgroundPosition:'0 0'});
               spans[1].setStyle({backgroundPosition:'100% 0'});
           }).observe('mouseout', function(){
               var spans = this.getElementsBySelector('span'); //.invoke('removeClassName', 'over');
               spans[0].setStyle({backgroundPosition:'0 -30px'});
               spans[1].setStyle({backgroundPosition:'100% -30px'});
           }).observe('mousedown', function(){
               var spans = this.getElementsBySelector('span'); //.invoke('addClassName', 'over');
               spans[0].setStyle({backgroundPosition:'0 100%'});
               spans[1].setStyle({backgroundPosition:'100% 100%'});
           });
        });
    }

    if ($$('.quick-access .links span.welcome')[0]) {
        $$('.top-link-order-status')[0].up('li').setStyle({borderRightWidth:'0px !important'});
    }
});

// -- This must be inside of a load event vs the above dom:loaded to avoid an IE quirk
Element.observe(Prototype.Browser.IE ? window : document, Prototype.Browser.IE ? 'load' : 'dom:loaded', function(event) {
     if(!$('loading-process')) {
         var loadingProcess = new Element('div', {'id':'loading-process'});
         Element.insert($$('body')[0], {bottom: loadingProcess});
     }
});

var header_flag_down = false;
jQuery(document).ready(function() {
jQuery("#follow_us_header").click(function() { 

 if(header_flag_down) {
	jQuery(".wrapper").css({position: 'relative'} );jQuery(".wrapper").animate({"top":"-=100px"},"slow");
	header_flag_down=false;;
 } else {
	jQuery(".wrapper").css({position: 'relative'} );jQuery(".wrapper").animate({"top":"+=100px"},"slow");
	header_flag_down=true;;
 }
});
});


