$("document").ready(function(){
	
	//so that target=_blank validates
	$('A[rel="_blank"]').each(function(){
		$(this).attr('target', '_blank');
	});
	
	$("#s").val("Search...");
	$("#s").click(function(){
		if ($("#s").val() == "Search...") {
			$("#s").val("");
		}
	}).focus(function(){$(this).click();});
	
	if($(".item").length > 0) {
		$(".item .info, .item .excerpt").click(function(){
			var loc = ($(this).parent().parent().children(".content").children("h3").children("a").attr("href"));
			if(loc) {
				window.location = loc;
			}
		});
		$(".item .imgbox").click(function(){
			var loc = ($(this).parent().children(".content").children("h3").children("a").attr("href"));
			if(loc) {
				window.location = loc;
			}
		});
	}
	if($(".home").length > 0) {
		$(".film .imgbox, .film p, .news-event .imgbox, .news-event p").click(function(){
			var loc = ($(this).parent().children("h4").children("a").attr("href"));
			if(loc) {
				window.location = loc;
			}
		});
	}
	
	/**
	 * Sponsor scroller in footer
	**/
	
	var tot_sponsors = $("div#sponsors div.sponsor img").length;
	//keep track of how many sponsors we've dropped of the left
	var sponsor_loc = 0;
	
	if(tot_sponsors > 5){
		//set up the css for the slider to work
		$('div#sponsor_right').addClass('active');
		$('div#sponsors div#sponsor_inner').css('width', function(){
			return tot_sponsors * 175;
		});
		
		//animate the controls
		$('div#sponsor_right.active').live('click',function(){
			if (tot_sponsors - sponsor_loc > 5){
			//we need to have at least 5 sponsors listed right of the start
				$('div#sponsors div#sponsor_inner').animate({
					left: '-=175'
				}, 200);	
				
				//now we know that there is something to the left, so activate button
				$('div#sponsor_left').addClass('active');
				
				//increment location counter
				sponsor_loc = sponsor_loc+1;
				
				//if we're at the end, deactivate right button
				if(tot_sponsors - sponsor_loc <= 5){
					$('div#sponsor_right').removeClass('active');					
				}
			}
		});
		
		$('div#sponsor_left.active').live('click',function(){
			if (sponsor_loc > 0){
			//do we have any on the left to see?
				$('div#sponsors div#sponsor_inner').animate({
					left: '+=175'
				}, 200);	
				
				//now we know that there is something to the right, so activate button
				$('div#sponsor_right').addClass('active');
				
				//increment location counter
				sponsor_loc = sponsor_loc-1;

				$('#tracker div').removeClass('on').addClass('off');
				$('#track'+curr_slide).removeClass('off').addClass('on');
				
				//if we're at the beginning, deactivate left button
				if(sponsor_loc == 0){
					$('div#sponsor_left').removeClass('active');					
				}
			}
		});
	}
	
	/**
	 * Slides scroller on front page
	**/
	
	var tot_slides = $("div.slide").length;
	//which slide are we on?
	var curr_slide = 1;
	
	if(tot_slides > 1){
		//set up the css for the slider to work
		$('div#slide_right').addClass('active');
		$('div#slides div#slides_inner').css('width', function(){
			return tot_slides * 912;
		});
		
		//set up the tracker
		for (i=1;i<=tot_slides;i=i+1){
			$('<div class="off" id="track'+i+'"></div>').appendTo('div#tracker');			
		}
		$('#track1').removeClass('off').addClass('on');
		
		//animate the controls
		$('div#slide_right.active').live('click',function(){
			right_click();
		});
		
		function right_click(){
			if (tot_slides - curr_slide > 0){
				$('#tracker div').removeClass('on').addClass('off');
			//we need to have another slide to scroll
				$('div#slides div#slides_inner').animate({
					marginLeft: '-=912'
				}, 200);	
				
				//now we know that there is something to the left, so activate button
				$('div#slide_left').addClass('active');
				
				//increment location counter
				curr_slide = curr_slide+1;
				
				//change tracker
				$('#track'+curr_slide).removeClass('off').addClass('on');
				
				//if we're at the end, deactivate right button
				if(tot_slides <= curr_slide){
					$('div#slide_right').removeClass('active');					
				}
			}
		};
		$('div#slide_left.active').live('click',function(){
			if (curr_slide > 1){
			//do we have any on the left to see?
				$('div#slides div#slides_inner').animate({
					marginLeft: '+=912'
				}, 200);	
				
				//now we know that there is something to the right, so activate button
				$('div#slide_right').addClass('active');
								
				//increment location counter
				curr_slide = curr_slide-1;
				
				//change tracker
				$('#tracker div').removeClass('on').addClass('off');
				$('#track'+curr_slide).removeClass('off').addClass('on');
				
				//if we're at the beginning, deactivate left button
				if(curr_slide < 2){
					$('div#slide_left').removeClass('active');					
				}
			}
		});
		
		$('div#tracker div').click(function(){
			//get the requested slide #
			id = parseInt($(this).attr('id').substring(5));
			curr_slide = id;
			
			$('#tracker div').removeClass('on').addClass('off');
			$('#track'+curr_slide).removeClass('off').addClass('on');
			
			//go to the requested slide
			newpos = -912*(curr_slide-1);
			$('div#slides div#slides_inner').animate({
				marginLeft: newpos
			}, 200);	
			
			//should left/right arrows still work?
			if (curr_slide > 1)
				$('div#slide_left').addClass('active');
			if (curr_slide < tot_slides)
				$('div#slide_right').addClass('active');	
			if (curr_slide <= 1)
				$('div#slide_left').removeClass('active');
			if (curr_slide >= tot_slides)
				$('div#slide_right').removeClass('active');
			
		});
		
		$("*").click(function(){
			clearInterval(autoslide);
		});		
		
		$("iframe").hover(function(){
			clearInterval(autoslide);
		});
		
		//scroll through automatically!
		autoslide = setInterval(function() {
			if(tot_slides - curr_slide > 0){
				right_click();
			}else{
				//go back the beginning
				$('div#slide_right').addClass('active');
				$('div#slide_left').removeClass('active');	
				curr_slide = 1;
				
				//reset tracker
				$('#tracker div').addClass('off').removeClass('on');
				$('#track1').addClass('on').removeClass('off');
				$('div#slides div#slides_inner').animate({
					marginLeft: '0'
				}, 200);	
			}
		}, 5000);
	}

	$(".film-left a").click(function(){
		$("#trailer").dialog({
			modal: true,
			width:'auto',
			autoOpen: true,
			resizable: false,
			title: $("h2").text()+' Film Trailer',
			open: function(){
				$(".ui-dialog-content").css("background-color", "#000");
			}
		});
		return false;
	});
	if($("#thumbnails").length > 0) {
		//create the gallery
		var gallery = $("<div><center><img id='viewing' /><br /><table id='thumbs'></table></center></div>");
		$("#thumbnails").after(gallery);
		gallery.hide();
		$("#thumbnails a").click(function(){
			$(".ui-dialog-content").css("background-color", "#000");
			var showing = $(this).attr("href");
			gallery.dialog({
				modal: true,
				width:700,
				autoOpen: true,
				resizable: false,
				position: "top",
				title: $("h2").text()+' Image Gallery',
				open: function(){
					$("#viewing").attr("src", showing);
					$("#thumbs").html($("#thumbnails").html());
					//which one are we on?
					$("#thumbs td a.current").removeClass("current");
					$("#thumbs td a").each(function(){
						if($(this).attr("href") == $("#viewing").attr("src")) {
							$(this).addClass("current");
						}
					});
					$("#thumbs tr td a").click(function(){
						$("#thumbs td a.current").removeClass("current");
						$(this).addClass("current");
						$("#viewing").attr("src", $(this).attr("href"));
						return false;
					});
					$(".ui-dialog-content").css("background-color", "#000");
				}
			});
			return false;
		});
	}
});
