var _url = "/common/poll/pollXML.jhtml"
var polls = [];
var progressIndex;
var overlayRequest = false;

$(document).ready(function() {
	$("#voting-error").hide();
	$("#thankyouScreen").hide();
	//loadVoting();
});

function refreshAds(){
	reportKCA();
	$("#miniAd-iframe").attr("src","/common/droplets/ads/75x50_iframe.jhtml?kcaCat="+progressIndex);
	$("#rectAd-iframe").attr("src","/common/droplets/ads/KCARect_iframe.jhtml?kcaCat="+progressIndex);
}

function refreshThankyouAds(){
	reportKCA();
	$("#miniAd-iframe-thankyou").attr("src","/common/droplets/ads/75x50_iframe.jhtml?kcaCat=thanks");
	$("#rectAd-iframe-thankyou").attr("src","/common/droplets/ads/KCARect_iframe.jhtml?kcaCat=thanks");
}

function loadVoting() {
  $.ajax({
    type: "POST",
    url: _url,
    dataType: "xml",
    data: "pollName=kca_2009_category_voting_online",
    success: handlePollData
  });
}

function handlePollData( data ){
  $(data).find("ballot").each(function(){
    var pollData = new Object(); 
    pollData.questionTitle = $(this).attr("questionTitle");
    var options = new Array();

    $(this).find("lineItem").each(function() {
      options.push({
        id:$(this).attr('id'), 
        name:$(this).attr('name'), 
        copy:$(this).attr('display3'),
        urlAlias:$(this).attr('display2')
      })
    });

    pollData.options = options;
    polls.push(pollData);
  });

  progressIndex = 0;

  if (overlayRequest) {
    OpenKCAOverlay();
  }
}

function restartVotes(){
	progressIndex = 0;
	$("#nominee-next-category").removeClass("last");
	$("#voting-error").hide();
	$("#thankyouScreen").hide();
	$("#voteScreen").show();
	loadNextCategory();
}

function loadNextCategory() {
  // Refresh iFrame Ads
	refreshAds();

  // Last Category
	if ( (polls.length - 1) ==progressIndex ) {
		$("#nominee-next-category").addClass("last");
	}

  // Categories with only 3 Nominees
	if (progressIndex == 10 ||progressIndex == 12 ) {
    $("#voteArea").addClass("skip");
	} else {
		$("#voteArea").removeClass("skip");
	}

  // Clear last Categories Data
	$("#voteArea h1").empty();
	$("#nominee-list-photos").empty();
	$("#nominee-list-data").empty();

	if ( polls && polls[progressIndex] ) {
		$("#voteArea h1").append(polls[progressIndex].questionTitle);

		for ( var i = 0; i < polls[progressIndex].options.length; i++ ) {

			if ( (progressIndex == 10 && i == 0) || ( progressIndex == 12 && i == 2 ) ) {
        // ??
			} else {
        var class_name = "";
				if ( i == (polls[progressIndex].options.length - 1) ) {
					class_name = "last";
				}

				//append text
				var txtHtml ="<li class='"+class_name+"'>"+
							"<h4>"+polls[progressIndex].options[i].name+"</h4>"+
							"<p>"+polls[progressIndex].options[i].copy+"</p>"+
						 "</li>";

				$("#nominee-list-data").append(txtHtml);
				//append images
				var imgName="";
				//work around images size issue
				if(progressIndex==11 && i==1){
					imgName = polls[progressIndex].options[i].urlAlias+"_voting_sm.jpg"
				}else{
					imgName = polls[progressIndex].options[i].urlAlias+"_voting.jpg"
				}
        
        $("<li />")
          .addClass(class_name)
          .mouseover(function() {
            $(this).addClass("hover");
          })
          .mouseout(function() {
            $(this).removeClass("hover")
          })
          .html(
            "<a href='javascript:void(0)'><img src='/kids-choice-awards/common/images/nominees/"+imgName+"' /><div class='layer'></div></a><input type='radio' name='nominee' value="+i+" />"
          )
          .appendTo($("#nominee-list-photos"));
			}
		}
	} else {
		doLog("> Bad Polls Data");
	}
	
	updateProgress();
	addClickEvent();
	if(firstLoad){
		addNextCategoryEvent();
		firstLoad = false;
	}
	
}

function updateProgress(){
	$("#votingProgress ul").empty();
	var progressList = ""
	for( var i=0;i <polls.length; i++){
		var class_name= "";
		var active = "";
		if(i<progressIndex){
			class_name = "class='complete'";
		}
		if(i==progressIndex){
			class_name = "class='active'"
			active = "<li class='active' style='display: none;'>Favorite "+polls[i].questionTitle+"</li>";

		}
		progressList = progressList+"<li "+class_name+">Favorite "+polls[i].questionTitle+"</li>"+active;
	}
	$("#votingProgress ul").append(progressList);
}

function addClickEvent() {
	$("#nominee-list-photos a").click(function() {
    	$("#nominee-list-photos a").attr("class", "skipped");
   		 $(this).attr("class", "checked").next().click();
    }
  );
}

function addNextCategoryEvent(){
	$("#nominee-next-category").click(function(){
		if ($('input:radio[name=nominee]:checked').val() == undefined) {
			$("#voting-error").show();
			return;
		}

		$("#voting-error").hide();

		$.get("/kids-choice-awards/common/castVotes.jhtml", 
      {
        progressIndex: progressIndex, 
        castValue: $('input:radio[name=nominee]:checked').val(), 
        pollName:pollSection
      },
      function (data) {
        progressIndex++;
        if ( progressIndex<polls.length ) {
          loadNextCategory();
        } else {
          showThankyou();
        }
      })
    });
}

function showThankyou(){
	$("#thankyouScreen").show() 
	$("#voteScreen").hide()
	refreshThankyouAds()
}

function openOverlay(){
  /*CloseKCAOverlay();

 //Nicktropolis function
 if(typeof kcaOverlayCallback == 'function'){
  	kcaOverlayCallback();
    setTimeout(function() {
      document.location = "/kids-choice-awards/vote.jhtml";
    }, 1000);
 } else {
    document.location = "/kids-choice-awards/vote.jhtml";
 }*/

	/*if(progressIndex >= polls.length){
		showThankyou();
	}else{
			loadNextCategory();
	}*/
}