/*
Copyright Justin Whitford 2006.
  http://www.whitford.id.au/
Perpetual, non-exclusive license to use this code is granted
on the condition that this notice is left in tact.
Modified By: Pavan Prasad - APN Online
Date: 28-02-2008
*/

var delim = '|';
var trailLength = 5;//number of links to display
var chunks;
var DAY = 24 * 60 * 60 * 1000;
var StTitle = '';

var trailLinks_name = 'trailLinks';
var trailText_name = 'trailText';
var newsflash_name = 'nfcookie';

function setCookie(name, value) { $.cookie(name, value, {path:'/', expires:1, domain:'nzherald.co.nz'}); }
function getCookie(name) { return $.cookie(name); }

function checkRecentVisits(StoryTitle){
    StTitle = encodeURIComponent(escape(StoryTitle));
    crumbList = new CrumbList();
    if(getCookie(trailLinks_name))
    {
        var staleLinkCrumbs = getCookie(trailLinks_name).split(delim);
        var staleTextCrumbs = getCookie(trailText_name).split(delim);
        var startPos = (staleTextCrumbs.length<=trailLength || document.location==staleLinkCrumbs[staleLinkCrumbs.length-1] ) ? 0 : 1;
        for(i=startPos;i<staleLinkCrumbs.length;i++) { crumbList.add(staleLinkCrumbs[i],staleTextCrumbs[i]); }
    }

    if(document.location!=crumbList.links[crumbList.links.length-1] ) { crumbList.add(document.location,StTitle); }

	var SearchStr = new String(staleTextCrumbs);
	if(SearchStr.search(StTitle) == -1 )
	{
    	setCookie(trailLinks_name,crumbList.links.join(delim));
        setCookie(trailText_name ,crumbList.text.join(delim));
	}
    crumbList.output();
}

function CrumbList(){
  this.links=new Array();
  this.text=new Array();
  this.add = crumbListAdd;
  this.output = crumbListShow;
}
function crumbListAdd(href,text){
	this.links[this.links.length]=href;
	this.text[this.text.length]=text;
}
function crumbListShow(){
	//only show this when the number of visited list is greater than 1 
	//since the first link will always the same story being viewed
	if(this.links.length > 1)// && this.text[this.links.length - 1 ] != StTitle
	{
		document.write('<div class="divider-dots"></div><span class="headlinesPanel-subheading">You Recently Viewed ...</span><div class="divider-dots"></div><ul class="linkList">');
		for(var i=this.links.length-1 ; i >=0 ;i--)
		{
		  	if(this.text[i] != StTitle)
			{
				document.write(" <li> <a href='" + this.links[i] + "'>"+ unescape(decodeURIComponent(this.text[i])) + "</a></li>" );
			}
		}
		document.write('</ul>');
	}
}

/* ** Codes for NewsFlash ** */
function removeNewsflash(id)
{
	var nfBlock = document.getElementById(id);
	nfBlock.innerHTML = '';
	nfBlock.style.display = 'none';
	setCookie('nfCookie'+newsflashid+'_'+newsflashbartype, newsflashurl);
}

function getNFCookie(id)
{
	var nfcookie = getCookie('nfCookie'+id+'_'+newsflashbartype);
	if(!nfcookie)
	{
		document.write(newsflash);
	}
	else if(nfcookie != newsflashurl)
	{
			document.write(newsflash);
	}
}

/* ** End Codes for NewsFlash ** */ 
//--------------------------------------
