/* new functions */
function eVoteAnimateResults() {  
   $(".evotebar").each(function(){  
       var percentage = $(this).next().text();  
       $(this).css({width: "0%"}).animate({  
                 width: percentage}, 'fast');  
   });  
} 

function eVoteOpenEvote(evoteId) {
	// add opend class
	$(".evote-item.opened").removeClass("opened");	
	$(".evote-item .evote-item-icon.opened").removeClass("opened");	
	$("#"+evoteId).parent().parent().addClass("opened");
	
	// Test 
	// console.log($("#"+evoteId).parent().parent());
	
	$("#"+evoteId).parent().parent().find(".evote-item-icon").addClass("opened");
	
	$(".evotebar").css({width: "0%"});
	$("#"+evoteId).parent().next().slideDown("fast",  function () {
		eVoteAnimateResults();		
	});
}

function eVoteEnableVote(obj) {
	// Test 
	// console.log ($(obj).parent().parent().parent()); 

	var button = $(obj).parent().parent().parent().parent().find('.lightBlueButtonDeactive');
	button.removeClass('lightBlueButtonDeactive');
}

function eVoteValidateVote(obj) {

	// Test 
	// console.log($(obj).parent().parent().parent().find(".lightBlueButtonDeactive")); 
	
	if ($(obj).parent().parent().parent().find(".lightBlueButtonDeactive").length > 0)
		return true; 
	else 
		return false 
		
}

function eVotedoVote(evoteId,obj) {
	var dtEvote = new Date();
	var tsEvote = dtEvote.getTime(); 	


	if(!eVoteValidateVote(obj)) {
		var evotebody = $(obj).parent().parent().parent().parent().parent().parent();
		var evoteform = $(obj).parent().parent().parent().parent().parent();
		
		// Test
		// console.log ($(obj).parent().parent().parent().parent().parent());
		
		$.ajax({
		 	type: "GET",
			beforeSend:function(){
				evotebody.height(evotebody.height());
				evotebody.empty();
				evotebody.html("<img id=spinner src=spinner_white_460.gif border=0 />");
				evotebody.find("#spinner").css("margin-top",((evotebody.height()/2)-16)+"px");},
			url: "displayevote.aspx?ajax=" + tsEvote + "&nohide=1&evote=" + evoteId +"&" + evoteform.serialize(),
			success: function(data){
				evotebody.replaceWith(data);
				eVoteAnimateResults();
		 	}
		});
	}
}

function loadResultEvote(evoteId,obj) {
	var dtEvote = new Date();
	var tsEvote = dtEvote.getTime(); 	

	var evotebody = $(obj).parent().parent().parent().parent().parent();
	 
	 $.ajax({
	 	type: "GET",
		beforeSend:function(){
			evotebody.height(evotebody.height());
			evotebody.empty();
			evotebody.html("<img id=spinner src=spinner_white_460.gif border=0 />");
			evotebody.find("#spinner").css("margin-top",((evotebody.height()/2)-16)+"px");
			evotebody.parent().find("img").remove();			
		},		
		url: "displayevote.aspx?ajax=" + tsEvote + "&nohide=1&evote=" + evoteId +"&displayresults=1",
		success: function(data){
			evotebody.replaceWith(data);
			eVoteAnimateResults();
	 	}
	});
}


/* old functions */
function animateResults(){  
   $(".evotebar").each(function(){  
       var percentage = $(this).next().text();  
       $(this).css({width: "0%"}).animate({  
                 width: percentage}, 'fast');  
   });  
} 

function openEvote(evoteId)
{
	$(".evotebar").css({width: "0%"});
	$("#"+evoteId).parent().next().slideDown("fast",  function () {
		animateResults();		
		});
}

function enableVote(obj)
{
	var button = $(obj).parent().parent().parent().find('button');
	button.addClass('enabled');
	button.removeAttr("disabled");
}

function validateVote(obj)
{
	var evoteform = $(obj).parent().parent().parent();
	if(evoteform.find(":checked").length > 0)
		return true
	else
	{
		alert("Bitte treffen Sie eine Auswahl.");
		return false;
	}	
}

function loadResult(evoteId,obj)
{
	 var evotebody = $(obj).parent().parent().parent().parent();
	 $.ajax({
	 	type: "GET",
		beforeSend:function(){
			evotebody.height(evotebody.height());
			evotebody.empty();
			evotebody.html("<img id=spinner src=spinner_white_460.gif border=0 />");
			evotebody.find("#spinner").css("margin-top",((evotebody.height()/2)-16)+"px");},
		url: "displayevote.aspx?ajax=1328367323165&nohide=1&evote=" + evoteId +"&displayresults=1",
		success: function(data){
			evotebody.replaceWith(data);
			 animateResults();
	 	}
	});
}

function loadVoting(evoteId,obj)
{
	 var evotebody = $(obj).parent().parent().parent();
	 $.ajax({
	 	type: "GET",
		beforeSend:function(){
			evotebody.height(evotebody.height());
			evotebody.empty();
			evotebody.html("<img id=spinner src=spinner_white_460.gif border=0 />");
			evotebody.find("#spinner").css("margin-top",((evotebody.height()/2)-16)+"px");},
		url: "displayevote.aspx?ajax=1328367323165&nohide=1&evote=" + evoteId,
		success: function(data){
			evotebody.replaceWith(data);
	 	}
	});
}

function doVote(evoteId,obj)
{
	if(validateVote(obj))
	{
		var evotebody = $(obj).parent().parent().parent().parent();
		var evoteform = $(obj).parent().parent().parent();
		$.ajax({
		 	type: "GET",
			beforeSend:function(){
				evotebody.height(evotebody.height());
				evotebody.empty();
				evotebody.html("<img id=spinner src=spinner_white_460.gif border=0 />");
				evotebody.find("#spinner").css("margin-top",((evotebody.height()/2)-16)+"px");},
			url: "displayevote.aspx?ajax=1328367323165&nohide=1&evote=" + evoteId +"&" + evoteform.serialize(),
			success: function(data){
				evotebody.replaceWith(data);
				 animateResults();
		 	}
		});
	}
}
