// JavaScript Document
function ESJClip(id,headline,read)
	{
//	alert('ESJClip, id=[' + id + '], headline=[' + headline + '], read=[' + read + ']');
	this.id = id;
	this.headline = headline;
	this.read = read;
//	this.clicked = markRead(id);
	}

var mySection = "main";
var allSections = new Array();
var esjSections = new Array();
var allESJClips = new Array();  //all ESJClips on page
var activeESJClips = new Array(); //all ESJClips stored by the user
var currentESJClip = 0; //where in the list of ESJClips is the user
var esjScreen = new screenObject(); 
var mouseX = mouseY =0;

function buildClips() {
	var oSpans = document.getElementsByTagName("span");
	//alert("# spans=" + oSpans.length);
	for (var i=0; i<oSpans.length; i++) {
		//alert(i + "=" + oSpans[i].id + "[" + oSpans[i].id.substring(0,3) + "]");
		if (oSpans[i].id.substring(0,3)=="clp") {
			var p = oSpans[i].parentNode;
			while (p) {
				//alert(p.id + "=" + p.tagName);
				if (p.tagName=="DIV") {
					// this is its DIV...
					if (esjSections.length==0) {
						esjSections[0] = new Array(p.id,0,0);
					} else {
						if (p.id == esjSections[esjSections.length-1][0]) {
							esjSections[esjSections.length-1][2]++;
						} else {
							esjSections[esjSections.length] = new Array(p.id,esjSections[esjSections.length-1][2]+1,esjSections[esjSections.length-1][2]+1);
						}
					}
//					alert("esjSections[" + (esjSections.length-1) + "]=" + esjSections[esjSections.length-1][0] + "," + + esjSections[esjSections.length-1][1] + "," + + esjSections[esjSections.length-1][2]);
					break;
				}
				p = p.parentNode;
			}
			allESJClips[allESJClips.length] =  new ESJClip(oSpans[i].id.substring(3,100),oSpans[i].title,false);
		}
	}
	if (esjSections.length==0) {
		mySection = ""; 
	} else {
		mySection = esjSections[0][0];
	};
	//alert("mySection=" + mySection);
}

function screenObject()
	{

	this.bottom = function(){
		if (document.body.scrollHeight) return document.body.scrollHeight};
	
	this.height = function() {
		if (document.body.offsetHeight) return document.body.offsetHeight;}
	
	this.visHeight = function() {
		if (window.innerHeight) return window.innerHeight;
		if (document.body.clientHeight) return document.body.clientHeight;}
	
	this.width = function() {
		if (document.body.offsetWidth) return document.body.offsetWidth;}
	
	this.scrollTop = function() {
		if(document.body.scrollTop) return document.body.scrollTop ;
		if (window.pageYOffset) return window.pageYOffset;
		else return 0;
		};
	}

function loadESJClips() {
//	alert("loading clips...");
//	alert("loading, cookie=[" + document.cookie + "]");
	//alert("continuing...");
	var SectionsString = "";
	ESJClipsString = null;
	tempArray = document.cookie.split(";");
	x = -1;
		
	for (tA = 0; tA < tempArray.length; tA++) {
//		alert(tempArray[tA]);
		if (tempArray[tA].indexOf("ESJClips=^") > -1) {
			tPos = tempArray[tA].indexOf("=")+2;
			ESJClipsString = tempArray[tA].substring(tPos,tempArray[tA].length); //striping out "ESJClips=^"
//			alert("ESJClipString=[" + ESJClipsString + "]");
			var iClips=0;
			var iSects=0;
			if (ESJClipsString != null) {		
				tempArray = ESJClipsString.split("^");
				if (tempArray.length > 1) {
					x=0;
					for (i=0; i < tempArray.length/3; i++) {
//						alert("tempArray[" + x + "]=" + tempArray[x]);
						if (tempArray[x].indexOf("Section") > -1) {
							allSections[iSects] = new Array(tempArray[x+1],tempArray[x+2]);
//							alert("allSections[" + iSects + "]=" + allSections[iSects][0] + ', ' + allSections[iSects][1]);
							iSects++;
						} else {
							activeESJClips[iClips] = new ESJClip(tempArray[x],tempArray[x+1],tempArray[x+2]);
							iClips++;
						}
						x=x+3;
					}
				}
			}
		}
	}			
	
	loadSection();
	
	drawESJClips();
	
	setESJClipsVisibility();
}

	
function saveESJClips()
	{
	tempCookie = "ESJClips=";
	for(i=0; i < activeESJClips.length; i++)
		{
		tempCookie=tempCookie+"^"+(activeESJClips[i].id)+"^"+(activeESJClips[i].headline)+"^"+(activeESJClips[i].read);
		}
//	tempCookie = tempCookie + "; \"Sections=";
//	alert("# sections=" + allSections.length);
	for(i=0; i < allSections.length; i++)
		{
		tempCookie=tempCookie+"^Section^" + allSections[i][0]+"^"+allSections[i][1];
		}
	 	var expire = new Date ();
   		expire.setTime (expire.getTime() + (7 * 24 * 3600000)); //expires in 7 days from users clock
   		expire = expire.toGMTString();
	finalCookie = tempCookie+"; path=/; expires="+expire+"";  	
//	alert("trying to save [" + finalCookie + "]");
  	document.cookie = finalCookie;
//    alert("saved cookie=[" + document.cookie + "]");	
	}

function loadSection() {
//	 alert("loadSection");
	var xPage = getCurrentPage();
	var myNewSection = "";
	for (var i=0; i<esjSections.length; i++) {
//		alert("Section " + i + ":" + esjSections[i][0]);
	}
	if (esjSections.length==1) {
		myNewSection = esjSections[0][0];
	} else {
		// alert("loadSection, xPage=" + xPage);
		for (var i=0; i < allSections.length; i++) {
			// alert("allSections[" + i + "]=" + allSections[i][0] + ", " + allSections[i][1]);
			if (allSections[i][0] == xPage) {
				myNewSection = allSections[i][1];
			}
		}
	}
	if (myNewSection != "") {
		mySection = myNewSection;
		showSection(mySection);
	}
//	 alert("mySection=[" + mySection + "]");
}

function saveSection() {
	var xPage = getCurrentPage();
	var xFound = false;
	// alert("saving section " + mySection + " for page=" + xPage);
	for (var i=0; i < allSections.length; i++) {
		if (allSections[i][0] == xPage) {
			allSections[i][1] = mySection;
			xFound = true;
		}
	}
	if (!xFound) {
		allSections[allSections.length] = new Array(xPage, mySection);
	}
	saveESJClips();
}

function showSection(s) {
	// alert("showSection start! - section=-" + s + ", #sections=" + esjSections.length);
	for (var i=0; i<esjSections.length; i++) {
		if (esjSections[i][0] != s) {
//			alert("hiding " + esjSections[i][0]);
			MM_showHideLayers(esjSections[i][0],'','hide');
		}
	}
	MM_showHideLayers(s,'','show');
//	alert("showSection end!");
}


function getCurrentPage() {
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	if (sPage.indexOf('?') > -1) {
		sPage = sPage.substring(0, sPage.indexOf('?'))
	}
	return(sPage);
}

function eventCheckForCookies()
	{
	//alert('Checking for Duplicates');
/*
	tempCookie = "ESJClips=";
	for(i=0; i < activeESJClips.length; i++)
		{
		tempCookie=tempCookie+"^"+(activeESJClips[i].id)+"^"+(activeESJClips[i].headline)+"^"+(activeESJClips[i].read);
		}
		
	
	 	var expire = new Date ();
   		expire.setTime (expire.getTime() + (7 * 24 * 3600000)); //expires in 7 days from users clock
   		expire = expire.toGMTString();
  	
	finalCookie = tempCookie+"; path=/; expires="+expire;  	
  	document.cookie = finalCookie;

  	checkForCookie = document.cookie.split(";");
  	
  		for (x=0; x < checkForCookie.length; x++)
  			{
  			if (checkForCookie[x].indexOf("ESJClips") >= 0) {cookiesOn = true;}
  			}
  		if (cookiesOn == false) alert("You will need to enable\ncookies to use eSJ MyClip's.");
	
*/		
//		alert('Done Checking for Duplicates.');
	}


function markRead(id)
	{
	// alert("marking " + id + " as read...");
	if (!id) id = this.id.substring(5,this.id.length);
	for (i=0; i < activeESJClips.length; i++)
		{
		if (activeESJClips[i].id == id) activeESJClips[i].read = "yes";
		}
		saveESJClips();
	}	

function eventShowNextESJClip()
	//reads next ESJClip in list
	{
	if (activeESJClips != null) 
		{
		total = 0;
		for (aC = 0; aC < activeESJClips.length; aC++)
			{
			if (activeESJClips[aC].read != "yes") 
				{
				markRead(activeESJClips[aC].id);
				saveESJClips();
				break;
				}
			}
		}
	}
	
function eventClearReadESJClips()
	{
	tempESJClips = new Array();
	x = 0;
	for (i=0; i < activeESJClips.length; i++)
		{
		
		//alert(activeESJClips[i].headline);
		//alert(activeESJClips[i].read);
		
		if (activeESJClips[i].read != "yes") {tempESJClips[x] = activeESJClips[i]; x++}
		}
	activeESJClips = tempESJClips;
	drawESJClips();
	setESJClipsVisibility();	
	updateESJClipCounter();
	saveESJClips();
	}
	
function eventClearAllESJClips()
	{
	activeESJClips = new Array();
	drawESJClips();
	setESJClipsVisibility();	
	updateESJClipCounter();
	saveESJClips();
	}
	
function drawESJClips()
	//creates the HTML for the ESJClips menu when a ESJClip is added
	//removed create element and used innerHTML instead
	{
	newHTML = "";
	
	if (activeESJClips.length < 1) 
		{
		//draw this text if there are no clippigns
		
		newHTML = "<span class='ESJClipItem'>There are currently no Clips saved.  To add a Clip click on the MyClip icon:";
		newHTML += "<img src='/images/clipping.gif'><br><br>";
		newHTML += "You can access your Clips through this menu."
		

		}
		else
		{
			newHTML = ""; //"<ul>";
		}
		
		
	for (i=0; i < activeESJClips.length; i++)
		{
		
		ESJClip = activeESJClips[i];
		
		if (ESJClip.read == "yes") tClass = "ESJClipItemRead";
		else tClass = "ESJClipItem";
				
		newHTML += "<a href='" + ESJClip.id + ".php' onmousedown='markRead(\""+ESJClip.id+"\")' class='"+tClass+"' id='cLink"+ESJClip.id+"'>";
		newHTML += "-&nbsp;" + ESJClip.headline + "</a>";
		
		}
//		if (activeESJClips.length > 0) newHTML += "</ul>";
//	alert("newHTML=[" + newHTML + "]");	


	obj = document.getElementById("myClips");
	if (obj) {
		obj.innerHTML = newHTML;
	}
	
	//make sure that the ESJClips container does not go off screen
	ESJClipsSetContainerHeight()

	}


function ESJClipsSetContainerHeight()
	{
/*	obj = document.getElementById("menuClippings"); // myClips");
	if (obj) {
		obj1 = document.getElementById("myClips");
		alert("clip height: " + obj1.style.height);
		obj.style.height = (parseInt(obj1.style.height) + 50) + "px;"
		alert("before Height=" +parseInt(obj.style.height) + ", vis=" + Math.round(esjScreen.visHeight()/2));
		if (parseInt(obj.style.height) > Math.round(esjScreen.visHeight()/2)) {
			obj.style.height= Math.round(esjScreen.visHeight()/2)+"px";
		}
		alert("after Height=" +parseInt(obj.style.height) + ", vis=" + Math.round(esjScreen.visHeight()/2));
	}*/
	}


clipAnim = 0;
clipOriginX = clipOriginY = 0; //the starting x,y position of the ESJClip
clipNavX = clipNavY = 0;


function checkForDuplicates()
	//make sure there is no other record of that ESJClip in use
	{
	for (i=0; i < activeESJClips.length; i++)
		{
		if (newESJClip == activeESJClips[i].id) {i = allESJClips.length; duplicate = true;}
		}
	}

var ESJClipsDivArray = new Array();

function createPageESJClipsArray()
	{

		d = document.getElementsByTagName("DIV");
		for (j=0; j < d.length; j++) if (d[j].id.indexOf("clp") > -1) 
			{
			ESJClipsDivArray[ESJClipsDivArray.length] = d[j];
			}
	 }


function ESJClipInstanceVisibility(id,state)
	//finds duplicate ESJClips on a page
{
/*	if (id == "clp20040119011") {
		var debug = true;
	}
	if (debug == true) {
		alert("setting " + id + " = " + state);
	} */
	t = document.getElementsByName(id);
//	alert("t.length=" + t.length);
	if(t.length > 0) {
		for (j=0; j < t.length; j++) {
//			alert("before: visibility=[" + t[j].style.visibility + "]");
//			t[j].className = "c" + state;
			t[j].style.visibility = state;
//			alert("after: visibility=[" + t[j].style.visibility + "]");
		}
	} else { 
/*	 	d = ESJClipsDivArray;
		alert("d.length=" + d.length);
		if (d.length > 0) {
			for (j=0; j < d.length; j++) {
				if (d[j].id == id) {
					alert("before: visibility=[" + d[j].style.visibility + "]");
		//			d[j].className = "c" + state;
					d[j].style.visibility = state;
					alert("after: visibility=[" + d[j].style.visibility + "]");
				}
			}		
		} else {
*/
			var obj = document.getElementById(id);
			if (obj) {
//				alert("before: visibility=[" + obj.style.visibility + "]");
				obj.style.visibility = state;
//				alert("after: visibility=[" + obj.style.visibility + "]");
			}
/*		}*/
	}
}
	
	
function setESJClipsVisibility() {
	//sets the visibility and events for an object;
//	alert('setting visibility, mySection=[' + mySection + ']');
	for (i=0; i < allESJClips.length; i++) {
		vis = RSfShow;
		
		//find if this ESJClip is already selected
		for (x=0; x <activeESJClips.length; x++) {
			if (allESJClips[i].id == activeESJClips[x].id) {
//				alert("hiding=" + allESJClips[i].id + ": " + allESJClips[i].headline);
				vis = RSfHide;
			}
		}
	
		// alert('clip=' + allESJClips[i].id + ', vis=' + vis);
		if (vis == RSfShow) {
			for (j=0; j<esjSections.length; j++) {
				if (esjSections[j][0] == mySection) {
					if (i < esjSections[j][1] || i > esjSections[j][2]) {
						vis = RSfHide;
					}
				} else {
					if (i >= esjSections[j][1] && i <= esjSections[j][2]) {
						vis = RSfHide;
					}
				}
			}
		} 
		objId = "clp"+allESJClips[i].id;
		ESJClipInstanceVisibility(objId,vis)
	}
}

clipTimer = false;

function addESJClip(x)
	{
	//this event is triggered when a ESJClip is clicked 
	newESJClip = x.id.substring(3,x.id.length);
//	alert('newESJClip=' + newESJClip);

	duplicate = false;
	pos = 0;
	for (i=0; i < allESJClips.length; i++) //find the position the allESJClips array of the selected ESJClip
		{
		if (newESJClip == allESJClips[i].id) {pos = i; i = allESJClips.length}
		}
		
	if (activeESJClips.length > 0)	checkForDuplicates() //make sure ESJClip is not already seletected
	
	
	if (!duplicate)
	{	
		
		eventCheckForCookies();
		if (cookiesOn == true)
			{
//			alert('Adding to activeESJClips, pos=' + pos);
//			alert('id=' + allESJClips[pos].id);
//			alert('headline=' + allESJClips[pos].headline);
//			alert('# active=' + activeESJClips.length);
			activeESJClips[activeESJClips.length] = allESJClips[pos]; // new ESJClip(allESJClips[pos].id,allESJClips[pos].headline,"no");
//			alert('hiding ' + x.id) ;
	 		ESJClipInstanceVisibility(x.id,RSfHide) //find all instances of the ESJClip in article and hides it
	 		
	 		//get the starting position of the ESJClip by finding the mouse position
	 		clipOriginX = mouseX;
	 		clipOriginY = mouseY+esjScreen.scrollTop();
	 		
	 		//find where on screen the button is for the ESJClips in the nav
	 		obj = document.getElementById("myClips");
	 		clipNavX = 600;
	 		clipNavY = parseInt(obj.style.top)//offsetTop+esjScreen.scrollTop()+28;
	 		
	 		drawESJClips();	
//	 		event.cancleBubble = true;
			saveESJClips();
		 	}
	 }
	 
	
	
	
	}

function updateESJClipCounter()
	//positions the numbers in the ESJClip counter 
	//based on the number of UNREAD ESJClips
	{
	
	t2 = 0;
	t1 = 0;
	if (activeESJClips != null) 
		{
		total = 0;
		for (aC = 0; aC < activeESJClips.length; aC++)
			{
			if (activeESJClips[aC].read != "yes") total++
			}
		t2 = total;
		
	if (total > 9) 
		{
		total = total+""; //make it a string;
		t1 = total.substring(0,1);
		t2 = total.substring(1,2);
		}
		}
	//document.getElementById("cln0").style.top = t1*-9;
	//document.getElementById("cln1").style.top = t2*-9;
	
	t2 = 0;
	t1 = 0;
	if (currentESJClip != null) 
		{
		total = currentESJClip;
		t2 = total;
		
	if (total > 9) 
		{
		total = total+""; //make it a string;
		t1 = total.substring(0,1);
		t2 = total.substring(1,2);
		}
		}
		
	}	


var cookiesOn = true; // ;

function initESJClips()
	//reads in the ESJClips on page from an array;
	{
	// alert("initESJClips!");
	createPageESJClipsArray();
	loadESJClips();
//	setESJClipsVisibility();		
//	updateESJClipCounter();
//	drawESJClips();
	}	

function Clip_readCookie(name) {
	var allcookies = document.cookie;
	var pos = allcookies.indexOf(name+"=");
  	if(pos == -1){
	   return Clip_setCookie();
       }
}


function Clip_setCookie()
{
/*
        today = new Date();
        mytime = Math.round((today.getTime()));
        var the_cookie = "ESJClips=" + mytime;
        var the_date = new Date("December 30, 2010");
        var the_cookie_date = the_date.toGMTString();
        the_cookie = the_cookie + " ;expires=" + the_cookie_date;
        the_cookie = the_cookie + " ;path=" + "/";
        the_cookie = the_cookie + " ;domain=" + ".sunjournal.com";

        document.cookie = the_cookie;
*/
}

Clip_readCookie("ESJClips");

function Clip_mousePosition(e)
	{
	if (RSie&&!RSopr6)
	{
		mouseX = window.event.clientX;
		mouseY = window.event.clientY;
	}
	if (RSn||RSns6)
	{
		mouseX = e.pageX - window.pageXOffset;
		mouseY = e.pageY - window.pageYOffset;
	}	
}
	
function Clip_closePage()
	{
	//alert("clip_closepage");
	saveESJClips();
	}

function displayFix()
	//in IE there is a bug will cause some elements to not draw on screen when the user uses the back button
	//the below fixes that problem by forcing the page to redraw
	{
	document.getElementById("bodyNode").style.display = "block"
	}	

// this will be handled on the pages which use this script... window.onload = initESJClips;
// this too...window.onunload = Clip_closePage;