var pollId=0;
var pollCookieName='pollCookie';

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// initalize javascript elements
$(document).ready(function(){
	//first get id for this poll
	pollId=$(".poll .pollId").html();
	pollCookieName=pollCookieName+pollId;
	//eraseCookie(pollCookieName);
	
	//now hide options and results
	$(".poll ul").hide();
	
	if(readCookie(pollCookieName)==null){
		$(".poll .options").show();
		
		$(".poll .options a").click(function(e){
			var pollOption=$(e.target).parent().children("span").html();

			//send vote
			$.get("includes/poll_data.php", {requestType:"vote", pollId:pollId, pollOptionId:pollOption},function(data){
   				$(".poll").html(data);
  			});
  			
  			createCookie(pollCookieName,pollOption,365)
  			
  			return false;
		});
		
	}else{
		$(".poll .results").show();
	}
});