//Set some variables
var rollspeed = 333;
var emailRegExp = /^[^@]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$/;

//Define file paths for ajax etc
var pathTellFriend = 'http://www.itrockstar.co.nz/wp-content/themes/itrockstarmain/tmpl.tellafriend.functions.php';
var pathContactSubmit = 'http://www.itrockstar.co.nz/wp-content/themes/ITRockstar2011/submit-contact-form.php';
var pathToImages = "http://www.itrockstar.co.nz/wp-content/themes/ITRockstar2011/images/";

//Preload Images
var imgArray = new Array('ribbon-roll.gif', 'popup-bg.png', 'entry-roll.png', 'popup-close.png', 'popup-close-roll.png', 'titles/title-email-signup.png', 'title-contact-us.png', 'titles/title-tell-a-friend.png', 'popup-submit.gif', 'buttons/btn-submit-roll.png', 'buttons/btn-submit-entry-roll.png', 'titles/title-submit-entry-roll.png', 'filename-bg.png');
var imgPreload = new Array();
for(i=0; i < imgArray.length; i++){
	imgPreload[i] = new Image();
	imgPreload[i].src = pathToImages+imgArray[i];	
}
function showScreenCover(){
	$('#screen-cover').css('display','block').animate({'height':'100%'},400);
}

$(document).ready(function(){
//Top right navigation rollovers
	$('#top-right-nav ul li a').mouseenter(function(){
		$(this).children('span').animate({'width':'100%'}, rollspeed);
	}).mouseleave(function(){
		$(this).children('span').animate({'width':'0'}, rollspeed);
	});
//Add class to #body for applying different background on homepage
	$('#homepage-content').parents('#body').addClass('homepage-body');
//Play video on home page when footer link is clicked
	$('.play-video-link').click(function(e){
		e.preventDefault();
		$('#tvc').modal({containerCss:{width:768,height:468}});
	});
//Play video on home page when video link is clicked
	$('.home-video-link').click(function(e){
		e.preventDefault();
		$('#joshvid').modal({containerCss:{width:768,height:468}});
	});
	if($('.home-video-link-ie6')){
		$('.home-video-link-ie6').click(function(e){
			e.preventDefault();
			$('#joshvid').modal({containerCss:{width:768,height:468}});
		});
	}
	$('.example-video-link').click(function(e){
		e.preventDefault();
		$('#examplevid').modal({containerCss:{width:768,height:468}});
	});
	
	
	
	
//-----------Poup windows
	$('.popup-link').click(function(event){
		showScreenCover();
		if($('#video object')){
			$('#video object').css('display','none'); //Hide video so it stops messing with the side of the popup
		}
	});
	$('.close-button').click(function(){
		$(this).parents('.inner').parents('.popup-wrapper').fadeOut('slow');
		$('#screen-cover').animate({'height':'0'},400, function(){ $('#screen-cover').css('display','block');});
		if($('#video object')){
			$('#video object').css('display','block');
		}
	});
  //Facebook like popup
	$('#likes-button').click(function(){ $('#like-this-popup').fadeIn('slow'); });
  //Tell a friend popup
  	$('.icon-friend a').click(function(){ $('#tell-a-friend-popup').fadeIn('slow'); });
  //Contact us popup
  	$('#nav-contact a').click(function(event){ event.preventDefault(); $('#contactFormPopup').fadeIn('slow'); showScreenCover(); });
  //Register popup
    $('#btn-email-signup').click(function(){ $('#regFormPopup').fadeIn('slow'); });
	
//Populate facebook like and share count on entry detail page
	if($('#fb-count-info')){
		var numLikes = $('like_count').html();	
		var numShares = $('share_count').html();
		$('#likes-count span').html(numLikes);
		$('.fb-count span').html(numShares);
	}
	
//Rollovers for entry list page
	$('.submission-entries li').mouseenter(function(){
		$(this).children('.item-content').addClass('roll');
	}).mouseleave(function(){
		$(this).children('.item-content').removeClass('roll');
	});
	
//Submit tell a friend form
	$('input[name=hype:friends_email]').clearingInput();
	$('input[name=hype:your_name]').clearingInput();
	$('#btn_submit_small').click(function(){
		$('form#hype').submit();
		return false;
	});
	$('form#hype').submit(function(e){
		var your_name = $('input[name=hype:your_name]').val();
		var friends_email = $('input[name=hype:friends_email]').val();
		var is_error = false;
		if( ( your_name == '') || (your_name == $('label[for=hype:your_name]').text()) ){
			$('input[name=hype:your_name]').clearingInput();
			$('input[name=hype:your_name]').effect("shake", {distance:10, times:3 }, 100);
			is_error = true;
		}
		if( (friends_email == '') || (friends_email == $('label[for=hype:friends_email]').text()) || (!emailRegExp.test(friends_email))){
			$('input[name=hype:friends_email]').clearingInput();
			$('input[name=hype:friends_email]').effect("shake", {distance:10, times:3 }, 100);
			is_error = true;
		}
		if(is_error == false){
			$.get(pathTellFriend+'?your_name='+your_name+'&friends_email='+friends_email, function(data){
				if(data == 'true'){
					$('#tell-friend-response').text('Thank you '+your_name+', Your message has been sent.');
				}else{
					$('#tell-friend-response').text('There was an error emailing your friend details of the competition. Please try again later.');
				}
				$('#tell-friend-form-content').hide('slow');
			});
		}else{
			$('input[name=hype:friends_email]').clearingInput();
			$('input[name=hype:your_name]').clearingInput();
		}			
		e.preventDefault();
		e.stopPropagation();
		return false;
	});	
//When user clicks submit on contact form
	$('#btn_submit_contact').bind('click', function(event){
		event.preventDefault();
		var userEmail = $('#contact-user-email').val();
		var userMessage = $('#contact-user-message').val();
		var errors = '';
		if(userMessage == ''){
			errors = '<span class="error">Please enter a message</span>';
		}
		if( (userEmail == '') || (!emailRegExp.test(userEmail)) ){
			errors = '<span class="error">A valid email address, please</span>';	
		}
		if(errors != ''){
			$('#contact-form-errors').html(errors).fadeIn('slow');
		}else{
			$.get(pathContactSubmit+'?userEmail='+userEmail+'&userMessage='+userMessage, function(data){
				if(data == 'true'){
					$('#contact-form-response').text('Thank you, your message has been sent.');
					$('#contactForm').hide('slow');
				}else{
					$('#contact-form-response').text('Sorry, there was an error sending your message.');
				}
			});
		}
	});
	$('#contact-user-email').bind('focus', function(){ $('#contact-form-errors').fadeOut('slow'); });
	$('#contact-user-message').bind('focus', function(){ $('#contact-form-errors').fadeOut('slow'); });
//When user clicks submit on registration form
	$('#btn_submit_reg').bind('click', function(event){
		event.preventDefault();
		var userEmail = $('#reg-user-email').val();
		var userName = $('#reg-user-name').val();
		var errors = '';
		if( (userEmail == '') || (!emailRegExp.test(userEmail)) ){
			errors = '<span class="error">A valid email address, please</span>';	
		}
		if(userName == ''){
			errors = '<span class="error">Please enter your name</span>';
		}
		if(errors != ''){
			$('#reg-form-errors').html(errors).fadeIn('slow');
		}else{
			$.get(pathContactSubmit+'?userEmail='+userEmail+'&userName='+userName, function(data){
				if(data == 'true'){
					$('#reg-form-response').text('Thank you, your message has been sent.');
					$('#regForm').hide('slow');
					$('.popup-content h3').css('display','none');
				}else{
					$('#reg-form-response').text('Sorry, there was an error sending your message. Please try again later.');
				}
			});
		}
	});
//Hide submit entry form if submission is successful
	$('.hide-form').hide('slow');
//Popup close button rollover
	$('.popup-wrapper .close-button').mouseenter(function(){ $(this).addClass('roll'); }).mouseleave(function(){ $(this).removeClass('roll'); });
//Popup submit button rollover
	$('.popup-submit').mouseenter(function(){ $(this).addClass('roll');	}).mouseleave(function(){ $(this).removeClass('roll'); });
//Comment submit button hover{
	$('.btn_comment').mouseenter(function(){ $(this).addClass('roll'); }).mouseleave(function(){ $(this).removeClass('roll'); });
//Comment submit button hover{
	$('#details-entry-link').mouseenter(function(){ $(this).addClass('roll'); }).mouseleave(function(){ $(this).removeClass('roll'); });
	//Comment submit button hover{
	$('#submit-entry-actions .title').mouseenter(function(){ $(this).addClass('roll'); }).mouseleave(function(){ $(this).removeClass('roll'); });
//Stop comments form from being posted if there's nothing in the textarea
	$('#commentForm .btn_comment').bind('click', function(event){
		if( $('#comment').attr('value') == '' ){
			event.preventDefault();
			alert('Please enter some text to submit a comment');
		}
	});
});
