$(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);
	}


	$('#to').change(function(){
		switch ($(this).val()) {
			case 'tomljanovic@sunshinesachs.com,chills@NewmansOwnFoundation.org':
				$('#mediaT').show();
				$('#generalT').hide();
				setHeights('media');
			break;
			case 'info@newmansownfoundation.org':
				$('#mediaT').hide();
				$('#generalT').show();
				setHeights('general');
			break;
			default:
				$('#mediaT').hide();
				$('#generalT').hide();
				setHeights('none');
			break;
		}
	});  
});

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');
	});
}

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

function setHeights(safsize) {
	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) {
		theHeight=280;
		if (safsize=='media')
			theHeight=500;
		if (safsize=='general')
			theHeight=440;
		if (safsize=='none')
			theHeight=280;
	}
	if(ie6) {
		theHeight+=80;
	}

	var limitz=900;

	if (theHeight < limitz)
		var theHeight2=theHeight+40;
	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}); 
	}
}