/* Author: David Krause

*/
function addBackground(bp) 
{
	
	if(bp) 
	{
		$("#popupContact").css({
				"background-image": "url(img/" + bp +")",
				"background-position": "0 75px",
				"background-repeat": "no-repeat"
		});
		
	}
	else
	{
		$("#popupContact").css({
				"background-image": ""
		});
	}
}

$(function (){
	
jQuery.fn.exists = function(){return jQuery(this).length>0;} //tests for existence of object

if ($("table.mclaren tr").exists()) //tests to see if a pic_slider is on page
{
	$("table.mclaren tr:nth-child(odd)").css({	background: "#DDD" });
}
	
$("#logo").click( function () {
	window.location.href = "index.php";
});

if ($("#ticket_container").length > 0) {
	
		var ticket_slider_width = ($('.show_preview').length) * 140;
		$('#ticket_container').css({ width: ticket_slider_width + "px"});
		$("#arrow_left").css("visibility","hidden");
	
}


$("#arrow_right").click( function() {
	
			$('#ticket_container').animate({left: '-=140px'}, function () {
				
				var position = $('#ticket_container').position();
				var position_left = Math.abs(position.left);
				if ( position_left >= ticket_slider_width-140 )
					$("#arrow_right").css("visibility","hidden");
				if ( position_left >= 0 )
					$("#arrow_left").css("visibility","visible");
				
			});
			
	});
	
$("#arrow_left").click( function() {
		$('#ticket_container').animate({left: '+=140px'}, function () {
			
			var position = $('#ticket_container').position();
				var position_left = Math.abs(position.left);
				if ( position_left <= 0 )
					$("#arrow_left").css("visibility","hidden");
				if ( position_left <= ticket_slider_width-140 )
					$("#arrow_right").css("visibility","visible");
		});
		
});
	
if ($("#shows_slider").length > 0) {
	
		$('#shows_slider div.show_wrapper:last').insertBefore($('#shows_slider div.show_wrapper:first'));
		var shows_slider_width = ($('.show_wrapper').length) * 808;
		$('#shows_slider').css({left: '-808px', width: shows_slider_width + "px"});
	
}

$("#move_shows_left").hover(function() {
	$(this).css("background","url(img/left_pointer_middle.png) 0 -60px no-repeat")
	}, function() {
		$(this).css("background","")
});

$("#move_shows_right").hover(function() {
	$(this).css("background","url(img/right_pointer_middle.png) 0 -60px no-repeat")
	}, function() {
		$(this).css("background","")
});

$("#move_shows_left").click(function() {
	$("#shows_slider").animate({left: '+=808px'}, function (){
		$("#shows_slider").css({left: '-808px'}); // Offsets div -808px
		$('#shows_slider div.show_wrapper:last').insertBefore($('#shows_slider div.show_wrapper:first'));
	});
});

$("#move_shows_right").click(function() {
	$("#shows_slider").animate({left: '-=808px'}, function() {
		$("#shows_slider").css({left: '-808px'}); // Offsets div -808px
		$('#shows_slider div.show_wrapper:first').insertAfter($('#shows_slider div.show_wrapper:last')); // Moves the first pic after last
	});
});


//Purchase tickets
$(".tickets").click(function() {
	$(".ticket_form").click( function(event) {
		event.preventDefault();
	});
	addBackground();
	ticket_link = $(this).parent().attr("action");
	if ( ticket_link != '')
	{
		window.open(ticket_link, 'order_window', 'width=611,height=510,status=yes,left=' + ((window.screen.width - 611)/2) + ',top='+((window.screen.height - 510)/2.5)).focus();
		
		//alert(ticket_link);
	}
	else
	{
		alert ("Tickets are not available yet.");
	}
});

//Members Reservations
$(".members").click(function() {
	$(".members_reservation_form").click( function(event) {
		event.preventDefault();
	});
	addBackground();
	show_string = $(this).parent().attr("action");
	show_index = show_string.indexOf('=')
	show_name = show_string.substring(show_index+1);
	$.get("members.php",  { show:show_name }, function(data) {
		$("#contactArea").html(data);
		$("#popupContact header").html("<h1>Member Reservations</h1>");
		$("#request_tickets").click( function () {
			$("#member_reservations_popup_form").submit( function(event) {
				event.preventDefault(event);
			});
			verified = jcap();
			if (verified)
			{
				$.post("member_request.php", $("#member_reservations_popup_form").serialize(), function () {
					alert("Your request has been submitted.");
					disablePopup();
				});
			}
			else
			{
				alert("ERROR: Enter the code as it is shown.");
			}
			
		});
		centerPopup();
		//load popup
		loadPopup();
	});
});


//array of theatres
var theatres = Array('davis_theatre','mabee_theatre','yucca_theatre');

//Select Seats
$(".seating").click(function() {
	$(".seating_button_form").click( function(event) {
		event.preventDefault();
	});
	theatre_pic_string = $(this).parent().attr("action");
	theatre_pic = theatre_pic_string.substring(theatre_pic_string.length - 1);
	addBackground();
	$("#popupContact header").html("<h1>Seating</h1>");
	$("#contactArea").html("<a href=\"img/" + theatres[theatre_pic] + "_large.jpg\" target=\"blank\"><img src=\"img/" + theatres[theatre_pic] + ".jpg\" border=\"0\"</a>");
	centerPopup();
	//load popup
	loadPopup();
});

//View cast
$(".cast").click(function() {
	$(".cast_button_form").click( function(event) {
		event.preventDefault();
	});
	addBackground();
	show_string = $(this).parent().attr("action");
	show_index = show_string.indexOf('=')
	show_name = show_string.substring(show_index+1);
	if ( show_name != 'nocast')
	{
		$.get("cast.php", { show:show_name }, function(data) {
			$("#contactArea").html(data);
			$("#popupContact header").html("<h1>Cast</h1>");
			centerPopup();
			//load popup
			loadPopup();
		});
	}
	else
	{
		alert ("Cast has not been selected yet.");
	}
});

//View auditions
$(".auditions").click(function() {
	$(".auditions_button_form").click( function(event) {
			event.preventDefault();
	});
	addBackground();
	audition_string = $(this).parent().attr("action");
	audition_index = audition_string.indexOf('=')
	audition_id = audition_string.substring(audition_index+1);
	if (audition_id != 'noauditions')
	{		
		$.get("auditions.php", { audition:audition_id }, function(data) {
			$("#contactArea").html(data);
			$("#popupContact header").html("<h1>Auditions</h1>");
			centerPopup();
			//load popup
			loadPopup();
		});
		
	}
	else
	{
		alert("Auditions are not open for this show");
	}
	
});


/* formerly Contact Code 
$(".contact_link").click( function () {
	contact_string = $(this).data('name');
	$.get("contact_form.php",  { email_name:contact_string }, function(data) {
		$("#contactArea").html(data);
		$("#popupContact header").html("<h1>Contact</h1>");
		$("#send_message").click( function () {
			$("#contact_form").submit( function(event) {
				event.preventDefault(event);
			});
			verified = jcap();
			if (verified)
			{
				$.post("contact_script.php", $("#contact_form").serialize(), function () {
					alert("Your email has been sent.");
					disablePopup();
				});
			}
			else
			{
				alert("ERROR: Enter the code as it is shown.");
			}
		});
		centerPopup();
		//load popup
		loadPopup();
	});
});
*/

//Purchase membership
$(".become_a_member").click(function() {
	$(".membership_form").click( function(event) {
		event.preventDefault();
	});
	ticket_link = $(this).parent().attr("action");
	if ( ticket_link != '')
	{
		window.open(ticket_link, 'order_window', 'width=611,height=510,status=yes,left=' + ((window.screen.width - 611)/2) + ',top='+((window.screen.height - 510)/2.5)).focus();
		
		//alert(ticket_link);
	}
	else
	{
		alert ("Tickets are not available yet.");
	}
});


//CLOSING POPUP
//Click the x event!
$("#popupContactClose").click(function(){
	disablePopup();
});
//Click out event!
$("#backgroundPopup").click(function(){
	disablePopup();
});
//Press Escape event!
$(document).keypress(function(e){
	if(e.keyCode==27 && popupStatus==1){
		disablePopup();
	}
});


});
























