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

EVENT.User = {};

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

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.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 = false;
this.cogix = "polling-onair";
this.validator= "timestamp|votes";
this.authClass = "com.mtvnet.auth.dis.HashDigestInputStrategy1";
this.categoryId = "";
this.projectcode = "MTV62";
this.style = "list";
this.thanksWindowUrl = "/shows/events/do_something_awards/2010/scripts/ajax/thanks.jhtml";
this.loginWindowUrl = "/shows/events/do_something_awards/2010/scripts/ajax/not_logged_in.jhtml";

this.init = function(){

$j(".vote-btn").each(function() {

});
}

this.preSubmit = function(){

if( typeof Flux.context == 'undefined' || Flux.context.user == null || typeof Flux.context.user == 'undefined' ) {
$j().jOverlay({url: EVENT.Voting.loginWindowUrl});
return false;
} else {

if( !Flux.context.user.communityMember ) {
$j().jOverlay({url: EVENT.Voting.loginWindowUrl});
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.postSubmit();
return true;
}
}

}

this.postSubmit = function(){
var categories = [
'the-do-something-award','movie-star','tv-show','music-artist','athlete','comedian','with-style','facebook','twitter','city','docu-style'
];

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];
$j().jOverlay({url: EVENT.Voting.thanksWindowUrl + '?next=' + next_category });
}
}

EVENT.Voting.Share = new function() {

this.categories = 
{'the-do-something-award': {'title': 'The Do Something Award', 'url': '/shows/events/do_something_awards/2010/the-do-something-award/', 'facebookUrl': 'http://bit.ly/dosomethingFB', 'twitterUrl': 'http://www.vh1.com/shows/events/do_something_awards/2010/the-do-something-award/?xrs=share_TW_dsa_dsa'},'movie-star': {'title': 'Movie Star', 'url': '/shows/events/do_something_awards/2010/movie-star/', 'facebookUrl': 'http://bit.ly/moviestarFB', 'twitterUrl': 'http://www.vh1.com/shows/events/do_something_awards/2010/movie-star/?xrs=share_TW_dsa_moviestar'},'tv-show': {'title': 'TV Show', 'url': '/shows/events/do_something_awards/2010/tv-show/', 'facebookUrl': 'http://bit.ly/tvshowFB', 'twitterUrl': 'http://www.vh1.com/shows/events/do_something_awards/2010/tv-show/?xrs=share_TW_dsa_tvshow'},'music-artist': {'title': 'Music Artist', 'url': '/shows/events/do_something_awards/2010/music-artist/', 'facebookUrl': 'http://bit.ly/musicFB', 'twitterUrl': 'http://www.vh1.com/shows/events/do_something_awards/2010/music-artist/?xrs=share_TW_dsa_music'},'athlete': {'title': 'Athlete', 'url': '/shows/events/do_something_awards/2010/athlete/', 'facebookUrl': 'http://bit.ly/AthlFB', 'twitterUrl': 'http://www.vh1.com/shows/events/do_something_awards/2010/athlete?xrs=share_TW_dsa_ath'},'comedian': {'title': 'Comedian', 'url': '/shows/events/do_something_awards/2010/comedian/', 'facebookUrl': 'http://bit.ly/comedianFB', 'twitterUrl': 'http://www.vh1.com/shows/events/do_something_awards/2010/comedian/?xrs=share_TW_dsa_comedian'},'with-style': {'title': 'With Style', 'url': '/shows/events/do_something_awards/2010/with-style/', 'facebookUrl': 'http://bit.ly/styleFB', 'twitterUrl': 'http://www.vh1.com/shows/events/do_something_awards/2010/with-style/?xrs=share_TW_dsa_style'},'facebook': {'title': 'Charities on Facebook', 'url': '/shows/events/do_something_awards/2010/facebook/', 'facebookUrl': 'http://bit.ly/facebookFB', 'twitterUrl': 'http://www.vh1.com/shows/events/do_something_awards/2010/facebook/?xrs=share_TW_dsa_facebook'},'twitter': {'title': 'Twitter', 'url': '/shows/events/do_something_awards/2010/twitter/', 'facebookUrl': 'http://www.vh1.com/shows/events/do_something_awards/2010/twitter/?xrs=share_FB_dsa_twitter', 'twitterUrl': 'http://www.vh1.com/shows/events/do_something_awards/2010/twitter/?xrs=share_TW_dsa_twitter'},'city': {'title': 'City', 'url': '/shows/events/do_something_awards/2010/city/', 'facebookUrl': 'http://bit.ly/dscityFB', 'twitterUrl': 'http://www.vh1.com/shows/events/do_something_awards/2010/city/?xrs=share_TW_dsa_city'},'docu-style': {'title': 'Docu Style', 'url': '/shows/events/do_something_awards/2010/docu-style/', 'facebookUrl': 'http://bit.ly/docustyleFB', 'twitterUrl': 'http://www.vh1.com/shows/events/do_something_awards/2010/docu-style/?xrs=share_TW_dsa_docustyle'}};

this.shareTitle = function() {
return 'I voted for a 2010 Do Something Awards ' + EVENT.Voting.Share.categories[EVENT.Voting.categoryId].title + ' nominee';
}
this.shareText = function() {
return 'Vote now for your favorite nominees and don\'t forget to watch the 2010 Do Something Awards on VH1 July 19!';
}
this.shareUrl = function(type) {
if( type == 'facebook' ) {
return EVENT.Voting.Share.categories[EVENT.Voting.categoryId].facebookUrl;
} else if ( type == 'twitter' ) {
return EVENT.Voting.Share.categories[EVENT.Voting.categoryId].twitterUrl;
} else {
return 'http://www.vh1.com/shows/events/do_something_awards/2010/';
}
}

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';
} else {
this.api_key = window.location.host.match(/vh1\.com$/) ? '41d2377c1cc1f578e51d2aba114b84c4' : '41d2377c1cc1f578e51d2aba114b84c4';
}
// this.api_key = 'e9674ffb1057eca635a79ff2be29fac3';

this.channel_path = '/shows/events/do_something_awards/2010/scripts/xd_receiver.htm'; 
this.is_logged_in = false;
this.debug = true;
var self = this;

this.init = function(){
try {
FB.init(self.api_key, self.channel_path); 
} catch ( err ) {
}
}

this.share = function() {
try {
mtvn.btg.Controller.sendLinkEvent( { linkName:'DSA10:Share:' + EVENT.Voting.Share.categories[EVENT.Voting.categoryId].title + ':facebook', linkType:'o' } );
FB.Connect.streamPublish('', {
'name': share.shareTitle(),
'href': share.shareUrl('facebook'),
'description': share.shareText(),
'media': [{'type': 'image',
'src': 'http://www.vh1.com/sitewide/css/charlie/themes/events/do_something_awards/2010/130x130-dsa-logo.jpg',
'href': share.shareUrl('facebook')}]
}, [{ 'text': 'Vote at DSA.VH1.com', 'href': share.shareUrl('facebook')}]);
} catch (err) {
console.log( err );
}
}

}(this)

this.tweetmeme = function(container, tweetmeme_style) {

//var _url = window.location.href;
var _url = EVENT.Voting.Share.shareUrl('twitter');
var _url = _url.replace(/((?:\?|&)?fbc_receiver=.+)?(?:#.*)?$/, "");
var url = escape((typeof tweetmeme_url == "string") ? tweetmeme_url : ((typeof TWEETMEME_URL == "string") ? TWEETMEME_URL : _url)).replace(/\+/g, "%2b");
var source = (typeof tweetmeme_source == "string") ? escape(tweetmeme_source) : ((typeof TWEETMEME_SOURCE == "string") ? escape(TWEETMEME_SOURCE) : false);
var style = (typeof tweetmeme_style == "string") ? escape(tweetmeme_style) : ((typeof TWEETMEME_STYLE == "string") ? escape(TWEETMEME_STYLE) : "normal");
var service = (typeof tweetmeme_service == "string") ? escape(tweetmeme_service) : ((typeof TWEETMEME_SERVICE == "string") ? escape(TWEETMEME_SERVICE) : false);
var service_api = (typeof tweetmeme_service_api == "string") ? escape(tweetmeme_service_api) : ((typeof TWEETMEME_SERVICE_API == "string") ? escape(TWEETMEME_SERVICE_API) : false);
var alias = (typeof tweetmeme_alias == "string") ? escape(tweetmeme_alias) : ((typeof TWEETMEME_ALIAS == "string") ? escape(TWEETMEME_ALIAS) : false);
var hashtags = (typeof tweetmeme_hashtags == "string") ? escape(tweetmeme_hashtags) : ((typeof TWEETMEME_HASHTAGS == "string") ? escape(TWEETMEME_HASHTAGS) : false);
var space = (typeof tweetmeme_space == "number") ? escape(tweetmeme_space) : ((typeof TWEETMEME_SPACE == "number") ? escape(TWEETMEME_SPACE) : false);
var src = "http://api.tweetmeme.com/button.js";
switch (style) {
case "compact":
var h = 20;
var w = 85;
break;
default:
var h = 61;
var w = 50;
break
}
src += "?url=" + url;
src += "&style=" + style;
if (source != false) {
src += "&source=" + source
}
if (service) {
src += "&service=" + service
}
if (service_api) {
src += "&service_api=" + service_api
}
if (alias) {
src += "&alias=" + alias
}
if (hashtags) {
src += "&hashtags=" + hashtags
}
if (space) {
src += "&space=" + space
}
$j(container).append('<iframe src="' + src + '" height="' + h + '" width="' + w + '" frameborder="0" scrolling="no"></iframe>');

}

this.myspace = new function(share) {
this.url = 'http://www.myspace.com/index.cfm?fuseaction=postto&t=';
this.share = function() {
mtvn.btg.Controller.sendLinkEvent( { linkName:'DSA10:Share:' + EVENT.Voting.Share.categories[EVENT.Voting.categoryId].title + ':myspace', linkType:'o' } );
var targetUrl = this.url + encodeURIComponent(share.shareTitle()) 
+ '&c=' + encodeURIComponent(share.shareText()) 
+ '&u=' + encodeURIComponent(window.location.href);
window.open(targetUrl, 'ptm', 'height=450,width=440').focus();
}
}(this);

}

$j(function() {

(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);

$j("ol.lst-nominees-list li p.relnk a[linkpretext]").each( function() {
$j(this).parent().prepend('<span>' + $j(this).attr('linkpretext') + '</span>');
});
// Only shuffle if there is no winner state
if( !$j("ol.lst-nominees-list li span.winner").length ) {
$j("ol.lst-nominees-list").shuffle();
}

$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");

EVENT.init();
EVENT.Utils.reloadUserContext();
//MTVN.UI.Overlay.init();
EVENT.Voting.Share.facebook.init();

// Subscribe form
$j('.subscribeForm a.submit').click( function() {

var $form = $j('.subscribeForm form');
var requestUrl = '/shows/events/do_something_awards/2010/scripts/ajax/gdc_submit.jhtml';
var fields = { format: 'json' };
var errors = [];

// Form element shortcuts
$name = $j('.subscribeForm #name');
$email = $j('.subscribeForm #email');
$month = $j('.subscribeForm #month');
$day = $j('.subscribeForm #day');
$year = $j('.subscribeForm #year');
$zip = $j('.subscribeForm #zip');
ds_newsletter = $j('.subscribeForm #ds_newsletter').attr('checked') ? 'yes' : 'no';
vh1_newsletter = $j('.subscribeForm #vh1_newsletter').attr('checked') ? 'yes' : 'no';

// error checking / post values assignments
if( $j.trim( $name.val() ) == '' ) { errors.push( $name ); } else { fields.name = $name.val(); }
if( $j.trim( $email.val() ) == '' ) { errors.push( $email ); } else { fields.email = $email.val(); }
if( $j('.subscribeForm #month').attr('selectedIndex') == 0 ) { errors.push( $month ); } else { fields.date_of_birth = $month.val() }
if( $j('.subscribeForm #day').attr('selectedIndex') == 0 ) { errors.push( $day ); } else { fields.date_of_birth += '/' + $day.val() }
if( $j('.subscribeForm #year').attr('selectedIndex') == 0 ) { errors.push( $year ); } else { fields.date_of_birth += '/' + $year.val() }
if( $j.trim( $zip.val() ) == '') { errors.push( $zip ); } else { fields.zip = $zip.val() }
fields.ds_newsletter = ds_newsletter;
fields.vh1_newsletter = vh1_newsletter;

// reset form errors
$j('.subscribeForm input, .subscribeForm select').css({ borderColor: '#d5d5d5' });

setTimeout( function() {
if( !errors.length ) {
$j('.subscribeForm .notifications').slideUp();
$j.ajax({
url: requestUrl, 
data: $form.serialize(), 
cache: false,
type: 'GET',
dataType: 'xml',
success: function(response) {
if( $j(response).find('response').attr('status') == 'ok') {
$j('form.subscribe').append('<div class="formOverlayBack"></div><div class="formOverlayText">Thank You for for signing up! Your subscription was received.<br /><br /><a href="#" class="closeOverlay">Close</a></div>');
$j('form.subscribe .formOverlayBack').animate({ opacity: 0.90 });
$j('form.subscribe .formOverlayText').fadeIn();
$j('form.subscribe .formOverlayText a.closeOverlay').click( function() {
$j('form.subscribe .formOverlayBack, form.subscribe .formOverlayText').fadeOut('fast', function() {
$j('form.subscribe .formOverlayBack, form.subscribe .formOverlayText').remove();
});
return false;
});
} else if ( $j(response).find('response').attr('status') == 'error' ) {
// check zip
if( $j(response).find('response entryErrors error[key="zip"]').length ) {
$zip.css({ borderColor: '#ff7878' });
$j('.subscribeForm .notifications').animate({
height: '20px'
},'slow', function() {
$j(this).html('Please correct the indicated field(s) and SUBMIT.');
});
}
// check date
if( $j(response).find('response entryErrors error[key="date_of_birth"]').length ) {
$day.css({ borderColor: '#ff7878' });
$month.css({ borderColor: '#ff7878' });
$year.css({ borderColor: '#ff7878' });
$j('.subscribeForm .notifications').animate({
height: '20px'
},'slow', function() {
$j(this).html('Please correct the indicated field(s) and SUBMIT.');
});
}
// check email
if( $j(response).find('response entryErrors error[key="email"]').length ) {
$email.css({ borderColor: '#ff7878' });
$j('.subscribeForm .notifications').animate({
height: '20px'
},'slow', function() {
$j(this).html('Please correct the indicated field(s) and SUBMIT.');
});
}
}
}
});

} else {
for(i=0; i < errors.length; i++) {
$j(errors[i]).css({ borderColor: '#ff7878' });
}
$j('.subscribeForm .notifications').animate({
height: '20px'
},'slow', function() {
$j(this).html('Please correct the indicated field(s) and SUBMIT.');
});
}
}, 200);

return false;
});

// Override Flux Sign In button so it reloads user context upon sign in.
$j('.signIn').live('click', function() {
Flux.createWidget('Authentication', {}, function(authentication) {
Flux.loadContext(function(context) {
context.onSignIn = EVENT.Utils.reloadUserContext;
});
authentication.showSignInForm();
}); 
});

$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/2010/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/2010/overlay-play-button.png">');
});

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() {};

