//global vars
var currentSlide = 0;
var rotatorTimer;

$(document).ready(function () {
    // open and close FAQ questions
    $('#faqToggle').find('dd').hide().end().find('dt').click(function () {
        $(this).next().slideToggle();
    });
    if ($("div#ctl00_ContentPlaceHolderMain_XmlEventScreening_PublicScreening+ul").length > 0) {
        prepareScreenings();
    }
	if(location.pathname.match(/\/filmcircuit/)) {
		$("div#footerFixed").hide();
	} 
	if($("div.rotator > div.slide").length > 1) {
		setupRotator();
	}
});

/*Function to replace screening times if more than 4 in a day. 
  Loops through number of screenings, adding them to as many
  new rows (uls) as required (dmallory)
*/

function prepareScreenings() {
    var s = "";
    var maxTimesPerRow = 4;
    var screenings = $("div#ctl00_ContentPlaceHolderMain_XmlEventScreening_PublicScreening+ul>li>ul.eventScreening");
    screenings.each(function () {
        var screening = $(this);
		var times = screening.children("li.screeningTime");
		var timeCount = times.length;
		if(timeCount > 4) { //only need to do something if it's more than 4 times in a given day
			var rowCount = Math.ceil(timeCount / 4);
			var lastRowTimeCount = timeCount % 4;
			var finalHtmlToAppend = "<li><ul class=\"eventScreening\"><li class=\"screeningDate\">&nbsp;&nbsp;</li><li class=\"end\">&nbsp;</li>";
			var htmlToAppend = "";
			for(var row = 1 ; row < rowCount ; row++) {
				var colCount = 4;
				if(row == rowCount - 1) { //the last row
					colCount = lastRowTimeCount;
				}
				for(var col = 0 ; col < colCount ; col++) {
					var index = col + (row * 4);
					var childToAppend = times.eq(index).html();
					htmlToAppend += "<li class=\"screeningTime\" style=\" margin-left: 3px\">" + childToAppend + "</li>";
					var separator = times.eq(index).next(".screeningDivider");
					if(separator.html() != null) {
						times.eq(index).next(".screeningDivider").remove();	
					}
					separator = times.eq(index).prev(".screeningDivider");
					if(separator.html() != null) {
						times.eq(index).prev(".screeningDivider").remove();	
					}
					htmlToAppend += "<li class=\"screeningDivider\" style=\" margin-left: 3px\">|</li>";
					times.eq(index).remove();
				}
			}
			if(htmlToAppend != "") {
				finalHtmlToAppend += htmlToAppend;
				finalHtmlToAppend += "</ul></li>";
				$(this).parent().after(finalHtmlToAppend);
			}
		}
    });
}

function setupRotator() {
	var rotator = $("div.rotator");
	var infos = $("div.rotator div.slide > div.info");
	//infos.css("opacity", "0.5");
	//infos.live("mouseleave", onRotatorInfoMouseover);
	//infos.live("mouseout", onRotatorInfoMouseout);
	var slideCount = rotator.children("div.slide").length;
	currentSlide = slideCount - 1;
	if(slideCount < 1) return;
	var nav = '<div class="nav"><ol>';
	for(var i = 0 ; i < slideCount ; i++) {
		nav += '<li id="slide-nav-' + i + '"';
		if(i == currentSlide) {
			nav += ' class="current"';
		}
		nav += '>';
		nav += '<a href="#rotator-slide-' + slideCount + '"></a>';
		nav += '</li>';
	}	
	nav += '</ol></div>';
	rotator.append(nav);
	$('li[id^="slide-nav-"]').css('cursor', 'pointer').live("click", onRotatorNavClick);
	//hide (but only with opacity!) all slides except current
	rotator.children("div.slide").css("opacity", "0");
	rotator.children("div.slide").last().css("opacity", "1");
	resetZIndices();
	rotatorTimer = setInterval(onSlideRefresh, 8000);
}
function resetZIndices() {
	$("div.rotator > div.slide").css("z-index", "-1");
	$($("div.rotator > div.slide").get(currentSlide)).css("z-index", "1");
	$("div.rotator > div.nav").css("z-index", "2");
}
function onSlideRefresh() {
	var rotator = $("div.rotator");
	var slideCount = rotator.children("div.slide").length;
	if(currentSlide < slideCount -1) {
		newSlideNum = currentSlide + 1;
	} else {
		newSlideNum = 0;
	}
	switchRotatorSlide(newSlideNum);
}
function onRotatorInfoMouseover() {
	//$(this).animate({opacity: 1}, 1000);
}
function onRotatorInfoMouseout() {
	//$(this).animate({opacity: 0.5}, 1000);
}
function switchRotatorSlide(newSlideNum) {
	var rotator = $("div.rotator");
	var lis = $("div.rotator li");
	lis.removeClass("current");
	newLi = $("div.rotator li#slide-nav-" + newSlideNum);
	newLi.addClass("current");
	oldSlide = rotator.children("div.slide").get(currentSlide);
	newSlide = rotator.children("div.slide").get(newSlideNum);
	if(currentSlide != newSlideNum) {
		$(oldSlide).animate({opacity: 0}, 1000);
		$(newSlide).animate({opacity: 1}, 1000);
	}
	currentSlide = newSlideNum;
	resetZIndices();
}
function onRotatorNavClick() {
	var newSlideNum = $(this).attr("id").split("-")[2];
	switchRotatorSlide(newSlideNum);
	//should reset the timer to 8 seconds again when you click
	clearInterval(rotatorTimer);
	rotatorTimer = setInterval(onSlideRefresh, 8000);
}


