var _jQueryLoaded = (typeof (jQuery) == "function");
//var _jQueryLoaded = (typeof (jQuery.noConflict) == "function");
var JQ = jQuery.noConflict();

if (_jQueryLoaded)
{
        JQ(document).ready(function() {
                InitHoverSwaps();
                MainNavHeightInit();
                TableCellBorderInit();

                //Custom Selectbox
                var checkDropdownExists = JQ(".custom-dropdown").size();
                if(checkDropdownExists != 0){
                        JQ(".custom-dropdown").selectbox();
                }

                // IE 6 PNG fix for elements:
                // Elements to "fix" are stored in array for readability, converted back to string to feed into fix.
                var DD_fix =
                        [
                                ".arrow-right",
                                ".searchcontainer",
                                "#TB_closeWindowButton"
                        ].toString();

                (typeof DD_belatedPNG == "object") ? DD_belatedPNG.fix(DD_fix) : "";

                // Activate tab sets
                // JQ("#tabs_updates").tabs();

                // Activate field hints
                JQ(".fieldhint").fieldhint();

                // Custom font replacements
                cufonReplacements();

                // Clear field default value on focus if nothing entered put default value back on blur
                ToggleDefaultValue();

        });
}

var _RolloverNavigationLoaded = false;
function InitRolloverNavigation() {
        if (_jQueryLoaded && !_RolloverNavigationLoaded) {
                JQ(".rollover-navigation a").each(function() {
                        JQ(this).find("img.navitem_prejs").each(function() {
                                JQ(this).attr("class", "navitem_off");

                                var newImage = JQ(this).clone();

                                var newImageSrc = newImage.attr("src").replace("_0.png", "_1.png");
                                newImage.attr("src", newImageSrc);

                                newImage.attr("class", "navitem_hover");

                                newImage.insertAfter(this);
                        });

                        JQ(this).focus(
                                function() {
                                        JQ(this).toggleClass("hover");
                                }
                        ).blur(
                                function() {
                                        JQ(this).toggleClass("hover");
                                }
                        );
                });

                _RolloverNavigationLoaded = true;
        }
}
function InitHoverSwaps()
{
        JQ(".hoverswap").each(function()
        {
                var imageSrc = JQ(this).attr("src");
                var imageName = imageSrc.substring(imageSrc.lastIndexOf("swap_") + 5, imageSrc.lastIndexOf("_"));

                for (var preloadLoop=0; preloadLoop < 2; preloadLoop++)
                {
                        if (eval("typeof(b" + imageName + preloadLoop + ")") != "object")
                        {
                                eval("b" + imageName + preloadLoop + " = new Image()");
                                if (imageSrc.indexOf(".gif") > -1) {
                                        eval("b" + imageName + preloadLoop + ".src = 'Assets/swap_" + imageName + "_" + preloadLoop + ".gif'");
                                } else if (imageSrc.indexOf(".jpg") > -1) {
                                        eval("b" + imageName + preloadLoop + ".src = 'Assets/swap_" + imageName + "_" + preloadLoop + ".jpg'");
                                } else if (imageSrc.indexOf(".png") > -1) {
                                        eval("b" + imageName + preloadLoop + ".src = 'Assets/swap_" + imageName + "_" + preloadLoop + ".png'");
                                }
                        }
                }
                JQ(this).mouseover(function(){
                        if (JQ(this).attr("class").indexOf("btn-disabled")<=-1)
                        {
                                JQ(this).attr("src", eval("b" + imageName + "1.src"));
                        }
                });
                JQ(this).mouseout(function()
                {
                        if (JQ(this).attr("class").indexOf("btn-disabled")<=-1)
                        {
                                JQ(this).attr("src", eval("b" + imageName + "0.src"));
                        }
                });
                JQ(this).click(function()
                {
                        JQ(this).attr("src", eval("b" + imageName + "2.src")).addClass("btn-disabled");
                });

        });

        JQ(".btn, .btn-small").click(function()
        {
                JQ(this).addClass("btn-disabled"); /* THIS NEEDS TO BE CHANGED to add another class that includes what the button says, e.g. "btn-close" becomes "btn-close btn-close-disabled" */
        });
}

// correct height of subnav container 
function MainNavHeightInit() {
        JQ("#main-nav ul li.sub ul li.sub").addClass("level3");

        var mainNavHeight = JQ("#bodycontainer").height();

        JQ("#main-nav ul li.sub").hover(function () {

                var mainNavHeight = JQ("#bodycontainer").height();

                var navPos = JQ(this).position();
                var posOffset = navPos.top + JQ("#bodyscroll").scrollTop();

                var findSubClass = JQ(this).find("li.sub").attr("class");

                if(findSubClass == "sub level3"){
                        JQ(this).find("li.sub").find("ul:last").addClass("last");
                }

                var findLastSubClass = JQ(this).find("ul").attr("class");

                if(findLastSubClass == "last"){
                        JQ(this).find("ul.last").css("margin-top", -posOffset).css("padding-top", posOffset).height(mainNavHeight - navPos.top);
                } else {
                        JQ(this).find("ul").css("margin-top", -posOffset).css("padding-top", posOffset).height(mainNavHeight - posOffset);
                }

                JQ(this).find("iframe").css("margin-top", -posOffset).css("padding-top", posOffset).height(mainNavHeight - posOffset);
        });

        JQ("#main-nav iframe").each(function () {
                var iframeHeight = JQ(this).parents("li.sub").find("ul").height();
                JQ(this).height(iframeHeight);
        });

};

JQ(function(){
                                         
        JQ(window).bind('resize', function() { //Event on resize 
                MainNavHeightInit();
        });

        //Expandable bar control. 
        var checkExpandableBar = JQ(".expandable-bar").size();
        if(checkExpandableBar != 0){
                JQ(".expandable-bar .hide").css("display", "none");

                JQ(".expandable-bar a.expandtitle").click(function(){
                        var expandShow = JQ(this).parents(".expandable-bar").find(".hide").css("display");                                                    
                        if(expandShow == "none"){
                                JQ(this).parents(".expandable-bar").find(".hide").css("display", "block");
                                JQ(this).addClass("expanded");
                        } else {
                                JQ(this).parents(".expandable-bar").find(".hide").css("display", "none");
                                JQ(this).removeClass("expanded");
                        }
                });
        }

});

// field Hints
jQuery.fn.fieldhint = function () {
  return this.each(function (){
    // get jQuery version of 'this'
    var t = jQuery(this); 
    // get it once since it won't change
    var title = t.attr('title'); 
    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      t.blur(function (){
        if (t.val() == '') {
          t.val(title);
          t.addClass('fieldhint_blur');
        }
      });
      // on focus, set value to blank if current value 
      // matches title attr
      t.focus(function (){
        if (t.val() == title) {
          t.val('');
          t.removeClass('fieldhint_blur');
        }
      });

      // clear the pre-defined text when form is submitted
      t.parents('form').submit(function(){ // THIS USED TO SAY form:first()
          if (t.val() == title) {
              t.val('');
              t.removeClass('fieldhint_blur');
          }
      });
          // do the same when the window is unloaded
          JQ(window).unload(function(){ // THIS USED TO SAY form:first()
          if (t.val() == title) {
              t.val('');
              t.removeClass('fieldhint_blur');
          }
      });

      // now change all inputs to title
      t.blur();
    }
  });
 }

function ToggleDefaultValue() {
        // For forms to clear values on focus and add values on blur
        JQ("input.searchinput[type=text]").focus(function() {
                currentVal = JQ(this).attr("title");
                (JQ(this).val() == currentVal) ? JQ(this).val("") : "";
        }).blur(function() {
                (JQ(this).val() == "") ? JQ(this).val(currentVal) : "";
        });
}

function cufonReplacements() {
        var checkh1 = JQ('h1, h2').size();

        if(checkh1 != 0){
                Cufon.replace('h1', { fontFamily: 'Myriad-Light' });
                Cufon.replace('h2, .main-intro01, .price, .promo-container span.title', { fontFamily: 'Myriad Pro' });
                Cufon.replace('h3', { fontFamily: 'Myriad Pro Semibold' });
		Cufon.replace(".main-intro01 strong",{fontFamily:"Myriad Pro Bold"});
        }
}

function TableCellBorderInit() {
        // Remove dotted border from first td
        JQ(".table01 tr td:first-child").each(function()
        {
                JQ(this).css("border-left","none");
        });
        JQ(".table01 tr th:first-child").each(function()
        {
                JQ(this).css("border-left","none");
        });
}

// Popup window profiles
var profiles =
{

        windowCenter:
        {
                height:550,
                width:878,
                center:1, 
                resizable:1, 
                createnew:0,
                scrollbars:1
        }

};


JQ(function(){

        JQ(".popupwindow").popupwindow(profiles);

        JQ("a.close-window").click(function(){
                window.close();
                return false;
        }); 

});


/*
Popupwindow plugin for jQuery.
by: Tony Petruzzi
homepage: http://rip747.wordpress.com
*/
jQuery.fn.popupwindow = function(p)
{

        var profiles = p || {};

        return this.each(function(index){
                var settings, parameters, mysettings, b, a;

                // for overrideing the default settings
                mysettings = (jQuery(this).attr("rel") || "").split(",");


                settings = {
                        height:600, // sets the height in pixels of the window.
                        width:861, // sets the width in pixels of the window.
                        toolbar:0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
                        scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
                        status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
                        resizable:0, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
                        left:0, // left position when the window appears.
                        top:0, // top position when the window appears.
                        center:0, // should we center the window? {1 (YES) or 0 (NO)}. overrides top and left
                        createnew:1, // should we create a new window for each occurance {1 (YES) or 0 (NO)}.
                        location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
                        menubar:0 // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
                };

                // if mysettings length is 1 and not a value pair then assume it is a profile declaration
                // and see if the profile settings exists

                if(mysettings.length == 1 && mysettings[0].split(":").length == 1)
                {
                        a = mysettings[0];
                        // see if a profile has been defined
                        if(typeof profiles[a] != "undefined")
                        {
                                settings = jQuery.extend(settings, profiles[a]);
                        }
                }
                else
                {
                        // overrides the settings with parameter passed in using the rel tag.
                        for(var i=0; i < mysettings.length; i++)
                        {
                                b = mysettings[i].split(":");
                                if(typeof settings[b[0]] != "undefined" && b.length == 2)
                                {
                                        settings[b[0]] = b[1];
                                }
                        }
                }

                // center the window
                if (settings.center == 1)
                {
                        settings.top = (screen.height-(settings.height + 110))/2;
                        settings.left = (screen.width-settings.width)/2;
                }

                parameters = "location=" + settings.location + ",menubar=" + settings.menubar + ",height=" + settings.height + ",width=" + settings.width + ",toolbar=" + settings.toolbar + ",scrollbars=" + settings.scrollbars  + ",status=" + settings.status + ",resizable=" + settings.resizable + ",left=" + settings.left  + ",screenX=" + settings.left + ",top=" + settings.top  + ",screenY=" + settings.top;

                jQuery(this).bind("click", function(){
                        var name = settings.createnew ? "PopUpWindow" + index : "PopUpWindow";
                        window.open(this.href, name, parameters).focus();
                        return false;
                });
        });

};
