VMA = new function(){	
this.init = function(){
if (VMA.Voting.active) VMA.Voting.init();

// append non-voting text
$j('.non-voting').parent().append('<p class="deck">[<em>This is a non-voting category</em>.] </p>');
}
}

VMA.NomineeVoting = new function(){
this.more = function(){
if(Flux.Context.isUserAuthenticated()){
window.open('/ontv/vma/2009/nominee_voting/index.jhtml?categoryId=' + VMA.Voting.categoryId, 'nominee_voting', 'height=532, width=749');
}
else{
MTVN.UI.ModalWindow.open(VMA.Voting.loginWindowUrl,null,"jquery");
return false;
}	
}
}

VMA.Utils = new function(){
this.arrayContains = function(ar, val){
var contains = false;
for(var i=0; i < ar.length; i++){ if(ar[i] == val) contains = true; }
return contains;	
}
}

VMA.Voting = new function(){
this.active = false;
this.cogix = "polling-onair";
this.validator= "timestamp|votes";
this.authClass = "com.mtvnet.auth.dis.HashDigestInputStrategy1";
this.categoryId = "";
this.projectcode = "44150";
this.style = "list";
this.thanksWindowUrl = "/ontv/vma/2009/scripts/ajax/thanks.jhtml";
this.loginWindowUrl = "/ontv/vma/2009/scripts/ajax/not_logged_in.jhtml";

this.init = function(){
$j(".vote-btn").each(function() {
var targetId = this.id;
var vars = targetId.split(":"); //get pollId and value
var pollId = vars[0];
var value = vars[1];
var embedId = "voteButton" + value;

var user = (Flux.Context.isUserAuthenticated()==true) ? Flux.Context._loggedUserUcid : "";

var flashVars = "";
flashVars += "cogix=" + VMA.Voting.cogix + "&amp;";
flashVars += "validator=" + VMA.Voting.validator + "&amp;";
flashVars += "authClass=" + VMA.Voting.authClass + "&amp;";
flashVars += "pollid=" + pollId + "&amp;";
flashVars += "answer=" + value + "&amp;";
flashVars += "user=" + user + "&amp;";
flashVars += "projectcode=" + VMA.Voting.projectcode + "&amp;";
flashVars += "category=" + VMA.Voting.categoryId;

var src = "/ontv/vma/2009/components/voteButton.swf";
var width = "140";
var height = "27";

if(VMA.Voting.style=="grid"){
src = "/ontv/vma/2009/components/voteButtonGrid.swf";
height = "130";
}

var so = new SWFObject(src, embedId, width, height, "9", "#000000");
so.addParam("flashVars", flashVars);
so.addParam("AllowScriptAccess", "sameDomain");
so.addParam("wmode", "transparent");
so.useExpressInstall('/sitewide/components/expressInstall/adobeExpressInstall.swf');
so.write(targetId);
});
}

this.preSubmit = function(){
var vauth = MTV.Utils.Cookies.read("vauth");
if (vauth == null)
vauth = "";

var alreadyVoted = VMA.Utils.arrayContains(vauth.split(","),VMA.Voting.categoryId);

if(Flux.Context.isUserAuthenticated()){
if(!alreadyVoted) {
vauth += VMA.Voting.categoryId + ",";
MTV.Utils.Cookies.create("vauth", vauth);
} 
}
else{
MTVN.UI.ModalWindow.open(VMA.Voting.loginWindowUrl,null,"jquery");
return false;
}

return true;
}

this.postSubmit = function(){

var categories = [
'female','hip-hop','male','new-artist','pop',
'rock','voty','breakthrough', 'video'
];

var category = VMA.Voting.categoryId;
var index = categories.indexOf(category);

if (index == categories.length-1) {
index = 0;
}
else {
index+=1;
}
var next_category = categories[index];

MTVN.UI.ModalWindow.open(VMA.Voting.thanksWindowUrl + '?next=' + next_category + '&categoryId=' + category,null,"jquery");
}
}

MTV.initArray.push(MTV.Player.init);
MTV.initArray.push(VMA.init);
MTV.initArray.push(MTVN.UI.Overlay.init);

var votingPreSubmit = VMA.Voting.preSubmit;
var revealThanks = VMA.Voting.postSubmit;

