var _officeItemListCombobox = null;

var _cssStyleSelectJQ = null;

var _animationTypeSelectJQ = null;

var _animationType = "slide";



$(function() {

    var comboboxSettings = { animationSpeed: 100 };



    // Create the example combobox

    setupCombobox();



    // Create the different styles combobox

    _cssStyleSelectJQ = $("#cssStyleSelect").combobox({}, comboboxSettings);

    // Combobox has its own onChange event, but the onChange of the Select element can be used as well.

    // changeAnimation works in this manner.

    _cssStyleSelectJQ.combobox.onChange =

		function() {

		    changeStyle();

		};



    // Create the Animation types combobox

    // Note: There is no need to configure the onChange event as the event is defined in the html

    _animationTypeSelectJQ = $("#animationTypeSelect").combobox({}, comboboxSettings);



    // enable validation on forms (that request it)

    $.validator.addMethod("fname", function(value, element) { return value.search(/your first name/i); }, 'First name is required.');

    $.validator.addMethod("lname", function(value, element) { return value.search(/your last name/i); }, 'Last name is required.');



    $("#morning_bell").validate({

        rules: {

            firstname: { fname: true },

            lastname: { lname: true }

        }

    });

    $("#footer_signup").validate({

        rules: {

            firstname: { fname: true },

            lastname: { lname: true }

        }

    });

    $("#bullseye").validate({

        rules: {

            firstname: { fname: true },

            lastname: { lname: true }

        }

    });

    $("#hotsheet_frm").validate({

        rules: {

            firstname: { required: true, fname: true },

            lastname: { required: true, lname: true },

            mediaaffiliation: { required: true }

        }

    });

});



// create a jquery combobx and set the Css class styles

function setupCombobox() {

    _officeItemListCombobox = $('.officeItemList').combobox({

        comboboxContainerClass: "comboboxContainer",

        comboboxValueContentContainerClass: "comboboxValueContainer",

        comboboxValueContentClass: "comboboxValueContent",

        comboboxDropDownClass: "comboboxDropDownContainer",

        comboboxDropDownButtonClass: "comboboxDropDownButton",

        comboboxDropDownItemClass: "comboboxItem",

        comboboxDropDownItemHoverClass: "comboboxItemHover",

        comboboxDropDownGroupItemHeaderClass: "comboboxGroupItemHeader",

        comboboxDropDownGroupItemContainerClass: "comboboxGroupItemContainer"

    },

		{

		    animationType: _animationType,

		    width: 82

		});

}



// Example to show the dynamic insertion of data directly into the original Select element

// and then calling the jQuery combobox to synchronise by calling update().

function updateCombobox() {

    var officeItemListSelectJQ = $("#officeItemList");

    _officeItemListCombobox.combobox.update();

}



var timeout = 0;

var closetimer = 0;

var ddmenuitem = 0;



function dd_follow_open() {

    dd_follow_canceltimer();

    dd_follow_close();

    ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');

}
function dd_follow_open2() {

    dd_follow_canceltimer();

    dd_follow_close();

    ddmenuitem = $(this).find('.heritage-dd').css('visibility', 'visible');

}




function dd_follow_close() {

    if (ddmenuitem) ddmenuitem.css('visibility', 'hidden');

}



function dd_follow_timer() {

    closetimer = window.setTimeout(dd_follow_close, timeout);
}



function dd_follow_canceltimer() {

    if (closetimer) {

        window.clearTimeout(closetimer);

        closetimer = null;

    }

}



/* function toggleRefControls(obj, cur_state) {

var obj = $(obj);

var elem = $("#" + obj.attr('for'));

if (cur_state == "up") {

$(elem).hide();

$(elem).addClass("form_arrow_up");

} else if (cur_state == "down") {

$(elem).show();

$(elem).addClass("form_arrow_down");

}

}

*/

function hideRefControl(label, hideClassName, showClassName) {

    var label = $(label);

    $("#" + label.attr('for')).hide();

    label.removeClass(showClassName);

    label.addClass(hideClassName);

    label.unbind(); // removing any previous click events

    label.click(function() { var obj = this; showRefControl(label, hideClassName, showClassName); return false; });



    return false;

}



function showRefControl(label, hideClassName, showClassName) {

    var label = $(label);

    $("#" + label.attr('for')).show();

    label.removeClass(hideClassName);

    label.addClass(showClassName);

    label.unbind();  // removing any previous click events

    label.click(function() { var obj = this; hideRefControl(label, hideClassName, showClassName); return false; });



    return false;

}



$(document).ready(function() {



    // Create Query String for Static Search

    if ($(".static-header-exists").length > 0) {

        $("#static_search_btn").click(function() {

            var q = $("#search-query").val();

            //var sect = $("#sections").val();

            var d = "http://www.heritage.org/";

            var url = d + "Search.aspx?query=" + q + "&section=|";

            // alert(url);

            window.location = url;

            return false;
        });
    }



    //hide the all of the element with class msg_body

    $(".refs-container").hide();

    //toggle the componenet with class msg_body

    $(".show-refs").click(function() {

        $(".refs-container").slideToggle(600);

        return false;

    });





    // Prepare Header Drop Downs 

    $('#follow_parent > li').bind('mouseover', dd_follow_open);

    $('#follow_parent > li').bind('mouseout', dd_follow_timer);

    $('#follow_parent > #more').bind('mouseover', dd_follow_open);

    $('#follow_parent > #more').bind('mouseout', dd_follow_timer);



    // Prepare Tabbed-Containers

    $("ul.tabbed-nav:not(.no-js) li a").click(function() {

        // Remove active class on active tabs and containers

        var ul = $(this).parent().parent();

        ul.children().removeClass("act");

        ul.parent().children().removeClass("act");



        // Add active class to clicked tab and container

        var tabClass = $(this).parent().attr("class");

        ul.siblings("." + tabClass + "-cont").addClass("act");

        $(this).parent().addClass("act");



        return false;

    });



    // Add blur/focus events on text boxes

    inputTxtObj = {


        "ContactEmail": "Your Mailing Address",

        "addr": "Your Mailing Address",

        "city": "City/Town",

        "email": "Your Email Address",

        "firstName": "Your First Name",

        "friendname": "Your Friend's Name",

        "friendemail": "Your Friend's Email Address",

        "lastName": "Your Last Name",

        "mediaattn": "Your Media Attention",

        "name": "Your Name",

        "org": "Your Organization",

        "phone": "Your Phone Number",

        "search": "Search Heritage for",

        "subject": "Expert's name or topic",

        "terms": "Topics, Issues, Keywords, Authors ...",

        "title": "Your Title",

        "zip": "Zip/Postal",

        "staffFilter": "Expert or Staff Member",

        "advSearch": "Enter term(s) you'd like to search Hertage.org for..."

    };



    $("input[name=search-query]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.search); });

    $("input[name=search-query]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.search); });



    $("input[name=name]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.name); });

    $("input[name=name]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.name); });



    $("input[name=first-name]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.firstName); });

    $("input[name=first-name]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.firstName); });



    $("input[name=last-name]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.lastName); });

    $("input[name=last-name]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.lastName); });



    $("input[name=firstname]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.firstName); });

    $("input[name=firstname]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.firstName); });



    $("input[name=lastname]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.lastName); });

    $("input[name=lastname]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.lastName); });



    $("input[name=phone]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.phone); });

    $("input[name=phone]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.phone); });



    $("input[name=email]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.email); });

    $("input[name=email]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.email); });



    $("input[name=friendname]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.friendname); });

    $("input[name=friendname]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.friendname); });



    $("input[name=friendemail]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.friendemail); });

    $("input[name=friendemail]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.friendemail); });



    $("input[name=mediaattn]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.mediaattn); });

    $("input[name=mediaattn]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.mediaattn); });



    $("input[name=subject]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.subject); });

    $("input[name=subject]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.subject); });



    $("input[name=filter-terms]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.terms); });

    $("input[name=filter-terms]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.terms); });



    $("input[name=title]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.title); });

    $("input[name=title]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.title); });



    //$(".title-input").focus( function() {obj = this;inputFocus(obj,inputTxtObj.title);});	

    //$(".title-input").blur( function() {obj = this;inputBlur(obj,inputTxtObj.title);});



    $("input[name=addr]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.addr); });

    $("input[name=addr]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.addr); });

    //me
    $("input[name=ContactEmail]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.ContactEmail); });

    $("input[name=ContactEmail]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.ContactEmail); });
    //end me



    $("input[name=org]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.org); });

    $("input[name=org]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.org); });



    $("input[name=city]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.city); });

    $("input[name=city]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.city); });



    $("input[name=zip]").focus(function() { obj = this; inputFocus(obj, inputTxtObj.zip); });

    $("input[name=zip]").blur(function() { obj = this; inputBlur(obj, inputTxtObj.zip); });



    $("input.focus-staff-filter").focus(function() { obj = this; inputFocus(obj, inputTxtObj.staffFilter); });

    $("input.focus-staff-filter").blur(function() { obj = this; inputBlur(obj, inputTxtObj.staffFilter); });



    $(".search-bar .search-input").focus(function() { obj = this; inputFocus(obj, inputTxtObj.advSearch); });

    $(".search-bar .search-input").blur(function() { obj = this; inputBlur(obj, inputTxtObj.advSearch); });



    /** Handles the drop downs in filters */

    $("label.form_arrow_up").each(function() { hideRefControl(this, "form_arrow_up", "form_arrow_down"); });

    $("label.form_arrow_down").each(function() { showRefControl(this, "form_arrow_up", "form_arrow_down"); });



    $(".heritage-dd ul").addClass("sub-ul");



    //$("label.form_arrow_up").click ( function() { toggleRefControls(this, "up"); });

    //$("label.form_arrow_down").click ( function() { toggleRefControls(this, "down"); });



    $(".search-filter h6").each(function() {

        var uls = $(this).next();

        if (uls.children().length <= 0) {

            uls.html("<li><em>no facets selected</em></li>");

        }

    });



    /** Displays 'Send Report to a Friend' popup **/

    //Click the send report link



    $(".send-to-friend").click(function() {

        centerPopup("#send-to-friend-popup");

        loadPopup("#send-to-friend-popup");



        return false;

    });



    $(".rsvp").click(function() {

        centerPopup("#rsvp-popup");

        loadPopup("#rsvp-popup");



        return false;

    });





    $(".request-interview").click(function() {

        centerPopup("#request-interview-popup");

        loadPopup("#request-interview-popup");

        $("#request-interview-popup").css('z-index', 999);



        return false;

    });





    //Click the X or Cancel button

    $(".closePopup").click(function() {

        disablePopup();

    });



    $(".popup-cancel").click(function() {

        disablePopup();

    });





    // Initialize hover state for Large Icon List

    if ($(".large-icon-list").hasClass("grid") == false) {

        $(".large-icon-list li a").hover(

			function() {

			    $(this).parent().addClass("hov");

			},

			function() {

			    $(this).parent().removeClass("hov");

			}

		);

    }



    // Initialize homepage carousel nav

    // Prepare Tabbed-Containers

    $(".carousel-nav li a").click(function() {

        // Remove active class on active tabs and containers

        $(".carousel-nav li.act").removeClass("act");



        // Add active class to clicked tab and container

        var tabClass = $(this).parent().attr("class");

        $(".carousel .act").removeClass("act")

        $(".carousel #cont-" + tabClass).addClass("act");



        $(this).parent().addClass("act");



        return false;

    });



    $(".spotlight button").click(function() {



        // Get number of tabs and position of active tab

        var num_tabs = $(".carousel-nav li").length;

        var act_pos = $(".carousel-nav li").index($(".carousel-nav li.act"));

        var new_pos;



        // Check if left or right btn 

        // Get new position based on button clicked, position of active tab, and number of tabs 

        if ($(this).hasClass("left-btn")) {

            new_pos = ((act_pos - 1) >= 0) ? act_pos - 1 : num_tabs - 1;

        } else if ($(this).hasClass("right-btn")) {

            new_pos = ((act_pos + 1) < num_tabs) ? act_pos + 1 : 0;

        }



        // Remove active class, put on correct tab

        $(".carousel-nav li.act").removeClass("act");

        $(".carousel-nav ." + new_pos).addClass("act");



        // Remove active class, put active on correct Container		

        $(".carousel div.act").removeClass("act");

        $(".carousel #cont-" + new_pos).addClass("act");



        return false;

    });



    // Add click/unclick state to Site Search dropdown

//    $(".comboboxContainer .comboboxValueContainer").click(function() {

//        if ($(this).hasClass("act")) {

//            $(this).removeClass("act");

//        } else {

//            $(this).addClass("act");

//        }

//    });



    /* Setting up references hide/show toggle */

    $(".references").css("display", "none");

    $(".refs-toggle").click(function() {

        if ($(".references").hasClass("act")) {

            $(".references").slideUp("slow").removeClass("act");

            $(this).html("Show references in this report")

        } else {

            $(".references").slideDown("slow").addClass("act");

            $(this).html("Hide references in this report").addClass("hide");

        }



        return false;

    });



    /* Initialilizing Homesplash Article/Video Spotlight*/

    $(".feature .feature-nav li").click(function() {

        var pos = $(".feature .feature-nav li").index(this) + 1;

        $(".feature .feature-nav li.act").removeClass("act");



        $(".feat-spotlight .act").removeClass("act");

        $(".feat-spotlight .feat-" + pos).addClass("act"); // For homepage



        $(".feature .act").removeClass("act");

        $(".feature .feat-media-" + pos).addClass("act");

        //$(".feat-spotlight .video-"+pos).addClass("act");   // For Multimedia



        $(this).addClass("act");



        return false;

    });



    $(".search-form .search-toggle").click(function() {

        var id = $(this).attr("id");

        if ($(this).hasClass("show")) {

            $(".block-" + id).slideDown("slow");

            $(this).removeClass("show").html("Collapse This");

        } else {

            $(".block-" + id).slideUp("slow");

            $(this).addClass("show").html("Expand This");

        }

    });



    // Reduces top padding when carousel nav item text goes to two lines

    $(".carousel-nav li a").each(function() {

        if ($(this).html().length > 22) {

            $(this).css("padding-top", "5px");

        }

    });

});



// Sets text input val to '' on focus if current value equals val

function inputFocus(obj, val) {

    return ($(obj).val() == val) ? $(obj).val('').addClass("reset-font") : false;

}



// Sets text input val to specficied 'val' on focus

function inputBlur(obj, val) {

    return ($(obj).val() == '') ? $(obj).val(val).removeClass("reset-font") : false;

}



function buildRangeSlider(div_id) {

    var div = $(div_id);

    if (div.length > 0) {

        var min = parseInt($(div_id + " + .range-footer > .min").text());

        var max = parseInt($(div_id + " + .range-footer > .max").text());

        var ends = Math.round((max - min) / 3);



        var re = /(\d{4}) - (\d{4})/;

        var value = $(div_id + " + .range-footer input.date-range-input").val();

        var values = [];



        if (re.test(value)) {

            var match = re.exec(value);

            values.push(parseInt(match[1]));

            values.push(parseInt(match[2]));

        }

        else {

            values.push(min);

            values.push(max);

        }



        $(div_id).slider({

            range: true, min: min, max: max, values: values,

            slide: function(event, ui) {

                $(div_id + " + .range-footer input.date-range-input").val(ui.values[0] + ' - ' + ui.values[1]);

            }

        });

        $(div_id + " + .range-footer > input").val(min + ' - ' + max);

    }

}



/* Displays 'Send Report to a Friend' popup */



//0 - disabled; 1 - enabled;

var popupStatus = 0;



//loading popup with jQuery 

function loadPopup(id) {

    //loads popup only if it is disabled

    if (popupStatus == 0) {

        $("#backgroundPopup").css({

            "opacity": "0.6"

        });

        $("#backgroundPopup").fadeIn("slow");

        $(id).fadeIn("slow");

        popupStatus = 1;

    }

}



//Disabling popup

function disablePopup() {

    //disables popup only if it is enabled

    if (popupStatus == 1) {

        $("#backgroundPopup").fadeOut("slow");

        $(".popupWindow").fadeOut("slow");

        popupStatus = 0;

    }

}



//Centering popup

function centerPopup(id) {

    //request data for centering

    var windowWidth = document.documentElement.clientWidth;

    var windowHeight = document.documentElement.clientHeight;

    var popupHeight = $(id).height();

    var popupWidth = $(id).width();

    var xy = getScrollXY();



    //centering

    $(".popupWindow").css({

        "position": "absolute",

        "top": windowHeight / 2 - popupHeight / 2 + xy[1],

        "left": windowWidth / 2 - popupWidth / 2

    });



    //only need force for IE6

    $(".backgroundPopup").css({

        "height": windowHeight

    });

}



//Determine Scrolling Offset if Any

function getScrollXY() {

    var scrOfX = 0, scrOfY = 0;

    if (typeof (window.pageYOffset) == 'number') {

        //Netscape compliant

        scrOfY = window.pageYOffset;

        scrOfX = window.pageXOffset;

    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {

        //DOM compliant

        scrOfY = document.body.scrollTop;

        scrOfX = document.body.scrollLeft;

    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {

        //IE6 standards compliant mode

        scrOfY = document.documentElement.scrollTop;

        scrOfX = document.documentElement.scrollLeft;

    }

    return [scrOfX, scrOfY];

}








