/* ################################################################# */
/*                                                                   */
/*  TITLE:        NBC_MOOD.JS                                        */
/*  VERSION:      2.01                                               */
/*  LAST UPDATED: 11/04/2009                                          */
/*  UPDATED BY:   Toby Spinks                                        */
/*                                                                   */
/*  NAMESPACE: NBC                                                   */
/*                                                                   */
/*  PUBLIC FUNCTIONS:                                                */
/*                                                                   */
/*  PREREQUISTES:                                                    */
/*                                                                   */
/*  @release 2009-11-05 spinks                                        */
/* ################################################################# */

NBC.Tag = function() {
    // initiatest poll function  
    $('a.mood').click(function(event) {
      var UrlArr = window.location.href.split('/'); // gets domain       
      var pathToMedia = $("#url").html();
      var mood = $("#mood_add").html();
      var content_id = $("#cid").html();
      var section_id = $("#sid").html();
      var existingmood = $("#existingmood").html();
      
      //if url hasn't been tagged with a mood yet.
      if ( existingmood == null || existingmood == undefined || existingmood == ''){
       var url = 'http://' + UrlArr[2] + '/i/dispatcher/?h=mood&action=addtag&tag='+mood+'&contid='+content_id+'&sectid='+section_id+'&r='+(Math.round(Math.random()*1000000));
         $.ajax({
           type: "GET",
           url: url,
           dataType: "html",
           success: function(data) {
           }
         }); 
        }
    })     
} // end of NBC Tag
 
nbc.countVoteClicks = {
    moodCount: "",
    moodVoted: ""
};

$(window).load(function() {
	
  $(".moodSelectContainer").css("opacity","1");
  $(".moodSelect").show();
  $("#processingMood").hide();
  nbc.countVoteClicks.moodCount = "0";
  U.log("url for doc " +document.URL);
  var moodCookie = U.readCookie('moodSet');
  var moodreadURL = U.readCookie('moodURL');
  var moodValueCookie = U.readCookie('moodValue');
  U.log("cookieURL "+unescape(moodreadURL));
  U.log("mood value "+unescape(moodValueCookie));
 U.log(document.URL);
 U.log("moodCookie "+moodCookie);
 if ((moodCookie  == 'alreadyVoted') &&  (unescape(moodreadURL) == document.URL)) {
    $(".moodSelect").attr("disabled","disabled");  
    if ($("#listMoods").attr("id")) {
	    $("#listMoods li").find("input").each(function() {
	         $(this).attr("disabled", "disabled");
	     });
    }   
    $(".moodResult li").each(function() {
        $(this).css("cursor","default");
    });
    $(".moodResult").find("li").each(function() {
        $(this).mouseover(function() { $(this).find(".hover_state_mood_section").css("display","none");	});
        $(this).mouseout(function() {  $(this).find(".hover_state_mood_section").css("display","none"); });
   });
    if (nbc.omniture.pageType != "interactive_page") {
    	$(".moodSelectContainer").hide();
    	$(".moodThankYou").css("display","block");
	    $(".moodResult").find("li").each(function() {
	    	$(this).find("."+moodValueCookie).parent().find(".hover_state_mood_section_justVoted").show();
	   });
    }    
    
 }
 else { 
	 
   $(".moodSelect").change(function() {
	U.log(nbc.countVoteClicks.moodCount);
   	if (nbc.countVoteClicks.moodCount == "0") {
   		nbc.countVoteClicks.moodCount = "1";
   
       $(this).find("option:selected").each(function() {
        var selMood="";
        selMood = $(this).text();
        nbc.omniture.userVote = selMood;     
        U.log("nbc.omniture.userVote "+nbc.omniture.userVote);
        G.doPixelTracking(7);
        U.log("moodSel "+ selMood);
        $("#mood_comm").val("");
        $("#mood_comm").val($(this).text());
        $("#existingmoodComm").val("");
        $("#existingmoodComm").val(selMood);                
        var UrlArr = window.location.href.split('/'); // gets domain       
        var pathToMedia = $("#url").html();
        var moodSel = $(this).find("option:selected").text();
        U.log("moodSel "+ moodSel);
        var content_id = $("#cid").html();
        var section_id = $("#sid").html();
        var existingmood = $("#existingmood").val();
        U.log("count 0 " + nbc.feelingArray[0].count);
        if (nbc.feelingArray[0].count != "$item.getString('count')") {
         if (nbc.omniture.pageType == "interactive_page") {
        	 updateUserViewFromInter(selMood);
         }
         else {
        	 updateUserViewFromInterPage(selMood);
        	 updateViewWithVoted(selMood);
         }  
	        $(".moodSelectContainer").hide();
	        $('.moodThankYou').show();
           // U.log("mood thank you show");
            U.createCookie("moodSet", "alreadyVoted", 365);
            var moodUrl1 = escape(document.URL);
            U.createCookie("moodURL", moodUrl1, 365);
            U.createCookie("moodValue", nbc.omniture.userVote, 365);
            var moodreadURL3 = U.readCookie('moodURL');
            U.log("cookieURL after setting "+unescape(moodreadURL3));
	       //if url hasn't been tagged with a mood yet.
	        if ( existingmood == null || existingmood == undefined || existingmood == ''){
	          var url = 'http://' + UrlArr[2] + '/i/dispatcher/?h=mood&action=addtag&tag='+selMood+'&contid='+content_id+'&sectid='+section_id+'&r='+(Math.round(Math.random()*1000000));
	          $.ajax({
	                  type: "GET",
	                  url: url,
	                  dataType: "html",
	                  success: function(data) {
	                     // updateUserView();
	        	       
	                  }
	            }); 
	          }
         }  // if kickapp responds 
      });
   	}
    });
   
   /* onclick on feeling  mood individually */
   $(".moodResult li").click(function() {
		U.log(nbc.countVoteClicks.moodCount);
	   	if (nbc.countVoteClicks.moodCount == "0") {
	   		nbc.countVoteClicks.moodCount = "1";
	   		selMood = $(this).find(".feeling").find("span").text();
	        nbc.omniture.userVote = selMood;
	        U.log("nbc.omniture.userVote "+nbc.omniture.userVote);
	        G.doPixelTracking(10);
	        U.log("moodSel "+ selMood);
	        $("#mood_comm").val("");
	        $("#mood_comm").val($(this).find(".feeling").find("span").text());
	        $("#existingmoodComm").val("");
	        $("#existingmoodComm").val(selMood);                
	        var UrlArr = window.location.href.split('/'); // gets domain       
	        var pathToMedia = $("#url").html();
	        var moodSel =$(this).find(".feeling").find("span").text();
	        U.log("moodSel "+ moodSel);
	        var content_id = $("#cid").html();
	        var section_id = $("#sid").html();
	        var existingmood = $("#existingmood").val();
	        U.log("count 0 " + nbc.feelingArray[0].count);
	        if (nbc.feelingArray[0].count != "$item.getString('count')") {
	        	 if (nbc.omniture.pageType == "interactive_page") {
	        		 updateUserViewFromInter(selMood);
	             }
	             else {
	            	 updateUserViewFromInterPage(selMood);
	            	 updateViewWithVoted(selMood);
	             }  
	           
	        	//  updateUserView();
		        $(".moodSelectContainer").hide();
		        $('.moodThankYou').show();
		           // U.log("mood thank you show");
		            U.createCookie("moodSet", "alreadyVoted", 365);
		            var moodUrl1 = escape(document.URL);
		            U.createCookie("moodURL", moodUrl1, 365);
		            U.createCookie("moodValue", nbc.omniture.userVote, 365);
		            var moodreadURL3 = U.readCookie('moodURL');
		            U.log("cookieURL after setting "+unescape(moodreadURL3));
		            $(".moodResult li").each(function() {
		                $(this).css("cursor","default");
		            });
		            
		       //if url hasn't been tagged with a mood yet.
		        if ( existingmood == null || existingmood == undefined || existingmood == ''){
		          var url = 'http://' + UrlArr[2] + '/i/dispatcher/?h=mood&action=addtag&tag='+selMood+'&contid='+content_id+'&sectid='+section_id+'&r='+(Math.round(Math.random()*1000000));
		          $.ajax({
		                  type: "GET",
		                  url: url,
		                  dataType: "html",
		                  success: function(data) {
		                     // updateUserView();
		        	       
		                  }
		            }); 
		          }
	         }  // if kickapp responds 
	   	}
	 });
   /* end of onclick */
   
   /* onclick on hover state  mood individually */
   /* $(".hover_state_mood_section").click(function() {
		U.log(nbc.countVoteClicks.moodCount);
	   	if (nbc.countVoteClicks.moodCount == "0") {
	   		nbc.countVoteClicks.moodCount = "1";
	   		selMood = $(this).parent().find("span.feeling").find("span").text();
	   		nbc.countVoteClicks.moodVoted = selMood;
	        nbc.omniture.userVote = selMood;
	        U.log("nbc.omniture.userVote "+nbc.omniture.userVote);
	        G.doPixelTracking(7);
	        U.log("moodSel "+ selMood);
	        $("#mood_comm").val("");
	        $("#mood_comm").val(selMood);
	        $("#existingmoodComm").val("");
	        $("#existingmoodComm").val(selMood);                
	        var UrlArr = window.location.href.split('/');      
	        var pathToMedia = $("#url").html();
	        var moodSel = selMood;
	        U.log("moodSel "+ moodSel);
	        var content_id = $("#cid").html();
	        var section_id = $("#sid").html();
	        var existingmood = $("#existingmood").val();
	        U.log("count 0 " + nbc.feelingArray[0].count);
	        if (nbc.feelingArray[0].count != "$item.getString('count')") {
	           updateUserViewFromInterPage(selMood);
	           updateViewWithVoted(selMood);
		        $(".moodSelectContainer").hide();
		        $('.moodThankYou').show();
		            U.createCookie("moodSet", "alreadyVoted", 365);
		            var moodUrl1 = escape(document.URL);
		            U.createCookie("moodURL", moodUrl1, 365);
		            var moodreadURL3 = U.readCookie('moodURL');
		            U.log("cookieURL after setting "+unescape(moodreadURL3));
		            $(".moodResult li").each(function() {
		                $(this).css("cursor","default");
		            });
		
		        if ( existingmood == null || existingmood == undefined || existingmood == ''){
		          var url = 'http://' + UrlArr[2] + '/i/dispatcher/?h=mood&action=addtag&tag='+selMood+'&contid='+content_id+'&sectid='+section_id+'&r='+(Math.round(Math.random()*1000000));
		          $.ajax({
		                  type: "GET",
		                  url: url,
		                  dataType: "html",
		                  success: function(data) {
		      		        	       
		                  }
		            }); 
		          }
	         } 
	   	}
	 }); */
   /* end of onclick */
   
     
    } 
  });


 function updateViewWithVoted(selMood) {
	 $(".moodResult").find("li").each(function() {
	        $(this).mouseover(function() { $(this).find(".hover_state_mood_section").css("display","none");	});
	        $(this).mouseout(function() {  $(this).find(".hover_state_mood_section").css("display","none"); });
	 });
    $(".moodResult").find("li").each(function() {
    	 currElem = $(this).find(".feeling").find("span").text();
    	 if (currElem == selMood) {
    		 $(this).find(".hover_state_mood_section").css("display","none");
    		 $(this).find(".hover_state_mood_section_justVoted").css("display","block"); 
    	 }
    });
 }
 
 function moodPostWithComm() {
    $("#commentsForm").css("opacity","0.5");   
    $("#processing_comm").show();  
    var str = "";
    var var_name = $("input[@name='mood_comm']:checked").val();
    var mood = var_name;
    $("#mood_comm").val("");
    $("#mood_comm").val(var_name);
    $("#existingmoodComm").val("");
    $("#existingmoodComm").val(var_name);              
    var UrlArr = window.location.href.split('/'); // gets domain       
    var pathToMedia = $("#url").html();
    var mood = $("#mood_comm").val();
    nbc.omniture.userVote = mood;
    U.log("nbc.omniture.moodvote "+nbc.omniture.userVote);
    G.doPixelTracking(8);
    var content_id = $("#cid").html();
    var section_id = $("#sid").html();
    var existingmood = $("#existingmood").val();
    U.log("count 0 " + nbc.feelingArray[0].count);
    if (nbc.feelingArray[0].count != "$item.getString('count')") {
	    updateUserViewFromComm();
	    updateViewWithVoted(nbc.omniture.userVote);
	    var moodUrl1 = escape(document.URL);
	    U.createCookie("moodSet", "alreadyVoted", 365);
        var moodUrl1 = escape(document.URL);
        U.createCookie("moodURL", moodUrl1, 365);
        U.createCookie("moodValue", nbc.omniture.userVote, 365);
        var moodreadURL3 = U.readCookie('moodURL');
        U.log("cookieURL after setting "+unescape(moodreadURL3));
        $(".moodResult li").each(function() {
            $(this).css("cursor","default");
        });
	    $("#commentsForm").css("opacity","1");   
	    $("#processing_comm").hide();  
	   //if url hasn't been tagged with a mood yet.
	    if ( existingmood == null || existingmood == undefined || existingmood == ''){
	      var url = 'http://' + UrlArr[2] + '/i/dispatcher/?h=mood&action=addtag&tag='+mood+'&contid='+nbc.cmts.contentId+'&sectid='+nbc.cmts.sectionId+'&r='+(Math.round(Math.random()*1000000));
	     $.ajax({
	       type: "GET",
	       url: url,
	       dataType: "html",
	       success: function(data) {
	            
	       }
	     }); 
	   }
    }  // if kickapps reponds    
 }
 
  
function jsObjectSortingTest() {
      nbc.feelingArray.sort(sortFeelingHandler);
     // alert("feeling list after sorting in 'descending' order");  
      displayFeelingList();
  }

function sortFeelingHandler(thisObject,thatObject) {
  if (thisObject.number > thatObject.number)
  {
      return -1;
  }
  else if (thisObject.number < thatObject.number)
  {
      return 1;
  }
  return 0;
}

function displayFeelingList() {
   for(var i=0; i < nbc.feelingArray.length; i++) {
  }
}


function updateUserView() { 
  var str = "";
  var sum = 0;
  var j=0;
  var k =0;
  var sum = 0;
  var j=0;
  var k =0;        
  var cntId = $("#cid").html();
  var snId = $("#sid").html();
  /* var urlText = snId+"_"+ cntId;
  var UrlArray = window.location.href.split('/'); // gets domain        
  var getUrl = 'http://' + UrlArray[2] + '/i/dispatcher/?action=getcontentdata&h=mood&url='+urlText;
  jQuery.ajax({
    type: "GET",
    url: getUrl,
    success: function(data) {
     // alert(data);
      $(".moodSelect").attr("disabled","disabled");
      $("#listMoods li").find("input").each(function() {
      $(this).attr("disabled","disabled");
      });
    }
  }); */
    
  $(".moodSelect").attr("disabled","disabled");
  if ($("#listMoods").attr("id")) {
	  $("#listMoods li").find("input").each(function() {
	  $(this).attr("disabled","disabled");
	  });
  }
  $(".moodResult li").each(function() {
      $(this).css("cursor","default");
  }); 
  
  $(".moodResult li").each(function () {
      nbc.feelingArray[k].mood = $(this).find(".feeling").find("span").text();
      nbc.feelingArray[k].percent = parseInt($(this).find(".percent").text());
      nbc.feelingArray[k].number =parseInt($(this).find(".number").text());
      k++;
  });

  var getIndex=0;
  var getMood ="";
  $(".moodSelect option:selected").each(function () {
     var getIndex=0;
     getMood= $(this).text();
     str += $(this).text() + " ";
     getIndex= parseInt($(this).val());
     for (i=0; i<nbc.feelingArray.length; i++) {
        if (nbc.feelingArray[i].mood == getMood) {
          U.log("nbc.feelingArray[i].mood "+nbc.feelingArray[i].mood+" i "+i);
           ++nbc.feelingArray[i].number;
        }
     } 
  })  
  
  for (i=0; i<nbc.feelingArray.length; i++) {
      sum+=parseInt(nbc.feelingArray[i].number);
  }
  var onePercent=0;
  if (sum==0) {onePercent = 0;}
  else {onePercent = 100/sum;}
  for (i=0; i<nbc.feelingArray.length; i++) {
       nbc.feelingArray[i].percent= Math.round(onePercent*nbc.feelingArray[i].number);
  }
  jsObjectSortingTest(); 
     //  $("div.text").text(str);
  changeTable();
}
 
function updateUserViewFromComm() { 
  var str = "";
  var sum = 0;
  var j=0;
  var k =0;         
  var cntId = $("#cid").html();
  var snId = $("#sid").html();
  /* var urlText = snId+"_"+ cntId;
  var UrlArray = window.location.href.split('/'); // gets domain        
  var getUrl = 'http://' + UrlArray[2] + '/i/dispatcher/?action=getcontentdata&h=mood&url='+urlText;
  jQuery.ajax({
    type: "GET",
    url: getUrl,
    success: function(data) {
     // alert(data);
      $("#listMoods li").find("input").each(function() {
      $(this).attr("disabled","disabled");
      });
      $(".moodSelect").attr("disabled","disabled");
    }
  }); */
       
	  $("#listMoods li").find("input").each(function() {
	    $(this).attr("disabled","disabled");
	  });
 

  $(".moodSelect").attr("disabled","disabled");
  $(".moodResult li").each(function() {
      $(this).css("cursor","default");
  });
    
  $(".moodResult li").each(function () {
    nbc.feelingArray[k].mood = $(this).find(".feeling").find("span").text();
    nbc.feelingArray[k].percent = parseInt($(this).find(".percent").text());
    nbc.feelingArray[k].number =parseInt($(this).find(".number").text());
    k++;
  });
    var getIndex=0;
    var getMood ="";
    $("#listMoods li").find("input[@name='mood_comm']:checked").each(function () {
       var getIndex=0;
       getMood= $(this).val();
       U.log("selected "+ getMood);
       getIndex= $("#listMoods li").index($(this).parent());
       U.log("index of "+ getMood + getIndex);
       for (i=0; i<nbc.feelingArray.length; i++) {
             if (nbc.feelingArray[i].mood == getMood) {
                ++nbc.feelingArray[i].number;
             }
       }
     })  
     for (i=0; i<nbc.feelingArray.length; i++) {
         sum+=parseInt(nbc.feelingArray[i].number);
     }
     var onePercent=0;
     if (sum==0) {onePercent = 0;}
     else {onePercent = 100/sum;}
     for (i=0; i<nbc.feelingArray.length; i++) {
         nbc.feelingArray[i].percent= Math.round(onePercent*nbc.feelingArray[i].number);
     }
     jsObjectSortingTest(); 
     changeTable();
}

function updateUserViewFromInterPage(selectedMood) { 
	  var str = "";
	  var sum = 0;
	  var j=0;
	  var k =0;         
	  var cntId = $("#cid").html();
	  var snId = $("#sid").html();
	  if ($("#listMoods").attr("id"))  {         
		 $("#listMoods li").find("input").each(function() {
		    $(this).attr("disabled","disabled");
		  });
	  }	 
	  $(".moodSelect").attr("disabled","disabled"); 
	  $(".moodResult li").each(function() {
	        $(this).css("cursor","default");
	  });
	    
	  $(".moodResult li").each(function () {
	    nbc.feelingArray[k].mood = $(this).find(".feeling").find("span").text();
	    nbc.feelingArray[k].percent = parseInt($(this).find(".percent").text());
	    nbc.feelingArray[k].number =parseInt($(this).find(".number").text());
	    k++;
	  });
	    var getIndex=0;
	    var getMood ="";
	    var getIndex=0;
		getMood= selectedMood;
	    U.log("selected "+ getMood);
	       for (i=0; i<nbc.feelingArray.length; i++) {
	             if (nbc.feelingArray[i].mood == getMood) {
	                ++nbc.feelingArray[i].number;
	             }
	       }
	     
	     for (i=0; i<nbc.feelingArray.length; i++) {
	         sum+=parseInt(nbc.feelingArray[i].number);
	     }
	     var onePercent=0;
	     if (sum==0) {onePercent = 0;}
	     else {onePercent = 100/sum;}
	     for (i=0; i<nbc.feelingArray.length; i++) {
	         nbc.feelingArray[i].percent= Math.round(onePercent*nbc.feelingArray[i].number);
	     }
	     //jsObjectSortingTest(); 
	    // changeTable();
	     var i=0;
	     $(".moodResult li").each(function() {
	       $(this).find('.feeling').addClass(nbc.feelingArray[i].mood);
	       $(this).find('.score').addClass("score_"+nbc.feelingArray[i].mood);
	       $(this).find(".percent").text(nbc.feelingArray[i].percent+"");
	       $(this).find(".number").text(Math.round(nbc.feelingArray[i].number)+"");
	        i++;
	      });
	}
	
function updateUserViewFromInter(selectedMood) { 
	  var str = "";
	  var sum = 0;
	  var j=0;
	  var k =0;         
	  var cntId = $("#cid").html();
	  var snId = $("#sid").html();
	          
	 /* $("#listMoods li").find("input").each(function() {
	    $(this).attr("disabled","disabled");
	  }); */
	  $(".moodSelect").attr("disabled","disabled"); 
	  $(".moodResult li").each(function() {
	        $(this).css("cursor","default");
	  }); 
	    U.log("in inter update");
	  $(".moodResult li").each(function () {
	    nbc.feelingArray[k].mood = $(this).find(".feeling").find("span").text();
	    nbc.feelingArray[k].percent = parseInt($(this).find(".percent").text());
	    nbc.feelingArray[k].number =parseInt($(this).find(".number").text());
	    U.log(nbc.feelingArray[k].number);
	    k++;
	  });
	  
	    var getIndex=0;
	    var getMood ="";
	    var getIndex=0;
		getMood= selectedMood;
	    U.log("selected "+ getMood);
	       for (i=0; i<nbc.feelingArray.length; i++) {
	             if (nbc.feelingArray[i].mood == getMood) {
	                ++nbc.feelingArray[i].number;
	             }
	       }
	     
	     for (i=0; i<nbc.feelingArray.length; i++) {
	         sum+=parseInt(nbc.feelingArray[i].number);
	     }
	     var onePercent=0;
	     if (sum==0) {onePercent = 0;}
	     else {onePercent = 100/sum;}
	     for (i=0; i<nbc.feelingArray.length; i++) {
	         nbc.feelingArray[i].percent= Math.round(onePercent*nbc.feelingArray[i].number);
	     }
	     jsObjectSortingTest(); 
	     changeTableInter();
	     var i=0;
	}

function changeTableInter() {
	var i=0;
	$(".moodResult li").each(function() {
	// alert(feelingList[i].mood+feelingList[i].percent+feelingList[i].number);
	  $(this).find(".feeling").find("span").text("");
	  $(this).find(".percent").text("");
	  $(this).find(".number").text("");
	  $(this).find('.feeling').attr("class","feeling");
	  U.log(nbc.feelingArray[i].mood);
	  $(this).find('.feeling').find("span").text(nbc.feelingArray[i].mood);
	  $(this).find('.feeling').addClass(nbc.feelingArray[i].mood);
	  $(this).find('.score').addClass("score_"+nbc.feelingArray[i].mood);
	  $(this).find(".percent").text(nbc.feelingArray[i].percent+"");
	  $(this).find(".number").text(Math.round(nbc.feelingArray[i].number)+"");
	   i++;
	 });
}

function changeTable() {
var i=0;
$(".moodResult li").each(function() {
// alert(feelingList[i].mood+feelingList[i].percent+feelingList[i].number);
  $(this).find(".feeling").find("span").text("");
  $(this).find(".percent").text("");
  $(this).find(".number").text("");
  $(this).find('.feeling').attr("class","feeling");
  $(this).find('.score').attr("class","score");
  U.log(nbc.feelingArray[i].mood);
  $(this).find('.feeling').find("span").text(nbc.feelingArray[i].mood);
  $(this).find('.hover_state_mood_section').find('span.voted').attr("class","voted");
  $(this).find('.hover_state_mood_section').find('span.voted').addClass("hover_voted_"+nbc.feelingArray[i].mood);
  $(this).find('.hover_state_mood_section_justVoted').find('span.voted').attr("class","voted");
  $(this).find('.hover_state_mood_section_justVoted').find('span.voted').addClass("hover_already_voted_"+nbc.feelingArray[i].mood);
  $(this).find('.feeling').addClass(nbc.feelingArray[i].mood);
  $(this).find('.score').addClass("score_"+nbc.feelingArray[i].mood);
  $(this).find(".percent").text(nbc.feelingArray[i].percent+"");
  $(this).find(".number").text(Math.round(nbc.feelingArray[i].number)+"");
   i++;
 });
 }

 $(document).ready(function() { 
    // $(".moodSelect").attr("disabled",false);
  // alert($(".moodSelect").attr("disabled"));
    $("#listMoods li").find("input").each(function() {
     // $(this).attr("disabled",false);
  });
}); 
