//this will be the javscript file for creationtheatre.co.uk

$.fn.clickableBlocks = function ()
{
	return this.each (function () {
		var $this = $(this);
		var $anchor = $this.find("a");
		if ($anchor.length)
		{
			$this.css("cursor", "pointer").click (function ()
			{
				location.href = $anchor[0].href;
			});
		}
	});
}

$(document).ready(function() {
	$(".tacbox .background").css({opacity: 0.79});
	
	$(".tacbox").clickableBlocks();
	$(".book-online").clickableBlocks();
	$(".cast").clickableBlocks();

	$('#ticker-area').vTicker({
		speed: 600,
		pause: 6000,
		showItems: 1,
		animation: 'fade',
		mousePause: false,
		height: 0,
		direction: 'up'
	});

});


$(function () {
	
	/******** MAIN NAV DROPDOWN SLIDE **********/
	
	$("ul#main-nav li").children("ul").css({opacity: 0});
	
	//need a way to remove the active state of an li and replace it on the correct one!?? CM maybe?!
	
	$("ul#main-nav li").hover( 
		function() {
			$(this).children("ul").stop().animate({opacity:1},500);			
			$(this).children("a").addClass("crap-hack");
		},
		function() {
			$(this).children("ul").stop().animate({opacity:0},500);
			$(this).children("a").removeClass("crap-hack");
		});
	
	
	
	/************* HOME PAGE IMAGE FLIP ***********/
	
	
	$('.fade-wrapper').innerfade({
			speed: 800,
			timeout: 4000,
			type: 'sequence',
			containerheight: '408px'
	});

	$('.nav-wrapper').innerfade({
			speed: 800,
			timeout: 4000,
			type: 'sequence'
	});	
	
	
	/************* HOME PAGE TIMELINE **************/
if (!(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6)) { 
	$("#home-timeline-container").removeClass("noJS");
	
	if($("#home-timeline a").length > 5)
	{
		$("a#scrollLeft").removeClass("noJS");
		
		var curMargin = (($("#home-timeline a").length - 7) * 115 + 43);
		curMargin = curMargin * -1;
		var startMargin = curMargin;
		$("#home-timeline").css({marginLeft: curMargin});
		
		var scrShift = 115 * 8 - 87;
		
	}
	else
	{
		$("#home-timeline").css({marginLeft: 0});
	}
	
	$("#home-timeline").jqDock({
				align: 'middle',
				coefficient: 1,
				distance: 200,
				duration: 600,
				labels: 'bc',
				size: 155
			});
			
	$("a#scrollLeft").click( function() {
		
		if(curMargin < 0)
		{
			curMargin = curMargin + scrShift;
			$("#home-timeline").stop().animate({marginLeft:curMargin},400);
			$("a#scrollRight").removeClass("noJS");
			
			if(curMargin > 0)
			{	$(this).addClass("noJS");}
		}
	});		

	$("a#scrollRight").click( function() {
		
		if(curMargin > startMargin)
		{
			curMargin = curMargin - scrShift;
			$("#home-timeline").stop().animate({marginLeft:curMargin},400);
		}
		
		$("a#scrollLeft").removeClass("noJS");
		
		if(curMargin == startMargin)
			$(this).addClass("noJS");
		
	});			
	
	$(document).keydown(function(e) {
  	switch(e.keyCode)
  	{
  		case 39:
  			if(curMargin > startMargin)
				{
					curMargin = curMargin - scrShift;
					$("#home-timeline").stop().animate({marginLeft:curMargin},400);
				
				}
				$("a#scrollLeft").removeClass("noJS");
		
				if(curMargin == startMargin)
					$("a#scrollRight").addClass("noJS");
				
				
  			break;
  		case 37:
  			if(curMargin < 0)
				{
					curMargin = curMargin + scrShift;
					$("#home-timeline").stop().animate({marginLeft:curMargin},400);
				}	
				$("a#scrollRight").removeClass("noJS");
				
				if(curMargin > 0)
					$("a#scrollLeft").addClass("noJS");
				
  			break;
  	}	
  });
	
} //end MSIE IF
	
	/****** TACTICAL BOX HOVERS ********/
	
	$(".tacbox").hover(
		function () {
			
			//AO: need some way of changin this per section???
			//$(this).find(".overlay h2").css({'color' : 'rgb(228,43,125)'});
			
			$(this).find(".overlay h2").addClass("active");
						
			$(this).children(".background, .overlay").stop().animate({top:65},400);
			
		},
		function () {
			$(this).children(".background, .overlay").stop().animate({top:121},400);
			
			//$(this).find(".overlay h2").css({'color' : 'rgb(51,51,51)'});
			$(this).find(".overlay h2").removeClass("active");
		}
	);
	
	
	
	/***** SHOWS SUB NAV ACCORDIAN ******/
	
	
	var showOneHeight = $("ul#shows-show-one").height();
	var showTwoHeight = $("ul#shows-show-two").height();
	var navHeight;
	
	(showOneHeight < showTwoHeight) ? navHeight=showTwoHeight : navHeight=showOneHeight;
	
	//alert("Show two = " + showTwoHeight + "; Show one = " + showOneHeight + "; Nav Height = " + navHeight);
	
	
	if (!(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6)) 
	{ 
		var closeHeight = 0;
	}
	else
	{
		var closeHeight = 20;
	}
	
	if($("ul#shows-show-two li").length==1)
		$("li#second-show-subnav").addClass("empty");
	else
	{
		if($("body").attr("id") == "show-one")
		{
			$("ul#shows-show-two.tertiary-sub-nav").height(closeHeight).children().hide();
		}
		else if($("body").attr("id") == "show-two")
		{
			$("ul#shows-show-one.tertiary-sub-nav").height(closeHeight).children().hide();
		}
	}
	
	if (!(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6)) { 
	
	$("h2 a#show-one-title").click( function() {
			
		if($("ul#shows-show-two li").length!=0)
		{
			$("ul#shows-show-one").stop().animate({height:navHeight},400);		
			$("ul#shows-show-one li").stop().animate({opacity:1},400);
		
			$("ul#shows-show-one li").css({opacity:0}).show();
		
			$("ul#shows-show-two").stop().animate({height:0},400);		
			$("ul#shows-show-two li").stop().animate({opacity:0},400, function() { $(this).hide(); });
		
			return false;
		}
		
	});
	
	$("h2 a#show-two-title").click( function() {
		
		$("ul#shows-show-one").stop().animate({height:0},400);		
		$("ul#shows-show-one li").stop().animate({opacity:0},400, function() { $(this).hide(); });
				
		$("ul#shows-show-two li").css({opacity:0}).show();
		
		$("ul#shows-show-two").stop().animate({height:navHeight},400);		
		$("ul#shows-show-two li").stop().animate({opacity:1},400);
		
		return false;
	});
	
	}
	
	/** DRAMA CLUB FAQ ANSWER SLIDER */
	//old containers!
	//#container.drama-club-faq #main-content, #container.join-us #main-content, #container.holiday-workshops #main-content, #container.drama-club #main-content
	
	var $container = $("#main-content #content-slider, #main-content .content-slider");
	var $h3s = $("h3", $container);
	$(".answer", $container).slideUp(1).width(470).hide(); // CM: setting width here to stop the dreaded slidedown jump :)
	$h3s.css("cursor", "pointer").append(' <span>(click to expand)</span>');
	$h3s.click (function () {
		$(".answer").slideUp();
		$h3s.removeClass("active");
		if ($(this).next().is(":hidden"))
		{
			$(this).addClass("active");
			$(this).next().stop().slideDown(1000);
		}
		else
		{
			$(this).removeClass("active");
			$(this).next().stop().slideUp(1000);
		}
	});
	
	
	/********** PAST SHOWS ACCORDION NAV **************/
	
	var years = {"2010":0,"2009":1,"2008":2,"2007":3,"2006":4,"2005":5,"2004":6,"2003":7,"2002":8,"2001":9,"2000":10,"1999":11,"1998":12,"1997":13,"1996":14};
	
	//AO: use to find current past show thingy and use Clinton's scrollTo code example!
	var currentYear = location.href.split("-");
	var Y = currentYear[currentYear.length - 1];
		
	$("div#accordion").accordion({ active: years[Y],  autoHeight: false });
	
	

	/********** PAST SHOWS TIMELINE NAV ***************/
	
	$("ul#timeline li a").hover(function() {
		$(this).children("span.BG").stop().animate({opacity: 0.59},600);
	}, function () {
		$(this).children("span.BG").stop().animate({opacity: 1},600);
	});
	
if (!(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6)) { 
	
	var w = 115;
	var total = $("ul#timeline li").length;
	var listWidth = total * w + 1;
	
	$("ul#timeline").removeClass("noJS");
	
	//alert("image width => " + w + " * total images => " + total + "equals = " + listWidth);
	
	//set the width of the list and hide it's overflow!
	$("ul#timeline").css({width:listWidth});
	$("#timeline-nav").css({"overflow":"hidden"});
	
	var sMargin = (w*(total - 7) + 1) * -1;
	var moveMargin = sMargin;
	var m = w*6; //690
		
	
	$("ul#timeline").css({marginLeft:sMargin});
	$("a#mvRight").css({opacity:0,"cursor":"default"}).show();
	
	$("a#mvRight").click( function () {
		if(moveMargin > 0)
		{					
			$("a#mvLeft").stop().animate({opacity:1},400).css({"cursor":"pointer"});
		}
		
		if(moveMargin > sMargin)
		{				
			moveMargin = moveMargin - m;
			
			$("ul#timeline").stop().animate({marginLeft:moveMargin},400);
		}
		
		if(moveMargin == sMargin)
		{
			$(this).stop().animate({opacity:0},400).css({"cursor":"default"});
		}
		
		return false;
		
	});
	
	$("a#mvLeft").click( function () {
		if(moveMargin==sMargin)
		{
			$("a#mvRight").stop().animate({opacity:1},400).css({"cursor":"pointer"});
		}
		
		if(moveMargin < 0)
		{					
			moveMargin = moveMargin + m;
			
			$("ul#timeline").stop().animate({marginLeft:moveMargin},400);
		}
		
		if(moveMargin >= 0)
		{
			$(this).stop().animate({opacity:0},400).css({"cursor":"default"});
		}
		
		return false;
		
	});
	
	$(document).keydown(function(e) {
  	switch(e.keyCode)
  	{
  		case 39:
  			if(moveMargin > 0)
				{					
					$("a#mvLeft").stop().animate({opacity:1},400).css({"cursor":"pointer"});
				}
		
				if(moveMargin > sMargin)
				{				
					moveMargin = moveMargin - m;
			
					$("ul#timeline").stop().animate({marginLeft:moveMargin},400);
				}
		
				if(moveMargin == sMargin)
				{
					$("a#mvRight").stop().animate({opacity:0},400).css({"cursor":"default"});
				}
  			
				break;
  		case 37:
  			if(moveMargin==sMargin)
				{
					$("a#mvRight").stop().animate({opacity:1},400).css({"cursor":"pointer"});
				}
		
				if(moveMargin < 0)
				{					
					moveMargin = moveMargin + m;
					
					$("ul#timeline").stop().animate({marginLeft:moveMargin},400);
				}
		
				if(moveMargin >= 0)
				{
					$("a#mvLeft").stop().animate({opacity:0},400).css({"cursor":"default"});
				}
  			
  			break;
  	}	
  });
	
		
}
else
{
	$(".arrow-wrapper").hide();
}		 //END IF MSIE TEST
	
});
