// Poll settings
var overlayOpacity = 0.8;
var overlayDuration = 0.2;
var pollInitialized = false;

//eraseCookie('pollVoted_1');

// Poll functions
function initPoll()
{
  // Create overlay
	divOverlay = document.createElement('div');
	divOverlay.setAttribute('id','overlay');
	divOverlay.style.display = 'none';
	
	divOverlay.style.backgroundColor = "#000000";
	divOverlay.style.top = '0px';
	divOverlay.style.left = '0px';
	divOverlay.style.width = '100%';
	divOverlay.style.height = '100%';
	divOverlay.style.zIndex = 50;

	// Set position
	divOverlay.style.position = 'absolute';
	
	// Add Contentwrapper
	divContentWrapper = document.createElement('div');
	divContentWrapper.setAttribute('id','contentWrapper');
	divContentWrapper.style.display = 'none';
	divContentWrapper.style.backgroundColor = "transparent";
	divContentWrapper.style.textAlign = 'center'; 
	divContentWrapper.style.position = 'absolute'; 
	divContentWrapper.style.top = '0px';
	divContentWrapper.style.left = '0px';
	divContentWrapper.style.width = '100%';
	divContentWrapper.style.zIndex = 51;

	// Add Poll div
	divPoll = document.createElement('div');;
	divPoll.setAttribute('id','poll');
	divPoll.style.position = 'relative'; 
	divPoll.style.visible = 'none';
	divPoll.style.backgroundColor = "#FFFFFF";
	divPoll.style.margin = '0px auto';
	//divPoll.style.padding = '0px';
	//divPoll.style.border = '1px solid #676767';
	divPoll.style.zIndex = 51;
	divPoll.style.textAlign = 'left'; 

	// Fill Poll
	new Ajax.Request('/ajaxHandler.php?module=poll&action=Get',
		{
			method:'get',
			onSuccess: function(transport, json)
				{
					if ((json == null) || (json.status != 'success'))
					{
						alert('Server send invalid response: ' + transport.responseText);
						return;
					}
					
					// Put returned html in div
					$('poll').innerHTML = json.html;					
				},
			
			onFailure: function(){ alert('Something went wrong...') }
		}
	);

	// Add to page
	document.body.appendChild(divOverlay);
	document.body.appendChild(divContentWrapper);
	divContentWrapper.appendChild(divPoll);
	
	pollInitialized = true;
}

function showPoll()
{
	if (pollInitialized == false) initPoll();
	
	var arrayPageSize = getPageSize();
  var arrayPageScroll = getPageScroll();

	// set height to take up whole page
	$('overlay').style.height = (arrayPageSize[1] + 'px');
	$('contentWrapper').style.height = (arrayPageSize[1] + 'px');

	// Set poll position
	$('poll').style.top = arrayPageScroll[1] + 100;
	
	// Show
	hideSelectBoxes();
	new Effect.Appear('overlay', { duration: overlayDuration, from: 0.0, to: overlayOpacity, afterFinish: showPollAfterOverlay });
}

function showPollAfterOverlay(obj)
{
	Element.show('contentWrapper');
}

function closePoll() {
	Element.hide('contentWrapper');
	new Effect.Fade('overlay', {duration: overlayDuration});
	showSelectBoxes();
}

function sendVote(pollAnswer)
{
	// Check selected item
	answerId = -1;
	for (i=0; i < pollAnswer.length; i++)
		if (pollAnswer[i].checked == true)
			answerId = pollAnswer[i].value;
	
	if (answerId == -1)
	{
		alert('Geen optie geselecteerd');
		return;
	}
	
	// Send vote
	new Ajax.Request('/ajaxHandler.php?module=poll&action=SendVote&answerId=' + answerId,
		{
			method:'get',
  		onSuccess: function(transport, json)
				{
					if ((json == null) || (json.status != 'success'))
					{
						alert('Server send invalid response: ' + transport.responseText);
						return;
					}
					
					// Put returned html in div
					$('poll').innerHTML = json.html;					
					
					// Set voted cookie
					createCookie('pollVoted_'+json.pollid, 'true', 3650);
    		},
			
			onFailure: function(){ alert('Something went wrong...') }
  	}
	);
}

//

function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}
