/**
 * @author jmcardle
 */
var dev = 0;
var contentPoll= "";
var contentPollID="";
var contentPollSelections= "";
var contentVoted = false;

var sidebarPoll= "";
var sidebarPollID="";
var sidebarPollSelections = "";
var sidebarVoted =  false;

var pollArray = "";
var pollCookieStr = "";
var postURL = server + '/shared/pollDataProxy.cfm';

function checkPoll()
{  
	var sidebarPollExists = $(document).find('#sidebarPoll');
	var contentPollExists = $(document).find('#contentPoll');
	
	pollCookieStr	=  $.cookie('pongo_pollCookie');
	
	if (pollCookieStr != null) 
	{
		pollArray = pollCookieStr.split(',');
	}
	
	if (sidebarPollExists.length > 0) 
	{
		sidebarPollExists = true;
		sidebarPoll = $('#sidebarPoll');
	    sidebarPollID = sidebarPoll.html();
		sidebarPollID = sidebarPollID.substring(6,sidebarPollID.length - 1);
		
		for(var i=0; i < pollArray.length;i++)
		{
			if(pollArray[i] == sidebarPollID)
			{
				sidebarVoted = true;
				break;
			}
		}
	}
	else
	{	
		sidebarPollExists = false;	
	}
	
	
	if (contentPollExists.length > 0) 
	{
		contentPollExists = true;
		contentPoll = $('#contentPoll');
	    contentPollID = contentPoll.html();
		contentPollID = contentPollID.substring(6,contentPollID.length - 1);
		
		for(var i=0; i < pollArray.length;i++)
		{
			if(pollArray[i] == contentPollID)
			{
				contentVoted = true;
				break;
			}
		}
	}
	else
	{	
		contentPollExists = false;	
	}
	
	
	if(contentPollExists && sidebarPollExists)
	{
		if(contentPollID != sidebarPollID)
		{
			getPollData('content');
		}
		
		getPollData('sidebar');
	}
	else
	{
		if(contentPollExists)
		{
			getPollData('content');
		}
		
		if(sidebarPollExists)
		{
			getPollData('sidebar');
		}
	}
}

function getPollData(pollLocation)
{
	switch(pollLocation)
	{
		case "content":
			contentPollSelections= "";
			$.post(postURL,{'action':'display','pollID':contentPollID,'voted':contentVoted,"pollLocation":pollLocation},function (data){checkResult(data,pollLocation);});
		break;
		
		case "sidebar":
			sidebarPollSelections = "";
			$.post(postURL,{'action':'display','pollID':sidebarPollID,'voted':sidebarVoted,"pollLocation":pollLocation},function (data){checkResult(data,pollLocation);});
		break;
	}
}

function checkResult(data,pollLocation)
{
	var swfName = '#' + pollLocation + 'PollSwf';
	var swfClass = "." + pollLocation + 'Media';
	var swfFound = "";
	var currentPoll = '';
	var mediaOptions = "";	
	
	switch (pollLocation)
	{
		case "content":
			currentPoll = contentPoll;
			//mediaOptions = {width:200,height:200};
			break;
			
		case "sidebar":
			currentPoll = sidebarPoll;
			//mediaOptions = {width:75,height:50};
			break;
	}

	currentPoll.html(data);
	currentPoll.show();
	
	if(pollLocation == 'sidebar')
	{
		if(!$.browser.msie)
		{
			$('#sidebarPollBox .boxtop').css('margin-bottom',0);
		}
		$('#sidebarPollBox').show();
	}
	

}


function updatePollSelections(pollLocation)
{
	var pollAnswers = [];
	var inputName = 'input[name=' + pollLocation + 'Choices]:checked'; 
	
	$(inputName).each(function(){pollAnswers.push(this.value)});
	
	switch(pollLocation)
	{
		case "content":
			contentPollSelections = pollAnswers;
		break;
		
		case "sidebar":
			sidebarPollSelections = pollAnswers;
		break;
	}
}


function postPoll(pollLocation)
{
	var pollSelections = "";
	
	switch(pollLocation)
	{
		case "content":
			pollSelections = contentPollSelections;
		break;
		
		case "sidebar":
			pollSelections = sidebarPollSelections;
		break;
	}
	
	if(pollSelections.length > 0)
	{
		$.post(postURL,{'action':'submit','ipAddress': ipAddress,'choices':pollSelections,'accountID':accountID,"pollLocation":pollLocation},function(data){postResult(pollLocation);});	
	}
	else
	{	
		alert('No options selected for ' + pollLocation + ' poll.')	
	}
	
}

function postResult(pollLocation)
{
	var postPollID = "";
	var cookieStr = "";
	
	switch(pollLocation)
	{
		case "content":
			postPollID = contentPollID;
			contentVoted = true;
		break;
		
		case "sidebar":
			postPollID = sidebarPollID;
			sidebarVoted = true;
		break;
	}
	
	pollCookieStr = $.cookie('pongo_pollCookie');
	
	if(pollCookieStr != null)
	{
		cookieStr = pollCookieStr + ',';
	}
	

	var ids = cookieStr + postPollID;
	$.cookie('pongo_pollCookie',ids,{path:'/', expires: 365});
	getPollData(pollLocation);
}

function clearPollData(pollLocation)
{
	if (dev == 1) 
	{
		$.cookie('pongo_pollCookie',null);
		
		switch (pollLocation)
		 {
			case "content":
				contentVoted = false;
				getPollData(pollLocation);
				//$.post(postURL, {'action': 'clear','pollID': contentPollID,'ipAddress': ipAddress,"pollLocation": pollLocation}, function(data){getPollData(pollLocation);});
			break;
				
			case "sidebar":
				sidebarVoted = false;
				getPollData(pollLocation);
				//$.post(postURL, {'action': 'clear','pollID': sidebarPollID,'ipAddress': ipAddress,"pollLocation": pollLocation}, function(data){getPollData(pollLocation);});
			break;
		}
		
	}
}

$(document).ready(checkPoll);