$(document).ready(function() {
	$('#cartHidden').val('');
	if($(".cart-row > .cart-row").size()<2){
		$('#cartSubmit').hide();
	}
	
	
	
	$('#cartSubmit').removeAttr('disabled');
	$('.do-remove-ticket').css('visibility','hidden');
	$('.poster-description').css('display','none');
	$('.menu').after(function(){
		var rollover = '<img src="'+$(this).attr('src').substring(0,$(this).attr('src').length-4)+'-rollover.png" alt="'+$(this).attr('alt')+'" title="'+$(this).attr('title')+'" class="menu-overlay" />';
		return rollover;
	});
	//$('.menu').next('.menu-overlay').css('top','-'+$(this).css('height'));
	
	$(".menu").hover(function(){
		$(this).attr('src', $(this).attr('src').substring(0,$(this).attr('src').length-4)+"-rollover.png" );
		/*//if(!$(this).next('.menu-overlay').is(':animated')){
			$(this).next('.menu-overlay').fadeIn(1000);
		}
		else{
			$(this).next('.menu-overlay').stop().fadeTo(1000, 1);
		}*/
	},
	function(){
		$(this).attr('src', $(this).attr('src').substring(0,$(this).attr('src').length-13)+".png" );
	/*	if($(this).next('.menu-overlay').is(':animated')){
			$(this).next('.menu-overlay').stop().fadeTo(1000, 1);
		}
		else{
			$(this).next('.menu-overlay').fadeOut(1000);
			
		}	*/
	});
	

	$('.menu').hover(function(){
	
		switch($(this).attr('alt')) {
			case 'About':
				$('#header-center').css('background-position', '520px 0px');
			break;
			
			case 'What\'s On':
				$('#header-center').css('background-position', '0px 460px');
			break;
			
			case 'Box Office':
				$('#header-center').css('background-position', '520px 230px');
			break;
			
			case 'Blog':
				$('#header-center').css('background-position', '520px 460px');
			break;
			
			case 'Find Us':
				$('#header-center').css('background-position', '0px 0px');
			break;
			
			case 'Links':
				$('#header-center').css('background-position', '0px 230px');
			break;
		}
	});
	
	
	
	$('.submenu div').hover(function() {
		var backgroundPos = $(this).css('background-position');
		if(backgroundPos.substr(-5)=='-40px'){
			var backgroundPosX = backgroundPos.substring(0,backgroundPos.length-5);
			$(this).css('background-position',backgroundPosX+' 0px');
		}
	},
	  function(){
		var backgroundPos = $(this).css('background-position');
		if(backgroundPos.substr(-3)=='0px'){
			var backgroundPosX = backgroundPos.substring(0,backgroundPos.length-3);
			$(this).css('background-position',backgroundPosX+' -40px');
		}
	});
	
	
	$(".icon-navigation").mouseover(function(){
		$(this).attr('src', $(this).attr('src').substring(0,$(this).attr('src').length-4)+"-rollover.png" );
	});
	$(".icon-navigation").mouseout(function(){
		$(this).attr('src', $(this).attr('src').substring(0,$(this).attr('src').length-13)+".png" );
	});
	$('a[href^=#]').click(function() {
		var elementClicked = $(this).attr("href");
		var destination = $(elementClicked).offset().top;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-20}, 2000,'easeInQuad' );
			return false;
	});
	$('.poster-image').after('<img src="./_img/image-overlay.png" alt="Click here for more information" class="overlay" />');		
	$('.poster-image').hover(function () {
		$(this).next('.overlay').css("display","inline");
		$(this).next('.overlay').css("top","-379px");
		$(this).next('.overlay').css("z-index","2");						
		$(this).css("z-index","1");
	  },
	function () {	
		$(this).next('.overlay').css("display","none");
		$(this).next('.overlay').css("top","0px");
	});
	$('.poster-date').hover(function () {
		$(this).parent().children('.poster').children('.poster-link').children('.overlay').css("display","inline");
		$(this).parent().children('.poster').children('.poster-link').children('.overlay').css("top","-379px");
		$(this).parent().children('.poster').children('.poster-link').children('.overlay').css("z-index","2");						
		$(this).parent().children('.poster').children('.poster-link').children('.poster-image').css("z-index","1");
	  },
	function () {	
		$(this).parent().children('.poster').children('.poster-link').children('.overlay').css("display","none");
		$(this).parent().children('.poster').children('.poster-link').children('.overlay').css("top","0px");
	}); 
	$('.poster-title').hover(function () {
		$(this).parents('.poster-container').children('div:first').children('.poster').children('.poster-link').children('.overlay').css("display","inline");
		$(this).parents('.poster-container').children('div:first').children('.poster').children('.poster-link').children('.overlay').css("top","-379px");
		$(this).parents('.poster-container').children('div:first').children('.poster').children('.poster-link').children('.overlay').css("z-index","2");						
		$(this).parents('.poster-container').children('div:first').children('.poster').children('.poster-link').children('.poster-image').css("z-index","1");
	  },
	function () {	
		$(this).parents('.poster-container').children('div:first').children('.poster').children('.poster-link').children('.overlay').css("display","none");
		$(this).parents('.poster-container').children('div:first').children('.poster').children('.poster-link').children('.overlay').css("top","0px");
	}); 
	$(".overlay").hover(
		function() {
			$(this).css("display","inline");
			$(this).css("top","-379px");
		},
		function() {
			$(this).css("display","none");
		}
	);
	$('.poster-description').each(function() {
		var descContents = $(this).html();
		var great = new RegExp('>','g');
		var less = new RegExp('<','g');
		descContents = descContents.replace(great, "&gt;");
		descContents = descContents.replace(less, "&lt;");
		
		$(this).html(descContents);
	});
	$(".poster-link").each(function(){
		var description = $(this).parents('.poster-container').children('div:last').children('.poster-description').text();
		var content = $(this).attr('href');
		var fboptions = {
			'href'		:	content,
			'transitionIn'	:	'fade',
			'transitionOut'	:	'fade',
			'speedIn'		:	200, 
			'speedOut'		:	200,
			'title'	: 	description,
			'titlePosition'  :  'inside'
		}; 
		$(this).parent().parent().children('div:first').fancybox(fboptions);
		$(this).parents('.poster-container').children('div:last').fancybox(fboptions);
		$(this).fancybox(fboptions);
	});
	$('.cart-row:odd').css('color','black');
	$('.do-buy-ticket').click(function() {
		var eventID = $(this).attr('rel');
		var eventTitle = $(this).parents('.ticket-row').children('.ticket-title').text();
		var ticketQuantity = 1;
		var ticketPrice = $(this).parents('.ticket-row').children('.ticket-price').text().substr(1);
		addToCart(eventID,eventTitle,ticketQuantity,ticketPrice);		
		return false;
	});
	$('.do-remove-ticket').click(function() {
		var eventID = $(this).attr('rel');
		var ticketPrice = $(this).parents('.ticket-row').children('.ticket-price').text().substr(1);
		removeFromCart(eventID, ticketPrice);
		return false;
	});
	$('#the-reply').dialog({
		autoOpen: false,
		modal: true
	});
	$('#cartForm').ajaxForm({
		success: function(responseText){
			$('#the-reply').html(responseText);
			$('#the-reply > noscript').remove();
			$('#reviewForm').submit(function(){
				var nameValue = $('input[name=aname]').fieldValue(); 
				var emailValue = $('input[name=email]').fieldValue(); 
				if (!nameValue[0] || !emailValue[0]) { 
					$('#payment').prepend('<div class="error">Please enter your name and email address.</div>');
					$('input[type=text]').css('background-color','#CD0C25');
					return false; 
				} 
			});
			$('.close-dialog').click(function(){
				$('#the-reply').dialog("close");
				return false;
			});
			$('#the-reply').dialog({
				autoOpen: true,
				modal: true,
				width : 800,
				height: 700,
			});
			$(".ui-dialog-titlebar").hide()       
			//return false;
		}
	});
	
	
});