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

EVENT.User = {};

EVENT.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;	
},
this.getParsedParam = function( string, param ) {
var params = string.split('&');
var parsedParams = {};
for(var i = 0; i<params.length;i++) {
var s = params[i].split('=');
parsedParams[s[0]] = s[1];
}
return parsedParams[param];
},
this.reload = function() {
window.location.reload();
}
}

EVENT.Voting = new function(){
this.active = true;
this.requireLogin = true;

this.style = "list";
this.thanksWindowUrl = "/shows/events/critics_choice/_2012/scripts/ajax/thanks.jhtml";
this.loginWindowUrl = "/shows/events/critics_choice/_2012/scripts/ajax/not_logged_in.jhtml";

this.init = function(){

}

this.preSubmit = function(){
if( EVENT.Voting.requireLogin && !Flux.context.user ) {
MTVN.UI.ModalWindow.open(EVENT.Voting.loginWindowUrl,null,"jquery");
return false;
} else {
var vauth = (MTVN.Utils.Cookies.read("vauth")!=null) ? MTVN.Utils.Cookies.read("vauth") : "";
var voted = EVENT.Utils.arrayContains(vauth.split(","),EVENT.Voting.categoryId);

if(!voted) {
vauth += EVENT.Voting.categoryId + ",";
MTVN.Utils.Cookies.create("vauth", vauth);
}

EVENT.Voting.votedAnswer = EVENT.Utils.getParsedParam( arguments[0], 'answer' );
EVENT.Voting.postSubmit( EVENT.Utils.getParsedParam( arguments[0], 'answer' ) );
return true;
}
}

this.postSubmit = function(answer){
var categories = ['best-picture','best-actor','best-actress','best-supporting-actor','best-supporting-actress','best-young-actor-actress','best-acting-ensemble','best-director','best-original-screenplay','best-adapted-screenplay','best-cinematography','best-art-direction','best-editing','best-costume-design','best-makeup','best-visual-effects','best-sound','best-animated-feature','best-action-movie','best-comedy','best-foreign-language-film','best-documentary-feature','best-song','best-score'];

var category = EVENT.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(EVENT.Voting.thanksWindowUrl + '?next=' + next_category + '&cat=' + category + '&answer=' + answer,null,"jquery");
}
}

EVENT.Voting.Share = new function() {

this.categories = {'best-picture': {'title': 'Best Picture', 'url': '/shows/events/critics_choice/_2012/categories/best-picture/'},'best-actor': {'title': 'Best Actor', 'url': '/shows/events/critics_choice/_2012/categories/best-actor/'},'best-actress': {'title': 'Best Actress', 'url': '/shows/events/critics_choice/_2012/categories/best-actress/'},'best-supporting-actor': {'title': 'Best Supporting Actor', 'url': '/shows/events/critics_choice/_2012/categories/best-supporting-actor/'},'best-supporting-actress': {'title': 'Best Supporting Actress', 'url': '/shows/events/critics_choice/_2012/categories/best-supporting-actress/'},'best-young-actor-actress': {'title': 'Best Young Actor/Actress', 'url': '/shows/events/critics_choice/_2012/categories/best-young-actor-actress/'},'best-acting-ensemble': {'title': 'Best Acting Ensemble', 'url': '/shows/events/critics_choice/_2012/categories/best-acting-ensemble/'},'best-director': {'title': 'Best Director', 'url': '/shows/events/critics_choice/_2012/categories/best-director/'},'best-original-screenplay': {'title': 'Best Original Screenplay', 'url': '/shows/events/critics_choice/_2012/categories/best-original-screenplay/'},'best-adapted-screenplay': {'title': 'Best Adapted Screenplay', 'url': '/shows/events/critics_choice/_2012/categories/best-adapted-screenplay/'},'best-cinematography': {'title': 'Best Cinematography', 'url': '/shows/events/critics_choice/_2012/categories/best-cinematography/'},'best-art-direction': {'title': 'Best Art Direction', 'url': '/shows/events/critics_choice/_2012/categories/best-art-direction/'},'best-editing': {'title': 'Best Editing', 'url': '/shows/events/critics_choice/_2012/categories/best-editing/'},'best-costume-design': {'title': 'Best Costume Design', 'url': '/shows/events/critics_choice/_2012/categories/best-costume-design/'},'best-makeup': {'title': 'Best Makeup', 'url': '/shows/events/critics_choice/_2012/categories/best-makeup/'},'best-visual-effects': {'title': 'Best Visual Effects', 'url': '/shows/events/critics_choice/_2012/categories/best-visual-effects/'},'best-sound': {'title': 'Best Sound', 'url': '/shows/events/critics_choice/_2012/categories/best-sound/'},'best-animated-feature': {'title': 'Best Animated Feature', 'url': '/shows/events/critics_choice/_2012/categories/best-animated-feature/'},'best-action-movie': {'title': 'Best Action Movie', 'url': '/shows/events/critics_choice/_2012/categories/best-action-movie/'},'best-comedy': {'title': 'Best Comedy', 'url': '/shows/events/critics_choice/_2012/categories/best-comedy/'},'best-foreign-language-film': {'title': 'Best Foreign Language Film', 'url': '/shows/events/critics_choice/_2012/categories/best-foreign-language-film/'},'best-documentary-feature': {'title': 'Best Documentary Feature', 'url': '/shows/events/critics_choice/_2012/categories/best-documentary-feature/'},'best-song': {'title': 'Best Song', 'url': '/shows/events/critics_choice/_2012/categories/best-song/'},'best-score': {'title': 'Best Score', 'url': '/shows/events/critics_choice/_2012/categories/best-score/'}};

this.shareTitle = function() {
return 'I voted for ' + EVENT.Voting.Share.categories[EVENT.Voting.categoryId].title + ' for the Critics\' Choice Movie Awards on VH1.com!';
}
this.shareText = function() {
return "Vote now for your favorite nominees and don't forget to watch the 17th Annual Critics' Choice Movie Awards live on VH1 on January 12 at 8/7c!";
}
this.shareUrl = function() {
//return EVENT.Voting.Share.categories[EVENT.Voting.categoryId].url;
return window.location.protocol + '//' + window.location.hostname + window.location.pathname;
}
this.tweetText = function() {
return 'I just voted for #CCMA ' + EVENT.Voting.Share.categories[EVENT.Voting.categoryId].title + ' on @VH1';
}

this.getImage = function(answer) {
for(var i=0;i<EVENT.Voting.nomineeImages.images.length;i++){
if( EVENT.Voting.nomineeImages.images[i].value == answer ) {
var image = EVENT.Voting.nomineeImages.images[i].image;
if( image.substring(0,1) == '/' ) {
image = 'http://www.mtv.com' + image;
}
return image;
}
}
}

this.facebook = new function(share) {

// if we're on live use MTV.com facebook app
// otherwise use MTV Staging app
/*
if( window.location.host.match(/x-jd/) ) {
this.api_key = 'e9674ffb1057eca635a79ff2be29fac3';
this.app_id = '406699125940';
this.fb_redirect = 'http://www.mtvx-jd.mtvi.com/ontv/vma/2011/scripts/close.jhtml';
} else {
this.api_key = window.location.host.match(/mtv\.com$/) ? '5fb041cdc3cb290ab86298efc582e4e3' : 'e9674ffb1057eca635a79ff2be29fac3';
this.app_id = window.location.host.match(/mtv\.com$/) ? '122150691493' : '406699125940';
this.fb_redirect = window.location.host.match(/mtv\.com$/) ? 'http://www.mtv.com/ontv/vma/2011/scripts/close.jhtml' : 'http://www.mtv-jq.mtvi.com/ontv/vma/2011/scripts/close.jhtml';
}
// this.api_key = 'e9674ffb1057eca635a79ff2be29fac3';
*/

if( window.location.host.match(/-d/) || window.location.host.match(/-jd/) || window.location.host.match(/-q/) || window.location.host.match(/-jq/) || window.location.host.match(/-fd/) ) {
this.api_key = 'c1bed2b236d87b05df4bf489326c14e0';
this.app_id = '235160356553823';
this.fb_redirect = 'http://www.vh1-fd.mtvi.com/shows/events/critics_choice/_2012/scripts/close.jhtml';
} else {
this.api_key = '0b7fc0d7f7868fcb9efb20d4c0c65e41';
this.app_id = '129400003839216';
this.fb_redirect = 'http://www.vh1.com/shows/events/critics_choice/_2012/scripts/close.jhtml';
}

this.is_logged_in = false;
this.debug = true;
var self = this;

this.share = function() {
var popUp = 'http://www.facebook.com/dialog/feed?app_id=' + this.app_id + '&display=popup'
+ '&redirect_uri=' + escape( this.fb_redirect )
+ '&caption=' + escape( share.shareText() )
+ '&name=' + escape( share.shareTitle() )
+ '&link=' + escape( share.shareUrl() )
+ '&picture=' + escape('http://www.vh1.com/sitewide/css/charlie/themes/events/critics_choice/2012/share.png')
+ '&action=' + escape('[{\'name\': \'Vote at VH1.com\', \'link\': \'http://ccma.vh1.com\'}]')
+ '&description=%20';

var w = 550;
var h = 300;

var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);

window.open(popUp,null,"height="+h+",width="+w+",status=yes,toolbar=no,menubar=no,location=no,top="+top+",left="+left,false);
}

}(this);

this.twitter = new function(share) {

this.shortenUrl = function(){
BitlyClient.shorten(encodeURIComponent(share.shareUrl()), 'EVENT.Voting.Share.twitter.publish');
}
this.publish = function(data){
var w = 550;
var h = 300;

var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);

var url = share.shareUrl();

if(data!=null)
{
for ( var url in data.results ) 
{
var shortUrl = data.results[url].shortUrl;

if(shortUrl != null) url = shortUrl;	
}
}

var twitter = 'http://twitter.com/share?text=' + encodeURIComponent( share.tweetText() ) + '&url=' + encodeURIComponent(url);

window.open(twitter,null,"height="+h+",width="+w+",status=yes,toolbar=no,menubar=no,location=no,top="+top+",left="+left,false);
};
}(this);

}

$j(function() {
EVENT.init();
MTVN.UI.Overlay.init();

MTVN.UI.Overlay.close = function(){
var mwElement = document.getElementById('modalWindow');
mwElement.style.left = "-9999px";
currentLocationId="";
$j('#overlay').addClass('closed').removeClass('open');
showVoteButtons();
this.isOpen = false;
}

// Override Flux Sign In button so it reloads user context upon sign in.
$j('.signIn').live('click', function() {
MTVN.UI.ModalWindow.close();
$('span.signIn.fluxBtn btnMedium').click();
/*
Flux.createWidget('Authentication', {}, function(authentication) {
Flux.loadContext(function(context) {
context.onSignIn = window.location.reload;
});
authentication.showSignInForm();
}); 
*/
});

// remove main 25% opacity on nominee pages
$j('.votingGrid a[title="Watch Clip"]').attr('target', '_blank');

// Fix categories page
$j('ol.lst-categories li:odd').addClass('odd');

});

if(!Array.prototype.indexOf){
Array.prototype.indexOf=function(obj,start){
for(var i=(start||0),j=this.length;i<j;i++){
if(this[i]==obj){return i;}
}
return -1;
}
}

function hideVoteButtons() {
/*$j('.votingGrid, .mdl.nominees').addClass('overlayed');*/
}
function showVoteButtons() {
/*$j('.votingGrid, .mdl.nominees').removeClass('overlayed');*/
}

var countDownDays = true;
var firstClick = true;
var votingPreSubmit = EVENT.Voting.preSubmit;
/*var revealThanks = EVENT.Voting.postSubmit;*/
var revealThanks = function() {};
var bitly_apikey = "R_ded7e68eb11e312e2c192225473c676d";
var bitly_user = "vh1url";
window.fbAsyncInit = EVENT.Voting.Share.facebook.init;

