/*
$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

function loadClippings()
	//get Clippings from a cookie
	{		//activeClippings[0] = new Clipping(127204)	
	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;
			}}
		}
	}

	

//clipping classfunction Clipping(id)
{
	this.id = id;
}


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

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

	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 = "img/disquete.gif";
			clippingSprite.setAttribute(classFix,"clippingSprite");
			clippingSprite.setAttribute("id","clippingSprite"+x)
			parentObj = document.getElementById("bodyNode");
			parentObj.appendChild(clippingSprite);
						
		}
	}
}





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 but 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;
		}
		
	}		
}
	
//sets the visibility and events for an object;
function setClippingsVisibility()
{

	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;

//função acionada no clique do disquete
function addClipping()
{
	
	newClipping = this.id.substring(3,this.id.length)
	duplicate = false;

	//find the position the allClippings array of the selected clipping
	for (i=0; i < allClippings.length; i++)
	{
		if (newClipping == allClippings[i].id) {pos = i; i = allClippings.length}
	}
		
	//make sure clipping is not already seletected
	if (activeClippings.length > 0)	checkForDuplicates()
		
	if (!duplicate)
	{			
		
		activeClippings[activeClippings.length] = new Clipping(allClippings[pos].id);
			
	 	//find all instances of the clipping in article and hides it
		clippingInstanceVisibility(this.id,'hidden') 
	 		
	 	//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 = parseInt(obj.style.left);
	 	clipNavY = parseInt(obj.style.top);//offsetTop+ihtScreen.scrollTop()+28;
	 		
	 	event.cancleBubble = true;
	 		
	 	// Faz o clipping andar
		clearInterval(clipTimer);
		clipTimer = setInterval("animateClipping()",10);
		
		// Salva a matéria para o cliente
		SalvaMateria(newClipping);
		 	
	 }
}

function SalvaMateria(CTD_Id)
{
	popupntr = window.open('/incluinoticia.asp?CTD_Id=' + CTD_Id,'popupsalvar','width=300,height=260,left=1000,top=1000,scrollbars=0');
	popupntr.window.opener.focus();
}

//reads in the clippings on page from an array;
function initClippings()
{
	
	//Cria os clippings voadores
	createClippingSprites();
	//Carrega os clippings escolhidos pelo cliente
	//loadClippings();
	
	setClippingsVisibility();
}