var imageWidth = 960;
var isGallery = false;
var isFontFaceSupported = (function(){  
  var sheet, doc = document,head = doc.head || doc.getElementsByTagName('head')[0] || docElement,style = doc.createElement("style"),impl = doc.implementation || { hasFeature: function() { return false; } };
 
  style.type = 'text/css';
  head.insertBefore(style, head.firstChild);
  sheet = style.sheet || style.styleSheet;
 
  var supportAtRule = impl.hasFeature('CSS2', '') ?
        function(rule) {
            if (!(sheet && rule)) return false;
            var result = false;
            try {
                sheet.insertRule(rule, 0);
                result = !(/unknown/i).test(sheet.cssRules[0].cssText);
                sheet.deleteRule(sheet.cssRules.length - 1);
            } catch(e) { }
            return result;
        } :
        function(rule) {
            if (!(sheet && rule)) return false;
            sheet.cssText = rule;
 
            return sheet.cssText.length !== 0 && !(/unknown/i).test(sheet.cssText) &&
              sheet.cssText
                    .replace(/\r+|\n+/g, '')
                    .indexOf(rule.split(' ')[0]) === 0;
        };
 
  return supportAtRule('@font-face { font-family: "font"; src: "font.ttf"; }');
})();

$(function() {
  //$('#header-rotation img').css("margin-left","0px");
  $(".locationPopup").css("left",$("#location").offset().left-82); 
  if(!isFontFaceSupported){
    $('.head span').css('font-size','18px');
    $('#content .leftContent .eclub .sendForm .enterEmail_lbl').css('font-size','12px');
    $('#nav ul li a').css('font-size','15px');
  }
  
  if($("#header-rotation img").length > 1){
    imageWidth = 1446;
    isGallery = true;
  }
  
  $(window).resize(resize);
  resize();
      
  var rotationGallery = $('#header-rotation');
  if(rotationGallery.cycle != null){
      rotationGallery.cycle({timeout: 8000,
      pager: "#imgNav",
      pagerEvent: "click",     
      pagerAnchorBuilder: function(idx, slide) { return '<a href="javascript:void()"><div class="imagesPagerItem">&nbsp;</div></a>';}
    });
  }      
  if(isGallery){
      $("#header-wrap").css({"height": 356});
  }
  
  $(".nutritionalInfoBox").each(function(){
    if($(this).closest(".column").is(":first-child")){
      $(this).css({"left" : "32px"});
      $(this).children(".box-arrow").addClass("box-arrow-left");
      //$(this).children(".box-close").addClass("box-close-right");
    }
    if($(this).closest(".column").is(":last-child")){
      $(this).css({"left" : -($(this).width()+32)});
      $(this).children(".box-arrow").addClass("box-arrow-right");
      //$(this).children(".box-close").addClass("box-close-left");
    }
  });
  
  $(".nutritionalInfo").click(function(){
    $(".nutritionalInfoBox").hide();  
    $(".nutritionalInfoBtnHolder").css("z-index","1");
    var selectedBox = $(this).parent().children(".nutritionalInfoBox");
    selectedBox.parent().css("z-index", "9999");
    selectedBox.show();
    selectedBox.css({"top" : -13-selectedBox.height()/2}).children(".box-arrow").css({"top" : -23+selectedBox.children(".box-content").height()/2});    
  });
  
  $(".nutritionalInfoBox .box-close").click(function(){
    $(this).parent().hide().parent().css("z-index","1");
  });
});

  function resize() {
    $(".locationPopup").css("left",$("#location").offset().left-102); 
    var wdims = windowDims();
    
    /*if(isGallery){
      $('#header-rotation').css({
        width: '100%'
      }); 
    }else{
      $('#header-rotation').css({
        width: imageWidth
      }); 
    }*/
    /*if($(window).width()>=960){
      $('#header-rotation a img').css({'left':-(imageWidth-$(window).width())/2});
    }*/
  }

  function windowDims() {
    var dims = { width: 0, height: 0 };
        
    dims.width = $(window).width();
    dims.height= $(window).height();
    
    return dims;
  }

  $.expr[':'].cssDisplay = function (elem, pos, match) {
        var matched = false;

        var displayValue = $(elem).css("display");
        var byValue = match[3].split(";");

        $.each(byValue, function (index, value) {
            if (matched == false) {
                matched = displayValue == value;
            }
        });
        
        return matched;
    };

$(function(){
    Fatz = {
        ApplyValidationStyle: function (summarySelector) {
            if(typeof WebForm_OnSubmit == 'undefined' || $.isFunction(WebForm_OnSubmit) === false ) {
                return;
            }
          
            var aspFormOnSubmit = WebForm_OnSubmit;
            
            WebForm_OnSubmit = function () {
                var isValid = aspFormOnSubmit();

                $(".txterror:cssDisplay(none)").each(function () {
                    $(this).parent().find("label:first").css({ "color": "#7B7B7B", "font-weight": "normal" });
                });

                $(".txterror:cssDisplay(inline;block)").each(function () {
                    $(this).parent().find("label:first").css({ "color": "#c90000", "font-weight": "bold" });
                });

                $(summarySelector).css("display", isValid ? "none" : "block");

                return isValid;
            };
        },
        ValidateAndClickOnce: function (selector, disabledCssClass) {
            var alreadyCalled = false;
            var confirmButton = $(selector);
            
            var validate = typeof WebForm_OnSubmit != 'undefined' && $.isFunction(WebForm_OnSubmit) === true ? 
                WebForm_OnSubmit:
                function () {return true;};
            
            confirmButton.click(function () {
                if (alreadyCalled === false && validate()) {
                    alreadyCalled = true;

                    confirmButton.attr("class", disabledCssClass);

                    return true;
                }

                return false;
            });
        }
    };
});
