
$(document).ready(function() {
	
	if($('#wdgt_articles_rotator_main').length > 0) { //only set the code to start the rotator if it exists
		var wdgt_rotator_timeout = setTimeout(wdgt_rotator_timeoutFunction, 8000);
	}
	
});


//timeout function used for controlling the automated scrolling of the widget rotator
var wdgt_rotator_thumb_clicked = false;
function wdgt_rotator_timeoutFunction() {
	if(!wdgt_rotator_thumb_clicked) {
		wdgt_rotator_showArticle('#wdgt_articles_rotator_main', wdgt_rotator_currentThumb+1);
		//wdgt_rotator_timeout = setTimeout(wdgt_rotator_timeoutFunction, 8000);
	}
}

/*
$(document).ready(function() {
	
	if($('#wdgt_articles_rotator_main').length > 0) { //only set the code to start the rotator if it exists
		var wdgt_rotator_timeout = setTimeout(wdgt_rotator_timeoutFunction, 8000);
	}
	
});


//timeout function used for controlling the automated scrolling of the widget rotator
var wdgt_rotator_thumb_clicked = false;
var wdgt_rotator_lastTime = null;
function wdgt_rotator_timeoutFunction() {
	if(wdgt_rotator_lastTime === null)
	if(!wdgt_rotator_thumb_clicked) {
		wdgt_rotator_showArticle('#wdgt_articles_rotator_main', wdgt_rotator_currentThumb+1);
		wdgt_rotator_timeout = setTimeout(wdgt_rotator_timeoutFunction, 8000);
	}
}
*/


function pullEventsWidget(date,weekday,action) {
	
	if(!$(".dates #tab"+weekday).hasClass("at")) {
		
		$("#eventWidgetContent").html('<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="/img/loading/bar1.gif">');
		
		$(".dayTab").removeClass("at");
		$(".dates #tab"+weekday).addClass("at");
		
		$.ajax({
			url:'/events/getWidgetEvents/date:' + date + '/action:' + action,
			success: function(data) {
				if(data == 'fail') {
					$("#eventWidgetContent").html('<br><br><br>There was an error pulling the events for ' + date);	
				} else {
					response = data.split("|");
					$("#eventWidgetContent").html(response[1]);
				}	
			}
		});
		
	}
}


var wdgt_rotator_currentThumb = 0;
var wdgt_rotator_interval;
function wdgt_rotator_showArticle(e, thumbNum, clk) {
	
	if(clk == true) wdgt_rotator_thumb_clicked = true; //if a thumb was actually clicked on, it stops the auto scroll
	
	if(thumbNum > (wdgt_rotator_numThumbs-1)) thumbNum = 0;
	wdgt_rotator_currentThumb = thumbNum;
	
	var newSrc = '/phpThumb/phpThumb.php?src=../img/uploads/'+wgt_rotator_articles[thumbNum]['photo']+'&w=500&h=232&aoe=1&zc=T&fltr[]=usm&fltr[]=lvl';
	
	$(e + ' h2 a').html(wgt_rotator_articles[thumbNum]['title']);
	$(e + ' h2 a').attr('href', '/articles/view/'+wgt_rotator_articles[thumbNum]['id']); //changes title link
	$(e + ' a.rotator_img_link').attr('href', '/articles/view/'+wgt_rotator_articles[thumbNum]['id']); //changes big image link
	
	$(e + ' .subtitle').html(wgt_rotator_articles[thumbNum]['subtitle'] + '&nbsp;&nbsp;<a href="/articles/view/'+wgt_rotator_articles[thumbNum]['id']+'">read more</a>');
	
	$(e + ' .photo2').attr('src', newSrc); //changes image
	
	$(e + ' .photo2').fadeIn(300, function() {
		$(e + ' .photo1').attr('src', newSrc);
		$(e + ' .photo2').delay(50).fadeOut(300); //don't actually need fade, but hide makes it flash, and can't be delayed
	});

	$(e + ' .thumb').removeClass('at');
	$(e + ' .thumb'+thumbNum).addClass('at');
	var thumbPos = $(e + ' .thumb'+thumbNum).position();
	$(e + ' .arrow').animate({
		top: thumbPos.top + 10
		}, function() {
			wdgt_rotator_timeout = setTimeout(wdgt_rotator_timeoutFunction, 8000);
	});
	$(e + ' .thumbs .border').animate({
		top: thumbPos.top
	});
}

var totalEvents = 12;
var currentEvent = 0;
var rotate = true;

function rotateEvents(){
	if(rotate == true){
		if(currentEvent+1>=totalEvents)
			switchTo = 0;	
		else
			switchTo = currentEvent+1;
			
		setTimeout("switchEvents('"+switchTo+"')",7000); 
	}
}
function switchEvents(to,bypass){
	
	if(rotate == false && !bypass)
		return;
	
	if(bypass == 1)
		rotate = false;

	$('#promoEvent_'+currentEvent).removeClass('active');
	$('#promoEvent_'+to).addClass('active');
	currentEvent = parseInt(to);
		
	$('#title').html(promoEvents[to]['title']);
	$('#title').attr('href',promoEvents[to]['slug']);
	$('#big_img').attr('href',promoEvents[to]['slug']);
	$('#desc').html(promoEvents[to]['description']);
	//$('#largePromoPhoto').fadeOut(10,function(){
		$('#largePromoPhoto').attr('src','');
		$('#largePromoPhoto').attr('src','/phpThumb/phpThumb.php?src=../img/uploads/' + promoEvents[to]['image'] + '&w=300&h=238&aoe=1&zc=T&fltr[]=usm&fltr[]=lvl');
		//$('#largePromoPhoto').fadeIn(10);
	//});
	
	
	if(rotate == true) {
		rotateEvents();
	}
}

firstswitch = true;

function hoverSwitch(to){
	
	if(firstswitch)
		$('.small_promo').first().removeClass('active');
	
	$('#title').html(promoEvents[to]['title']);
	$('#title').attr('href','/events/view/'+promoEvents[to]['slug']);
	$('#desc').html(promoEvents[to]['description']);
	$('#largePromoPhoto').fadeOut(150,function(){
		$('#largePromoPhoto').attr('src','');
		$('#largePromoPhoto').attr('src','/phpThumb/phpThumb.php?src=../img/uploads/' + promoEvents[to]['image'] + '&w=85&h=85&aoe=1&zc=T&fltr[]=usm&fltr[]=lvl');
		$('#largePromoPhoto').fadeIn(150);
	});	
}





//FIND THEATERS WIDGET
var switching = false;
function theaterTab(which) {
	if(which == 'search') {
		if(switching != true) {
			switching = true;
			$(".favoriteTab").removeClass("at");
			$(".searchTab").addClass("at");
			$("#favoriteTheater").fadeOut("fast",function() {
				$("#searchTheater").fadeIn("fast", function() { 
					switching = false;										
				});										  
			});
		}
	} else if(which == 'favorite') {
		if(switching != true) {
			switching = true;
			$(".searchTab").removeClass("at");
			$(".favoriteTab").addClass("at");
			$("#searchTheater").fadeOut("fast",function() {
				$("#favoriteTheater").fadeIn("fast",function() {  
					switching = false;											 
				});										  
			});
		}
	}
}

function searchTheaters() {
	$("#resultsSearch").html('<br><br><br>&nbsp;&nbsp;&nbsp;<img src="/img/loading/bar1.gif">');
	zipcode = $("#ff_zipcode").val();
	$.ajax({
		url:'/theaters/get_widget_theaters/zipcode:' + zipcode,
		success: function(data) {
			
			if(data == 'fail') {
				$("#resultsSearch").html("<br><br><br>Your search didn't return any results.");
			} else {
				$("#resultsSearch").html(data);
			}
		}
	});
	$.ajax({
		url:'/movies/get_favorites',
		success: function(data) {
			data = data.split('|');
			$("#favoriteTheater").html(data[1]);
			if(parseInt(data[0]) > 0) {
				theaterTab('favorite');
			}
		}
	});	
}
