function MTVNI_EMA_VOTES() {
  this.categoriesList = [];
  this.previousCategory = null;
  this.hideIntroFlag = false;
  this.hasIntroFlag = false;
  this.lockclick = true;
};

MTVNI_EMA_VOTES.prototype.init = function() {
// Always call by .add
  try {
	var html = jQuery("#divid_votes_intropage").html();
	if (html != "") {
    // Only render if votes intro exist
      var introPage = this.formatIntroPageDiv(html);
	  jQuery("#"+this.getParams().displayDiv).append(introPage);
	  this.hasIntroFlag = true;
	};
	
	var regional = mtvni.ema.getGlobalVariable("vote_regional_json");
	var top5Override = mtvni.ema.getGlobalVariable("vote_top5_json");
	if (regional && (regional.votecategory.length > 0) ) {
		this.preloadCategories(regional, "regional");
	} else {
		this.preloadCategories(top5Override, "top5Override");
	}
	this.preloadCategories(this.getParams().json, null);
	  
	self.setTimeout('mtvni.ema.votes.unlockClick()',1000);	// Delay unlock click on category navigation	
  } catch(e) {
  // Error
	
  };
};

MTVNI_EMA_VOTES.prototype.preloadCategories = function(json, type) {
// Preloading Categories
  try {
    var list = json.votecategory;
    var length = list.length;
    var firstCategoryId = null;
    var category = null;
    for (var x=0; x<length; x++) {
	  category = this.preloadCategory(list[x], type);	  
	  if (category) {
	    this.categoriesList.push(category);
	  };
    };
  } catch(e) {
	
  };
};

MTVNI_EMA_VOTES.prototype.preloadCategory = function(category, type) {
  var obj = null;
  if ((category.id.substring(0, 13) != "ema_09!region") || (category.id == "ema_09!region_arabia")) {
    obj = {};  
    obj.id = category.id;
    obj.type = type;
    category.divid = category.id.replace(/!/, "-");
  
    obj.params = category;
    jQuery("#"+this.getParams().categoriesDiv).append(this.formatCategoryDiv(category, type));
    jQuery("#"+this.getParams().displayDiv).append(this.formatDisplayCategory(category, type));
  };
  return obj;
};

// vote left nav
MTVNI_EMA_VOTES.prototype.formatCategoryDiv = function(category, type) {
  var maxlength = "null";  
  /* Add type switch case if want to display limited max artists
  switch(type) {
    case "regional": maxlength = "5"; break;
    default: maxlength = "null"; break;
  };
  */
  var  html = "<li class='"+this.getParams().categoryCss+"' id='voteCategoryDivId"+category.divid+"'>" 
	  		  +"<h3><a href=\"javascript:a();\" onclick=\""+this.getHTMLReference()+".showCategory('"+category.id+"', "+maxlength+")\">"+category.shortDescription+"</a></h3>"
	  		  +( (category.description != "") ? "<p class=\"voteDescription\">"+category.description+"</p>" : "" )
	  		+"</li>";
  return html;
};

MTVNI_EMA_VOTES.prototype.getCategory = function(value, type) {
  var list = this.categoriesList;
  var length = list.length;
  for (var x=0; x<length; x++) {
	switch(type) {
	  case "urlalias": if (list[x].params.urlalias == value) return list[x]; break;
	  default: if (list[x].id == value) return list[x]; break;
	};
  };
  return null;
};

MTVNI_EMA_VOTES.prototype.hideCategoryArtists = function(category, maxlength) {  
  var artists = (category.artist) ? category.artist : category.artists;
  var length = artists.length;
  if (!maxlength) maxlength = length; 
	  
  for (var x=0; x<length; x++) {
    if (x < maxlength) jQuery("#artist-"+category.divid+"-"+x).css("display", ""); 
    else jQuery("#artist-"+category.divid+"-"+x).css("display", "none");
  };
};

MTVNI_EMA_VOTES.prototype.showCategory = function(id, maxlength) {
  try {
	if (this.lockclick == false) {
	    var category = this.getCategory(id);    
	    if (!this.hideIntroFlag) {
	    // Hide intro page
	      this.hideIntroPage();      
	    };
	    
	    if ((this.previousCategory) && (this.previousCategory.id != id)) {
	   	// Hide and set new previous category
	      this.hideCategory(this.previousCategory);      
	    };
	    
	    // Check if regional and reload html base on full or top5
	    if (category.type == "regional") {	    
	      // CSS toggle

		  if (!maxlength) { 
			jQuery("#votePageInner").addClass("extendedPage");
		  }	    	
		  
		  // HTML restructure
	      var artistHtml = "";	      
	      if (maxlength) {
	    	var top5 = mtvni.ema.getGlobalVariable("vote_top5_json");
	    	if (top5) params = top5.votecategory[0]; 
	    	else params = category.params;
	    	artistHtml = this.formatDisplayCategoryArtists(params, "regional");
	      } else {
	        artistHtml = this.formatDisplayRegionalCategoryArtists(category.params);
	      };
	      
	      var divid = "#voteArtistDivId"+category.params.divid;
	      jQuery(divid).html(artistHtml);
	      
	      // TODO: need to fix hack to force display on init load
	      this.setCurrentCategory(category);  
	      this.showCategoryOnComplete(maxlength);	// Display buttons  
	    };
	    
	    // Check max length to display
	    this.hideCategoryArtists(category.params, maxlength);
	    
	    // Display current category
	    if ( (!this.previousCategory) || ( (this.previousCategory) && (this.previousCategory.id != id) ) ) {  
	      this.setCurrentCategory(category);       
	      if (category.type != "regional") this.showCategoryOnComplete(maxlength);	// Display buttons      
	      this.animateDiv("#voteDisplayDivId"+category.params.divid, null);      
	      jQuery(".voteNav").removeClass("selected");
	      // safari doesnt support the speed param...
	      if(jQuery.browser.msie || jQuery.browser.mozilla){
	    	  jQuery("#voteCategoryDivId"+category.params.divid).addClass("selected", "slow");
	      } else  {
	    	  jQuery("#voteCategoryDivId"+category.params.divid).addClass("selected");
	      };
	    };
	    
	    this.previousCategory = category;
	};
  }	catch(e) {
	
  };
};

MTVNI_EMA_VOTES.prototype.showCategoryOnComplete = function(maxlength) {	
  try {
	// Outside current object "this" parameter
	var category = mtvni.ema.votes.getCurrentCategory();
	var artists = (category.params.artist) ? category.params.artist : category.params.artists;
	var length = artists.length;	
	var timer = 0;
	for (var x=0; x<length; x++) {	
	  timer = 150 * (x+.1);
	  self.setTimeout('mtvni.ema.votes.cascadeVoteLoad('+x+', '+maxlength+')', timer);
    };	  
  } catch(e) {
	
  };
};

MTVNI_EMA_VOTES.prototype.cascadeVoteLoad = function(index, maxlength) {
  try {
	  var category = this.getCurrentCategory();	    
	  var artists = (category.params.artist) ? category.params.artist : category.params.artists;
	  
	  var params = {};
	  params.css = mtvni.ema.votes.getParams().voteCss;
	  params.refresh = mtvni.ema.votes.getParams().voteRefreshSwf;
	  params.pollid = category.id;
	  params.value = artists[index].voteid; 
	  
	  if ((params.value) && (params.value != 0) && (params.value != 9999)) {
	  // Only create button if it has an answer id
	    params.targetid = mtvni.ema.votes.formatVoteTargetId(params.pollid, params.value);
	    params.funcDenied = function(msg) {
		  				// TODO: Needs styling
		  				//Style end-slates after invoking vote action		  			
		  				var html = ""+msg+"";		  				
		  				return html;
	  				};
	  				
	  	params.funcCallback = function(params) {
	  		
	  	};
	  	
	  	switch(category.type) {
	  	  case "regional":
			// SWF button config
			if (!maxlength) {
			  params.funcSuccess = function(msg) {
	  				var html = "<img src='/scripts/cogix/images/votes/thankyou_75x75/thankyou_75x75_"+mtvni.ema.getGlobalVariable("region")+".png'/>";
	  				return html;
			  };
			  MTVN_Cogix.VoteButton.params.config = mtvni.ema.getGlobalVariable("vote_regional_xml").xml;
			  MTVN_Cogix.VoteButton.height = mtvni.ema.getGlobalVariable("vote_regional_xml").height;
			  MTVN_Cogix.VoteButton.width = mtvni.ema.getGlobalVariable("vote_regional_xml").width;
			} else {				
			  params.funcSuccess = function(msg) {
					var html = "<img src='/scripts/cogix/images/votes/thankyou_105x105/thankyou_105x105_"+mtvni.ema.getGlobalVariable("region")+".png'/>";
					return html;
			  };
			  MTVN_Cogix.VoteButton.params.config = mtvni.ema.getGlobalVariable("vote_category_xml").xml;
			  MTVN_Cogix.VoteButton.height = mtvni.ema.getGlobalVariable("vote_category_xml").height;
			  MTVN_Cogix.VoteButton.width = mtvni.ema.getGlobalVariable("vote_category_xml").width;
			};
	  		break;
	  	  default:
	  		params.funcSuccess = function(msg) {
				var html = "<img src='/scripts/cogix/images/votes/thankyou_105x105/thankyou_105x105_"+mtvni.ema.getGlobalVariable("region")+".png'/>";
				return html;
			};
			
			// SWF button config
			MTVN_Cogix.VoteButton.params.config = mtvni.ema.getGlobalVariable("vote_category_xml").xml;
			MTVN_Cogix.VoteButton.height = mtvni.ema.getGlobalVariable("vote_category_xml").height;
			MTVN_Cogix.VoteButton.width = mtvni.ema.getGlobalVariable("vote_category_xml").width;
	  		break;
	  	};
	  	
	    mtvni.ema.cogix.createButton(params);	
	  };  
  } catch(e) {
	 
  };
};

MTVNI_EMA_VOTES.prototype.formatVoteTargetId = function(pollid, voteid) {
  return "voteButton"+pollid + "-" + voteid;
};

MTVNI_EMA_VOTES.prototype.hideCategory = function(category) {
  try {
    mtvni.ema.cogix.deleteCurrentPoll(false, category.id);
    var displayid = "#voteDisplayDivId"+category.params.divid;
    this.setCurrentHideId(displayid);
    jQuery(displayid).animate({opacity: 0}, 250, "linear", this.hideCategoryOnComplete);    
  } catch(e){
	
  };
};

MTVNI_EMA_VOTES.prototype.hideCategoryOnComplete = function() {
  jQuery(mtvni.ema.votes.getCurrentHideId()).css("display", "none");	
};

MTVNI_EMA_VOTES.prototype.setCurrentHideId = function(value) { this.getParams().hideCategoryId = value; };
MTVNI_EMA_VOTES.prototype.getCurrentHideId = function() { return this.getParams().hideCategoryId; };

MTVNI_EMA_VOTES.prototype.formatDisplayCategory = function(category, type) {
  var artistsHtml = "";
  var headerHtml = "";
  switch(type) {
    case"regional":
      headerHtml = this.formatDisplayRegionalCategoryArtistsHeader(category);
      artistsHtml = this.formatDisplayRegionalCategoryArtists(category);
      break;
    case"top5Override":
        artistsHtml = this.formatDisplayCategoryArtists(category,type);
        break;
    default:
      artistsHtml = this.formatDisplayCategoryArtists(category);
      break;
  };
	
  	var html = "<div id='voteDisplayDivId"+category.divid+"' style='display:none;position:absolute;top:0;left:0;'>"
  		 + headerHtml
		 + "<div class=\"promoList\" id='voteArtistDivId"+category.divid+"'>"
		 + artistsHtml
		 + "</div>"
	  +"</div>";
  
  return html;  
};

MTVNI_EMA_VOTES.prototype.formatImagePath = function(src, width){
  var path = null;
  if (src.indexOf("/ema-2009") != -1) {
	path = src.replace("/ema-2009", this.getParams().imagePath)+"?width="+width;  
  } else {
	path = src;
  };
  
  return path;
};

MTVNI_EMA_VOTES.prototype.formatDisplayRegionalCategoryArtistsHeader = function(category) {
	/* comment out top 5/full list switcher for initial launch 09/08/2009 */
	/**/
    var header = "<div id=\"regionalVoteNavBar\">"
		  +"<a id=\"voteFull\" class=\""+category.id+" \" href=\"#\" >" 
		  +mtvni.ema.getGlobalVariable("vote_regional_link_full")
		  +"</a>"
		  +" | " 
		  +"<a id=\"voteTop5\" class=\""+category.id+"\" href=\"#\" >"
		  +mtvni.ema.getGlobalVariable("vote_regional_link_top5")
		  +"</a>"
		  +"</div>";
	/**/
	/*
	var header = "";
	*/  
	return header;
};

MTVNI_EMA_VOTES.prototype.formatDisplayRegionalCategoryArtists = function(category) {
	  var html = "";
	  var artists = (category.artist) ? category.artist : category.artists;
	  var length = artists.length;
	 // var thumbWidth = this.getParams().thumbWidth;
	  var thumbWidth = 100; // hardcoded now - full list. DA 
	  var regionalThumbWidth = this.getParams().regionalThumbWidth;
	  
	  for (var x=0; x<length; x++) {
		var artist = artists[x];
		
		var itemName = artist.title;
		var itemDescription = artist.description;
		var artistImage = (artist.image && artist.image.src) ? artist.image.src : "";
		
		var votevalue = artist.voteid.replace(/\"/g, "");
	    var voteData = votevalue.split("|");
		if (voteData.length > 1) {
		  artist.voteid = voteData[0];
		  artist.flag = voteData[1];		  
		};
		
		if (artist.voteid == 9999) artist.winner = 1;
		var winnerClass = "";
		var winnerFlag = "";
		if (artist.winner == 1){
			winnerClass = "voteWinner";
			winnerFlag = "<span class=\"winner\"></span>";
		}

		// full list - smaller thumbs, different format
	    html += "<div id=\"artist-"+category.divid+"-"+x+"\" class=\"row row" + thumbWidth + " " + winnerClass + "\" >"
				+"<div class=\"innerWrap clearAfter\">"
	    		+"<div class=\"thumbcontainer thumb" + thumbWidth + "\"><img src=\"" +this.formatImagePath(artistImage, thumbWidth)+ "\" class=\"thumbnail\" alt=\""+itemName+"\" /><span class=\"overlay\"></span>" + winnerFlag + "</div>"
				+"<div class=\"meta\">"
				+ "<span title=\"" + artist.flag + "\" class=\"flag " + artist.flag + "\" >" + artist.flag + "</span>"
				 +"<h3>"+itemName+"</h3>"
				  +"<p class=\"short-description\">"
				  	+artist.shortDescription
				  +"</p>"			  
				+"</div>"
				+ this.addPollVote(category.id, artist.voteid)
				+ this.buildMoreLink(artist.urlAlias)
				+"</div>"
		    +"</div>";

	  };

	  jQuery("#regionalVoteNavBar a").removeClass("voteSelected");
	  jQuery("#voteFull").addClass("voteSelected");
	 
	  return html;
};


/* DA */
jQuery(document).ready(function(){
	jQuery("#regionalVoteNavBar a").bind("click", function(e){
		var $this = jQuery(this);
		var $categoryId = $this.attr("class");
		var $itemId = $this.attr("id");

		e.preventDefault();

		if (!($this.hasClass("voteSelected"))){

			jQuery("#regionalVoteNavBar a").removeClass("voteSelected");
		
			if ($itemId == "voteTop5") { 
				var $switchParam = 5;
			}
			else if ($itemId == "voteFull") { 
				var $switchParam = null;
				jQuery("#votePageInner").addClass("extendedPage");
			}

			mtvni.ema.votes.showCategory($categoryId, $switchParam);
			$this.addClass("voteSelected");
		
		}

	});
});



MTVNI_EMA_VOTES.prototype.formatDisplayCategoryArtists = function(category, type) {
  var html = "";
  var artists = (category.artist) ? category.artist : category.artists;
  var length = artists.length;
  var thumbWidth = this.getParams().thumbWidth;
  var regionalThumbWidth = this.getParams().regionalThumbWidth;

  // Regional restriction
  if (type == "regional") {
	if (length > 5) length = 5;
  };
  
  for (var x=0; x<length; x++) {
	var artist = artists[x];
	
	var itemName = artist.title;
	var itemDescription = artist.description;
	var artistImage = (artist.image && artist.image.src) ? artist.image.src : "";
	var buildmorelink = true;
	
	artistalias = artist.urlalias;
	if (category.urlalias.substr(0, 13) == "vote_regional") {
		buildmorelink = false;
	};
	if ((type == "regional") || (type == "top5Override"))  {
		buildmorelink = true;
		artistalias = artist.urlAlias;
		
		var votevalue = artist.voteid.replace(/\"/g, "");
	    var voteData = votevalue.split("|");
		if (voteData.length > 1) {
		  artist.voteid = voteData[0];
		  artist.flag = voteData[1];		  
		};		
	};
	
	if (artist.voteid == 9999) artist.winner = 1;
	var voteFlag = "";
	if(artist.flag) {
		voteFlag = "<span title=\"" + artist.flag + "\" class=\"flag " + artist.flag + "\" >" + artist.flag + "</span>";
	}

	var winnerClass = "";
	var winnerFlag = "";
	if (artist.winner == 1){
		winnerClass = "voteWinner";
		winnerFlag = "<span class=\"winner\"></span>";
	}
	
    html += "<div id=\"artist-"+category.divid+"-"+x+"\" class=\"row row" + thumbWidth + " " + winnerClass + "\" >"
			+"<div class=\"thumbcontainer thumb" + thumbWidth + "\"><img src=\"" +this.formatImagePath(artistImage, thumbWidth)+ "\" class=\"thumbnail\" alt=\""+itemName+"\" /><span class=\"overlay\"></span>" + winnerFlag + "</div>"
			+"<div class=\"meta\">"
			+ voteFlag
			+"<h3>"+itemName+"</h3>"
			  +"<p class=\"short-description\">"
			  	+artist.shortDescription
			  +"</p>"
			  + ( (buildmorelink == true) ? this.buildMoreLink(artistalias) : "")				  
			+"</div>"
			+ this.addPollVote(category.id, artist.voteid)
	    +"</div>";
  };
  return html;
};

MTVNI_EMA_VOTES.prototype.buildMoreLink = function(oalias) {
  var html = "";
  if (oalias) {
	var data = oalias.split("-");
	var alias = "";
	if ( (data[data.length-1] == "video") || (data[data.length-1] == "song") || (data[data.length-1] == "full") ) {
	  oalias = data[0];
	};
	
	html = "<p class=\"moreLink\">"
			+"<a href='"+mtvni.ema.getGlobalVariable("routeUrlArtist")+oalias+"'>"
			  +mtvni.ema.getGlobalVariable("voteReadMore")
			+"</a>"
		  +"</p>";
  };
   
  return html;
};

MTVNI_EMA_VOTES.prototype.addPollVote = function(pollid, voteid) {
  var html = "";  
  if (voteid) {
  	html = '<div id="'+pollid+':'+voteid+'" class="MTVN_Cogix_vote-btn">'
  				+'<div id="'+this.formatVoteTargetId(pollid, voteid)+'" class="'+this.getParams().voteCss+'">'
  					+ (swfobject.getFlashPlayerVersion() ? "" : mtvni.ema.getGlobalVariable("noSwfFound"))
  				+'</div>'
  			+'</div>';
  };
  return html;
};

MTVNI_EMA_VOTES.prototype.animateDiv = function(divid, func) {
  try {
    jQuery(divid).css("display", "");
    jQuery(divid).css("opacity", "0");
    jQuery(divid).animate({opacity: 1}, 1250, "linear", func);
  } catch(e) {
	
  };
};

MTVNI_EMA_VOTES.prototype.formatIntroPageDiv = function(html) {
  try {
    html = '<div id="MTVNI_votes_intropage" class="promoList voteIntro"  style="">'
    	  			+html
  	  		+'</div>';
  } catch (e) {
	
  };
  return html;
};

MTVNI_EMA_VOTES.prototype.showIntroPage = function() {
  this.animateDiv("#MTVNI_votes_intropage", null);  
};

MTVNI_EMA_VOTES.prototype.hideIntroPage = function() {
  this.hideIntroFlag = true;
  jQuery("#MTVNI_votes_intropage").css("display", "none");
  //jQuery("#MTVNI_votes_intropage").animate({opacity: 0}, 250, "linear", null);
};

MTVNI_EMA_VOTES.prototype.unlockClick = function() {
  this.lockclick = false;
};

MTVNI_EMA_VOTES.prototype.displayPage = function() {  
  var categoryUrlAlias = this.getGlobalVariable("vote_category_selected");
  if (categoryUrlAlias) {
	this.hideIntroPage();	
	var category = this.getCategory(categoryUrlAlias, "urlalias");
	if (category) this.showCategory(category.id);
  } else {
	
  };
};

MTVNI_EMA_VOTES.prototype.setFirstCategory = function(value) { this._categoryFirstId = value; };
MTVNI_EMA_VOTES.prototype.getFirstCategory = function() { return this._categoryFirstId; };
MTVNI_EMA_VOTES.prototype.showFirstCategory = function() { this.showCategory(this.getFirstCategory()); };
MTVNI_EMA_VOTES.prototype.setCurrentCategory = function(obj) {this._currentCategory = obj; };
MTVNI_EMA_VOTES.prototype.getCurrentCategory = function() { return this._currentCategory; };