
/* Footer Menu */

jQuery(document).ready(function() {
 //background-color for IE7 
  if (navigator.appVersion.indexOf("MSIE") != -1) {
     ieVer = parseFloat(navigator.appVersion.split("MSIE")[1]);
     if (ieVer <= 7) {
          jQuery(function(){
          jQuery("#topNav ul li").hover(
        function(){jQuery(this).addClass("hover");},
        function(){jQuery(this).removeClass("hover");}
          )}); 
 }}
    jQuery("#topNav li.level0").hover(function() {
        var subItems = jQuery(this).find("li.level1");
        var subItemsHeight = jQuery(subItems[0]).height()*subItems.length;
        jQuery(this).find("ul.level0").show(); //.animate({height:subItemsHeight+'px'}, { queue: false, duration: 250 });
    }, function() {
        jQuery(this).find("ul.level0").hide();//.animate({height:'0px'}, { queue: false, duration: 250 });
    });

    //footer menu
    jQuery('#pageAbsoluteFooter li#cart-menu a.menu span, #pageAbsoluteFooter li#recent-menu a.menu span').hover(ShowFooterMenu);
    jQuery('#pageAbsoluteFooter li#cart-menu, #pageAbsoluteFooter li#recent-menu').mouseleave(HideFooterMenu);

    // all "add to cart" buttons
    jQuery('button.button-addtocart').click(function(ev){
        ev.preventDefault(); 
        
        var ddata = {popup: 1, product: this.id.substr(4), qty: jQuery(this.parentNode).find('input')[0].value};
        var superAttrib = jQuery.data(this, "superattrib");
        if (superAttrib)
            ddata[superAttrib[0]] = superAttrib[1];

        jQuery.ajax({
            type: "post",
            url: "/checkout/cart/add",
            data: ddata,
            success: function(response){
                if (response.length > 20000) {
                    alert('Unable to complete request. Please try again later.');
                } else {
                    HideFooterMenu("cart-menu");
                    jQuery("#cart-menu div#footer-cart-menu").html(response);
                    var hh = jQuery('#pageAbsoluteFooter #footer-cart-menu').height() + 20;
                    jQuery('#pageAbsoluteFooter #footer-cart-menu').css({bottom:'-'+hh+'px'});
                    setTimeout('ShowFooterMenu("cart-menu", "nowait")', 50);
                    setTimeout('HideFooterMenu("cart-menu")', 5000);
                }
            },
            error: function(){
                alert( "Unable to complete request. Please try again later.");
            }
        });
    });

    // the purpose of this is to make the first 'slide-up' more smoother,
    // since these boxes will be aligned at one level after this piece of code
    jQuery('#pageAbsoluteFooter li[id]').each(HideFooterMenu);
    
});

// MenuStatus
// 0 - not visible
// 10 - showing pause
// 15 - showing animation started
// 20 - visible
// 30 - hiding
function ShowFooterMenu(idd, nowait){
    var thisByIdd;
    if (idd && idd.length) {
        thisByIdd = jQuery("#"+idd)[0];
    } else {
        idd = this.parentNode.parentNode.id;
        thisByIdd = this.parentNode.parentNode;
    }
    var footerMenuStatus = jQuery.data(thisByIdd, "MenuStatus") || 0;

    if (footerMenuStatus == 0) {
        //debugger;
        jQuery.data(thisByIdd, "MenuStatus", 10);
        
        // if nowait, than skip delay
        if (nowait === "nowait") {
            DoShowMenu(idd);
        } else {
            setTimeout("DoShowMenu('"+idd+"');", 500);
        }
    } else if (footerMenuStatus == 30) {
        jQuery.data(thisByIdd, "AfterAnimationEvent", function(){ShowFooterMenu(idd)});
    }
}
function DoShowMenu(id) {
    var thisByIdd = jQuery("#"+id)[0];
    var footerMenuStatus = jQuery.data(thisByIdd, "MenuStatus") || 0;
    if (footerMenuStatus == 10) {
        jQuery.data(thisByIdd, "MenuStatus", 15);
        setTimeout("jQuery('#"+id+"').addClass('hovered');", 250);
        jQuery('#pageAbsoluteFooter #footer-'+id).animate({bottom:'35px'}, { duration: 500, complete: function(){
            var menuItem = jQuery("#"+this.id.substr(7))[0];
            jQuery.data(menuItem, "MenuStatus", 20);
            if (jQuery.data(menuItem, "AfterAnimationEvent")){
                jQuery.data(menuItem, "AfterAnimationEvent")();
                jQuery.data(menuItem, "AfterAnimationEvent", null);
            }
        }});
    }
}
function HideFooterMenu(idd){
    var thisByIdd = this;
    if (idd && idd.length) {
        thisByIdd = jQuery("#"+idd)[0];
    } else {
        idd = this.id;
    }
    var footerMenuStatus = jQuery.data(thisByIdd, "MenuStatus") || 20;
    if (footerMenuStatus == 20) {
        jQuery.data(thisByIdd, "MenuStatus", 30);        
        setTimeout('jQuery("#'+idd+'").removeClass("hovered");', 200);
        var hh = jQuery('#pageAbsoluteFooter #footer-'+idd).height() + 20;
        jQuery('#pageAbsoluteFooter #footer-'+idd).animate({bottom:'-'+hh+'px'}, { duration: 500, complete: function(){
            var menuItem = jQuery("#"+this.id.substr(7))[0];
            jQuery.data(menuItem, "MenuStatus", 0);
            if (jQuery.data(menuItem, "AfterAnimationEvent")){
                jQuery.data(menuItem, "AfterAnimationEvent")();
                jQuery.data(menuItem, "AfterAnimationEvent", null);
            }
        }});
    } else if (footerMenuStatus == 15) {
        jQuery.data(thisByIdd, "AfterAnimationEvent", function(){HideFooterMenu(idd)});
    } else if (footerMenuStatus == 10) {
        jQuery.data(thisByIdd, "MenuStatus", 0);
    }
}

/* End Footer Menu */

/* Header Popups */

var methodShow = 'show';
var methodHide = 'hide';

function toggleBox(boxClass, method) {
  var displayStyle = (method=='show')?'block':'none';
  $$("div."+boxClass).first().setStyle({display:displayStyle});
}

Event.observe(window, 'load', function(){

  if ($$("li#topLinkLogin a").first() != undefined) {
    $$("li#topLinkLogin a").first().observe("click", function(e) {
      toggleBox('header-email-popup', methodHide);
      toggleBox('header-login-popup', methodShow);
      e.stop();
    });
  }
  
  if ($$("a.popup-login-link").first() != undefined) {
      $$("a.popup-login-link").first().observe("click", function(e) {
        toggleBox('header-login-popup', methodHide);
        e.stop();
      });
  }
  
  if ($$("li#topLinkEmail a").first() != undefined) {
      $$("li#topLinkEmail a").first().observe("click", function(e) {
        toggleBox('header-login-popup', methodHide);
        toggleBox('header-email-popup', methodShow);
        e.stop();
      });
  }
  
  if ($$("p.popup-email-link").first() != undefined) {
      $$("p.popup-email-link").first().observe("click", function(e) {
        toggleBox('header-email-popup', methodHide);
        e.stop();
      });
  }
  
  if ($("newsletter-validate-detail") != null) {
      $("newsletter-validate-detail").observe("submit", function(e) {
        if (!newsletterSubscriberFormDetail.validator.validate()) {
          e.stop();
          return false;
        }
        new Ajax.Request(e.findElement().action, {
          method: 'post',
          parameters: {email: e.findElement().down("input").value},
          onSuccess: function(transport) {
            var responseJSON = transport.responseText.evalJSON();
            //if (responseJSON.code == 200) {
              $("newsletter-validate-detail").innerHTML = responseJSON.message;
            //}
            if (jQuery('#signup-overlay')) {
              setTimeout(function() {
                jQuery('#signup-overlay').click()
              }, 4000);
            }
          }
        });
        e.stop();
      });
  }
  
  $(document).observe("click", function(e) {    
    var divClassName = new Array();
    divClassName[0] = 'header-email-popup';
    divClassName[1] = 'header-login-popup';
    for (i=0;i<2;i++){
        if ($$('.' + divClassName[i]).size() == 0) {
            continue;
        }
    
    if(!e.findElement().up("."+divClassName[i]) && !e.findElement().hasClassName(divClassName[i])) {
      toggleBox(divClassName[i], methodHide);
    }}
  });

});

/* End Header Popups */



