allCollectors = new Array();  //all Collectors on page
activeCollectors = new Array() //all Collectors stored by the user
currentCollector = 0; //where in the list of Collectors is the user

function addToShelf(id, caption, link)
{
	allCollectors[allCollectors.length] =  new Collector(id, caption, link, "no");
}

function loadCollectors()
{
	CollectorsString = null;
	tempArray = document.cookie.split(";");
	x = -1;
		
	for (tA = 0; tA < tempArray.length; tA++)
	{
		if (tempArray[tA].indexOf('Collectors=') > -1) //found the Collectors section
		{
			tPos = tempArray[tA].indexOf("=")+2;
			CollectorsString = tempArray[tA].substring(tPos,tempArray[tA].length); //striping out "Collectors=^"
		}
	}
	
	if (CollectorsString != null)
	{		
		tempArray = CollectorsString.split("^");
		if (tempArray.length > 1)
		{
			x=0;
			for (i=0; i < tempArray.length/4; i++)
			{
				//alert(tempArray[x]+" "+tempArray[x+1])
				activeCollectors[i] = new Collector(tempArray[x],tempArray[x+1],tempArray[x+2],tempArray[x+3])
				x=x+4;
			}
		}
	}
}
	
function saveCollectors()
{
	tempCookie = "Collectors=";
	for(i=0; i < activeCollectors.length; i++)
	{
		tempCookie=tempCookie+"^"+(activeCollectors[i].id)+"^"+(activeCollectors[i].headline)+"^"+(activeCollectors[i].url)+"^"+(activeCollectors[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 = "Collectors=";
	for(i=0; i < activeCollectors.length; i++)
		{
		tempCookie=tempCookie+"^"+(activeCollectors[i].id)+"^"+(activeCollectors[i].headline)+"^"+(activeCollectors[i].url)+"^"+(activeCollectors[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("Collectors") >= 0) {cookiesOn = true;}
  			}
  		if (cookiesOn == false) alert("Engedélyezni kell a cookie-kat a cikkgyüjtõ használatához!");
	
	}

function Collector(id,headline,url,read)
{
	this.id = id;
	this.headline = headline;
	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 < activeCollectors.length; i++)
		if (activeCollectors[i].id == id) 
		{
			activeCollectors[i].read = "yes";
			saveCollectors();
		}
}	

function eventShowNextCollector()
{
	if (activeCollectors != null) 
	{
		total = 0;
		for (aC = 0; aC < activeCollectors.length; aC++)
		{
			if (activeCollectors[aC].read != "yes") 
			{
				markRead(activeCollectors[aC].id);				
				saveCollectors();
				document.location = activeCollectors[aC].url;
				break;
			}
		}
	}
}

function eventClearReadCollectors()
{
	tempCollectors = new Array()
	x = 0;
	for (i=0; i < activeCollectors.length; i++)
		if (activeCollectors[i].read != "yes") 
		{
			tempCollectors[x] = activeCollectors[i]; 
			x++
		}
	
	activeCollectors = tempCollectors;
	drawCollectors();
	setCollectorsVisibility();	
	updateCollectorCounter();
	saveCollectors();
}
	
function eventClearAllCollectors()
{
	activeCollectors = new Array();
	drawCollectors();
	setCollectorsVisibility();	
	updateCollectorCounter();
	saveCollectors();
}
	
function drawCollectors()
{
	newHTML = "";
	
	if (activeCollectors.length < 1) 
	{
		newHTML = "<span class='CollectorItem'>A Cikkgyűjtő segítségével nem marad le semmiről. Nézze át oldalunkat, és válogassa össze azokat a cikkeket, amelyek felkeltették az érdeklődését! Egy cikk felvételéhez kattintson a címe mellett található <img src='/images/doc_small.gif' border='0'/> jelre.<br>";
		newHTML += "</span>";
	} 
		
	for (i=0; i < activeCollectors.length; i++)
	{
		
		aCollector = activeCollectors[i];
		
		if (aCollector.read == "yes") 
			tClass = "CollectorItemRead";
		else 
			tClass = "CollectorItem";
		
		newHTML += "<a href='"+aCollector.url+"' onmousedown=\"markRead('"+aCollector.id+"')\" class='"+tClass+"' id='cLink"+aCollector.id+"'>";
		newHTML += unescape(aCollector.headline)+"</a>";
		
	}
		
	obj = document.getElementById("CollectorsContainer");
	if (obj)
		obj.innerHTML = newHTML;
}

function checkForDuplicates()
{
	for (i = 0; i < activeCollectors.length; i++)
		if (newCollector == activeCollectors[i].id) 
		{
			i = allCollectors.length; 
			duplicate = true;
		}
}

var CollectorsDivArray = new Array();

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


function CollectorInstanceVisibility(id, state)
	//finds duplicate Collectors on a page
{
	t = document.getElementsByName("clp" + id);
	//alert("2");
	if(t.length > 0)
		{
		for (j=0; j < t.length; j++) 
			{	
			t[j].style.visibility = state;
			t[j].onclick = addCollector;
			}
		}
	else { //this is a hack, and possibly slow bu IE5mac and NS6 do not return a length on Object collections
	 	d = CollectorsDivArray;
  		{  		
		for (j=0; j < d.length; j++) if (d[j].id == id) 
			{
			d[j].style.visibility = state;
			d[j].onclick = addCollector;
			}
		}
		}		
	}
	
function setCollectorsVisibility()
{
	//sets the visibility and events for an object;
	for (i=0; i < allCollectors.length; i++)
	{
		vis = "visible";
		
		//find if this Collector is already selected
		for (x=0; x <activeCollectors.length; x++) 
			if  (allCollectors[i].id == activeCollectors[x].id) 
				vis = "hidden";
	
			obj = "clp" + allCollectors[i].id;
			if (obj)
				CollectorInstanceVisibility(obj,vis)
	}			
}

function addCollector()
{
	newCollector = this.id.substring(3,this.id.length)
	duplicate = false;
	for (i=0; i < allCollectors.length; i++) //find the position the allCollectors array of the selected Collector
		if (newCollector == allCollectors[i].id) 
		{
			pos = i; 
			i = allCollectors.length
		}
		
	if (activeCollectors.length > 0)	
		checkForDuplicates();
	
	if (!duplicate)
	{	
		eventCheckForCookies();
		if (cookiesOn == true)
		{
			activeCollectors[activeCollectors.length] = new Collector(allCollectors[pos].id, escape(allCollectors[pos].headline), allCollectors[pos].url, "no");
			
	 		CollectorInstanceVisibility(this.id, 'hidden') //find all instances of the Collector in article and hides it
	 			 		
	 		drawCollectors();	
	 		
	 		/*event.cancleBubble = true;	 		*/
			updateCollectorCounter();
			saveCollectors();
 		}
	}
}

function updateCollectorCounter()
	//positions the numbers in the Collector counter 
	//based on the number of UNREAD Collectors
	{
	t2 = 0;
	t1 = 0;
	if (activeCollectors != null) 
		{
		total = 0;
		for (aC = 0; aC < activeCollectors.length; aC++)
			{
			if (activeCollectors[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 (currentCollector != null) 
		{
		total = currentCollector;
		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 initCollectors()
//reads in the Collectors on page from an array;
{
	createPageCollectorsArray()
	loadCollectors();
	setCollectorsVisibility();		
	updateCollectorCounter();
	drawCollectors();	
}	

function initialize()
{
	initCollectors(); 
}


function ShowCollectors()
{
    var obj = document.getElementById("menuCollectors");
    var offsetTop = $('header').offsetTop;	   
    var height = Element.getHeight('header') + 24;	
    
    obj.style.top = offsetTop + height + "px"; 	    
	    	        
    if (obj.style.display == '' || obj.style.display == 'none'){
        obj.style.display = 'block';
    } else {
        obj.style.display = 'none';
    }

}

function HideCollectors()
{
	document.onclick = null;
	obj = document.getElementById("menuCollectors");
	if (obj) 
	{
		obj.style.display = "none";
	}
}




/* Foci VB */

function addVote(voteID, answerID, articleID)
{
	if (!answerID || answerID == "")
	{
		return false;
	}
	
	HVG.Controls.VoteList.AddVote
	(
        voteID, 
        answerID, 
        articleID,
        function(res) 
        { 
			if (res.error)
			{
				alert (res.error);				
			} else {
				if (res.value.length != 0)
				{
					var div = document.getElementById("AnswerDiv" + voteID);
					if (div)
						div.innerHTML = res.value;
				}
			}
        }
    );

}

function AddUserQuestion(userID, questionID)
{
	if (!userID || userID == "")
	{
		return false;
	}

	if (!questionID || questionID == "")
	{
		return false;
	}

	HVG.Controls.QuestionPageLink.AddUserQuestion
	(
        userID, 
        questionID,
        function(res) 
        { 
			if (res.error)
			{
				alert(res.error);				
			} 
			else 
			{
				if (res.value.length != 0)
				{
					var div = document.getElementById("back");
					if (div)
						div.innerHTML = res.value;	
				}
			}
        }
    );
}
