var odometer;

$(document).ready(function() {
	$("#predictdate").selectbox();
	
	$("input,textarea").click(function() {
		var normal = $(this).attr("rel");
		
		if ($(this).val() == normal) {
			$(this).val("");
		}
		$(this).css("background-color","#fff");
	});
	
	$("#emailsignup").click(function() {
		var email = $("#email").val();
		if (check_email(email)) {
			if (email == "name@example.com") {
				alert("Please enter a valid email address.");
				return false;
			}
			$.post("addmail.php", { email: email });
			$("#signup").hide();
			$('<div class="thanks nomarg">Thank You!</div>').insertAfter("#signup");
		} else {
			alert("Please enter a valid email address.");
		}
		return false;
	});
	
	setInterval(loadComments, 5000);
	setInterval("odometer.trigger('advanceCounter')", 1000);
	
	$("#nextvid").click(function() {
		var vidid = $("#curvid").val();
		
		$.post("ajax_loadrandvid.php", { id: vidid }, function(data) {
			document.getElementById('videocode').innerHTML = data;
		});
		/*
		// reset comment form
		$("#message").val("Add your message here..."); $("#message").css("background-color","#fff");
		$("#name").val("Name"); $("#name").css("background-color","#fff");
		$("#writearea").show();
		$("#writearea").next("div").hide();*/
		return false;
	});

	$("div.poster").hover(function() {
		$(this).css("background-color","#fff");
		$("#posterimg").attr("src","images/text_download_poster_over.png");
	},function() {
		$(this).css("background-color","#00B2D9");
		$("#posterimg").attr("src","images/text_download_poster.png");
	});
	
	$("div.poster").click(function() {
		window.location = 'http://www.nextnewbillion.com/downloads/?file=One Billion Views - Next New Networks Poster.pdf';
	});
	
	$("#castvote").click(function() {
		var vote = $("#predictdate").val();
		$.post("castvote.php", { vote: vote });
		$("#bars").load('loadbars.php');
		$("#bargraph").slideDown("fast");
		$("#predictbox").html('<div class="thanks">Thank You!</div>');
		return false;
	});
	
	$("#sendcomment").click(function() {
		var cname = $("#name").val(); var czip = $("#zip").val(); var com = $("#message").val(); var error = 0;
		if (cname == "Name") { $("#name").css("background-color","yellow"); error++; }
		if (com == "Add your message here...") { $("#message").css("background-color","yellow"); error++ }
		if (error > 0) return false;
		// process
		$.post("addcomment.php", { name: cname, zip: czip, comment: com });
		$("#writearea").hide();
		$('<div class="thanks">Thank You!</div>').insertAfter("#writearea");
		return false;
	});
});

function CountDescChars() {
	var total_chars = 140;
	var txtbox = document.getElementById('message');
	var txtlength = txtbox.value.length;
	if (txtlength > total_chars) {
		txtbox.value = txtbox.value.substring(0,total_chars);
	}
	if (txtlength > 89) {
		$("#message").css("height","85px");
	} else if (txtlength <= 89) {
		$("#message").css("height","58px");
	}
	var charleftstring = 140-txtlength;
	$('#charsleft').text(charleftstring);
}

function loadComments() {
	var current = $("#currcomm").val();
	$('#comments').fadeTo("fast", 0, function() {
	$('#comments').load('showcomment.php?id='+current, function() { $('#comments').fadeTo("slow", 1); });
	});
}

function check_email(e) {
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

	for(i=0; i < e.length ;i++) {
		if(ok.indexOf(e.charAt(i))<0){ 
			return (false);
		}	
	} 

	if (document.images) {
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two)) {
			return (-1);		
		} 
	}
}
