// Global variable definitions
// DB column numbers
var OPT_ID = 0;
var OPT_TITLE = 1;
var OPT_VOTES = 2;

var votedID;

$(document).ready(function(){
	setHeights();
	$("#thepoll").submit(formProcess); // setup the submit handler
	
	if ($("#poll-results").length > 0 ) {
		animateResults();
	}
		pollid = $("input[@name='pollNumber']").attr("value");
	
	if ($.cookie('vote_id'+pollid)) {
		$("#pollContainer").empty();
		votedID = $.cookie('vote_id'+pollid);
		$.get("poll.php?vote=none&pollid="+pollid,loadResults);
	}

});

function formProcess(event){
	event.preventDefault();
	
	var id = $("input[@name='pollAns']:checked").attr("value");
	id = id.replace("opt",'');
	
	$("#pollContainer").fadeOut("slow",function(){
		$(this).empty();
		
		votedID = id;
		$.get("poll.php?vote="+id+"&pollid="+pollid,loadResults);
		
		$.cookie('vote_id'+pollid, id, {expires: 365});
		});
}

function animateResults(){
	$("#poll-results div").each(function(){
			var percentage = $(this).width();
			$(this).css({width: "0%"}).animate({
				width: percentage}, 'slow');
				if ($.browser.msie)
					this.style.removeAttribute('filter');
				});
}

function loadResults(data) {
	$("#pollContainer").append(data).fadeIn("slow",function(){
		animateResults();});
}

function setHeights() {
	var ua   = navigator.userAgent.toLowerCase();
	var ie6  = (ua.indexOf("msie") && document.all && ua.indexOf("netscape") == -1);
	var gek  = (ua.indexOf("gecko")     != -1);
	var ff1  = (ua.indexOf("firefox")   != -1);
	var mac  = (ua.indexOf("mac")       != -1);
	var saf  = (ua.indexOf("safari")    != -1);

	var theHeightA=$("#copy2").height();
	if ( $("#quote").length > 0 )
		var theHeightB=$("#quote").height();
	if ( $("#factoid").length > 0 )
		var theHeightB=$("#factoid").height();
	if ( $("#poll").length > 0 )
		var theHeightB=$("#poll").height();
	if (theHeightA>theHeightB)
		var theHeight=theHeightA;
	else
		var theHeight=theHeightB;

	if(saf)
		var limitz=900;
	else
		var limitz=1400;

	if (theHeight < limitz)
		var theHeight2=theHeight+170;
	else
		var theHeight2=theHeight*1.34;
//	alert(theHeight+' - '+theHeight2);
	$("#theCenter").css({'height': theHeight}); 
	$("#theleft").css({'height': theHeight}); 
	$("#theright").css({'height': theHeight}); 

	$("#quote").css({'height': theHeight2}); 
	$("#factoid").css({'height': theHeight2}); 
	$("#poll").css({'height': theHeight2});

	$("#blocker").css({'height': theHeight}); 
	$("#blocker2").css({'height': theHeight}); 
	$("#blocker3").css({'height': theHeight}); 

	if ( ( $("#quote").length > 0 ) || ( $("#factoid").length > 0 )  || ( $("#poll").length > 0 )){
		$("#copy2").css({'width': 525}); 
	}
	if ( ( $("#blocker").length > 0 ) || ( $("#blocker2").length > 0 )  || ( $("#blocker3").length > 0 )){
		$("#copy2").css({'width': 750}); 
	}
}
