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.reloadUserContext = function() {
jQuery.ajax({
async: false,
cache: false,
dataType: 'script',
url: EVENT.Utils.userContextUrl()
});
},
this.userContextUrl = function() {
var host = window.location.host;
var userContextUrl = 'http://widgets3.flux.com/context/short/87F6FFFF0099CB900002FFFFF687';
if( host.match(/.vh1-d.mtvi.com/) ){
userContextUrl = 'http://widgets3.flux-staging.com/context/short/87F6FFFF0099CB900002FFFFF687';
}
return userContextUrl;
}
}

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

this.style = "list";
this.thanksWindowUrl = "/shows/events/do_something_awards/2011/scripts/ajax/thanks.jhtml";
this.loginWindowUrl = "/shows/events/do_something_awards/2011/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 = ['thedsa','athlete','charitysong','city','comedian','concert','couple','docustyle','facebook','festival','moviestar','musicartist','realitytvshow','style','tvshow','tvstar','twitter'];

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 = {'thedsa': {'title': 'The Do Something Award', 'url': '/shows/events/do_something_awards/2011/the-do-something-award/', 'urlTwitter': 'http://on.vh1.com/TheDSA', 'urlFacebook': 'http://on.vh1.com/TheDSA'},'athlete': {'title': 'Athlete', 'url': '/shows/events/do_something_awards/2011/athlete/', 'urlTwitter': 'http://on.vh1.com/DSAthlete', 'urlFacebook': 'http://on.vh1.com/DSAthlete'},'charitysong': {'title': 'Charity Song', 'url': '/shows/events/do_something_awards/2011/charity-song/', 'urlTwitter': 'http://on.vh1.com/DSsong', 'urlFacebook': 'http://on.vh1.com/DSsong'},'city': {'title': 'City', 'url': '/shows/events/do_something_awards/2011/city/', 'urlTwitter': 'http://on.vh1.com/DSAcity', 'urlFacebook': 'http://on.vh1.com/DSAcity'},'comedian': {'title': 'Comedian', 'url': '/shows/events/do_something_awards/2011/comedian/', 'urlTwitter': 'http://on.vh1.com/DSAcmdn', 'urlFacebook': 'http://on.vh1.com/DSAcmdn'},'concert': {'title': 'Concert', 'url': '/shows/events/do_something_awards/2011/concert/', 'urlTwitter': 'http://on.vh1.com/DSconcert', 'urlFacebook': 'http://on.vh1.com/DSconcert'},'couple': {'title': 'Couple', 'url': '/shows/events/do_something_awards/2011/couple/', 'urlTwitter': 'http://on.vh1.com/DScouple', 'urlFacebook': 'http://on.vh1.com/DScouple'},'docustyle': {'title': 'Docu-Style', 'url': '/shows/events/do_something_awards/2011/docu-style/', 'urlTwitter': 'http://on.vh1.com/DSAdoc', 'urlFacebook': 'http://on.vh1.com/DSAdoc'},'facebook': {'title': 'Facebook', 'url': '/shows/events/do_something_awards/2011/facebook/', 'urlTwitter': 'http://on.vh1.com/DSfacebk', 'urlFacebook': 'http://on.vh1.com/DSfacebk'},'festival': {'title': 'Festival', 'url': '/shows/events/do_something_awards/2011/festival/', 'urlTwitter': 'http://on.vh1.com/DSfestival', 'urlFacebook': 'http://on.vh1.com/DSfestival'},'moviestar': {'title': 'Movie Star', 'url': '/shows/events/do_something_awards/2011/movie-star/', 'urlTwitter': 'http://on.vh1.com/DSmstar', 'urlFacebook': 'http://on.vh1.com/DSmstar'},'musicartist': {'title': 'Music Artist', 'url': '/shows/events/do_something_awards/2011/music-artist/', 'urlTwitter': 'http://on.vh1.com/DSmusic', 'urlFacebook': 'http://on.vh1.com/DSmusic'},'realitytvshow': {'title': 'Reality TV Show', 'url': '/shows/events/do_something_awards/2011/reality-tv-show/', 'urlTwitter': 'http://on.vh1.com/DSreality', 'urlFacebook': 'http://on.vh1.com/DSreality'},'style': {'title': 'Style', 'url': '/shows/events/do_something_awards/2011/style/', 'urlTwitter': 'http://on.vh1.com/DSstyle', 'urlFacebook': 'http://on.vh1.com/DSstyle'},'tvshow': {'title': 'TV Show', 'url': '/shows/events/do_something_awards/2011/tv-show/', 'urlTwitter': 'http://on.vh1.com/DSshow', 'urlFacebook': 'http://on.vh1.com/DSshow'},'tvstar': {'title': 'TV Star', 'url': '/shows/events/do_something_awards/2011/tv-star/', 'urlTwitter': 'http://on.vh1.com/DStvstar', 'urlFacebook': 'http://on.vh1.com/DStvstar'},'twitter': {'title': 'Twitter', 'url': '/shows/events/do_something_awards/2011/twitter/', 'urlTwitter': 'http://on.vh1.com/DSAtwitter', 'urlFacebook': 'http://on.vh1.com/DSAtwitter'}};

this.shareTitle = function() {
return 'I voted for ' + EVENT.Voting.Share.categories[EVENT.Voting.categoryId].title;
}
this.shareText = function() {
return "Vote now for your favorite nominees and don't forget to watch the 2011 VH1 Do Something Awards live on August 18 at 9/8c.";
}
this.shareUrl = function(type) {
if( !type ) {
return EVENT.Voting.Share.categories[EVENT.Voting.categoryId].url;
} else {
if( type == 'twitter' ) {
return EVENT.Voting.Share.categories[EVENT.Voting.categoryId].urlTwitter;
}
if( type == 'facebook' ) {
return EVENT.Voting.Share.categories[EVENT.Voting.categoryId].urlFacebook;
}
}
//return window.location.protocol + '//' + window.location.hostname + window.location.pathname;
}
this.tweetText = function() {
return 'I voted for ' + EVENT.Voting.Share.categories[EVENT.Voting.categoryId].title + ' #DSAwards';
}

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.vh1.com' + image;
}
return image;
}
}
}

this.facebook = new function(share) {

// if we're on live use VH1.com facebook app
// otherwise use Vh1 Staging app
if( window.location.host.match(/-d/) || window.location.host.match(/-jd/) || window.location.host.match(/-q/) || window.location.host.match(/-jq/) ) {
this.api_key = 'a6440f731bf5e18a3d6806b38ec7f173';
this.app_id = '154041507386';
this.fb_redirect = 'http://www.vh1-jd.mtvi.com/shows/events/do_something_awards/2011/scripts/close.jhtml';
} else {
this.api_key = window.location.host.match(/vh1\.com$/) ? '41d2377c1cc1f578e51d2aba114b84c4' : '41d2377c1cc1f578e51d2aba114b84c4';
this.app_id = window.location.host.match(/vh1\.com$/) ? '221473237891673' : '154041507386';
this.fb_redirect = window.location.host.match(/vh1\.com$/) ? 'http://www.vh1.com/shows/events/do_something_awards/2011/scripts/close.jhtml' : 'http://www.vh1-jq.mtvi.com/shows/events/do_something_awards/2011/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('facebook') )
+ '&picture=' + escape('http://www.vh1.com/sitewide/css/charlie/themes/events/do_something_awards/2011/130x130-dsa-logo.jpg')
+ '&action=' + escape('[{\'name\': \'Vote at VH1.com\', \'link\': \'http://www.vh1.com/shows/events/do_something_awards/2011/\'}]')
+ '&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.publish = function() {
var twitter = 'http://twitter.com/share?text=' + encodeURIComponent( share.tweetText() ) + '&url=' + encodeURIComponent( share.shareUrl('twitter') );
var w = 550;
var h = 300;

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

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();
EVENT.Utils.reloadUserContext();
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');
this.isOpen = false;
}
*/

$j("ol.lst-nominees-list li p.relnk a[linkpretext]").each( function() {
$j(this).parent().prepend('<span>' + $j(this).attr('linkpretext') + '</span>');
});

// Fix categories page
$j("ol.lst-nominees-list li").attr("class", "");
$j("ol.lst-nominees-list li:odd").addClass("alt");
$j("ol.lst-nominees-list li:last").addClass("last");

// not sure
$j('.votingGrid a[title="Watch Clip"]').attr('target', '_blank');

// Fix home page belt.
$j('.belt .mdl').children('div').eq(1).addClass('scroller');

// Add overlay on Video & Photos pages
$j('#listHub.video_photos .group-ab .title2 a').append('<img class="overlayHover" width="140" height="105" src="/sitewide/css/charlie/themes/events/do_something_awards/2011/background-pixel-item-overlay.png">');
$j('#listHub .group-ab .videos .title2 a').append('<img class="overlayPlay" src="/sitewide/css/charlie/themes/events/do_something_awards/2011/overlay-play-button.png">');

// Add a link back to VH1 upon Flux load
function fluxNavLoaded() {
if( jQuery('#wQuickMenu .quickMenu .container span').length ) return true;
return false;
}

var fluxLoadingTimeout = setInterval( function() {
if( fluxNavLoaded() ) {
jQuery('#wQuickMenu .quickMenu .container').prepend('<a style="color: #E72492;" href="http://www.vh1.com">VH1.com &raquo;</a>&nbsp;&nbsp;|&nbsp;&nbsp;');
clearInterval( fluxLoadingTimeout );
}
}, 200 );

// Only shuffle if there is no winner state
/*
if( !$j("ol.lst-nominees-list li span.winner").length ) {
//$j("ol.lst-nominees-list").shuffle();
}
*/
});

// Shuffle plugin
(function($){
$.fn.shuffle = function() {
return this.each(function(){
var items = $(this).children();
return (items.length)
? $(this).html($.shuffle(items))
: this;
});
}

$.shuffle = function(arr) {
for(
var j, x, i = arr.length; i;
j = parseInt(Math.random() * i),
x = arr[--i], arr[i] = arr[j], arr[j] = x
);
return arr;
}
})(jQuery);

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;
}
}

var firstClick = true;
var votingPreSubmit = EVENT.Voting.preSubmit;
/*var revealThanks = EVENT.Voting.postSubmit;*/
var revealThanks = function() {};

