/*
 * Base JavaScript Files to handle common tasks
 */

/* prepend article tools to #content */
$(document).ready(function() {
	$('#article #content .articleTools ul').append('<li class="clickabilitySave"><a href="#" onclick="return(ST());" onMouseOver="return(STMouseOver());" onMouseOut="return(STMouseOut());">Save</a></li><li class="reprint"><a href="http://www.icopyright.com/' + icopy + '?icx_id=' + siteURL + '">Reprint</a></li><li class="clickabilityEmail"><a href="#" onclick="return(ET());" onMouseOver="return(ETMouseOver());" onMouseOut="return(ETMouseOut());">Email</a></li><li class="clickabilityPrint"><a href="#" onclick="return(PT());" onMouseOver="return(PTMouseOver());" onMouseOut="return(PTMouseOut());">Print</a></li>'); 

	$('#streamingMediaPage #content .articleTools ul').append('<li class="clickabilitySave"><a href="#" onclick="return(ST());" onMouseOver="return(STMouseOver());" onMouseOut="return(STMouseOut());">Save</a></li><li class="clickabilityEmail"><a href="#" onclick="return(ET());" onMouseOver="return(ETMouseOver());" onMouseOut="return(ETMouseOut());">Email</a></li>'); 

});

/* add _blank to any link with class="popup" */
$(document).ready(function() { 
	$('a.popup').click(function() {
		this.target = "_blank";
	});
});

/* fix IE6's inability to have :hover on anything other than an anchor tag */ 
$(document).ready(function() {  
	$("#topNav li").bind("mouseenter", function(){ 
  		$(this).addClass("over"); 
    }); 
	
	$("#topNav li").bind("mouseleave", function(){ 
	    $(this).removeClass("over"); 
	});
}); 


/*====
 * 'The Top 10' tabs
 *================================================ */

$(document).ready(function() { 
	$('div#top10').addClass("active");
	$('div#top10 #top10Popular').css('display', 'none');
	$('div#top10 #top10Emailed').css('display', 'none');

	$('div#top10 h3.top10Current').click(function() {
		$('div#top10 h3.top10Current').addClass("on");
		$('div#top10 h3.top10Popular').removeClass("on");
		$('div#top10 h3.top10Emailed').removeClass("on");
		$('div#top10 #top10Current').show();
		$('div#top10 #top10Popular').hide();
		$('div#top10 #top10Emailed').hide();
	});	
	$('div#top10 h3.top10Popular').click(function() {
		$('div#top10 h3.top10Popular').addClass("on");
		$('div#top10 h3.top10Current').removeClass("on");
		$('div#top10 h3.top10Emailed').removeClass("on");
		$('div#top10 #top10Current').hide();
		$('div#top10 #top10Popular').show();
		$('div#top10 #top10Emailed').hide();
	});	
	$('div#top10 h3.top10Emailed').click(function() {
		$('div#top10 h3.top10Emailed').addClass("on");
		$('div#top10 h3.top10Popular').removeClass("on");
		$('div#top10 h3.top10Current').removeClass("on");
		$('div#top10 #top10Current').hide();
		$('div#top10 #top10Popular').hide();
		$('div#top10 #top10Emailed').show();
	});	
});


/*====
 * 'The Top 10' tabs
 *================================================ */

/*====
 * 'Top Articles' tabs
 *================================================ */

$(document).ready(function() { 
	$('div#topArticles').addClass("active");
	$('div#topArticles #topArtEmailed').css('display', 'none');
	$('div#topArticles #topArtRecent').css('display', 'none');
	$('div#topArticles #topArtDiscussed').css('display', 'none');

	$('div#topArticles h3.topArtPopular').click(function() {
		$('div#topArticles h3.topArtPopular').addClass("topArtPopularOn");
		$('div#topArticles h3.topArtEmailed').removeClass("topArtEmailedOn");
		$('div#topArticles h3.topArtRecent').removeClass("topArtRecentOn");
		$('div#topArticles h3.topArtDiscussed').removeClass("topArtDiscussedOn");

		$('div#topArticles #topArtPopular').show();
		$('div#topArticles #topArtEmailed').hide();
		$('div#topArticles #topArtRecent').hide();
		$('div#topArticles #topArtDiscussed').hide();

		if ($('div#topArticles h3.topArtPopular span').length == 0) {
			$('div#topArticles h3.topArtPopular').prepend("<span>Most </span>");
		}
		$('div#topArticles h3.topArtEmailed span').remove();
		$('div#topArticles h3.topArtRecent span').remove();
		$('div#topArticles h3.topArtDiscussed span').remove();
	});	

	$('div#topArticles h3.topArtEmailed').click(function() {
		$('div#topArticles h3.topArtPopular').removeClass("topArtPopularOn");
		$('div#topArticles h3.topArtEmailed').addClass("topArtEmailedOn");
		$('div#topArticles h3.topArtRecent').removeClass("topArtRecentOn");
		$('div#topArticles h3.topArtDiscussed').removeClass("topArtDiscussedOn");

		$('div#topArticles #topArtPopular').hide();
		$('div#topArticles #topArtEmailed').show();
		$('div#topArticles #topArtRecent').hide();
		$('div#topArticles #topArtDiscussed').hide();

		$('div#topArticles h3.topArtPopular span').remove();
		if ($('div#topArticles h3.topArtEmailed span').length == 0) {
			$('div#topArticles h3.topArtEmailed').prepend("<span>Most </span>");
		}
		$('div#topArticles h3.topArtRecent span').remove();
		$('div#topArticles h3.topArtDiscussed span').remove();
	});		

	$('div#topArticles h3.topArtRecent').click(function() {
		$('div#topArticles h3.topArtPopular').removeClass("topArtPopularOn");
		$('div#topArticles h3.topArtEmailed').removeClass("topArtEmailedOn");
		$('div#topArticles h3.topArtRecent').addClass("topArtRecentOn");
		$('div#topArticles h3.topArtDiscussed').removeClass("topArtDiscussedOn");

		$('div#topArticles #topArtPopular').hide();
		$('div#topArticles #topArtEmailed').hide();
		$('div#topArticles #topArtRecent').show();
		$('div#topArticles #topArtDiscussed').hide();

		$('div#topArticles h3.topArtPopular span').remove();
		$('div#topArticles h3.topArtEmailed span').remove();
		if ($('div#topArticles h3.topArtRecent span').length == 0) {
			$('div#topArticles h3.topArtRecent').prepend("<span>Most </span>");
		}
		$('div#topArticles h3.topArtDiscussed span').remove();
	});	

	$('div#topArticles h3.topArtDiscussed').click(function() {
		$('div#topArticles h3.topArtPopular').removeClass("topArtPopularOn");
		$('div#topArticles h3.topArtEmailed').removeClass("topArtEmailedOn");
		$('div#topArticles h3.topArtRecent').removeClass("topArtRecentOn");
		$('div#topArticles h3.topArtDiscussed').addClass("topArtDiscussedOn");

		$('div#topArticles #topArtPopular').hide();
		$('div#topArticles #topArtEmailed').hide();
		$('div#topArticles #topArtRecent').hide();
		$('div#topArticles #topArtDiscussed').show();

		$('div#topArticles h3.topArtPopular span').remove();
		$('div#topArticles h3.topArtEmailed span').remove();
		$('div#topArticles h3.topArtRecent span').remove();
		if ($('div#topArticles h3.topArtDiscussed span').length == 0) {
			$('div#topArticles h3.topArtDiscussed').prepend("<span>Most </span>");
		}

	});	
});



/*====
 * 'Top Articles' tabs
 *================================================ */





/*====
 * rotator
 *================================================ */

var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;

$(document).ready(function(){
	$("div.onlineResources").removeClass('noscript'); 
	$("div.onlineResources").addClass("active");
	headline_count = $("div.resourcesItem").size();
	$("div.resourcesItem:eq("+current_headline+")").css('display','block');

	headline_interval = setInterval(headline_rotate,10000); //time in milliseconds
	$("div.onlineResources").append('<div id="topStoryButtons"><ul></ul></div>');
	$('div.onlineResources .resourcesItem h3').each(function(index) {
		var $sectionLabel = $(this).remove().text();
		$("div#topStoryButtons ul").append('<li class="' + $sectionLabel + '">' + $sectionLabel + '</li>');
		$("#topStoryButtons li:eq(0)").addClass('activeControls'); 
	});
	$("div.onlineResources").append('</ul></div>');
	$("#topStoryButtons li:eq(0)").click(function() { 
		selected_headline = 0;
		$("div.resourcesItem:eq(" + current_headline + ")").hide();
		$("#topStoryButtons li:eq(" + current_headline + ")").removeClass('activeControls'); 
		$("#topStoryButtons li:eq(0)").addClass('activeControls'); 
		$("div.resourcesItem:eq(0)").show(); 
		current_headline = selected_headline;
		clearInterval(headline_interval);
		return false;
	});
	$("#topStoryButtons li:eq(1)").click(function() { 
		selected_headline = 1;
		$("div.resourcesItem:eq(" + current_headline + ")").hide();
		$("#topStoryButtons li:eq(" + current_headline + ")").removeClass('activeControls'); 
		$("#topStoryButtons li:eq(1)").addClass('activeControls'); 
		$("div.resourcesItem:eq(1)").show(); 
		current_headline = selected_headline;
		clearInterval(headline_interval);
		return false;
	});
	$("#topStoryButtons li:eq(2)").click(function() { 
		selected_headline = 2;
		$("div.resourcesItem:eq(" + current_headline + ")").hide();
		$("#topStoryButtons li:eq(" + current_headline + ")").removeClass('activeControls'); 
		$("#topStoryButtons li:eq(2)").addClass('activeControls'); 
		$("div.resourcesItem:eq(2)").show(); 
		current_headline = selected_headline;
		clearInterval(headline_interval);
		return false;
	});
	$("#topStoryButtons li:eq(3)").click(function() { 
		selected_headline = 3;
		$("div.resourcesItem:eq(" + current_headline + ")").hide();
		$("#topStoryButtons li:eq(" + current_headline + ")").removeClass('activeControls'); 
		$("#topStoryButtons li:eq(3)").addClass('activeControls'); 
		$("div.resourcesItem:eq(3)").show(); 
		current_headline = selected_headline;
		clearInterval(headline_interval);
		return false;
	});
});

function headline_rotate() {
	current_headline = (old_headline + 1) % headline_count; 
	$("div.resourcesItem:eq(" + old_headline + ")").hide();
	$("#topStoryButtons li:eq(" + old_headline + ")").removeClass('activeControls'); 
	$("#topStoryButtons li:eq(" + current_headline + ")").addClass('activeControls'); 
	$("div.resourcesItem:eq(" + current_headline + ")").show(); 
	old_headline = current_headline;
}
/*====
 * end rotator
 *================================================ */











/* grab search terms from google */
/*
$(document).ready(function() {
  if (!document.referrer) return;
  var matches = document.referrer.match(/[?&]q=([^&]*)/);
  if (!matches) return;
  var terms = unescape(matches[1].replace(/\+/g, ' '));
  var re = new RegExp().compile('(' + terms + ')', 'i');
  $("#content *").each(function() {
    if ($(this).children().size() > 0) return;
    if ($(this).is("xmp, pre")) return;
    var html = $(this).html();
    var newhtml = html.replace(re, '<span class="qterm">$1</span>');
    $(this).html(newhtml);
  });
});
*/

$(document).ready(function() {
  if (!document.referrer) return;
  var matches = document.referrer.match(/[?&]q=([^&]*)/);
  if (!matches) return;
  var search404Term = unescape(matches[1].replace(/\+/g, ' '));
  $("#404searchSite").val(search404Term);
});



/*====
 * replace search box input with label text
 *================================================ */

$(document).ready(function() { 
	var searchLabel = $('div#searchBox label').remove().text();
	var searchLabelBtm = $('div#searchBoxBtm label').remove().text();
	if ($('#searchSite').val() == '') {
		$('#searchSite').addClass('placeholder').val(searchLabel).focus(function() {
			if (this.value == searchLabel) {
				$(this).removeClass('placeholder').val('');
			};
		}).blur(function() {
			if (this.value == '') { 
				$(this).addClass('placeholder').val(searchLabel);
			};
		});
		$('#search form').submit(function() {
			if ($('#searchSite').val() == searchLabel) {
				$('#searchSite').val('');
			}
		});
	}
});
