var _tabbedContentTop;

$(document).ready(function () {
    $("body").addClass("js");
    _tabbedContentTop = $("#tabbed-content").length ? $("#tabbed-content").position().top : 0;
    //$("input:text, textarea", ".w1").defaultFormValues();

    $("#quick-search").defaultFormValues({ alternativeValue: $("#quick-search-default-value:hidden").val() });

    $("#quick-search-submit").click(function (event) {
        if ($("#quick-search").val() == $("#quick-search-default-value:hidden").val()) {
            event.preventDefault();
        }
    });
  
    InitRecentlyViewedSlider();

    InitSnippetsFunctionality();
    InitTabs();

    $("#nav li").menu();

    InitjQueryCycleElements();

    var imagePreload = new Image();
    $("#thumbnails a", "#image-gallery").each(function (index, item) {
        var _this = $(item);
        var _link = _this.attr("href");
        var match = new RegExp("x=([0-9]+)\&y=([0-9]+)", "").exec(_link);

        var _width = match[1];
        var _height = match[2];

        imagePreload.src = _this.attr("href");

        $(_this).click(function (event) {
            event.preventDefault();
            $("#main-image > img").attr("src", _link);
            $("#main-image > img").attr("width", _width);
            $("#main-image > img").attr("height", _height);
        });
    });
});    

jQuery.fn.defaultFormValues = function (options) {
    fnOptions = jQuery.extend({
        alternativeValue: null
    }, options);

    var defaultValues = [];

    return this.each(function (i, item) {
        var _this = $(item);
        defaultValues[i] = fnOptions.alternativeValue != null ? fnOptions.alternativeValue : $(this).val();

        _this.bind('focusin', { index: i }, function (e) {
            if (_this.val() == defaultValues[e.data.index]) {
                _this.val('');
            }
        });

        _this.bind('focusout', { index: i }, function (e) {
            if (trim(_this.val()) == '') {
                _this.val(defaultValues[e.data.index]);
            }
        });
    });

    function trim(stringToTrim) {
        return stringToTrim.replace(/^\s+|\s+$/g, "");
    }
    function ltrim(stringToTrim) {
        return stringToTrim.replace(/^\s+/, "");
    }
    function rtrim(stringToTrim) {
        return stringToTrim.replace(/\s+$/, "");
    }
}

function InitTabs() {
    $("ul.tabset a", "#tabbed-content").each(function (index, item) {
        var _this = $(item);
        var _href = _this.attr("href");
        _this.click(function (event) {
            event.preventDefault();
            //$(window).scrollTop(_tabbedContentTop);
            _this.parent("li").addClass("active").siblings("li").removeClass("active");
            $("div" + _href, "#tabbed-content").show().siblings("div.tabarea").hide();
        });
    });
$("ul.tabset li.active a", "#tabbed-content").click();
//    if (document.location.hash != null) {
//        $("ul.tabset li a[href='" + document.location.hash + "']", "#tabbed-content").click();
//    }
//    else {
//        
//    }
}
function InitjQueryCycleElements() {
    //a bit of a fix, delay causes a js error when there aren't any valid cycle on the page
    if ($("#banner-spotlights > .promo").length > 1) {
        $("#banner-spotlights").cycle({
            fx: "scrollLeft",
            speed: 4000,
            delay: 3000
        });
    }

    if ($("#gallery #slider > li").length > 1) {
        $("#gallery #slider").cycle({
            fx: "scrollLeft",
            speed: 4000,
            delay: 3000,
            pager: "#switcher",
            activePagerClass: "active",
            before: function(){
             $("#switcher a[class = 'active']").removeClass("active").parent("li").addClass("active");
            }
        });

        $("#switcher a").wrap("<li></li>");
    }
}

function InitSnippetsFunctionality() {
    $("select").change(function (event) {
        var selected = $("option.go-to-url:selected", $(this));

        if (selected.length > 0) {
            window.location = selected.val();
        }
    });
}

//Validation on contact form

jQuery(document).ready( function() {
                // binds form submission and fields to the validation engine
                jQuery("#ctl00").validationEngine();
            });

function InitRecentlyViewedSlider() {
  $(".snippet-recently-viewed-products", "#sidebar").each(function(index, item){
    var _this = $(item);
    var _height = 0;
    var _maxHeight = 0;
    
    $("li", _this).each(function(index, item){      
      var _thisHeight = $(item).outerHeight(true);
      
      if(index < 2)
      {
        _height += _thisHeight;
      }
      
      _maxHeight += _thisHeight;
    });
    
    $("ul", _this).height(_height);
    var clicker = $(".next", _this);
    clicker.addClass("closed");  
    
    clicker.click(function(event){     
      if(clicker.hasClass("closed"))
      {
        clicker.removeClass("closed");
        $("ul", _this).height(_maxHeight);
      }
      else
      {
        
    clicker.addClass("closed");  
        $("ul", _this).height(_height).addClass("closed");
      }
    });
  });
}

function showMap(map) 
 {
    $("#location-map iframe").remove(); 
    var url = map.getAttribute("rel");  
    $("#location-map").prepend('<iframe width="560" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' + url + '"></iframe>')      
 }

//franchise grid hover code for images
      $(document).ready(function(){
        $('.franchise-image-link').hover(
          function(){
            $(this).next('.franchise-btn').addClass('active');
          },
          function(){
            $(this).next('.franchise-btn ').removeClass('active');
          }
        );
      });

