/**
 * @author davidchiu
 */
function validateComment() {
	$('#error').remove();
	
	var author=$('#author').val();
	var email=$('#email').val();
	var comment=$('#comment').val();
	if ($('#author').length<1) {
		author='...';
		email='.......'
	}
		
	if (comment.length<10 || email.length<7 || author.length<2) {
		$('.commentform').append('<div align="right" id="error"><br clear="all">Please fill in the required fields.</div>');
		
		return false;		
	}
}

function main(){
	$('<sc' + 'ript type="text/javascript" src="/wp-content/themes/1912bungalow/js/jquery.color.js"></sc' + 'ript>').appendTo('head');
	$('<sc' + 'ript type="text/javascript" src="/wp-content/themes/1912bungalow/js/jquery.ajax_upload.js"></sc' + 'ript>').appendTo('head');
		
	var hash = location.href.split('#');
	
	if (hash.length > 0 && hash[1]!='contestForm') {
		$('#' + hash[1]).css('background-color', '#ecf3c9');
		$('#' + hash[1]).css('margin-left', '-10px');
		$('#' + hash[1]).css('width', '487px');
		$('#' + hash[1]).css('padding', '0px 10px 10px 10px');
		$('#' + hash[1]).animate({
			backgroundColor: '#ecf3c9'
		}, 250).animate({
			backgroundColor: '#ffffff'
		}, 750);
	}
	
	$('.contestTab').hover(function(){
		$(this).css('margin-top', '-33px');
	}, function(){
		$(this).css('margin-top', '0px');
	})
	
	// if the browser isn't wide enough to handle the large background, attach everything to the left margin
	if (location.href.indexOf('contest') > 0) {
		try {
			if (hash[1].indexOf('comment') == 0) 
				$('#voteThankYou').css('display', 'block');
		}
		catch (e)
		{}
		
		if ($(window).width() < 1130) {
			$('BODY').css('margin-left','20px');
			$('BODY').css('background-position','-155px 0px');
		}
		fileUpload();
	}
	
}

function contestSubmit() {
	
	var valid=true;

$('#contestForm #name').css('border', '1px solid #6e6e6e');
$('#contestForm #screenname').css('border', '1px solid #6e6e6e');
$('#contestForm #contestemail').css('border', '1px solid #6e6e6e');
$('#contestForm #street').css('border', '1px solid #6e6e6e');
$('#contestForm #city').css('border', '1px solid #6e6e6e');
$('#contestForm #zip').css('border', '1px solid #6e6e6e');
$('#contestForm #phone1').css('border', '1px solid #6e6e6e');
$('#contestForm #phone2').css('border', '1px solid #6e6e6e');
$('#contestForm #phone3').css('border', '1px solid #6e6e6e');
	
	if ($('#contestForm #name').val().length == 0) {
		$('#contestForm #name').css('border', '1px solid #f60f5c');
		valid=false;
	}
	
	if ($('#contestForm #screenname').val().length == 0) {
		$('#contestForm #screenname').css('border', '1px solid #f60f5c');
		valid=false;
	}
	
	if ($('#contestForm #contestemail').val().length == 0) {
		$('#contestForm #contestemail').css('border', '1px solid #f60f5c');
		valid=false;
	}

	if ($('#contestForm #contestemail').val().indexOf('@') < 1) {
		$('#contestForm #contestemail').css('border', '1px solid #f60f5c');
		valid=false;
	}
	
	if ($('#contestForm #contestemail').val().indexOf('.com') < 1 && $('#contestForm #contestemail').val().indexOf('.net') < 1 && $('#contestForm #contestemail').val().indexOf('.org') < 1 && $('#contestForm #contestemail').val().indexOf('.edu') < 1) {
		$('#contestForm #contestemail').css('border', '1px solid #f60f5c');
		valid=false;
	}
	
	if ($('#contestForm #street').val().length == 0) {
		$('#contestForm #street').css('border', '1px solid #f60f5c');
		valid=false;
	}
	
	if ($('#contestForm #city').val().length == 0) {
		$('#contestForm #city').css('border', '1px solid #f60f5c');
		valid=false;
	}
	
	if ($('#contestForm #zip').val().length == 0) {
		$('#contestForm #zip').css('border', '1px solid #f60f5c');
		valid=false;
	}
	
	if ($('#contestForm #phone1').val().length == 0) {
		$('#contestForm #phone1').css('border', '1px solid #f60f5c');
		valid=false;
	}
	
	if ($('#contestForm #phone2').val().length == 0) {
		$('#contestForm #phone2').css('border', '1px solid #f60f5c');
		valid=false;
	}
	
	if ($('#contestForm #phone3').val().length == 0) {
		$('#contestForm #phone3').css('border', '1px solid #f60f5c');
		valid=false;
	}
	
	if ($('#contestForm #filename').val().length == 0) {
		alert('Please upload a photo. This is a photo contest!')
		valid=false;
	}
	
	if ($('#contestForm #agree').val())
		
	
	if (!valid) 
		location.href = '#contestForm';
	else {
		$('#author').val($('#contestForm #screenname').val());
		$('#email').val($('#contestForm #contestemail').val());
		$('#url').val('http://1912bungalow.com/images/contest/'+$('#contestForm #filename').val());
		$('#comment').val('{"name":"' + $('#contestForm #name').val() + '", "screenname":"' + $('#contestForm #screenname').val() + '", "email":"' + $('#contestForm #email').val() + '", "street":"' + $('#contestForm #street').val() + '", "city":"' + $('#contestForm #city').val() + '","state":"' + $('#contestForm SELECT').val() + '", "zip":"' + $('#contestForm #zip').val() + '", "phone":"' + $('#contestForm #phone1').val() + $('#contestForm #phone2').val() + $('#contestForm #phone3').val() + '", "filename":"/images/contest/' + $('#contestForm #filename').val() + '", "votes":"1"}')
		document.forms["commentform"].submit();
	}
	return false;
}

function fileUpload() {
		$.ajax_upload('#uploadphotobutton', {
		action : '/contest_submission.php',
		name: 'photo',
 		onSubmit : function(file, ext){
			$('#uploadphotobutton img').attr('src','http://1912bungalow.com/imgdir1/upload_a_photo_box-loading.gif');			
			var allowed = ['jpg', 'jpeg', 'JPG', 'JPEG'];
			if ($.inArray(ext, allowed ) === -1){
				// extension is not allowed
				alert('Error: only images are allowed');
				// cancel upload
				$('#uploadphotobutton img').attr('src','http://1912bungalow.com/imgdir1/upload_a_photo_box.gif');			
				return false;
			}
		},
		onSuccess : function(file){
			var imgFile=file.split('/');

			$('#filename').val(imgFile[imgFile.length-1]);
			$('#uploadphotobutton img').attr('src','http://1912bungalow.com/images/contest/s_'+imgFile[imgFile.length-1]);
			$('#uploadphotobutton').css('background','transparent');
		},
		onError : function(file, response){
			alert(response);
		}		
	});

}

function contactUs() {
	location.href="mailto:"+"heather@1912"+"bungalow"+".com"
}

function showTerms() {
	$('#termsAndConditions').css('display', 'block');
}

function closeTerms() {
	$('#termsAndConditions').css('display', 'none');
}

function closeThankYou() {
	$('#voteThankYou').css('display', 'none');
}

function enableContestSubmit() {
	$('#submitEntryButton').css('display','block');
	$('#agree').attr("disabled", true);
}

function showPhotoEntry(entry, code) {
	var msg='', msg1='';
	if (entry==0) {
		msg="1st prize winner";
		msg1="<ul><strong>Prize:</strong><li>Schoolhouse Electric Co. Otis light fixture in satin nickel with a Modern Aqua shade; valued at $137</li></ul>"
	}
	if (entry==1) {
		msg="2nd prize winner";
		msg1="<ul><strong>Prize:</strong><li>$75.00 Home Depot gift certificate </li></ul>"
	}
	if (entry==2) {
		msg="3rd prize winner";
		msg1="<ul><strong>Prize:</strong><li>$50.00 Home Depot gift certificate</li></ul>"
	}
	var html='<img src="'+contestdata[entry].filename+'"> \
	<div id="largeImgInfo"> \
	<img src="http://1912bungalow.com/imgdir1/avatar1.gif" align="left"> \
	<div id="msg"> \
	'+msg+' \
	</div> \
	<div id="user"> \
	'+contestdata[entry].screenname+'<br> \
	'+contestdata[entry].city+', '+contestdata[entry].state+'<br> \
	</div> \
	<div id="voting"> \
	Number of votes: '+contestdata[entry].votes+'</div>'+msg1;
	$('#largeImg').html(html)
}
