$(document).ready(function() {

    //search text
    $("#section-bar div.search fieldset input.text").val("Search");

    $("#section-bar div.search fieldset input.text").click(function() {
        if ($(this).val() == "Search")
            $(this).val("");
    });

    $("#section-bar div.search fieldset input.text").blur(function() {
        if ($(this).val() == "")
            $(this).val("Search");
    });

    // primary nav mega dropdown
    var captionText = "";
    //$("#primarynav li div.wrapper-parent").hide();
    $("#primarynav ul > li:has(ul)").hover(function() {
        $(this).find('div.wrapper-parent').show();
        $(this).addClass("hover");
    },
        function() {
        $(this).find('div.wrapper-parent').hide();
        $(this).removeClass("hover");
        });
    $("#primarynav li ul li").hover(function() {
        captionText = $(this).find("div.caption").html();
        $("#primarynav li div.sub-wrapper").html(captionText);
    },
	    function() {
	        $("#primarynav li div.sub-wrapper").html("<p>&nbsp;</p>");
	    });

    //quick-nav accordion
    $("div.right-column div.quick-nav").accordion({ autoHeight: false });

    // secondary navigation
    $("div#sub-nav ul ul").hide();
    var openIcon = "<img src='Images/ICN-sub-nav-parent-open-on.gif' alt='' width='9' height='9' />"
    var closedIcon = "<img src='Images/ICN-sub-nav-parent-closed.gif' class='rollover' alt='' width='9' height='9' />"

    $("div#sub-nav li:has(ul)").prepend(closedIcon);
    $("div#sub-nav > ul > li.current").find("ul").show();
    $("div#sub-nav > ul > li.current > img").attr("src", "Images/ICN-sub-nav-parent-open-on.gif");
    $("div#sub-nav img").click(function() {
        if ($(this).parent().find("ul").is(":hidden")) {
            $(this).parent().find("ul").slideDown();
            $(this).attr("src", "Images/ICN-sub-nav-parent-open-on.gif");
            $(this).parent("li").addClass("current");
            $(this).parent().siblings().find("ul").not(":hidden").slideUp();
            $(this).parent().siblings("li").removeClass("current");
            $(this).parent().siblings().find("img").attr("src", "Images/ICN-sub-nav-parent-closed.gif");
        } else {
            $(this).parent().find("ul").slideUp();
            $(this).attr("src", "Images/ICN-sub-nav-parent-closed.gif");
            $(this).parent("li").removeClass("current");
        }
    });

    $("div#sub-nav img").mouseover(function() {
        if (this.src.indexOf('ICN-sub-nav-parent-closed') > 0)
            $(this).attr("src", $(this).attr("src").split(".gif").join("-on.gif"));
    }).mouseout(function() {
        if (this.src.indexOf('ICN-sub-nav-parent-closed') > 0) {
            if (this.src.indexOf('-on') > 0)
                $(this).attr("src", $(this).attr("src").replace('-on', ''));
            //this.src = this.src.replace('-on', '');
        }
    });

    $("div#sub-nav > ul > li:has(img) > a").mouseover(function() {
        var controlImage = $(this).parent().find("img").attr("src");
        //alert(controlImage);
        if (controlImage.indexOf('ICN-sub-nav-parent-closed') > 0)
            $(this).parent().find("img").attr("src", $(this).parent().find("img").attr("src").split(".gif").join("-on.gif"));
        //alert($(this).parent().find("img").attr("src"));
    }).mouseout(function() {
        var controlImage = $(this).parent().find("img").attr("src");
        if (controlImage.indexOf('ICN-sub-nav-parent-closed') > 0) {
            if (controlImage.indexOf('-on') > 0)
                $(this).parent().find("img").attr("src", $(this).parent().find("img").attr("src").replace('-on', ''));
        }
    });

    // faqs ui
    $("#faq-grid").tabs();
    var currentTab = "";
    $("div.faq-sidebar ul li").hover(function() {
        $(this).addClass("over").siblings().removeClass("over");
    },
        function() {
            $(this).removeClass("over");
        });

    $("div.faq-sidebar ul li").click(function() {

        if (currentTab != $(this).attr("ID")) {
            $("#sub-tab-" + currentTab).hide();
            $("#subtab-" + currentTab + "-questions").tabs('destroy');
        }

        currentTab = $(this).attr("ID");
        $(this).addClass("active").siblings().removeClass("active");
        $("#sub-tab-" + currentTab).show();
        $("#subtab-" + currentTab + "-questions").tabs();
    });

    //$("#subtab-01-questions").tabs();

    // rfq ui
    $("#rfq-grid").tabs();

    //glossary ui
    $("#glossary").tabs();

    // popup windows
    $("#aligal-launch").click(function() {
        var agigalPopup = window.open('Aligal_popup.aspx', 'AligalPopup', 'height=515,width=525');
    });

    $("#rata-launch").click(function() {
        var rataPopup = window.open('Rata_popup.aspx', 'RataPopup', 'height=630,width=811');
    });

    $("#reference-launch").click(function() {
        var referencePopup = window.open('Reference_popup.aspx', 'ReferencePopup', 'height=630,width=811');
    });

    $("#acublend-launch").click(function() {
        var acublendPopup = window.open('Acublend_popup.aspx', 'AcublendPopup', 'height=630,width=811');
    });

    $("#certified-launch").click(function() {
        var certifiedPopup = window.open('Certified_popup.aspx', 'CertifiedPopup', 'height=630,width=811');
    });

    $("#natural-gas-launch").click(function() {
        var certifiedPopup = window.open('Natural_Gas_popup.aspx', 'NaturalGasPopup', 'height=630,width=811');
    });

    // SMARTOP Video
    $("a[rel^='prettyPhoto']").prettyPhoto({

        animationSpeed: 'normal', /* fast/slow/normal */
			opacity: 0.80, /* Value between 0 and 1 */
			showTitle: true, /* true/false */
			allowresize: true, /* true/false */
			default_width: 100,
			default_height: 100,
			counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
			theme: 'facebook', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
			hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
			wmode: 'window', /* Set the flash wmode attribute */
			autoplay: true, /* Automatically start videos: True/False */
			modal: false /* If set to true, only the close button will close the window */

    });
});
