/*

Clippings version 1.0.6.18.2001

summary 
- - - - -
script handles all Clipping functionality
this is a stripped down version of the IHT Clippings code
the primary difference is the removal of animation code which I am not that happy with
and a few minor improvements

+  Add to Clippings
+  Link to Clipping
+  Update Clipping as read
+  Remove Clipping(s)
+  Save Clippings

comments
- - - - - - 
Clippings only work with DOM browsers.  Opera at this point does not have enough of the DOM to support clippings.
There is little or no feature/browser compatibiliy checking in this code, so use wisely.
The code has been tested on IE 5.0, 5.5, 6.0 on Windows, IE 5.0 on Mac, and various versions of Netscape 6.0 and Mozilla.

loadClippingURL() will control how Clipping urls are handled.  You will want to customize this method.
clippingsTag is a variable which defines what tag your Clippings icon will be nested in.  All clipping icons must be in the same type of tag.

there is a fix for a display error in Mozilla on lines 266-268 and again on 

*/


allClippings = new Array();  
activeClippings = new Array();
currentClipping = 0; 
clippingsDivArray = new Array(); 
cookiesOn = false; 

clippingsTag = "A"; 

var mouseX = mouseY =0;

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 mousePosition(e)
	{
	if (e) event = e;   
	mouseX = event.clientX;
	mouseY = event.clientY;
	}
	
	
function windowResize()
	{
	clippingsSetContainerHeight()
	}

function initClippings()
	{
	createPageClippingsArray()
	loadClippings();
	setClippingsVisibility();		
	drawClippings();
	}	






function loadClippings()
	{
	clippingsString = null;
	tempArray = document.cookie.split(";");
	x = -1;
		
		for (tA = 0; tA < tempArray.length; tA++)
			{
			if (tempArray[tA].indexOf('clippings=') > -1) 
				{
				tPos = tempArray[tA].indexOf("=")+2;
				clippingsString = tempArray[tA].substring(tPos,tempArray[tA].length); 				}
			
			}
	if (clippingsString != null)
		{		
		tempArray = clippingsString.split("^");
		if (tempArray.length > 1)
		{
		x=0;
		for (i=0; i < tempArray.length/4; i++)
			{
			
			activeClippings[i] = new Clipping(tempArray[x],tempArray[x+1],tempArray[x+2],tempArray[x+3])
			x=x+4;
			}}
		}
	}

function saveClippings()
	{
	tempCookie = "clippings=";
	for(i=0; i < activeClippings.length; i++)
		{
		tempCookie=tempCookie+"^"+(activeClippings[i].id)+"^"+(activeClippings[i].name)+"^"+(activeClippings[i].URL)+"^"+(activeClippings[i].read);
		}
		var expire = new Date ();
   		expire.setTime (expire.getTime() + (6 * 24 * 3600000)); 
   		expire = expire.toGMTString();
	finalCookie = tempCookie+"; path=/; expires="+expire;  	
  	document.cookie = finalCookie;
	}


function eventCheckForCookies()
	{
	document.cookie = "cookies=on";
  	
  	checkForCookie = document.cookie.split(";");
  	
  		for (x=0; x < checkForCookie.length; x++)
  			{
  			if (checkForCookie[x].indexOf("cookies") >= 0) {cookiesOn = true;}
  			}
  		if (cookiesOn == false) alert("Per utilizzare questa funzione\ndevi abilitare i Cookies");
	}

function Clipping(id,name,URL,read)
	{
	this.id = id;
	this.name = name;
	this.URL = URL;
	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";
		}
		
	
	drawClippings();
	}	


function loadClippingURL(pos)
	{
	n = window.open(activeClippings[pos].URL,'_self',+activeClippings[pos].id);
	markRead(activeClippings[pos].id);
	saveClippings();
	}

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();	
	saveClippings();
	}

function eventClearAllClippings()
	{
	
	activeClippings = new Array();
	setClippingsVisibility();
	drawClippings();	
	saveClippings();
	}

function drawClippings()
	{
	newHTML = "";
	
	if (activeClippings.length < 1) 
		{
		newHTML = "<span class=clippingon>Non hai salvato nessun articolo. Per salvare un articolo basta cliccare su <img src='/immagini/add.gif' width='11' height='11'> accanto al suo titolo</span>";
		}
	newHTML +="<table cellpadding=1 cellspacing=2 width=100% align=center>"
	for (i=0; i < activeClippings.length; i++)
		{
		clipping = activeClippings[i];
		
		if (clipping.read == "yes") tClass = "clippingletto";
		else tClass = "clippingon";

		newHTML += "<tr><td><a href='#'  onclick=\"javascript:loadClippingURL("+i+");\" class='"+tClass+"' id='cLink"+clipping.id+"'>";
		newHTML += clipping.name.replace(/\"/i, "");+"</a></td></tr>";
		
		}
		newHTML +="</table>"
	obj = document.getElementById("clippingsContainer");
	
	obj.innerHTML = newHTML;
	
	obj.style.display = "none";
	obj.style.display = "block";
	
	clippingsSetContainerHeight();
	}


function clippingsSetContainerHeight()
	{
	obj = document.getElementById("clippingsContainer");
	if (userScreen.visHeight() > 2000)
		{
		obj.style.height= (userScreen.visHeight()-350)+"px";
		}
	}


function checkForDuplicates()
	{
	for (i=0; i < activeClippings.length; i++)
		{
		if (newClipping == activeClippings[i].id) {i = allClippings.length; duplicate = true;}
		}
	}


function createPageClippingsArray()
	{
		d = document.getElementsByTagName(clippingsTag)
		for (j=0; j < d.length; j++) if (d[j].id.indexOf("clp") > -1) 
			{
			clippingsDivArray[clippingsDivArray.length] = d[j];
			}
	 }

function clippingInstanceVisibility(id,state)
	{
	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 {
	 	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()
	{
	
	for (i=0; i < allClippings.length; i++)
		{
		vis = "visible";
		
		for (x=0; x <activeClippings.length; x++) if (allClippings[i].id == activeClippings[x].id) vis = "hidden";
			obj = "clp"+allClippings[i].id;
			clippingInstanceVisibility(obj,vis)
			}
			
		}

function addClipping()
	{
	newClipping = this.id.substring(3,this.id.length)
	duplicate = false;
	for (i=0; i < allClippings.length; i++) 
		{
		if (newClipping == allClippings[i].id) {pos = i; i = allClippings.length}
		}
		
	if (activeClippings.length > 0)	checkForDuplicates() 
	
	if (!duplicate)
	{	
		eventCheckForCookies();
		if (cookiesOn == true)
			{
			activeClippings[activeClippings.length] = new Clipping(allClippings[pos].id,allClippings[pos].name,allClippings[pos].URL);
			
			clippingInstanceVisibility(this.id,'hidden') 
	 		
	 		drawClippings();	
	 		if (document.all) event.cancleBubble = true;
	 		saveClippings();
		 	}
	 	}
	}
//Added by msanti @ 20050428
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

function ShowHide(divObj)
{
  DOM = (document.getElementById); // IE 5+ et NS 6+ 
  IE4 = (document.all); // IE 4 
  NS4 = (document.layers); // NS 4
  var state = ""; 

  if (DOM) { 
    state = document.getElementById(divObj).style.display;
    if(state == "none" && state != null) {
      document.getElementById(divObj).style.display = "block";
    } else {
      document.getElementById(divObj).style.display = "none";
    }
  } 
  else if (IE4) { 
    state = document.all.divObj.style.display;
    if(state == "none" && state != null) {
      document.all.divObj.style.display = "block";
    } else {
      document.all.divObj.style.display = "none";
    }
  } 
  else if (NS4) { 
    state = document.layers[divObj].style.display;
    if(state == "none" && state != null) {
      document.layers[divObj].style.display = "block";
    } else {
      document.layers[divObj].style.display = "none";
    }
  } 

    if(state == "none" && state != null) {
      // mostro il DIV
      createCookie('cliph','aperto',365);
      document.cliph.src = "/immagini/hide_details.gif";
    } else {	
	  // nascondo il DIV
	  eraseCookie('cliph')
      createCookie('cliph','chiuso',365);
	  document.cliph.src = "/immagini/show_details.gif";
	}

}
// End 20050428


function init()
		{		
		if (document.all) classFix = "className";
		else classFix = "class";
		
		userScreen = new screenObject(); 
		
		window.onresize = windowResize;
		
		initClippings();
		}
		