﻿



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");
        }
    });
});

$(function () {
    $(".subscriptions input").keypress(function (e) {
        if (e.keyCode == 13) {
            e.preventDefault();

            $(".subscriptions 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();
    });

    $('#cyoCurrentSeatingChart').click(function(event) {
        openWin2(this.href, "350", "390");
        event.preventDefault();
    });
});






$(document).ready(function(){


$('.txtCommentsLimit').keyup(function(){
    if ($(this).val().length >250){
    alert("Please limit your comments to 250 characters.");
        $(this).val($(this).val().substring(0, 250));
    }
}).parent().parent().parent().parent().mousemove(function(){
	var t = $('.txtCommentsLimit');
    if (t.val().length >250){
     alert("Please limit your comments to 250 characters.");
        t.val(t.val().substring(0, 250));
    }
});






// 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;

    var cyoLinks = $(".cyoViewProgramDetailPop"),
    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");
    });

    $.each(cyoLinks, 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


});

/*** Content Switcher ***/

var contentSwitcher = function(target, options){
	var base = this;

	base.options = {
		currentIndex: 0,
		maxItems: 5,
		tabDrawer: true,
		autoCycle: true,
		cycleSpeed: 6000,
		duration: 600,
		elWrapper: $('div.wrapper'),
		elDrawer:'div.drawer'
	};

	if(typeof options == 'object'){
		$.extend(base.options, options);
	}

	base.elTab = target;
	base.animating = false;
	base.cycling = false;

	base.init();
};

contentSwitcher.prototype = {
	init:function(){
		var base = this;

		base.elCurrentTab = $(base.elTab[base.options.currentIndex]);

		if(base.options.tabDrawer){
			base.openDrawer($(base.elTab[base.options.currentIndex]));
		}

		base.elTab.hide();
		base.elCurrentTab.show().addClass('on');
		base.buildNav();

		if(base.options.autoCycle){
			base.cycle = setTimeout(function(){
				base.autoCycle();
			}, base.options.cycleSpeed);
		}
	},
	buildNav:function(){
		var base = this, i, ii,
			elNavLink,
			itemCount;

		base.elNavWrapper = $('<ul class="nav-tabs" />')

		if(base.elTab.length > base.options.maxItems){
			itemCount = base.options.maxItems;
		}else{
			itemCount = base.elTab.length;
		}

		if(itemCount > 1){
			for(i = 0, ii = itemCount; i < ii; i++){
				elNavLink = $('<li><a href="#">Tab ' + i + '</a></li>');

				if(i === base.options.currentIndex){
					elNavLink.addClass('on');
				}

				elNavLink.data('link', $(base.elTab[i]).attr('id'));
				elNavLink.addClass('featuredContent' + i);
				base.elNavWrapper.append(elNavLink);
			}

			base.options.elWrapper.append(base.elNavWrapper);

			base.elNavWrapper.children('li').click(function(e){
				e.preventDefault();

				base.switchTab($(this));

				clearTimeout(base.cycle);
				clearTimeout(base.progressInterval);
			});
		}
	},
	switchTab:function(el){
		var base = this,
			elTargetTab;

		if(el){
			elTargetTab = $('#' + el.data('link'));
		}else{
			if(base.elCurrentTab.next('li').length && base.elCurrentTab.next('li').index() + 1 <= base.options.maxItems){
				elTargetTab = base.elCurrentTab.next('li');
			}else{
				elTargetTab = $(base.elTab[0]);
			}
		}

		if(elTargetTab.attr('id') !== base.elCurrentTab.attr('id') && !base.animating){
			base.animating = true;

			base.elNavWrapper.children('li').removeClass('on');

			$($('ul.nav-tabs li')[elTargetTab.prevAll().length]).addClass('on');

			base.elCurrentTab.removeClass('on').fadeOut(base.options.duration)

			if(base.options.tabDrawer){
				base.closeDrawer(base.elCurrentTab);
			}

			elTargetTab.addClass('on').fadeIn(base.options.duration, function(){
				if(base.options.tabDrawer){
					base.openDrawer(elTargetTab);
				}
			});

			base.elCurrentTab = elTargetTab;
		}
	},
	openDrawer:function(el){
		var base = this,
			elDrawer = el.children(base.options.elDrawer);

		$(base.options.elDrawer).css({
			'opacity' : '0'
		});

		$(elDrawer).animate({
			'opacity' : '1'
		}, base.options.duration, function(){
			base.animating = false;
		});
	},
	closeDrawer:function(el){
		var base = this,
			elDrawer = el.children(base.options.elDrawer);

		$(elDrawer).animate({
			'opacity' : '0'
		}, base.options.duration, function(){
			base.animating = false;
		});
	},
	autoCycle:function(){
		var base = this,
			i = base.elCurrentTab.index() + 1;

		(function cycleNode(){
			base.switchTab();
			base.progressInterval = setTimeout(cycleNode, base.options.cycleSpeed);
		})();
	}
};
