﻿function centerPage() {
    try {
        var left = (($(window).width() - 960) / 2);
        $('div#background').css('margin-left', (left - 320) + 'px');
        if (left < 0) left = 0;
        $('div#center').css('left', left + 'px');
    }
    catch(ex) {}
}

function setBackground() {
    centerPage();
    
    try {
        var left = (($(window).width() - 960) / 2);
        var pos = $('div#main').position();
        $('div#topnavPopup').css('left', left + 'px');
        $('div#topnavPopup').css('top', (pos.top - 50) + 'px');
    }
    catch(ex) {}
}

function setMargins(left, right, offset) {
    if (!offset) offset = 0;
    try {
        var height_l = $(left).height() + offset;
        var height_r = $(right).height() + offset;
       
        if (height_r < height_l) $(right).height(height_l);
        if (height_l < height_r) $(left).height(height_r);
       
    }
    catch(ex) {
       
    }
}

function setNavigation() {
    var url = location.pathname.substring(_vpath.length).toLowerCase();
        
    var sections = {'season':'1', 'subscriptions':'1', 'support':'2', 'music':'3', 'community':'4', 'projects':'5', 'about':'6'};
    var section = '';
    var match = url.match(/^\/([^\/]+)\//);
    if (match) section = match[1];
        
    $('a.menuFirst').bind('mouseover', function(){var img=this.getElementsByTagName('img')[0]; if (img.src.indexOf('menu_on') == -1) img.src=img.src.replace('menu_','menu_on_');});
    $('a.menu').bind('mouseover', function(){var img=this.getElementsByTagName('img')[0]; if (img.src.indexOf('menu_on') == -1) img.src=img.src.replace('menu_','menu_on_');});
    $('a.menuFirst').bind('mouseout', function(){var img=this.getElementsByTagName('img')[0]; if (img.id != 'imgMenu_' + sections[section]) img.src=img.src.replace('menu_on_','menu_');});
    $('a.menu').bind('mouseout', function(){var img=this.getElementsByTagName('img')[0]; if (img.id != 'imgMenu_' + sections[section]) img.src=img.src.replace('menu_on_','menu_');});

    // highlight current section menu
    if (section) $('#imgMenu_' + sections[section]).attr('src', _vpath + '/App_Themes/SFSMain/i/menu_on_0' +  + sections[section] + '.gif');
}

$(window).bind('resize', setBackground);
$(document).ready(setBackground);
$(document).ready(function(){setMargins('div#mainLeft','div#mainRight');});
$(document).ready(setNavigation);
setTimeout(function(){setMargins('div#mainLeft','div#mainRight');}, 5000);

function search(txtSearch) {
   location.href = _vpath + '/Search.aspx?q=' + document.getElementById(txtSearch).value;
}

function playVideo(file, width, height) {
    if (!width) width = 248;
    if (!height) height = 96;
    
    var win = window.open(_vpath + '/media/player.aspx?mediapath=&amp;mediaid=' + escape(file) + '&width=' + width + '&height=' + height, 'video', 'width=' + width + ',height=' + (height + 64));
    if (win.focus) win.focus();
}

function deframe(loc) {
    if (self.parent.frames.length > 0) self.parent.location=loc;
}


// For promo boxes to trigger the enter button instead of searching.
$(function() {
    $(".infosummary input[type='text']").keyup(function(e) {
        if (e.keyCode == 13) {
            $(".infosummary input[type='submit']").trigger("click");
        }
    });
});



//Opening popup boxes in new windows taken from SFS existing code
function openWin2(filename,height,width) {
 win2 = window.open(filename, "Window2",
 "width=" + width + ",height=" + height + ",scrollbars=yes");
}

$(document).ready(function(){
    
    $('#what_patron_id"').unbind().click(function(){
        openWin2('patronid.htm','290','270' );
    });
    
    $('#why_duplicates').unbind().click(function(){
        openWin2('whyPreventDuplicates.htm','200','270' );
    });
    
    $("a[id*='uxPackageVenue']").click(function(event){
        openWin2(this.href,"350","390");
        event.preventDefault();
    });
});






$(document).ready(function(){

// start of image switcher
    
    $('a.view_hide_alternates').each(function(){
        $(this).click(function(){
            var src = $(this).find('img').attr("src");
            if (src == "../../images/btn_show_hide.png"){
                $(this).find('img').attr("src","../../images/btn_up.png");
            }
            else{
                $(this).find('img').attr("src","../../images/btn_show_hide.png"); 
            }
        });
        //$(this).find('img').attr("src","");
    });
    
// end of image switcher


    
//start of content tab switcher
    var wrapper = $(".select-package-wrapper");
    wrapper.each(function(index,valueOfElement){
 
        var $nav_links = $(valueOfElement).find("#staticTabNav ul li a"),
        $panels = $(valueOfElement).find("#staticTabPanels div.panel"),
        defaultPanel = 0;
        //initialize the switcher
        hideShow($nav_links,$panels,defaultPanel);

        $.each($nav_links, function(indexInArray, valueOfElement){
            $(this).click(function(event){
                hideShow($nav_links,$panels,indexInArray);
                event.preventDefault();
            });
        });
    });
   
    
    
    function hideShow($nav_links,$panels,panelIndex){
       hidePanels($nav_links,$panels);
       if($("#staticTabPanels div.panel").length){
       
       showPanel($nav_links,$panels,panelIndex); 
       }
       setMargins('div#mainLeft','div#mainRight');  
      
    }
    
    function hidePanels($nav_links,$panels){
        $.each($nav_links,function(){
            $(this).removeAttr("class");
        }); 
         
        $.each($panels,function(){
            $(this).css({"display":"none"});
        });
    }
    
    function showPanel($nav_links,$panels,panelIndex){
        $nav_links.eq(panelIndex).attr("class","active");
        $panels.eq(panelIndex).css({"display":"block"});
        clearAndSetCorrectHeight(200);
         
         //hide all popups when user requests a new panel
         if (isPopupShow == true){
                        hidePopup();
                    } 
    }
       
    function clearAndSetCorrectHeight(offset){
        var contentLeft =  $("#contentLeft"),
        contentRight =  $("#contentRight"),
        mainLeft = $("#mainLeft"),
        mainRight = $("#mainRight"),
        _offset = offset;
        
        //get rid of heights set previously  
        contentLeft.removeAttr("style");
        contentRight.removeAttr("style");
        mainLeft.removeAttr("style");
        mainRight.removeAttr("style");
        
        try {
            var height_l = contentLeft.height();
            var height_r = contentRight.height();
           
            if (height_r < height_l) contentRight.height(height_l + _offset);
            if (height_l < height_r) contentLeft.height(height_r + _offset); 
            }
        catch(ex)
           {
            //do nothing
            }  
    }  
    
//end of content tab switcher
    
    
//start of ajax popup call
  
    var links = $("#staticTabPanels div.contentpanes div.pane a"),
    isPopupShow = false,
    currentAjaxRequest;
    
    //append popupcontainer to end of body tag and apply styles
    $("body").append("<div id='popup_container'></div>")
    var popup_container = $("#popup_container");
    popup_container.css({"visibility":"hidden"});
    
    function positionPopup(event_information){
        var popup_height = popup_container.height();
        
        //get mouse position
        popup_container.css({
            "top":event_information.pageY - popup_height/2,
            "left":event_information.pageX + 7
        });  
        
        $("#popup_container div.arrow_container").css({'top': popup_height/2+ 0 + "px"});
        
    }
    
    function cancelAjaxRequest(){
         try{
            currentAjaxRequest.abort();
            $("#popup_container").html("");
        }
        catch(err){
            //do nothing. 
        }
    }
    
    function hidePopup(){
        isPopupShow = false;
        $("#popup_container").html("");
        popup_container.css({"visibility":"hidden"});
       
    }
    
     function showPopup(href,event){
            //for each new popup request, if there was a previous ajax request, cancel it and clear the popup contents
            cancelAjaxRequest();

            //get data from server and populate popup
            currentAjaxRequest =$.get(href,function(data){
            //clear previous content 
            popup_container.html("");
            popup_container.append("<div class='top' /><div class='body'><div class='body_inner' /></div> <div class='bottom' /><div class='arrow_container'><div class='arrow' /></div><div class='close'>Close X</div>");
            popup_container.find('div.body_inner').html(data);
            
            positionPopup(event);
            
            //set visiblity to show once everything is done
            popup_container.css({"visibility":"visible"});
            isPopupShow = true;
            
             //add event handlers for the popup
                $("#popup_container div.close").unbind().bind("click",function(event){
                   
                    if (isPopupShow == true){
                        hidePopup();
                    } 
                    event.preventDefault();              
                });  
            
        });  
       
    }
    
    //add eventhandler to mouseover of links
    $.each(links,function(indexInArray, valueOfElement){
        $(this).bind("click",function(event){
                var href = $(this).attr("href");  
                showPopup(href,event);
                event.preventDefault();   
        }).css("text-decoration","none");
    });
   
   
   
    
//end of ajax popup call


});