// JScript source code

var displayNewTandCs = false;

$(document).ready(function() {

    if ($(".defaultOpen").length > 0) {
        buttonOpenPanel_OnClick();
    } else {
        $('.openedContainer').hide();
    }
    $('.buttonOpenPanel').click(buttonOpenPanel_OnClick);

    $('#buttonClosePanel').click(function(e) {
        e.stopPropagation();
        hideLoginPanel();
        $('html').unbind('click');
    });

    $('.openedContainer').bind('click', function(e) { e.stopPropagation(); })

    AnimatePageSections();
    AnimateResourceSections(true);
    HomePanelRotate();

    // check whether videoPlayer should be opened on pageload
    checkVidRequest();

    if (displayNewTandCs) {
        newtandcs();
    }

    $('.keywordsearch input').keyup(function(e) {
        if (e.keyCode == 13) {
            eval($('.directorySearch .Gobutton')[0].href);
            return false;
        }
    });

    SetupFormLabels();

});

/**
 * Copys label text into the form element that are for it
 * the label has the class hiddenLabel
**/
function SetupFormLabels() {

    $(".hiddenLabel").each(function() {
    
        var input = $("#" + $(this).attr("for"));
        var label = $(this);
        
        if( input.val() == "" )
            input.val($(this).html());

        input.focus(function() {
            if (input.val() == label.html())
                $(this).val("");
            else
                $(this).select();
        });

        input.blur(function() {
            if (input.val() == "")
                $(this).val("Search...");
        });

    });
    
}

function hideLoginPanel () {
	$('.openedContainer').hide();
}

function repositionOpenedLoginContainer() {
//    var $vis = $('#openedContainer:visible');
//    if ( $vis.length > 0 ) {
//        $vis.css("left", ($(".pageContent").width() - $("#container").width()) / 2 + 664 );
//    }
}

function buttonOpenPanel_OnClick(e) {
    if (e) {
		e.stopPropagation();
	}

    $(document).scrollTop(0);
	$('.openedContainer').show().find("input:first").focus();
	repositionOpenedLoginContainer();
	$('html').bind('click',hideLoginPanel);
	
	return false;
}

function AnimatePageSections()
{
    $(".openPanel").not(".default").hide();
    $(".sectionHead").click(ChangeSectionState);
}


function ChangeSectionState()
{
    if($(this).parent("h3").next(".openPanel").not(":visible").size() > 0)
    {
        $(this).parent("h3").next(".openPanel").slideDown("fast");
        $(this).addClass("open");
    }
    else
    {
        $(this).parent("h3").next(".openPanel").slideUp("fast");
        $(this).removeClass("open");
        
    }
    
    return false;
}

function AnimateResourceSections(hide)
{
    if(hide)
        $(".resourcePanel").not(".default").hide();
    $(".resourceHead").click(ChangeResourceState);
}

function ChangeResourceState()
{
    if($(this).parent("h4").next(".resourcePanel").not(":visible").size() > 0)
    {
        $(this).parent("h4").next(".resourcePanel").slideDown("fast");
        $(this).addClass("open");    
    }
    else
    {
        $(this).parent("h4").next(".resourcePanel").slideUp("fast");
        $(this).removeClass("open");
    }
}

function HomePanelRotate()
{
    $('.slides').cycle({
		fx: 'fade',
		timeout: 6000
	});
}

function newtandcs(){
	var pageDimensions = getPageDimensions();
	
    createOverlay( {
        pageDimensions: pageDimensions,
        id: 'newtandcsoverlay',
        onclick: function() { $("#newtandcs").remove(); }
    } );


    $(document.body).append("<div id='newtandcs'><iframe src='/one-Ambassadorsv2/pages/NewTandCs.aspx'></iframe></div>");
    $("#newtandcs").css({
        width :     "800px",
        height :    "400px",
        position :  "absolute",
        top :       ((pageDimensions.height/2)+pageDimensions.scrollY)-250 + "px",
        left :      (pageDimensions.width/2)-400 + "px",
        zIndex :    "501"
    });
}

function closeTandCPopup() {
    $("#newtandcsoverlay, #newtandcs").remove();
}
