/*
PEN Javascript File
AUTHOR: Jason Zajac & TJ Nicolaides
VERSION: 1.1
****************************/

if (!window.console) { 
    window.console = {
        log: function(obj){ /* define own logging function here, or leave empty */ }
    };
}


$(document).ready(function() {	
	 
	 /* Mobile Redirect Message */

	var mobileCookie = getParameterByName(window.location.href, 'PENsite');
	var cookie = readCookie('PENMobileCookie');
	var expDate =new Date();
	expDate.setDate(expDate.getDate()+1);
	expDate = expDate.formatDate("D, j M Y G:i:s %U%T%C");
	if(mobileCookie === "desktop") {
		document.cookie = 'PENMobileCookie=desktop; expires=' + expDate + '; path=/';
	} else {
		if (cookie === "mobile") {
			window.location.href = 'http://m.975thefanatic.com';
		} else if (cookie === "desktop") {
			//Do Nothing
		} else {
			if (jQuery.browser.mobile === true) {
				$("body").append("<div class=\"mobile-alert\"><div class=\"message\"><h3>The Fanatic's Gone Mobile...</h3>Hey! You're on a mobile device. Are you sure you want to be browsing the desktop site? How about checking out the mobile version?<div class=\"user-options\"><a href=\"#\" class=\"yes\">Yes, please</a><a href=\"#\" class=\"no\">No thanks</a></div></div></div>");
				
				//Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
				var popMargTop = ($('.mobile-alert .message').height() + 80) / 2;
				var popMargLeft = ($('.mobile-alert .message').width() + 80) / 2;
				
				//Apply Margin to Popup
				$('.mobile-alert .message').css({
					'margin-top' : -popMargTop,
					'margin-left' : -popMargLeft
				});
				
				var expDate =new Date();
				expDate.setDate(expDate.getDate()+14);
				expDate = expDate.formatDate("D, j M Y G:i:s %U%T%C");
				
				$(".mobile-alert .message a").live("click", function(e) {
				e.preventDefault();
				var option = $(this).attr("class");
				if (option === "no") {
					document.cookie = 'PENMobileCookie=desktop; expires=' + expDate + '; path=/';
					$(".mobile-alert").remove();
				} else if (option === "yes") {
					document.cookie = 'PENMobileCookie=mobile; expires=' + expDate + '; path=/';
					window.location.href = 'http://m.975thefanatic.com';
					return false;
				}
				});
			}
		}
	}
	 	   
						   
	$("div.memberStatus span.lnkLogin a").append(" - MVP Club");
	$("div.memberStatus span").css("display", "inline-block");
	$("div.memberStatus span.delimiter").remove();
	$("div.memberStatus").each(function(){
		h = -$(this).outerHeight();
		$(this).css("margin-bottom", h);
	});
		
		
	
/* AUDIO PLAYER TOOLTIPS */
if(FlashDetect.installed){

	$(function () {
	  $('div[id*="Audio"]').each(function () {
		$(this).addClass("blog-audio-player");
		$(this).parent('div').addClass("blog-audio-container");
		var blogEntryID = ($(this).attr('id')).split('_')[1].slice(0,8);
	
		$(this).after("<div class='blog-audio-tooltip'><p>To listen, <b>click the play button</b>.</p><p>Don't see it? Listen on our <a href='http://m.975thefanatic.com/?entry_id="+blogEntryID+"'>mobile site</a> or <a href='http://www.975thefanatic.com/about/podcasts/'>get help here</a>.</p></div>");
		
		// options
		var distance = 30;
		var time = 250;
		var hideDelay = 500;
		
		if ($(this).height() > 50) {
			var init_bottom = 25;
		} else {var init_bottom = 10; }
		
		var hideDelayTimer = null;
	
		// tracker
		var beingShown = false;
		var shown = false;
		
		var trigger = $(this);
		var popup = $(trigger).siblings('.blog-audio-tooltip').css('opacity', 0);
		var popupWidth = $(trigger).width();
		
		// animate when the page loads
		setTimeout(function() {      
			beingShown = true;
			popup.css({
				  bottom: init_bottom, display: 'block' // brings the popup back in to view
				  })        
				.animate({opacity:1}).animate({left:'-=5px'}).animate({left:'+=5px'}).animate({left:'-=5px'}).animate({left:'+=5px'}).animate({left:'-=5px'}).animate({left:'+=2px'}, function () {
			  // reset the timer if we get fired again - avoids double animations
			  if (hideDelayTimer) clearTimeout(hideDelayTimer);
					  beingShown = false;
					  shown = true;
			  // store the timer so that it can be cleared in the mouseover if required
			  hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				popup.animate({
				  bottom: '-=' + distance + 'px',
				  opacity: 0
				}, time, 'swing', function () {
				  // once the animate is complete, set the tracker variables
				  shown = false;
				  // hide the popup entirely after the effect (opacity alone doesn't do the job)
				  popup.css('display', 'none');
				});
			  }, 4000);
			});
		}, 4000);
	
		// set the mouseover and mouseout on both element
		$([trigger.get(0), popup.get(0)]).mouseover(function () {
		  // stops the hide event if we move from the trigger to the popup element
		  if (hideDelayTimer) clearTimeout(hideDelayTimer);
	
		  // don't trigger the animation again if we're being shown, or already visible
		  if (beingShown || shown) {
			return;
		  } else {
			beingShown = true;
			// reset position of popup box
			popup.css({
			  bottom: init_bottom + 30,
			  //left: -10,
			  display: 'block' // brings the popup back in to view
			})
			// (we're using chaining on the popup) now animate it's opacity and position
			.animate({
			  bottom: '-=' + distance + 'px',
			  opacity: 1
			}, time, 'swing', function() {
			  // once the animation is complete, set the tracker variables
			  beingShown = false;
			  shown = true;
			});
		  }
		}).mouseout(function () {
		  // reset the timer if we get fired again - avoids double animations
		  if (hideDelayTimer) clearTimeout(hideDelayTimer);
		  
		  // store the timer so that it can be cleared in the mouseover if required
		  hideDelayTimer = setTimeout(function () {
			hideDelayTimer = null;
			popup.animate({
			  bottom: '-=' + distance + 'px',
			  opacity: 0
			}, time, 'swing', function () {
			  // once the animate is complete, set the tracker variables
			  shown = false;
			  // hide the popup entirely after the effect (opacity alone doesn't do the job)
			  popup.css('display', 'none');
			});
		  }, hideDelay);
		});
	  });
	});
} else {
	$('div[id*="Audio"]').each(function () {
		$(this).addClass("blog-audio-player");
		$(this).parent('div').addClass("blog-audio-container");
		var blogEntryID = ($(this).attr('id')).split('_')[1].slice(0,8);
	
		$(this).html("<div class='blog-audio-tooltip'><p>Listen on our <a href='http://m.975thefanatic.com/?entry_id="+blogEntryID+"'>mobile site</a> or <a href='http://www.975thefanatic.com/about/podcasts/'>get help here</a>.</p></div>");
		
		var trigger = $(this);
		var popup = $(trigger).children('.blog-audio-tooltip').css('display', 'block').css('background-position', '100% 100%').css('position', 'relative').css('left', 0);
	});
}

	
		/** Random Masthead Image **/
			var currentDirectory_array = window.location.pathname.split( '/' );
		var mh = ["masthead-red-lidge.jpg", "masthead-red-ryan-howard.jpg", "masthead-red-reggie-white.jpg", "masthead-red-desean-jackson.jpg", "masthead-red-flyers-1976-stanley-cup.jpg", "masthead-red-flyers-2009.jpg", "masthead-red-julius-irving.jpg", "masthead-red-julius-irving.jpg", "masthead-red-villanova-1985.jpg"  ];
	var mhpath = "http://www.975thefanatic.com/Pics/Global/Masthead/";
	var bgpath = "http://www.975thefanatic.com/Pics/Global/Backgrounds/";
	var mhf = "";
	var bg = "";
	var randomNumber = 0;

	//Replace Masthead
	switch (currentDirectory_array[2]) {
		case "phillies":
			mhf = 'masthead-red-lidge.jpg';
			break;
		case "flyers":
			mhf = 'masthead-orange-flyers-1976-stanley-cup.jpg';
			bg = 'bg-orange.jpg';
			break;
		case "eagles":
			mhf = 'masthead-green-reggie-white.jpg';
			bg = 'bg-green.jpg';
			break;
		case "sixers":
			mhf = 'masthead-blue-julius-irving.jpg';
			bg = 'bg-blue.jpg';
			break;
		case "college":
			mhf = 'masthead-midnight-villanova-1985.jpg';
			bg = 'bg-midnight-blue.jpg';
			break;
		default:
			mhf = mh[Math.round(Math.random()*(mh.length-1))];
			bg = '';
		break;
	};

	$('img#top_logo').attr('src', mhpath + mhf);
	
	if (bg != '') {
		$('body').css("background-image", "url(" + bgpath + bg + ")" );
	}

	// NOW ON AIR Control
	$("div.onairnow").each(function(){
			$(this).hide();
			var url = $(this).find("h2 a").attr("href");
			var className = $(this).find("div:eq(1)").html();
			$("#header").append("<a id='on-air-now' class='"+ className +"' href='"+ url +"'></a>");
	});
	 
	$("ul.sf-menu").superfish({ 
		autoArrows:  false,  
		dropShadows: false, 
		delay: 1000, 
		onBeforeShow:  function() {
			$("ul#twitter_top li").fadeTo(10, 0);
		}, 
		onShow:  function() {
			$("ul#twitter_top li").fadeTo(10, 0);
		}, 
		onHide: function() {
			$("ul#twitter_top li").fadeTo(1, 0).fadeTo(1000, 1);
		} 
	});
	
		/**POP-UP WINDOWS **/
	/*AV PLAYER*/ $(".av-popup").popup({width: 675, height: 320});
	/*Listen Live HD1*/ $(".stream-popup-lc").popup({width: 680,height: 511});
	/*Listen Live STW*/ $(".stream-popup-stw, a[target='stream']").popup({width: 742,height: 388,resizeable: false,scrollbars: false,titlebar:false,status:false,toolbar:false,menubar:false});
	/*text club*/ $(".text-popup").popup({width: 750,height: 600});
	/** RSS **/ $(".rss-email-popup").popup({width: 675,height: 550});
	   

	// ALLOWS YOU TO SELECT ALL EXTERNAL LINKS WITH $('a:external')					
	$.expr[':'].external = function(obj){
    return !obj.href.match(/^mailto\:/) && (obj.hostname != location.hostname) && !obj.href.match(/^javascript\:/);
	};
	
		
	//FORCES ALL EXTERNAL LINKS TO OPEN IN NEW WINDOW
	$('a:external').attr("target","_blank");

	/** Facebook "Like" Button // Twitter // Google + // AddThis **/	

	var page_name = returnDocument();
	
	if(page_name == "home.aspx" || page_name == null || page_name =="index.aspx" || page_name == "") {
			var pathName = returnPath();
			
		$('table.blogs_home_link').each(function(){
			var entryLink = $(this).find("a:last").attr("href");
			var shareHTML = makeShareButtons.init({url:entryLink});
			$(this).after(shareHTML);

		});
		
		$('a.jumperlink').not($("div.contestTeaser a.jumperlink")).each(function(){
			var entryLink = pathName + $(this).attr("href");
			var shareHTML = makeShareButtons.init({url:entryLink});
			$(this).after(shareHTML);
		});
		
		FB.XFBML.parse();

	}
	else if (page_name == "blogentry.aspx") {	
		var shareHTML = makeShareButtons.init({at:true, tw:true, gp:true});
		$('table.blog_entry td.mini').append(shareHTML);
		$.addthis('jzajac');
		$.getScript("http://platform.twitter.com/widgets.js");
		$.getScript("https://apis.google.com/js/plusone.js");
		FB.XFBML.parse();
	}
	else if (page_name == "story.aspx") {
		var shareHTML = makeShareButtons.init({at:true, tw:true, gp:true});
		$('a.jumperlink[href*="TellAFriend"]').after(shareHTML).remove();	
		$.addthis('jzajac');
		$.getScript("http://platform.twitter.com/widgets.js");
		$.getScript("https://apis.google.com/js/plusone.js");
		FB.XFBML.parse();
	}
	
	$(".share-this").each(function(){
		if($(this).hasClass("all")) {
			var shareHTML = makeShareButtons.init({at:true, tw:true, gp:true});
			$(this).html(shareHTML);
			$.addthis('jzajac');
			$.getScript("http://platform.twitter.com/widgets.js");
			$.getScript("https://apis.google.com/js/plusone.js");
			FB.XFBML.parse();
		} else {
			var shareHTML = makeShareButtons.init({});
			$(this).html(shareHTML);
			FB.XFBML.parse();
		}
	});
	

	/*The Fanatic - CURRENT CONTESTS
   ******************************/
	if ( $("div[ class *= 'contestTeaser' ] div[ id *= 'ctl00_MainBotLeft_GenericControl5_uc7f2ab769d8fd450899fdbdaf6113d835_NormalMode' ] ul li p span[ id *= 'rptContestList_ctl01_lblContestDescription' ]").length > 0 ) {

		$("div[ class *= 'contestTeaser' ] div[ id *= 'ctl00_MainBotLeft_GenericControl5_uc7f2ab769d8fd450899fdbdaf6113d835_NormalMode' ] ul li p span[ id *= 'ContestDescription' ]").each(function() {																																																													
																																																							
			$(this).remove();	

		});
	}
	
	if ( $("div[ class *= 'contestTeaser' ] div[ id *= 'ctl00_MainBotLeft_GenericControl6_uc7f2ab769d8fd450899fdbdaf6113d835_NormalMode' ] ul li p span[ id *= 'rptContestList_ctl01_lblContestDescription' ]").length > 0 ) {

		$("div[ class *= 'contestTeaser' ] div[ id *= 'ctl00_MainBotLeft_GenericControl6_uc7f2ab769d8fd450899fdbdaf6113d835_NormalMode' ] ul li p span[ id *= 'ContestDescription' ]").each(function() {																																																													
																																																							
			$(this).remove();	

		});
	}
	
	if ( $("div[ class *= 'contestTeaser' ] div[ id *= 'ctl00_MainBotRight_GenericControl7_uc7f2ab769d8fd450899fdbdaf6113d835_NormalMode' ] ul li p span[ id *= 'ContestDescription' ]").length > 0 ) {

		$("div[ class *= 'contestTeaser' ] div[ id *= 'ctl00_MainBotRight_GenericControl7_uc7f2ab769d8fd450899fdbdaf6113d835_NormalMode' ] ul li p span[ id *= 'ContestDescription' ]").each(function() {																																																													
																																																							
			$(this).remove();	

		});
	}

	//Remove LOGIN/REGISTER form from On-Air Contest posts
	if ( $("span.header span:contains('[ON-AIR]')").length > 0 ) {
		$("span.header span:contains('[ON-AIR]')").each(function() {
		$("div.uloginverify div[ id *= 'ucUnivLoginVerify_pnlLogin' ], div.hdiv11, div[ id *= 'MemberAuthenticated' ]").remove();
		});
	} else if ( $("span.header span:contains('[ONLINE*]')").length > 0 ) {
		$("span.header span:contains('[ONLINE*]')").each(function() {
		$("div.uloginverify div[ id *= 'ucUnivLoginVerify_pnlLogin' ], div.hdiv11, div[ id *= 'MemberAuthenticated' ]").remove();
		});
	}
	
if(pageTracker != null && typeof(pageTracker) != 'undefined') {
	
	$("div[ id *= 'AVPlayerFlash' ] > *").bind("click", function() {
		trackThis("AV Player", this);
	});
	
	$("div[ id *= 'Images_' ] > *").bind("click", function() {
		trackThis("Photo Gallery", this);
	});
	
	$("div[ id *= 'Audio_' ] > *").one("click", function() {
		trackThis("Audio Embed", this);	
	});
	
	$("a[href *='.mp3']").bind("click", function() {
		pageTracker._trackEvent('MP3 Download', $(this).context.hostname, $(this).attr('href'));
	});
	
	$('a:external').bind("click", function() {
		 pageTracker._trackEvent('Outbound Traffic', $(this).context.hostname, $(this).attr('href'));
	});
	
}
	// ALERT VIP MEMBERS TO FIND THEIR PASSWORD
	$('.button_memberlogin').after("<div id=\"vip-help\" class=\"button_memberlogin\"><a href=\"#\"><span class=\"question\">Already a member, but no password?</span> Help!</a></div><div class=\"friendly-warning\"><p>We've got your password waiting for you! We'll deliver it to your email inbox right away, in three easy steps:<ol><li>Enter your email address in the field above. In the password field, enter \"Fanatic\", and click \"Login\".</li><li>You'll get an error saying this password is incorrect. Don't worry! Click \"Send Reminder\" and we'll give you the real one.</li><li>Once you've checked your email and you've got your new password, come back here and log in with it.</li></ol></p><p>If you've got any problems, please don't hesitate to drop <a href=\"mailto:webmaster@975thefanatic.com?Subject=Trouble logging in to the new 975thefanatic.com!\">tech support</a> an email.</p></div>");
	
	$('#vip-help').bind("click", function() {
		var warningDisplay = $("div.friendly-warning").css("display");
				if (warningDisplay == "none") {
					$("div.friendly-warning").slideDown("slow");
				}
				else {
					$("div.friendly-warning").slideUp("slow");
				}		
			return false;
		});
	
	/*EMAIL SUBSCRIBE BOX
	****************************************/
	$("div#email-subscribe p#no-java").remove();
	$("div#email-subscribe input#es-input, div#email-subscribe input#subscribe-button").css("display", "inline-block");
	
	$("input#es-input").focus(function(){
		$("input#es-input").val("");
    });
	$("input#es-input").blur(function(){
		emailValue = $("input#es-input").val();
		$("input#es-input").val(emailValue);
    });
	
	$("input#subscribe-button").click(function(){
		formEmail = "http://www.feedburner.com/fb/a/mailverify?uri=WPEN/all&email=" + $("input#es-input").val();
		window.open(formEmail , "", "scrollbars=yes,width=550,height=520");								   
    });
	
});


