$(document).ready(function(){
	$('input.prompt').focus(function(){
		if(this.value == this.defaultValue) this.value = '';
	}).blur(function(){
		if(this.value == '') this.value = this.defaultValue;
	});

	$('area.learn-more-toggler').click(function(e){
		$('div.more-callout').fadeIn();
		return false;
	});
	$('div.more-callout a.close').click(function(e){
		$('div.more-callout').fadeOut();
		return false;
	});

	$('#tour-togglers a.how-flicketz-work').click(function(){
		$('#how').fadeOut();
		$('#intro').fadeOut();
		$('#video').show();
		$('#tour-togglers a.membership-benefits').removeClass('active');
		$(this).addClass('active');
		
		var tour = new SWFObject("flash/howflicketzworks.swf", "mymovie", "559", "340", "9", "#ffffff");
		tour.useExpressInstall('flash/expressInstall.swf');
		tour.addParam("wmode", "opaque");
		tour.write("video");
		return false;
	});
	$('#tour-togglers a.membership-benefits').click(function(){
		$('#intro').fadeOut();
		$('#video').fadeOut();
		$('#how').fadeIn();
		$('#tour-togglers a.how-flicketz-work').removeClass('active');
		$(this).addClass('active');
		return false;
	});

	$('#people-saying div.quotes').cycle({'sync': 0, 'slideExpr': 'div.quote', 'timeout': 18000, 'speed': 2800});
	
	$('.reflect').reflect({ height: 0.2 });
});

var theImages = new Array() // do not change this
// To add more image files, continue with the
// pattern below, adding to the array.

theImages[0] = 'media/images/layout/FanScore_Score.png'
theImages[1] = 'media/images/layout/FanScore_Quest.png'
theImages[2] = 'media/images/layout/FanScore_Stoked.png'

// do not edit anything below this line

var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="'+theImages[whichImage]+'" border="0">');
}

var theImages2 = new Array() // do not change this
// To add more image files, continue with the
// pattern below, adding to the array.

theImages2[0] = 'media/images/layout/flicketz_01_3D.jpg'
theImages2[1] = 'media/images/layout/flicketz_01_3wheel.jpg'
theImages2[2] = 'media/images/layout/flicketz_01_nana.jpg'

// do not edit anything below this line

var j2 = 0
var p2 = theImages2.length;
var preBuffer2 = new Array()
for (i2 = 0; i2 < p2; i2++){
   preBuffer2[i2] = new Image()
   preBuffer2[i2].src = theImages2[i]
}
var whichImage2 = Math.round(Math.random()*(p2-1));
function showImage2(){
document.write('<img src="'+theImages2[whichImage2]+'" border="0">');
}

//  End -->

function process_invite_form(form) {
	var subject = form.elements['invite_send_subject'].value;
	var your_email = form.elements['your_email'].value;
	var friend_email = form.elements['friend_email'].value;
	
	var params = '&subject=' + subject + '&your_email=' + your_email + '&friend_email=' + friend_email;
	$("#invite_form_submit").fadeTo("fast", 0.5);
	$("#invite_form_submit").attr("disabled","disabled");
	submit_invite_form_info(params);
}

function submit_invite_form_info(params) {
	var xhr = createAJAXobject();
	var output = 'WHAT';

	if (!xhr) {
		return true;
	}
	
	var url = 'lib/invitefriend_process.php';
	
	xhr.open('POST', url, true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", params.length);
	xhr.setRequestHeader("Connection", "close");

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200 || xhr.status == 304) {
				output = xhr.responseText;
				process_invite_output(output);
			}
		}
	};
	
	xhr.send(params);
	
	return false;
}

function process_invite_output(output) {
	if (output == '') {
		displayFlashMessage('Message Sent');
		$("#friend_email").val(' ');
		$("#invite_error").html('Moviegoing is more fun with friends and family, so why not invite them to join Flicketz?');
	} else {
		output = '<span style="color: red; font-weight: bold">' + output + '</span>';
		$("#invite_error").html(output);
	}
	$("#invite_form_submit").fadeTo("fast", 1);
	$("#invite_form_submit").removeAttr("disabled");
}

function process_login(form, movie) {
	var email = form.elements['email'].value;
	var password = form.elements['password'].value;
	return submit_login_info(email, password, movie);
}

function submit_login_info(email, password, movie) {
	var xhr = createAJAXobject();
	var output = '';

	if (!xhr) {
		return true;
	}

	var url = 'users/login.php';
	var params = 'form_id=login&silent=1&email=' + email + '&password=' + password;

	xhr.open('POST', url, true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", params.length);
	xhr.setRequestHeader("Connection", "close");

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200 || xhr.status == 304) {
				output = xhr.responseText;
				return process_login_output(output, movie);
			}
		}
	};

	xhr.send(params);

	return false;
}

function process_login_output(output, movie) {
	var out_array = output.split('|');
	if (out_array[0] == 'SUCCESS') {
		tb_remove();
		location.href = movie;
	} else {
		$("#error").html(output);
	}
}