var currentState = '';
var player = null;

function videoListener(obj) //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
{
	currentState = obj.newstate;
}

function playerReady(obj)
{
	player = document.getElementById(obj['id']);
	player.addModelListener("STATE", "videoListener");
}

$(document).ready(function() {
	
	//creating the video									
	var s1 = new SWFObject('video/player.swf','videocontext','240','360','9','#000');
	s1.addParam('allowscriptaccess','always');
	s1.addParam('wmode','transparent');
	s1.addParam('autostart','false');
	s1.addParam('controlbar','none');
	if (lang == 'it' || lang == 'es')
		lang = 'en';
	s1.addParam('flashvars','file=MusicLoop-' + lang + '.m4v&autostart=false&controlbar=none');
	s1.write('video');
	
	var movielink = '.tomovie';
	
	if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i))
		movielink = '.dummy';
	
	$('#mentions').animate({'opacity': 0}, 0);

	$('#mentions').twitterMentions(['MusicLoopApp'],{ 
		callback : function() { $('#mentions').animate({'opacity': 1}, 300); },
		maximum : 3,
		ulClass : 'mentions',
		odd : false
	});
	
	$('#presenter').jCarouselLite({
		circular: false,
		visible: 1,
		scrolling: 1,
		speed: 500,
		start: 0,
		btnGo: ["#logo", movielink, ".toscreenshot1", /* ".toscreenshot2", */ ".toscreenshot3", ".toscreenshot4"],
		beforeStart: function(a) {
			if (currentState == 'PLAYING')
			{
				try
				{
					player.sendEvent('PLAY');
				}
				catch (err) {}
			}
		},
		afterEnd: function(a) {
			if (a.attr('id') == 'video')
			{
				try
				{
					player.sendEvent('PLAY');
				}
				catch (err) {}
			}
		}
	});
	
	$('.contactlink').click(function() {
		$('#contactmessage').val($('#contactmessage').attr('defaultValue'));
		$('#contactmessage').removeClass('selectedinput');
		showContactForm();
		return false;
	});
	
	$('.newsletterlink').click(function() {
		$('#contactmessage').val(newsletterSubscribe);
		$('#contactmessage').addClass('selectedinput');
		hideThanksOverlay();
		showContactForm();
		return false;
	});
	
	$('#contactbackground, #closeform').click(function() {
		hideContactForm();
		return false;
	});
	
	$('#contactbackground, #contactform').animate({'opacity': 0}, 0);

	$('#contactform input[type=text], #contactform textarea')
		.focus(function() {
			if ($(this).val() == $(this).attr('defaultValue'))
			{
				$(this).val('');
				$(this).addClass('selectedinput');
			}
		})
		.blur(function() {
			if ($(this).val() == '')
			{
				$(this).val($(this).attr('defaultValue'));
				$(this).removeClass('selectedinput');
			}
		});
	
	$('#contactsubmit').click(function() {
		if (checkEmpty('#contact-name') && checkEmail('#contactmail', 1) && checkEmpty('#contactmessage'))
			submitContactForm();
			
		return false;
	});
	
	$('#contactmail').keyup(function() {
		checkEmail(this);
	});
	
	$('#contact-name, #contactmessage').keyup(function() {
		checkEmpty(this);
	});
	
	if (reportContent != '')
	{
		$('#contactmessage').addClass('selectedinput').val(reportContent);
		showContactForm();
	}
	
	$('#thanks').click(function() {
		hideThanksOverlay();
	});

	showThanksOverlay();
	
	setInterval("$('.neon').animate({'opacity': 0.4}, 1000).animate({'opacity': 1}, 1000);", 2000)
	
});

function showContactForm()
{
	$('#contactbackground').stop().show(0).animate({'opacity': 0.8}, 300);
	$('#contactform').stop().show(0).animate({'opacity': 1}, 300);
}

function hideContactForm()
{
	$('#contactbackground').stop().animate({'opacity': 0}, 300, function() {$(this).hide(0)});
	$('#contactform').stop().animate({'opacity': 0}, 300, function() {$(this).hide(0)});
}

function submitContactForm()
{
	var dataString = 'contact-name='+ $('#contact-name').val() + '&contactmail=' + $('#contactmail').val() + '&contactmessage=' + $('#contactmessage').val();
	
	$('#contactsubmit').remove();
	$('#contactform').append('<img id="wait" src="images/global/wait.gif" />');
	
	$.ajax({
		type: "POST",
		url: "index.php",
		data: dataString,
		success: function(data) {
			if (data == 'ok')
			{
				$('#contactform').html('<a href="#"><img src="images/global/formsuccess.png" /></a>');
				$('#contactform a').click(function() {
					hideContactForm();
				});
			}
		}
	});

	return false;
}

function showThanksOverlay()
{
	$('#thanks').animate({'opacity': 0}).show(0).animate({'opacity': 0.95}, 500);
}

function hideThanksOverlay()
{
	$('#thanks:visible').animate({'opacity': 0}, 300, function() {$(this).hide(0)});
}

function checkEmail(input) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

	if (pattern.test($(input).val()))
	{
		$(input).css('border', 'none');
		return true;
	}
	else
	{
		$(input).css('border', '1px solid red');
		$(input).focus();
		return false;
	};
}

function checkEmpty(input) {
	if ($(input).val() == $(input).attr('defaultValue'))
	{
		$(input).css('border', '1px solid red');
		$(input).focus();
		return false;
	}
	else
	{
		$(input).css('border', 'none');
		return true;
	}
}
