﻿jQuery.noConflict();
var $j = jQuery;

var isIE6 = false;
jQuery(document).ready(function() {
    if ((jQuery.browser.msie) && (parseInt($j.browser.version, 10) == "6")) {
        isIE6 = true;
        try {
            document.execCommand('BackgroundImageCache', false, true);
        }
        catch (e) {
        }
    }

    //Watermarking.
    jQuery('.value-replace').each(function() {
        var defaultValue = '';
        var keypressed = false;
        jQuery(this).focus(function() { if (defaultValue === '' && !keypressed) { defaultValue = jQuery(this).val(); jQuery(this)[0].value = ''; } });
        jQuery(this).blur(function() { if (jQuery(this).val() === '' && !keypressed) { jQuery(this)[0].value = defaultValue; defaultValue = ''; } });
        jQuery(this).keypress(function() { keypressed = true; });
        jQuery(this).change(function() { if (jQuery(this).val() === '') keypressed = false; });
    });

    configurePopups();
});

function initializeTopNavigation_ForDropdown(){
    // Signal that the nav is to be dropdownable
    
    jQuery('#top').addClass('drop');

    // Get all the li elements from the configured level down
    // Hook em 'n Hide em

    var selectorForHookableListItems = '#top ul.level' + jQuery('#TopNavigation_dropdownLevel').val() + ' li';

    jQuery(selectorForHookableListItems).hover(
      function() {
          jQuery(this).children('ul').addClass('expand');
      },
      function() {
          jQuery(this).children('ul').removeClass('expand');
      }
    );
}

function configurePopups(){

    /* hijack click behaviour for links that have the classname 'popup' to open in a popup window */
    jQuery('a.popup').click(function(e) {
        var href = jQuery(this).attr("href");

        window.open(href, "popup", "width=500,height=700,resizable=yes,scrollbars=yes");
        e.preventDefault();
        e.stopPropogation();
    });

    /* hijack click behaviour for links that have the classname 'popup' to open in a popup window */
    jQuery('a.popupNoFurniture').click(function(e) {
        var href = jQuery(this).attr("href");

        if (href.indexOf('?') > -1) {
            href += "&showfurniture=false";
        }
        else {
            href += "?showfurniture=false"
        }

        window.open(href, "popup", "width=500,height=700,resizable=yes,scrollbars=yes");
        e.preventDefault();
        e.stopPropogation();
    });
}

String.format = function() {
    var s = arguments[0];
    for (var i = 0; i < arguments.length - 1; i++) {
        var reg = new RegExp("\\{" + i + "\\}", "gm");
        s = s.replace(reg, arguments[i + 1]);
    }

    return s;
}

