jQuery(document).ready(function() { /* json structure { bracketid:{ roundid:{ pollid:answerid, pollid2:answerid2 }, roundid2:{ pollid3:answerid3, pollid4:answerid4 } } }*/ // fix for IE Object.keys = Object.keys || (function () { var hasOwnProperty = Object.prototype.hasOwnProperty, hasDontEnumBug = !{toString:null}.propertyIsEnumerable("toString"), DontEnums = [ 'toString', 'toLocaleString', 'valueOf', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'constructor' ], DontEnumsLength = DontEnums.length; return function (o) { if (typeof o != "object" && typeof o != "function" || o === null) throw new TypeError("Object.keys called on a non-object"); var result = []; for (var name in o) { if (hasOwnProperty.call(o, name)) result.push(name); } if (hasDontEnumBug) { for (var i = 0; i < DontEnumsLength; i++) { if (hasOwnProperty.call(o, DontEnums[i])) result.push(DontEnums[i]); } } return result; }; })(); // global variables so we can access them in the matchup template window.bracketid = jQuery('.bracket').data('bracketid'); window.roundid = jQuery('.round-active').first().data('roundid'); window.useLocalStorage = (function(){try{return ('localStorage' in window) && (window['localStorage'] !== null);}catch(e){return false;}})(); window.bracketCookieName = 'BRACKETS.POLL'; window.espnCookieName = 'ESPN.POLL'; window.roundCount = 0; window.totalMatchesInRound = parseInt(jQuery("#total_matches").html()); window.jsonObj = {}; // check for undefined's... if(jsonObj[bracketid] == undefined) { jsonObj[bracketid] = {}; } if(jsonObj[bracketid] != undefined) { if(jsonObj[bracketid][roundid] == undefined) { jsonObj[bracketid][roundid] = {}; } } var autoAdvance = true; var hideVotes = true; window.updateBracketCookie = function updateBracketCookie() { if(useLocalStorage) { // use local storage localStorage.setItem(bracketCookieName, JSON.stringify(jsonObj)); } else{ // use the cookie espn.cookie.set(bracketCookieName, JSON.stringify(jsonObj), 21); } updateRoundCount(Object.keys(jsonObj[bracketid][roundid]).length); } // returns answerid if has a result, or true if it just knows you voted, else false // need to check both the ESPN.POLL cookie & our own BRACKETS.POLL window.checkHasVoted = function checkHasVoted(bracketid, roundid, pollid) { var hasVoted = false; bracketCookieValue = useLocalStorage ? localStorage.getItem(bracketCookieName) : espn.cookie.get(bracketCookieName); espnCookieValue = useLocalStorage ? localStorage.getItem(espnCookieName) : espn.cookie.get(espnCookieName); if(bracketid != null && roundid != null && pollid != null) { if(bracketCookieValue != null && bracketCookieValue != undefined) { jsonObj = JSON.parse(bracketCookieValue); if(jsonObj[bracketid] != undefined) { if(jsonObj[bracketid][roundid] != undefined) { if(jsonObj[bracketid][roundid][pollid] != undefined) { hasVoted = jsonObj[bracketid][roundid][pollid]; } } } } // check for undefined's again... if(jsonObj[bracketid] == undefined) { jsonObj[bracketid] = {}; } if(jsonObj[bracketid] != undefined) { if(jsonObj[bracketid][roundid] == undefined) { jsonObj[bracketid][roundid] = {}; } } // also check the espn.poll to see if it exists in the global poll cookie // if this cookie exists, we will manually have to not show results in the poll // this will not return us the voted answerid, just true if(hasVoted == false) { if(espnCookieValue != null && espnCookieValue != undefined) { encodedPollId = pollid.toString(32); if(typeof(espnCookieValue) === 'string') { var votedPollIds = espnCookieValue.split(','); if(typeof(votedPollIds) === 'object'){ for(i in votedPollIds){ if(votedPollIds[i] === encodedPollId){ hasVoted = true; break; } } } } } } } return hasVoted } function displayVoteContent(html) { jQuery('#mvc-vote-container').css('overflow','visible'); jQuery('#mvc-vote-container').html(html); jQuery('#mvc-vote-container #mvc-vote-content').fadeIn(); jQuery('.video-placeholder').each(function() { loadVideoIframe(jQuery(this)); }); if(jQuery('#pollClosed').length > 0){ jQuery('.final_vote_message').show(); } updateVoteMatchupViews(); var pageName = jQuery('#pageName').html(); if (typeof anTrackPage === 'function') { anTrackPage(0,'espn','brackets','feature','','poll',pageName,'en',''); } jQuery("#mcv-vote-container,#mcv-container").addClass("nobackground"); } function openVoteContainer(html) { if (jQuery('#mvc-vote-container').length === 0) { if (html.indexOf('mcv12-finals-votes') === -1 && html.indexOf('mcv12-finals-result') === -1) { jQuery('
').appendTo(jQuery('#mcv-container')); } else { jQuery('
').appendTo(jQuery('#mcv-container')); } jQuery('#mvc-vote-container').animate({ top: '0px', left: '0px', width: "924px", height: "494px" },{ complete: function() { displayVoteContent(html); } }); } else { jQuery('#mvc-vote-container #mvc-vote-content').fadeOut(); displayVoteContent(html); } } function closeVoteContainer(html) { updateMatchupViews(); jQuery('#mvc-vote-container').removeClass('mcv-matchup-finals').remove(); jQuery("#mcv-vote-container,#mcv-container").removeClass("nobackground"); jQuery('#mvc-vote-container #mvc-vote-content').fadeOut(400, function() { jQuery('#mvc-vote-container').animate({ top: '+=140px', left: '+=390px', width: "146px", height: "159px" },{ complete: function() { jQuery('#mvc-vote-container').remove(); } }); }); } function loadVideoIframe(element) { var videoId = jQuery(element).attr('data-videoid'); var videoWidth = 384, videoHeight = 216; if (!!videoId) { jQuery(element).append(''); } } function updateRoundCount(newCount) { roundCount = newCount; jQuery('#completed_votes').html(roundCount); } function updateMatchupViews() { if(jQuery('.round-active li').length > 0) { jQuery('.round-active li').each(function () { var $this = jQuery(this), pollId = $this.attr('data-pollid'); if (!!pollId) { pollId = parseInt(pollId,10); hasVoted = checkHasVoted(bracketid, roundid, pollId); if(hasVoted != false) { $this.find('div.matchup').addClass("voted"); $this.find('.vote-view').hide(); $this.find('.matchup-view').show(); if(hasVoted != true) { $this.find('div.matchup .player-name[data-answerid="'+hasVoted+'"] div').addClass("winner"); $this.find('div.matchup .player-name:not([data-answerid="'+hasVoted+'"]) div').addClass("loser"); } } } }); } else { jQuery('.round-active').each(function () { var $this = jQuery(this); var pollId = 0; if(jQuery('.round-active').hasClass('round-4a')) { pollId = $this.find('.p1').attr('data-pollid'); } if (!!pollId) { pollId = parseInt(pollId,10); if(checkHasVoted(bracketid, roundid, pollId) != false) { $this.find('.vote-view').hide(); $this.find('.matchup-view').show(); } } }); } // update round count updateRoundCount(Object.keys(jsonObj[bracketid][roundid]).length); jQuery.each(jQuery('.round-active > .matchup-cont'), function() { pollid = jQuery(this).data("pollid"); hasVoted = checkHasVoted(bracketid, roundid, pollid); if(hasVoted == true) { jQuery(this).find('div.matchup').addClass("voted"); } else if(hasVoted != false) { jQuery(this).find('div.matchup').addClass("voted"); jQuery(this).find('div.matchup .player-name[data-answerid="'+hasVoted+'"] div').addClass("winner"); jQuery(this).find('div.matchup .player-name:not([data-answerid="'+hasVoted+'"]) div').addClass("loser"); } }); } function updateVoteMatchupViews() { jQuery('.more-matchups li').each(function () { var $this = jQuery(this), pollId = $this.attr('data-pollid'); if (!!pollId) { pollId = parseInt(pollId,10); if(checkHasVoted(bracketid, roundid, pollId) != false) { $this.addClass('voted'); } } }); jQuery('.more-matchups').show(); var $bothPlayersDivs = jQuery(".vs_wrapper.bracket .player_left,.vs_wrapper.bracket .player_right"), pollId = $bothPlayersDivs.parent(".vs_wrapper").attr("data-pollId"), hasVoted = checkHasVoted(bracketid, roundid, pollId); if(hasVoted != false && hasVoted != true) { $bothPlayersDivs.each(function(i) { if(jQuery(this).attr("data-answerId") == hasVoted) { jQuery(this).addClass("voted"); } else { jQuery(this).addClass("loser"); } }); } } function tweetButton(target, lang, url, originalUrl, text, count) { if (!!url && !!text) { var tweetButtonIframe = [''); tweetButtonIframe = tweetButtonIframe.join(''); jQuery(target).html(tweetButtonIframe); } } if(typeof(loadurl) !== 'undefined') { jQuery.ajax({ type: "GET", url: loadurl, dataType: "html", cache: true, success: function (response) { openVoteContainer(jQuery.trim(response)); } }); } if (jQuery('#mvc-vote-container').length > 0) { jQuery('.video-placeholder').each(function() { loadVideoIframe(jQuery(this)); }); updateVoteMatchupViews(); } jQuery('.matchup-link').live('click', function(e) { e.preventDefault(); url = jQuery(this).attr('href'); if (typeof(url) === 'undefined') { url = jQuery(this).find('a').attr('href'); } jQuery.ajax({ type: "GET", url: url, dataType: "html", cache: true, success: function (response) { openVoteContainer(jQuery.trim(response)); } }); return false; }); jQuery('#close-btn').live('click', function(e) { e.preventDefault(); closeVoteContainer(); }); jQuery('#next-btn').live('click', function(e) { e.preventDefault(); $active = jQuery('.more-matchups .active'); var $next = $active.next(); if ($next.length === 0 || $next.hasClass('hidden')) { $next = jQuery('.more-matchups li').first(); } $next.find('a').trigger('click'); }); jQuery('#prev-btn').live('click', function(e) { e.preventDefault(); $active = jQuery('.more-matchups .active'); var $prev = $active.prev(); if ($prev.length === 0 || $prev.hasClass('hidden')) { $prev = jQuery('.more-matchups li').last(); } $prev.find('a').trigger('click'); }); jQuery('.retweet').live('click',function(e) { window.open(jQuery(this).attr('href'),'reweet','width=550,height=400,scrollbars=no,resizable=yes'); e.preventDefault(); }); tweetButton(jQuery('.twitter-button'), "EN", 'http://es.pn/hF4Z2F', 'http://www.espn.com/nfl/feature/maddenvote', "Who will grace the #MaddenCover this year? Vote now and see who wins on SportsCenter at 6pm ET!", true); updateMatchupViews(); if(autoAdvance == true) { jQuery('div.poll').live("pollsubmit", function(e) { var $active = jQuery('.more-matchups .active'), $next = $active.next(); if ($next.length === 0 || $next.hasClass('hidden')) { } else { $next.find('a').trigger('click'); } }); } var voteTotalCount = jQuery(".player-name-wrapper.matchup.voted").length; function advanceBracket() { var $active = jQuery('.round_nav_box .active'), $next = $active.next(); if ($next.length === 0 || $next.hasClass('hidden')) { $next = jQuery('.round_nav_box li').first(); } $next.find('a').trigger('click'); } jQuery("body").on("mouseenter mouseleave", ".check_circle", function(e) { var $playerParent = jQuery(this).parents(".player_left,.player_right"); if(!$playerParent.hasClass("voted") && !$playerParent.hasClass("loser")) { var $highlightBox = jQuery("highlight_bkg"); if(e.type == "mouseenter") { $highlightBox.addClass("hover"); } else if(e.type == "mouseleave") { $highlightBox.removeClass("hover"); } } }); jQuery("body").on("click", ".twitter_handle a, .twitter_btn, .fb_btn", function(e) { e.preventDefault(); var $this = jQuery(this); if($this.hasClass("twitter_btn")) { anTrackLink(this,'espn','nfl/madden15cover', "postVoteMessageTwitter"); } else if($this.hasClass("fb_btn")) { anTrackLink(this,'espn','nfl/madden15cover', "postVoteMessageFacebook"); } else { anTrackLink(this,'espn','nfl/madden15cover', "playerTwitter"); } var shareUrl = $this.attr("href"), windowOptions = 'scrollbars=yes,resizable=yes,toolbar=no,location=yes', width = 550, height = 420, winHeight = screen.height, winWidth = screen.width, left, top; left = Math.round((winWidth / 2) - (width / 2)); top = 0; if (winHeight > height) { top = Math.round((winHeight / 2) - (height / 2)); } window.open(shareUrl, 'intent', windowOptions + ',width=' + width + ',height=' + height + ',left=' + left + ',top=' + top); }); $bothPlayersDivs = jQuery(".vs_wrapper.bracket .player_left,.vs_wrapper.bracket .player_right"); jQuery("body").on("mouseenter mouseleave click", ".check_circle", function(e) { if(e.type == "click") { e.preventDefault(); var $this = jQuery(this), pollId = $this.parents(".vs_wrapper").attr("data-pollId"), answerId = $this.parents(".player_left,.player_right").attr("data-answerId"), $navHeader = jQuery(".round_nav_header"), thisRound = $navHeader.attr("data-round-id"); if(thisRound == roundid) { hasVoted = checkHasVoted(bracketid, roundid, pollId); if(hasVoted == false) { jQuery.ajax({ type: "POST", data: { "pollId":pollId, "answerId":answerId }, url: "/espn/poll2012/submitVote", success: function (dataCheck) { if(dataCheck == "true") { $this.addClass("voted"); voteTotalCount = voteTotalCount + 1; jsonObj[bracketid][roundid][pollId] = answerId; updateBracketCookie(); updateRoundCount(Object.keys(jsonObj[bracketid][roundid]).length); setTimeout(function() { advanceBracket(); }, 500); } } }); } } } else { var $playerParent = jQuery(this).parents(".player_left,.player_right"); if(!$playerParent.hasClass("voted") && !$playerParent.hasClass("loser")) { var $highlightBox = $playerParent.find(".highlight_bkg"); if(e.type == "mouseenter") { $highlightBox.addClass("hover"); } else if(e.type == "mouseleave") { $highlightBox.removeClass("hover"); } } } }); });