jQuery(document).ready(function(){

    jQuery('#block-search-form input[type="text"]').attr("value", "Search");
    jQuery('#block-search-form input[type="text"]').focus(function() { 
        if (jQuery(this).attr("value") === "Search")
            jQuery(this).attr("value",""); 
    });

    // Top Level Nav behavior
    jQuery('#block-system-main-menu .menu:first > li > a').hover(
        function(evt) { 
            var el = jQuery('ul:first', this.parentNode)
            if(!el[0])
                return
                
            if(el[0].isDown) {
                el[0].needsToGoBackUp = true
                return
            }
            
            el[0].isDown = true
            el.slideDown('800'); 
        },
        
        function() {
            var el = jQuery('ul:first', this.parentNode)
            if(!el[0])
                return
                
            el[0].needsToGoBackUp = false
        }
    );

    jQuery('#block-system-main-menu .menu:first > li').hover(
        function() {},
        function() { 
            var el = jQuery('ul:first', this)

            if(!el[0] || !el[0].isDown)
                return
                
            el.slideUp('600', function() {
                this.isDown = false
                if(!this.needsToGoBackUp)
                    return
                this.isDown = true
                jQuery('ul:first', this.parentNode).slideDown('800')
            }); 
        }
    );

    // Open blog link in new window
    jQuery('#block-system-main-menu .menu li a').each(function() {
        if (/.*blog\.highjump\.com.*/.test(jQuery(this).attr("href"))) {
            jQuery(this).attr("target","_new");
        }
    });
            
            

    // End top level nav behavior


    // Secondary level nav behavior
    // nth-child(2) is so only Solutions has second-level flyout
    jQuery('#block-system-main-menu .menu li:nth-child(2) .menu > li').hover(
        function() {
            jQuery(this).find('ul').css('filter', 'alpha(opacity=100)');
            jQuery(this).find('ul').fadeIn('fast');
        },
        function() { 
            jQuery(this).find('ul').fadeOut('fast');
        }
    );


    // Customer login box
    jQuery("#customer-login-link").click(function() { 
            var position = jQuery("#customer-login-link").offset();
            jQuery("#customer-login").dialog({
                modal: true, 
                resizable: false, 
                draggable: false, 
                position: [position.left, position.top + 15],
                open: function(event, ui) { jQuery("#navigation").css("position","static"); }, // These two lines are a hack to make sure the dialog is on top of the main menu in IE7 
                close: function(event, ui) { jQuery("#navigation").css("position","relative"); }
            }); 
            // This is a hack so the browser doesn't focus on the first link when the dialog pops up
            jQuery("#dia-focus").focus();
        });
    jQuery("#customer-login #close-link").click(function() { jQuery("#customer-login").dialog("close"); });
    jQuery(document).click(function(e){
        var $clicked = jQuery(e.target);
        if (!($clicked.is('#customer-login') || $clicked.is('#customer-login-link'))) {
            jQuery("#customer-login").dialog("close");
        }
        else {
            return false;
        }
    });


    // Vertical tabs behavior
    if (jQuery("#tabs").length != 0) {
        jQuery("#tabs").tabs().addClass('ui-tabs-vertical ui-helper-clearfix');
        jQuery("#tabs li").removeClass('ui-corner-top').addClass('ui-corner-left');
    }

    // Resource Center styling
    jQuery(".node-type-resource-page .views-view-grid td.col-last").each(function() {
        if (jQuery.trim(jQuery(this).html()).length == 0) {
            jQuery(this).css("background-image","none");
        }
    });

    //Right sidebar border styling
    if (jQuery(".region-sidebar-second .block-contact-button").length != 0) {
        if (jQuery(".region-sidebar-second .block-contact-button").prevAll().length == 0) {
            jQuery(".region-sidebar-second .block-contact-button").css("background","none");
        }
    }
    else if (jQuery(".region-sidebar-second .block-no-border").length != 0) {
        jQuery(".region-sidebar-second .block-no-border").first().prev().css("padding-bottom","0px").css("margin-bottom","0px");
        jQuery(".region-sidebar-second .block-no-border").first().before("<div class='block corner-round-right'><div>");
    }
    else {
        jQuery(".region-sidebar-second .block").last().css("padding-bottom","0px");
        jQuery(".region-sidebar-second .section").css("background","#fefaf9 url('/sites/all/themes/highjump1/images/corner-round-right.png') no-repeat bottom right").css("padding-bottom","2px");
    }

    // Contact widget behavior
    jQuery("#contact-widget .call-tab").hover(function() { 
                jQuery(this).find("a").css("background-position","-106px -1px");
                jQuery("#contact-widget .email-tab a").css("background-position","-1px -38px");
                jQuery("#contact-widget .contact-tab a").css("background-position","-1px -76px");
                jQuery("#contact-widget #email").fadeOut();
                jQuery("#contact-widget #contact").fadeOut();
                jQuery("#contact-widget #call").fadeIn();
          });
    jQuery("#contact-widget .email-tab").hover(function() { 
                jQuery(this).find("a").css("background-position","-106px -37px");
                jQuery("#contact-widget .call-tab a").css("background-position","-1px -1px");
                jQuery("#contact-widget .contact-tab a").css("background-position","-1px -76px");
                jQuery("#contact-widget #contact").fadeOut();
                jQuery("#contact-widget #call").fadeOut();
                jQuery("#contact-widget #email").fadeIn();
          });
    jQuery("#contact-widget .contact-tab").hover(function() { 
                jQuery(this).find("a").css("background-position","-106px -76px");
                jQuery("#contact-widget .call-tab a").css("background-position","-1px -1px");
                jQuery("#contact-widget .email-tab a").css("background-position","-1px -38px");
                jQuery("#contact-widget #call").fadeOut();
                jQuery("#contact-widget #email").fadeOut();
                jQuery("#contact-widget #contact").fadeIn();
          });


    // Form-protect White Papers
    jQuery(".node-type-resource-page .views-view-grid a, #block-views-related-resources-block .field-content a").each(function() {
            if (/.*\/WP-US-(EDI|What-Is-EDI).*/.test(jQuery(this).attr("href"))) {
                jQuery(this).attr("href","/solutions/truecommerce/request-whitepaper");
                jQuery(this).attr("target","_new");
            }
            else if (/.*\/WP-US-.*/.test(jQuery(this).attr("href"))) {
                jQuery(this).attr("href","/registration-form?rurl=" + jQuery(this).attr("href"));
                jQuery(this).attr("target","_new");
            }
            else if (/.*\/(RFP|ROI)-.*\.(xls|xlsx)$/.test(jQuery(this).attr("href"))) {
                jQuery(this).attr("href","/registration-form?rurl=" + jQuery(this).attr("href"));
                jQuery(this).attr("target","_new");
            }
    });

    jQuery(".node-type-resource-page .views-view-grid .field-content").each(function() {
            if (/Special Report/.test(jQuery(this).html())) {
                jQuery(this).html("Whitepaper");
            }
    });


    // Form-protect TC videos
    jQuery(".page-node-357 .views-view-grid td a").each(function() {
            if (/.*(lskKwA5-xhM|6UlXLpwAWHM).*/.test(jQuery(this).attr("href"))) {
                jQuery(this).attr("href","/solutions/truecommerce/view-presentation?purl="+jQuery(this).attr("href"));
            }
            else {
                jQuery(this).addClass("video");
            }
    });

    // Tracking & form protect & format right-column resources
    jQuery("#block-views-related-resources-block a").each(function() {
        jQuery(this).html(jQuery(this).html().replace(/Whitepaper:/ig, "<b>Whitepaper:</b><br>"));
        jQuery(this).html(jQuery(this).html().replace(/Case Study:/ig, "<b>Case Study:</b><br>"));
        jQuery(this).html(jQuery(this).html().replace(/Datasheet:/ig, "<b>Datasheet:</b><br>"));
        jQuery(this).html(jQuery(this).html().replace(/Brochure:/ig, "<b>Brochure:</b><br>"));
        jQuery(this).html(jQuery(this).html().replace(/ROI Calculator:/ig, "<b>ROI Calculator:</b><br>"));
        if (/Video:.*/.test(jQuery(this).html())) {
            jQuery(this).addClass("video");
            jQuery(this).html(jQuery(this).html().replace(/Video:/ig, "<b>Video:</b><br>"));
        }
    });

    jQuery("#block-views-related-resources-block .field-content").each(function() {
        jQuery(this).html(jQuery(this).html().replace(/,/ig, "<br>"));
    });

    // Set up file downloads with Eloqua Tracking
    jQuery("#content a, #block-views-related-resources-block .field-content a").each(function() {
        if (/.*Resources\/.*\.(pdf|xls|xlsx|doc|docx)$/.test(jQuery(this).attr("href"))) {
            jQuery(this).attr("target","_new");
            if (/.*(registration-form|elqNow).*/.test(jQuery(this).attr("href"))) {
                return; 
            }
            else {
                jQuery(this).click(function() {
                    jQuery.domainName = "http://"+this.hostname;
                    jQuery.pdfLink = jQuery(this).attr("href");
                    jQuery.openPdf = jQuery.domainName+jQuery.pdfLink
                    elqFCS(jQuery.openPdf);
                });
            }
        }
    });

    // Forms
    jQuery(".page-node-383 .form-item-submitted-request-Request-a-demo input.form-checkbox").attr("checked",true);
    jQuery(".page-node-386 .form-item-submitted-request-Request-a-white-paper input.form-checkbox").attr("checked",true);
    jQuery(".page-node-10233 .form-item-submitted-request-Request-a-quote input.form-checkbox").attr("checked",true);
    jQuery(".page-node-320 .form-item-submitted-request-Request-information-from-an-EDI-specialist input.form-checkbox").attr("checked",true);


    //Partner Page 
    jQuery(".page-node-8 #become-partner-tab").click( function() {
            location.href="partners/become-partner";
    });

    // Alphabetical navigation for Trading Partner Maps page
    if (jQuery(".trading-partner-list").length != 0){
        jQuery('.trading-partner-list').attr('id','trading-partner-list');
        jQuery('#trading-partner-list').listnav();
    }

    // YouTube Lightboxes
    if (jQuery(".video a, a.video").length != 0){
        jQuery(".video a, a.video").each(function() {
            jQuery(this).attr("href",jQuery(this).attr("href").replace("watch\?v=","embed/"));
        });
        jQuery(".video a, a.video").colorbox({iframe:true, innerWidth:500, innerHeight:400});
    }

});
;

