$(function() {
	var $content = $("#content");
	
	var $contentLeft = $content.find('#content-left');
	var $contentRight = $content.find('#content-right');
	
	Cufon.replace('.page-title', {
		fontSize:	'90px'
	});
	
	if ($contentRight.height() > $contentLeft.height()) {
		$contentLeft.css('min-height', $contentRight.height() + 40);
	}
	
	$features = $content.find('#features');
	if ($features.length > 0) {
		$features.cycle({
			speed: 1000,
			timeout: 5000
		});
	}
	
	var $accordian = $content.find('.accordian');
	$accordian.find('.title').click(function() {
		var $this = $(this);
		if ($this.data('open')) {
			$this
				.removeClass('active')
				.next('.info').addClass('hidden');
			$this.data('open', false);
		} else {
			$this
				.addClass('active')
				.next('.info').removeClass('hidden');
			$this.data('open', true);
		}
		return false;
	});
	
	var $likeButton = $content.find('.likeButton');
	if ($likeButton.length > 0) {
		$likeButton.bind('click', function() {
			var params = new Object();
			params.id = $likeButton.attr('rel');
			//params.at = $content.find('#at').val();
			$.ajax({
				type: 'POST',
				dataType: 'json',
				url: 'functions/like.php',
				data: params,
				success: function(data) {
					if (data.code != 0) {
						alert('ERROR ' + data.code + ': ' + data.msg);
					} else {
						alert('Success! ' + data.test + data.result);
					}
				},
				error: function() {
					alert('ERRRORRR!');
				}
			});
		});
	}
	
	if ($content.find("#photo").length > 0) {
		$(window).scrollTop($content.find("#sp").val());
		
		$content.find(".staticScroll").bind('click', function() {
			location.href = $(this).attr('href') + "&sp=" + $(window).scrollTop();
			return false;
		});
		
		$(document).bind('keypress', function(event) {
			if (event.keyCode == 37) {
				location.href = $content.find('#previous').attr('href') + "&sp=" + $(window).scrollTop();
			} else if (event.keyCode == 39) {
				location.href = $content.find('#next').attr('href') + "&sp=" + $(window).scrollTop();
			}
		});
	}	
	
	var $crewForm = $content.find('#crewForm');
	if ($crewForm.length > 0) {
		$crewForm.bind('submit', function() {
			var $button = $crewForm.find('button');
			$button.attr('disabled', true);
			
			// NAME
			if (!$crewForm.find('#name').val().match(/^[a-zA-Z\xE1\xE9\xED\xF3\xFA\xC1\xC9\xCD\xD3\xDA\' \.\-]+$/)) {
				alert("Please fill the 'Name' field correctly.");
				$button.attr('disabled', false);
				return false;
			}
			
			// EMAIL
			if (!$crewForm.find('#email').val().match(/^[a-z0-9&\'\.\-_\+]+@([a-z0-9\-]+\.)+[a-z]{2,4}/i)) {
				alert("Please fill the 'Email Address' field correctly.");
				$button.attr('disabled', false);
				return false;
			}
			
			// PHONE
			if (!$crewForm.find('#phone').val().match(/^[0-9\+\(\)\- ]+$/)) {
				alert("Please fill the 'Phone Number' field correctly.");
				$button.attr('disabled', false);
				return false;
			}
			
			// COURSE
			if (!$crewForm.find('#course').val().match(/^[a-zA-Z\xE1\xE9\xED\xF3\xFA\xC1\xC9\xCD\xD3\xDA\' \.\-]+$/)) {
				alert("Please fill the 'Course' field correctly.");
				$button.attr('disabled', false);
				return false;
			}
		});
	}
	
	var $storyForm = $content.find('#storyForm');
	if ($storyForm.length > 0) {
		$storyForm.bind('submit', function() {
			var $button = $storyForm.find('button');
			$button.attr('disabled', true);
			
			// NAME
			if (!$storyForm.find('#name').val().match(/^[a-zA-Z\xE1\xE9\xED\xF3\xFA\xC1\xC9\xCD\xD3\xDA\' \.\-]+$/)) {
				alert("Please fill the 'Name' field correctly.");
				$button.attr('disabled', false);
				return false;
			}
			
			// STORY
			if ($storyForm.find('#story').val() == '') {
				alert("Please fill the 'Story' field correctly.");
				$button.attr('disabled', false);
				return false;
			}
			if ($storyForm.find('#story').val().match(/[;\*]+/)) {
				alert("Please fill the 'Story' field correctly. The characters ';' and '*' are not allowed.");
				$button.attr('disabled', false);
				return false;
			}
		});
	}
	
	var $ticketsForm = $content.find('#purchaseForm');
	if ($ticketsForm.length > 0) {
		$ticketsForm.bind('submit', function() {
			var $button = $ticketsForm.find('button');
			$button.attr('disabled', true);
			
			// NAME
			if (!$ticketsForm.find('#name').val().match(/^[a-zA-Z\xE1\xE9\xED\xF3\xFA\xC1\xC9\xCD\xD3\xDA\' \-]+$/)) {
				alert("Please fill the 'Your Name' field correctly.");
				$button.attr('disabled', false);
				return false;
			}
			
			// CARDHOLDER
			if (!$ticketsForm.find('#cardholder').val().match(/^[a-zA-Z\xE1\xE9\xED\xF3\xFA\xC1\xC9\xCD\xD3\xDA\' \-]+$/)) {
				alert("Please fill the 'Cardholder's Name' field correctly.");
				$button.attr('disabled', false);
				return false;
			}
			
			// ADDRESS
			if (!$ticketsForm.find('#address').val().match(/^[0-9a-zA-Z\xE1\xE9\xED\xF3\xFA\xC1\xC9\xCD\xD3\xDA\',.\n\r \-]+$/)) {
				alert("Please fill the 'Address' field correctly.");
				$button.attr('disabled', false);
				return false;
			}
			
			// POSTCODE
			if (!$ticketsForm.find('#postcode').val().match(/^[0-9a-zA-Z ]*$/)) {
				alert("Please fill the 'Postcode' field correctly.");
				$button.attr('disabled', false);
				return false;
			}
			
			// PHONE
			if (!$ticketsForm.find('#phone').val().match(/^[0-9\+\(\)\- ]+$/)) {
				alert("Please fill the 'Phone Number' field correctly.");
				$button.attr('disabled', false);
				return false;
			}
			
			// EMAIL
			var $email = $ticketsForm.find('#email');
			if (!$email.val().match(/^[a-z0-9&\'\.\-_\+]+@([a-z0-9\-]+\.)+[a-z]{2,4}/i)) {
				alert("Please fill the 'Email Address' field correctly.");
				$button.attr('disabled', false);
				return false;
			}
			
			// SET CASH AMOUNT
			var tickets = $ticketsForm.find('#tickets').val();
			$ticketsForm.find('#amount').val(+$ticketsForm.find('#price').val() * tickets);
			
			var err = false;
			$.ajax({
				async: false,
				dataType: 'json',
				type: 'POST',
				url: 'functions/tickets-reserve.php',
				data: {
					eid: $ticketsForm.find('#eid').val(),
					email: $email.val(),
					tickets: tickets
				},
				success: function(result) {
					if (result.code != 0) {
						alert('Error #0' + result.code + ': ' + result.msg);
						err = true;
					}
				}
			});
			if (err) {
				$button.attr('disabled', false);
				return false;
			}
		});
	}
});
