// MTVNI_EMA_Cogix: Cogix wrap around code
function MTVNI_COGIX() {  
  
};

MTVNI_COGIX.prototype.init = function() {
  // Always init when new object is created, call by ".add"
  var scriptfolder = this.getBaseObject().getScriptFolder();
  this.setRemoveIeMemory(true);		// set as default
  document.write('<script language="javascript" type="text/javascript" src="'+scriptfolder+'/mtvn/js/dependencies.js?lang="'+this.getBaseObject().getLanguageCode()+' id="MTVN-dependencies"></script>');
  document.write('<script language="javascript" type="text/javascript" src="'+scriptfolder+'/mtvn/js/core.js" id="MTVN-core"></script>');	
  document.write('<script language="javascript" type="text/javascript" src="'+scriptfolder+'/cogix/js/core_11_06_2009.js" id="MTVN_Cogix-core"></script>');
  this.getParams()._pollsList = [];
  this.getParams()._pollsAnswer = [];
};

MTVNI_COGIX.prototype.createButton = function(params) {
  try {
	MTVN_Cogix.addPoll(params.pollid);	// Calling mtvn/cogix object to check if poll was already registered
	
	// Initializing swf object
	var pollId = params.pollid;
	var value = params.value;	
	var listid = pollId+":"+value;
	var pollObj = this.getPollObject(listid);
	
	if ((!pollObj) || ( (pollObj) && (pollObj.deleted == false) ) ) {
	  var targetId = params.targetid;
	  var oparams = { allowScriptAccess: "sameDomain", wmode: "transparent" };	
	  var attributes = { id: targetId, name: targetId};			
	  var flashvars = MTVN_Cogix.VoteButton.params;
	  flashvars.pollid = pollId;
	  flashvars.answer = value;		  	  
	  swfobject.embedSWF(MTVN_Cogix.VoteButton.src, targetId, MTVN_Cogix.VoteButton.width, MTVN_Cogix.VoteButton.height, MTVN_Cogix.VoteButton.flashVersion, MTVN_Cogix.VoteButton.expressInstallSwfUrl, flashvars, oparams, attributes, params.funcCallback);
	  
  	  // Storing current poll object		
	  this.setPollObject(listid, {pollid: pollId, value: value, targetid: targetId, css: params.css, deleted: false, funcDenied: params.funcDenied, funcSuccess: params.funcSuccess});
	  this.addPollAnswers(pollId, value);
	};
  } catch (e){
	
  };
};

MTVNI_COGIX.prototype.deleteButton = function(listid, deletePoll) {
  try {
    var pollObj = mtvni.ema.cogix.getPollObject(listid);
    if ((!pollObj) || ( (pollObj) && (pollObj.deleted == false) ) ) {
      if ((this.getRemoveIeMemory()) && (jQuery.browser.msie)) this.removeFlashLeakInIE(pollObj.targetid);
      obj = document.getElementById(listid);   
      if (obj) obj.innerHTML = "<div id='"+pollObj.targetid+"' class='"+(pollObj.css ? pollObj.css : "")+"'></div>";
      if (deletePoll) pollObj.deleted = true;  // Officialy lock vote button for current poll to this html      
    };
  } catch(e) {
	
  };
};

MTVNI_COGIX.prototype.setPollsList = function(params) {
  var pollList = this.getParams()._pollsAnswer;
  var list = params.pollslist;
  var length = list.length;
  var item = null;
  var listid = null;
  var targetid = null;
  
  for (var x=0; x<length; x++) {
	item = list[x];
	for (var y=0; y<item.answers.length; y++) {
	  
	  if ((item.answers[y] != "") && (item.answers[y] != 0) && (item.answers[y] != 9999)){
	  // Create vote button if value is not set to "0"
	    listid = item.pollid+":"+item.answers[y];
	    targetid = "voteButton-"+item.pollid.replace("!", "-")+"-"+item.answers[y];
  	    this.setPollObject(listid, {pollid: item.pollid, value: item.answers[y], targetid: targetid, deleted: false, funcDenied: params.funcDenied, funcSuccess: params.funcSuccess});
	    this.addPollAnswers(item.pollid, item.answers[y]);
	  };
	};
  };
};

MTVNI_COGIX.prototype.addPollAnswers = function (id, value) {
  var list = this.getParams()._pollsAnswer[id];
  if (!list) {
    this.getParams()._pollsAnswer[id] = [];
    list = this.getParams()._pollsAnswer[id];
  };
  
  list.push(value);
};

MTVNI_COGIX.prototype.setPollObject = function(id, obj) { this.getParams()._pollsList[id] = obj; }; 
MTVNI_COGIX.prototype.getPollObject = function(id) {  return this.getParams()._pollsList[id]; };

MTVNI_COGIX.prototype.setCurrentPollListId = function(value) { this.getParams().currentPollListId = value; };
MTVNI_COGIX.prototype.getCurrentPollListId = function() { return this.getParams().currentPollListId; };

MTVNI_COGIX.prototype.setCurrentPollId = function(value) { this.getParams().currentPollId = value; };
MTVNI_COGIX.prototype.getCurrentPollId = function() { return this.getParams().currentPollId; };

MTVNI_COGIX.prototype.preSubmit = function (vars) {
  // Retrieving pollid
  vars = unescape(vars);
  var pollId = "";
  var params = vars.split("&");
  for (var i=0; i<params.length; i++) {
    var pair = params[i].split("=");
	if(pair[0]=="pollid"){
	  pollId = pair[1];	  
	} else if (pair[0]=="answer"){
	  value = pair[1];
	};
  };
  
  var cogix = mtvni.ema.cogix;
  var currentPoll = MTVN_Cogix.PollRegistry[pollId];
  this.setCurrentPollListId(pollId+":"+value);  
  this.setCurrentPollId(pollId);
  try {
	  if(currentPoll.isLoginRequired){	// not logged in
		cogix.onNotLoggedIn();
		return false;
	  }	else{
		var cookieVal = MTVN.Util.Cookies.read(currentPoll.id);
		if(cookieVal == null){	// not voted yet
		  MTVN.Util.Cookies.create(currentPoll.id, "1", currentPoll.cookieDuration);
		  return true;
		} else{ 
		  if(currentPoll.isLimited){
			if(parseInt(cookieVal) < currentPoll.voteLimit){	// check vote limit
			  cookieVal = parseInt(cookieVal) + 1;
			  MTVN.Util.Cookies.create(currentPoll.id, cookieVal, currentPoll.cookieDuration);
			  return true;
			} else{ 
			// already voted
			  cogix.onVoteDenied();
			  return false;
			};
		  } else{		  
			return true;
		  };
		};
	  };
  } catch(e) {
	 
  };
};

MTVNI_COGIX.prototype.deleteCurrentPoll = function(deletePoll, pollid) {
  try {
	  pollid = this.getCurrentPollId() ? this.getCurrentPollId() : pollid;  
	  var list = this.getParams()._pollsAnswer[pollid];
	  var length = list.length;
	  var listid = "";

	  for (var x=0; x<length; x++) {
		listid = pollid+":"+list[x];
		this.deleteButton(listid, deletePoll);  			
	  };
  } catch(e) {
	  
  };
};

MTVNI_COGIX.prototype.displayCurrentPollMessage = function(msg, type) {
  try {
	var listid = this.getCurrentPollListId();
	var pollObj = this.getPollObject(listid);
	var displayMessage = MTVN.Dictionary.getPhrase(msg).replace(/"/gi, "'");
	var html = "";
	
	switch(type) {
	  case "denied": html = (pollObj.funcDenied) ? pollObj.funcDenied(displayMessage) : displayMessage; break;
	  case "success": html = (pollObj.funcSuccess) ? pollObj.funcSuccess(displayMessage) : displayMessage; break;
	};
	
	// Switch message
	var obj = document.getElementById(pollObj.targetid); 	
	obj.innerHTML = html;
	
	// Switching parent div	
	obj = document.getElementById("parent-"+pollObj.targetid); 		
	if (obj) {  		
	  jQuery(obj).addClass("hasVoted");
	  jQuery(obj).parent(".voteCategory").addClass("categoryVoted");
	};
  } catch(e) {
	
  };
};

MTVNI_COGIX.prototype.setRemoveIeMemory = function(value) { this.getParams()._ieRemove = value; };
MTVNI_COGIX.prototype.getRemoveIeMemory = function() { return this.getParams()._ieRemove; };

// NOT IN "this" object. Must use "mtvni.ema.cogix.*" as reference
MTVNI_COGIX.prototype.postSubmit = function() {
  try {
	mtvni.ema.cogix.deleteCurrentPoll(false, null);	
	mtvni.ema.cogix.displayCurrentPollMessage('VotingThanksCopy', 'success');  
  } catch(e) {
	
  };
};

MTVNI_COGIX.prototype.onNotLoggedIn = function(){
  
};

MTVNI_COGIX.prototype.onVoteDenied = function(){
  try {
	mtvni.ema.cogix.deleteCurrentPoll(true, null);
	mtvni.ema.cogix.displayCurrentPollMessage('VoteDeniedHeader', 'denied');
  } catch(e) {
	
  };
};

MTVNI_COGIX.prototype.onLoadComplete = function(){
  try {
	
  } catch(e) {
	
  };
};
