// Place Date Handle Print Function

$(window).ready(getIssue());
function getIssue(){
	
	var thisLocation2 = location.href;
	if(window.name != "printWindow"){
		var thisMonthLoc2 = thisLocation2.match(/(january)|(jan)|(february)|(feb)|(march)|(mar)|(april)|(apr)|(may)|(june)|(july)|(august)|(aug)|(september)|(sept)|(october)|(oct)|(november)|(nov)|(december)|(dec)/i);
	//alert(thisMonthLoc[0]);
	var dateIssueTwo3 = thisLocation2.match(/[_]\d{1,2}[_]/);
	var dateIssFin2 = dateIssueTwo3[0].match(/\d{1,2}/);
	var thisDateLoc2 = thisLocation2.match(/[_]\d{4}\//);
	var thisYearLoc2 = thisDateLoc2[0].match(/\d{4}/);
	var pubDateCheck = $(".printerFriendly").text();
	//alert(pubDateCheck.text());
	if(pubDateCheck != ""){
		//alert("#issueDate");
		$("#issueDate").text(thisMonthLoc2[0]+" "+dateIssFin2[0]+", "+thisYearLoc2[0]+" | ");
	} else if(pubDateCheck == ""){
		//$("#main > h1").after("<br /><div><span id=\"issueDate\" style=\"font-weight:bold;\"></span><a href=\"\" class=\"printerFriendly\">Printer Friendly Version</a></div><br />");
		//$("#issueDate").text(thisMonthLoc2[0]+" "+dateIssFin2[0]+", "+thisYearLoc2[0]+" | ");
	} else if(pubDateCheck == null){
		//$("#main > h1").after("<br /><div><span id=\"issueDate\" style=\"font-weight:bold;\"></span><a href=\"\" class=\"printerFriendly\">Printer Friendly Version</a></div><br />");
		//$("#issueDate").text(thisMonthLoc2[0]+" "+dateIssFin2[0]+", "+thisYearLoc2[0]+" | ");
	}
	
	$("a").filter(".date").html("Print / view <br />"+thisMonthLoc2[0]+" "+dateIssFin2[0]+", "+thisYearLoc2[0]);
	$("#pubDateTop").text("Published "+thisMonthLoc2[0]+" "+dateIssFin2[0]+", "+thisYearLoc2[0]);
		 
	var printBtn = $("a").filter(".printerFriendly").click(function () { 
	  var storyHTML = $("#main").html();
      //alert();
	  var newWinHead = $("head").html();
	  var newWinLogo = "<div style=\"width:725px;border:none; margin:0 auto; margin-bottom:10px;\">	<a href=\"http://www.occc.edu/pioneer/index.html\"><img src=\"http://www.occc.edu/pioneer/images/header.gif\" alt=\"Pioneer Online header\" width=\"266\" height=\"100\" border=\"0\" vspace=\"6\" hspace=\"6\"/></a> </div>";
	  var oldAlink = $("a").filter(".printerFriendly");
	  var oldAtext = $("a").filter(".printerFriendly").html();
	  //alert(remScript);
	  var remScript = $("a").filter(".printerFriendly").removeClass("printerFriendly").addClass("printGo").attr("href", "");
	  var oldScript = $("#main > script");
	  // alert(oldScript[0].text);
	  var oldWinHTML = storyHTML.replace(oldAtext, "Print");
	  var newWinHTML = oldWinHTML.replace(oldAlink, remScript);
	  //var finalWinHTML = newWinHTML.replace(oldScript[0].html, "var printBtn = $(\"a\").filter(\".printGo\").click(function () { window.print(); window.close(); });");
	  //alert(newWinHTML);
	  var newWindow = window.open("","printWindow","menubar=no, toolbar=no, scrollbars=yes, width=760");
	  //select & remove twitter link\/
	  
	  
	  newWindow.document.write("<head>"+newWinHead+"</head>"+newWinLogo+"<div style=\"width:725px; margin:0 auto;\">"+newWinHTML+"</div>");	  
    });
	}
	else if(window.name == "printWindow"){
		$("a:contains('Follow Us On Twitter')").remove();
		$("a").removeAttr("href").css("color","#660000");
		$("a").filter(".printerFriendly").css("cursor", "pointer");
		$("a").filter(".printerFriendly").click(function () { 
			$(this).css("visibility", "hidden");
			window.print();
			window.close();
		});
		
	}
	
}