// centered pop up window
function centeredPopUpWindow(mypage, myname, w, h, scroll){
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

// get position of child node
function getPosition(main,spec) {
	var items = main.getElementsByTagName(spec.tagName);
	var found = 0;
	for (p = 0; p < items.length; p++) {
		if (items[p] == spec) {
			found = 1;
			break;
		}
	}
	if (found) {
		return p;
	}
	else {
		return -1;
	}
}

// set date elements
var yy = new Date().getYear();
var year = (yy < 1000) ? yy + 1900 : yy;
var dd = new Date().getDate();
var mm = new Array("january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december");
var monthNumber = new Date().getMonth();
var monthLabel = mm[monthNumber];

// check dimensions of browser window
var windowWidth = $(window).width();
var windowHeight = $(window).height();
var halfWindowWidth = windowWidth / 2;
var halfWindowHeight = windowHeight / 2;

// get class name of element
$.fn.getClassNames = function(){
	if (name = this.attr("className")) {
		return name.split(" ");
	}
	else {
		return [];
	}
};
// get name of element
$.fn.getElementNames = function(){
	if (elementName = this.attr("name")) {
		return elementName.split(" ");
	}
	else {
		return [];
	}
};

$(document).ready(function(){
	
	// links with rel="external" will open in a new window
	$("a[rel=external]").click(function(){
		 window.open(this.href);
		 return false;
	});
	
	// navigation rollovers
	$("ul#primaryNav li").hover(
		function(){
			$(this).css({cursor: "pointer", backgroundPosition: "0px -25px"})
			.find("a").css({color: "#104545", backgroundPosition: "100% -25px"})
		},
		function(){
			$(this).css({cursor: "default", backgroundPosition: "0px 0px"})
			.find("a").css({color: "#fff", backgroundPosition: "100% 0px"})
		}
	);
	// selected navigation
	$("ul#primaryNav li.active").hover(
		function(){
			$(this).css({cursor: "pointer", backgroundPosition: "0px -25px"})
			.find("a").css({color: "#104545", backgroundPosition: "100% -25px"})
		},
		function(){
			$(this).css({cursor: "default", backgroundPosition: "0px -25px"})
			.find("a").css({color: "#104545", backgroundPosition: "100% -25px"})
		}
	);
	
	
	// home page
		// add top margin to the second ticket stub in the now playing focus area
		$("div#nowPlaying div.ticketStub:eq(1)").css({marginTop: "15px"});
		
		// add top margin to the second ticket stub in the coming soon focus area
		$("div#comingSoon div.ticketStub:eq(1)").css({marginTop: "15px"});
		
		
		// shut off dots for last news item
		$("#homeContent div#columnTwo div#theatreNews ul li:last-child").css({background: "none", paddingBottom: "0"});
		$("#homeContent div#columnTwo div#getInvolved ul li:last-child").css({background: "none", paddingBottom: "0"});
		
		// var homeContentHeight = $("div#columnOne").height();
		// $("div#columnTwo").css({height: homeContentHeight});
		
	
	// calendar
		$("span.currentMonth").text(monthLabel);
		$("span.currentYear").text(year); // shared with footer date display
		
		$("div#monthYear").text(monthLabel + " " + year);
		$("a.selectMonth").text(monthLabel + " " + year);
		
		// show current month's calendar
		$("table#" + monthLabel + year).show();
		
		// highlight current day
		$("table#" + monthLabel + year + " tr td.day" + dd).css({background: "url(../i/calendar-border-current.gif)"});
		$("table#" + monthLabel + year + " tr td.day" + dd + " span.numbers").css({color: "#fff", fontWeight: "bold"});
		
		// expand the width of last column
		$("table#eventsCalendar tr td:nth-child(7)").css({width: "102px", width: "96px"});
		
		
		$("a.selectMonth").click(function(){
			
			if ( $(this).hasClass("ddVisible") ) {
				$(this).removeClass("ddVisible");
				$("ul#monthList").slideUp(500);
				return false;
			} else {
				$(this).addClass("ddVisible");
				$("ul#monthList").slideDown(500);
				return false;
			}
		});
		
		$("ul#monthList li a").click(function(){
			
			// get class name from link to display corresponding calendar
			var selectedMonth = $(this).getClassNames();
			
			$("ul#monthList li a").removeClass("highlightedMonth");
			$(this).addClass("highlightedMonth");
			$("table.eventsCalendar").hide();
			$("table#" + selectedMonth).show();
			$("ul#monthList").slideUp(500);
			$("a.selectMonth").removeClass("ddVisible");
			
			var newMonthYear = $("table#" + selectedMonth).attr("title");
			$("div#monthYear").text(newMonthYear);
			$("a.selectMonth").text(newMonthYear);
			
			return false;
		});
		
		// highlight current month in select dropdown
		$("ul#monthList li a." + monthLabel + year).addClass("highlightedMonth");
		
		
		// home page
		$("div.ticketStub img").click(function(){
			
			// get the class name from link to display corresponding event content
			selectedEvent = $(this).getElementNames();
			escapeToClose();
			showEventPopUp();				
			return false;
			
		});
		$("div.ticketStub a").click(function(){
			
			// get the class name from link to display corresponding event content
			selectedEvent = $(this).getClassNames();
			escapeToClose();
			
			if ( $(this).hasClass("" + selectedEvent + "") ) {
				showEventPopUp();				
			} else {
				// do nothing if the link doesn't have a class defined
				return false;
			}
			
			return false;
			
		});
		// calendar
		$("table.eventsCalendar tr td a").click(function(){
			
			// get the class name from link to display corresponding event content
			selectedEvent = $(this).getClassNames();
			escapeToClose();
			
			if ( $(this).hasClass("" + selectedEvent + "") ) {
				showEventPopUp();					
			} else {
				// do nothing if the link doesn't have a class defined
				return false;
			}
			
			return false;
			
		});
		
		$("a.closeEvent").click(function(){
			$(this).parent().fadeOut(500);
			return false;
		});
		
				
		
	
	// footer
	$("a#facebook").hover(
		function(){ $(this).animate({ opacity: .5 }, { queue:false, duration:250 }); },
		function(){ $(this).animate({ opacity: 1 }, { queue:false, duration:250 }); }
	);
	
}); // $(document).ready(function()

function showEventPopUp(){

	$("div.popUpContent").hide();
		
	// if the viewport is too small for the popup content
	// reset positioning to the top/left of the interface
	// and focus the viewport up to the top
	if ( windowWidth < 660 || windowHeight < 480  ){
		$("div.popUpContent").css({
			position: "absolute",
			top: "0px",
			left: "0px",
			marginTop: "0px",
			marginLeft: "0px"
		});
		$("div#" + selectedEvent).fadeIn(500);
		$("div.eventContent").removeClass("addVerticalScroll");
		eventContentHeight = $("div#" + selectedEvent + " div.eventContent").height();
		checkEventContentHeight();
							
	} else {
	
		$("div#" + selectedEvent).fadeIn(500);
		$("div.eventContent").removeClass("addVerticalScroll");
		eventContentHeight = $("div#" + selectedEvent + " div.eventContent").height();
		checkEventContentHeight();
		return false;
		
	}
	
	return false;

}

function escapeToClose(){
	// hit esc to close $("div.popUpContent")
	$(document).keyup(function(event){
		if (event.keyCode == 27) {
			$("div.popUpContent").fadeOut(500);
		}
	});
}

function checkEventContentHeight(){

	if ( eventContentHeight < 350 ) {
		$("div.eventContent").removeClass("addVerticalScroll");
	}
	if ( eventContentHeight > 350 ) {
		$("div.eventContent").addClass("addVerticalScroll");
	}

}