// EPA's Core JS file
// Last edited: 25 January 2010
// Added new ForeSee code for ACSI
// Modified date string (Date Last Updated)
// Questions? hessling.michael@epa.gov

var epaCore = {
  //Date related functions
    takeYear: function(theDate) { var x = theDate.getYear(); var y = x % 100; y += (y < 38) ? 2000 : 1900; return y; },
    //nths: function(day) { if (day == 1 || day == 21 || day == 31) return 'st'; if (day == 2 || day == 22) return 'nd'; if (day == 3 || day == 23) return 'rd'; return 'th'; },
  
  //Bookmarklet popups
	postPopUp: function(url, name, params) { var win = window.open(url, name, params); }
	
}; // end epaCore

// Use jQuery via jQuery(...)
jQuery(document).ready(function(){

  //Load Notice Script
	var noticeScript = '<scr' + 'ipt type="text/javascript" src="http://www.epa.gov/epahome/notice.js"></scr' + 'ipt>';
	jQuery("body").append(noticeScript);
	
  //Search Autosuggest
	jQuery("#searchbox").autocomplete("autocomplete",{minChars:2,delay:200,matchSubset:false,selectFirst:false});
	// set autocomplete result event callback to submit the form
	jQuery("#searchbox").result(function (event, data, formatted) {
		jQuery('#EPAsearch').submit();
	});

  //Load the ForeSee ACSI Survey code
	//var keepAliveSurveyScript = '<scr' + 'ipt type="text/javascript" src="http://www.epa.gov/epafiles/js/third-party/foresee/foresee-alive.js"></scr' + 'ipt>';
	var surveyScript = '<scr' + 'ipt type="text/javascript" src="http://www.epa.gov/epafiles/js/third-party/foresee/foresee-trigger.js"></scr' + 'ipt>';
	jQuery("head").append(surveyScript);


  //Stripe all tables with class="zebra"
	jQuery('.zebra tr:even').addClass('rowTint');
  
  
  //Insert date last modified
        if (document.lastModified == "")
        {
           var d = new Date()
        } 
        else
        {
	   var d = new Date(document.lastModified);
        }
	var dateUpdated =  "Last updated on " + d.toLocaleDateString();

	jQuery('#globalfooter').after('<p id="date"></p>');
	jQuery('#date').append(dateUpdated);
  // Insert url, but hide for screen
	jQuery('#footer').append('<p id="url"></p>');
	jQuery('#url').append(document.createTextNode(window.location.href));
	
  //NEW! icon
	if(!document.getElementById('content')) return;
	if(!document.getElementsByTagName('ins')) return;
	var new_item = document.getElementById('content').getElementsByTagName('ins');
	var j = new_item.length;
	var x = new Date(); var today = new Date(x.toGMTString());
	var now = (Date.UTC(epaCore.takeYear(today),today.getMonth(),today.getDate(),0,0,0))/86400000;
	for ( var i = 0; i < j; i++) {
		if (!new_item[i].getAttribute('datetime')) continue;
		var a = new_item[i].getAttribute('datetime');
		var b = a.split('-');
		var posted_date = (Date.UTC(b[0],b[1],b[2],0,0,0))/86400000;
		var time_left = posted_date - (now + 1);
		if (time_left < 31  &&  time_left > 0) {
			var icon = document.createElement('img');
			icon.alt = 'New!'; icon.src = 'http://www.epa.gov/epafiles/images/new-en.gif';
			icon.width = '34'; icon.height = '16';
			new_item[i].appendChild(icon);
		}
	};
	
  // Bookmarklet
    // Adding it next to Contact Us link in upper right of all pages
	jQuery('#content').append('<ul id="share"><li><a href="#area">Share</a></li></ul>');
	var bookmarkList = '<ul><li class="delicious"><a href="#area" title="delicious">Del.icio.us</a></li><li class="digg"><a href="#area" title="digg">Digg</a></li><li class="facebook"><a href="#area" title="facebook">Facebook</a></li><!--li class="reddit"><a href="#area" title="reddit">reddit</a></li><li class="slashdot"><a href="#area" title="slashdot">Slashdot</a></li--><li class="stumble"><a href="#area" title="stumble">StumbleUpon</a></li><li class="whatisthis"><a href="#area" title="whatisthis">What is this?</a></li></ul>';
	jQuery('#share li').append(bookmarkList).hover(function() {jQuery(this).addClass("on");}, function() {jQuery(this).removeClass("on");});
	jQuery("#share li ul li a").click(function () {
    	var site = jQuery(this).attr('title');
    	var popUpUrl = encodeURIComponent(window.location.href);
		var title = encodeURIComponent(document.title);
		switch (site) {
//			case "slashdot": epaCore.postPopUp('http://slashdot.org/slashdot-it.pl?op=basic&url='+popUpUrl, 'slashdot', 'status=0,height=600,width=600,scrollbars=yes,resizable=yes');
//			break;
			case "facebook": epaCore.postPopUp('http://www.facebook.com/sharer.php?u='+popUpUrl + '&t=' + title, 'facebook', 'status=0,height=436,width=646,scrollbars=yes,resizable=yes');
			break;
			case "digg": epaCore.postPopUp('http://digg.com/remote-submit?phase=2&url='+popUpUrl + '&title=' + title, 'digg', 'status=0,height=450,width=650,scrollbars=yes,resizable=yes');
			break;	
			case "delicious": epaCore.postPopUp('http://del.icio.us/post?v=4&noui&jump=close&url='+popUpUrl + '&title=' + title, 'delicious', 'status=0,height=400,width=700,scrollbars=yes,resizable=no');
			break;			
//			case "reddit": epaCore.postPopUp('http://reddit.com/submit?url='+popUpUrl+'&title=' + title, 'reddit', 'status=0,height=400,width=700,scrollbars=yes,resizable=no');
//			break; 
			case "stumble": epaCore.postPopUp('http://www.stumbleupon.com/submit?url='+popUpUrl+'&title=' + title, 'stumble', 'status=0,height=400,width=700,scrollbars=yes,resizable=no');
			break; 
			case "whatisthis": epaCore.postPopUp('http://www.epa.gov/epahome/bookmarks.htm', 'whatisthis', 'status=0,height=600,width=1000,scrollbars=yes,resizable=no');
			break;
		}
	});

});


