var uploadCompleted = false;

$(document).ready(function(){
    // handler for the testimonial box (character countdown)
    $('#cover_testimonial').bind('keyup',function(){
	var charCount = $('#cover_testimonial').val().length;
	if(charCount <= 500) {
	    $('#charactersRemaining').html(500 - charCount);
	} else {
	    var currentScrollPosition = $("#cover_testimonial").scrollTop();
	    $('#cover_testimonial').val($('#cover_testimonial').val().substr(0, 500));
	    $("#cover_testimonial").scrollTop(currentScrollPosition);
	}
    });

    // handler for the error close box
    $('#errorDialogue_close').bind('click',function(){
	$('#errorDialogue').hide('slow');
	return false;
    });

    // email validation handler
    $('#cover_email').validateEmail('');

    // handler for the form submission (checks for valid and complete data)
    $('#submitYourEntryFormElement').submit(function(){
	var requiredFields = Array(
	    {field:'cover_title',		type:'text',	error:'Please enter the publication\'s title'},
	    {field:'cover_date',		type:'text',	error:'Please let us know what issue it is'},
	    {field:'cover_testimonial',	type:'text',	error:'Give us a description of what makes the cover special'},
	    {field:'cover_testimonialBy',	type:'text',	error:'Who is the testimonial by?'},
	    {field:'cover_jobTitle',	type:'text',	error:'What is the job title of the person who wrote the testimonial?'},
	    {field:'cover_enteredBy',	type:'text',	error:'Please give us your name'},
	    {field:'cover_phone',		type:'text',	error:'Please enter a contact phone number'},
	    {field:'cover_category',	type:'radio',	error:'Please select a category to enter your publication into'},
	    {field:'cover_donate',		type:'radio',	error:'Please let us know if you are willing to donate to the Rays of Sunshine childrens charity'},
	    {field:'cover_terms',		type:'radio',	error:'You must agree to The Maggies Terms &amp; Conditions to enter'}
	);

	// loop through all the fields and validate them
	var errorText = Array();							// array of error messages to display back to the user
	var badField = false;							// boolean flag for the field being checks
	for(i in requiredFields) {
	    badField = false;							// reset the flag for if the field is bad or not
	    switch(requiredFields[i].type) {
		case 'text':							// check text fields are above the minimum length
		    if($('[name='+requiredFields[i].field+']').val().length == 0) {
			badField = true;
		    }
		break;

		case 'radio':							// check radios have been selected
		    if($('[name='+requiredFields[i].field+']:checked').val() == undefined) {
			badField = true;
		    }
		break;
	    }

	    if(badField) {								// the field failed
		errorText.push(requiredFields[i].error);				// add the message to the error explanation array
		$('[name='+requiredFields[i].field+']').addClass('error');		// add the css class to alert the user it's bad
		$('[name='+requiredFields[i].field+']').removeClass('ok');		// remove the "ok" class
	    } else {
		$('[name='+requiredFields[i].field+']').addClass('ok');		// add the css class to alert the user it's OK
		$('[name='+requiredFields[i].field+']').removeClass('error');	// remove the "error" class
	    }
	}

	// check for an uploaded file
	if(!uploadCompleted) {
	    errorText.push('Please upload a cover image, or wait for the upload to complete before continuing');
	}

	// check for an OK email field
	if($('#cover_email.ok').val() == undefined) {
	    errorText.push('Please enter a valid contact email address');
	}

	if(errorText.length == 0) {						// check if there are any errors at all
	    return true;
	} else {								// if there are errors, alert the user, and stop the form from submitting
	    var htmlText = '<ul><li>';
	    htmlText += errorText.join('</li><li>');
	    htmlText += '</li></ul>';
	    $('#errorDialogue .errorDialogue_content div').html(htmlText);
	    $('html, body').animate({scrollTop:0}, 'slow');
	    $('#errorDialogue').show('slow');
	    return false;
	}
    });
});

// use window.load rather than document.ready so that we ensure the images are loaded before the animation starts
$(window).load(function(){
	// start the animation after a short delay
	setTimeout("animate()",1000);
});


// animate the logo sign
function animate() {
    var spd = 1500;

    // show the coming soon logo
    $('#comingSoon_coverVoting').fadeIn('slow');

    $('#aboutIsubscribe_voucher').animate({"top": "-270px"}, spd, "swing");

    // show the main logo and scroll
    $('#theMaggies_logo').animate({"top": "0"}, spd, "swing");
    $('#scroll').animate({"top": "80px"}, spd, "swing", function(){
	// add some glitter to the scroll
	$('#stage-front').addSparkleGroup(501,116,4,220,50);

	// move the first section across
	$('#pollResults_holder').animate({"left": "0"}, spd, "swing",function(){
	    // show the nav
	    $('#pollResults_nav').fadeIn('slow');
	});
    });
};

function setUploadStatus(returnCode,errorCode) {
    uploadCompleted = true;
}

