/*
$RCSfile: ihtClippings.js,v $
$Author: john $
$Date: 2001/02/25 07:36:08 $
$Locker:  $
$Revision: 1.3 $
*/

/*

summary 
- - - - -
script handles all clipping functionality

+  Add to Clippings
+  Link to Clipping
+  Update Clipping as read
+  Remove Clipping(s)
+  Save/Email Clippings

terminology
- - - - - - -


methods
- - - - - - -


notes
- - - - - - -
when the user add a clipping already in the activeClippings I need a method to clear out the old
clipping and add the new one to the top

*/

allClippings = new Array();  //all Clippings on page
activeClippings = new Array() //all Clippings stored by the user
currentClipping = 0; //where in the list of Clippings is the user


function loadClippings()
	//get Clippings from a cookie
	{
	clippingsString = null;
	tempArray = document.cookie.split(";");
	x = -1;
		
		for (tA = 0; tA < tempArray.length; tA++)
			{
			if (tempArray[tA].indexOf('clippings=') > -1) //found the clippings section
				{
				tPos = tempArray[tA].indexOf("=")+2;
				clippingsString = tempArray[tA].substring(tPos,tempArray[tA].length); //striping out "clippings=^"
				}
			
			}
			
	if (clippingsString != null)
		{		
		tempArray = clippingsString.split("^");
		if (tempArray.length > 1)
		{
		x=0;
		for (i=0; i < tempArray.length/3; i++)
			{
			//alert(tempArray[x]+" "+tempArray[x+1])
			activeClippings[i] = new Clipping(tempArray[x],tempArray[x+1],tempArray[x+2])
			x=x+3;
			}}
		}
	}

	
function saveClippings()
	{
	tempCookie = "clippings=";
	for(i=0; i < activeClippings.length; i++)
		{
		tempCookie=tempCookie+"^"+(activeClippings[i].id)+"^"+(activeClippings[i].headline)+"^"+(activeClippings[i].read);
		}
		
	
	 	var expire = new Date ();
   		expire.setTime (expire.getTime() + (6 * 24 * 3600000)); //expires in 6 days from users clock
   		expire = expire.toGMTString();
	finalCookie = tempCookie+"; path=/; expires="+expire;  	
  	document.cookie = finalCookie;
  	
	}


function eventCheckForCookies()
	{
	tempCookie = "clippings=";
	for(i=0; i < activeClippings.length; i++)
		{
		tempCookie=tempCookie+"^"+(activeClippings[i].id)+"^"+(activeClippings[i].headline)+"^"+(activeClippings[i].read);
		}
		
	
	 	var expire = new Date ();
   		expire.setTime (expire.getTime() + (6 * 24 * 3600000)); //expires in 6 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("clippings") >= 0) {cookiesOn = true;}
  			}
  		if (cookiesOn == false) alert("You will need to enable\ncookies to use IHT Clipping's.");
	
	}

function Clipping(id,headline,read)
	//clipping class
	{
	this.id = id;
	this.headline = headline;
	this.read = read;
	this.clicked = markRead;
	}

function markRead(id)
	{
	if (!id) id = this.id.substring(5,this.id.length);
	for (i=0; i < activeClippings.length; i++)
		{
		if (activeClippings[i].id == id) activeClippings[i].read = "yes";
		}
	}	

function eventShowNextClipping()
	//reads next clipping in list
	{
	if (activeClippings != null) 
		{
		total = 0;
		for (aC = 0; aC < activeClippings.length; aC++)
			{
			if (activeClippings[aC].read != "yes") 
				{
				markRead(activeClippings[aC].id);
				saveClippings();
				document.location = "/articles/"+activeClippings[aC].id+".html"
				break;
				}
			}
		}
	}
	
function eventClearReadClippings()
	{
	tempClippings = new Array()
	x = 0;
	for (i=0; i < activeClippings.length; i++)
		{
		if (activeClippings[i].read != "yes") {tempClippings[x] = activeClippings[i]; x++}
		}
	activeClippings = tempClippings;
	drawClippings();
	setClippingsVisibility();	
	updateClippingCounter();
	saveClippings();
	}
	
function eventClearAllClippings()
	{
	activeClippings = new Array();
	drawClippings();
	setClippingsVisibility();	
	updateClippingCounter();
	saveClippings();
	}
	
function drawClippings()
	//creates the HTML for the clippings menu when a clipping is added
	//removed create element and used innerHTML instead
	{
	newHTML = "";
	
	if (activeClippings.length < 1) 
		{
		//draw this text if there are no clippigns
		newHTML = "<span class='clippingItem'>To add an article to this Clippings menu click on the Clipping icon next to any headline:";
		newHTML += "&nbsp;<img src='/images/icon/clipping.gif'><br><br>";
		

		}
		
		
	for (i=0; i < activeClippings.length; i++)
		{
		
		clipping = activeClippings[i]
		//a bug in IE5 on mac exists which causes the a not work, will need to find a work around
		
		if (clipping.read == "yes") tClass = "clippingItemRead";
		else tClass = "clippingItem";
				
		newHTML += "<a href='/articles/"+clipping.id+".html' onmousedown='markRead("+clipping.id+")' class='"+tClass+"' id='cLink"+clipping.id+"'>";
		newHTML += clipping.headline+"</a>";
		
		}
		//parentObj.innerHTML = newHTML;
		
	obj = document.getElementById("clippingsContainer");
	obj.innerHTML = newHTML;
	
	//make sure that the clippings container does not go off screen
	clippingsSetContainerHeight()

	}


function clippingsSetContainerHeight()
	{
	obj = document.getElementById("clippingsContainer");
	obj.style.height= Math.round(ihtScreen.visHeight()/2)+"px";
	}


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

function animateClipping()
	// creates a flying icon to the clipping the bar
	{

	if (clipAnim < 5)
		{
		for(x = 0; x < 4; x++)
			{
			obj = document.getElementById("clippingSprite"+x)
			obj.style.visibility = "visible"
			tX  = clipOriginX+(clipAnim*((clipNavX-clipOriginX)/(7-x)))
			tY  = clipOriginY+(clipAnim*((clipNavY-clipOriginY)/(7-x)))
			obj.style.left = tX// clipOriginX+(clipAnim*(clipNavX-clipOriginX))
			obj.style.top = tY//clipOriginY+clipAnim*(clipNavY-clipOriginY)
			}
		clipAnim++;
		}
	else 
		{
		for(x = 0; x < 4; x++)
			{
			obj = document.getElementById("clippingSprite"+x)
			obj.style.visibility = "hidden"
			}
		clipAnim = 0;
		clearInterval(clipTimer)
		}
	}

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

function createClippingSprites()
	//creates the HTML for the clippings sprite which is animated when the user adds a clipping to the queue
	{
	for (x=0; x < 4; x++)
	{
	if (document.getElementById("clippingSprite") == null)
		{
		clippingSprite = document.createElement("img");
		clippingSprite.src = "/images/icon/clippingGhost.gif";
		clippingSprite.setAttribute(classFix,"clippingSprite");
		clippingSprite.setAttribute("id","clippingSprite"+x)
		parentObj = document.getElementById("bodyNode");
		parentObj.appendChild(clippingSprite);
		}
		}
	}

var clippingsDivArray = new Array();

function createPageClippingsArray()
	{

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


function clippingInstanceVisibility(id,state)
	//finds duplicate clippings on a page
	{
	t = document.getElementsByName(id);
	if(t.length > 0)
		{
		for (j=0; j < t.length; j++) 
			{
			t[j].style.visibility = state;
			t[j].onclick = addClipping;
			}
		}
	else { //this is a hack, and possibly slow bu IE5mac and NS6 do not return a length on Object collections
	 	d = clippingsDivArray;
  		{
		for (j=0; j < d.length; j++) if (d[j].id == id) 
			{
			d[j].style.visibility = state;
			d[j].onclick = addClipping;
			}
		}
		}		
	}
	
function setClippingsVisibility()
	{
	//sets the visibility and events for an object;
	
	for (i=0; i < allClippings.length; i++)
		{
		vis = "visible";
		
		//find if this clipping is already selected
		for (x=0; x <activeClippings.length; x++) if (allClippings[i].id == activeClippings[x].id) vis = "hidden";
	
	
			obj = "clp"+allClippings[i].id;
			clippingInstanceVisibility(obj,vis)
			}
			
		}

clipTimer = false;

function addClipping()
	{
	//this event is triggered when a clipping is clicked 
	
	newClipping = this.id.substring(3,this.id.length)
	duplicate = false;
	for (i=0; i < allClippings.length; i++) //find the position the allClippings array of the selected clipping
		{
		if (newClipping == allClippings[i].id) {pos = i; i = allClippings.length}
		}
		
	if (activeClippings.length > 0)	checkForDuplicates() //make sure clipping is not already seletected
	
	
	if (!duplicate)
	{	
		
		
		eventCheckForCookies();
		if (cookiesOn == true)
			{
			activeClippings[activeClippings.length] = new Clipping(allClippings[pos].id,allClippings[pos].headline);
			
	 		clippingInstanceVisibility(this.id,'hidden') //find all instances of the clipping in article and hides it
	 		
	 		//get the starting position of the clipping by finding the mouse position
	 		clipOriginX = mouseX;
	 		clipOriginY = mouseY+ihtScreen.scrollTop();
	 		
	 		//find where on screen the button is for the clippings in the nav
	 		obj = document.getElementById("navTop")
	 		clipNavX = 600;
	 		clipNavY = parseInt(obj.style.top)//offsetTop+ihtScreen.scrollTop()+28;
	 		
	 		drawClippings();	
	 		
	 		event.cancleBubble = true;
	 		clearInterval(clipTimer);
			clipTimer = setInterval("animateClipping()",10);
			updateClippingCounter();
			saveClippings();
		 	}
	 }
	 
	
	
	
	}

function updateClippingCounter()
	//positions the numbers in the clipping counter 
	//based on the number of UNREAD clippings
	{
	t2 = 0;
	t1 = 0;
	if (activeClippings != null) 
		{
		total = 0;
		for (aC = 0; aC < activeClippings.length; aC++)
			{
			if (activeClippings[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 (currentClipping != null) 
		{
		total = currentClipping;
		t2 = total;
		
	if (total > 9) 
		{
		total = total+""; //make it a string;
		t1 = total.substring(0,1);
		t2 = total.substring(1,2);
		}
		}
	}	


		
	
var cookiesOn = false;

function initClippings()
	//reads in the clippings on page from an array;
	{
	createPageClippingsArray()
	createClippingSprites();
	loadClippings();
	setClippingsVisibility();		
	updateClippingCounter();
	drawClippings();
	}	

