var pageNumbers;

function setPageNumbers()
{
    pageNumbers = new Array();
    pageNumbers.push(0);
    for (var i=0;i<data.spreads.length;i++)
    {
        pageNumbers.push(i);
        if (data.spreads[i].isspread)
            pageNumbers.push(i);
    }
}

function getSpreadIndex(pageNumber)
{
    return pageNumbers[pageNumber];
}

function getPageNumber(spread)
{
    for (var i=0;i<pageNumbers.length;i++)
    {
        if (pageNumbers[i] == spread)
        {
            return i;
        }
    }
    return null;
}
function getPageOnNextSpread(currentSpread)
{
    for (var i=0;i<pageNumbers.length;i++)
    {
        if (pageNumbers[i] == currentSpread + 1)
        {
            return i;
        }
    }
    return null;
}

function getPageOnPreviousSpread(currentSpread)
{
    
    for (var i=0;i<pageNumbers.length;i++)
    {
        if (pageNumbers[i] == currentSpread - 1)
        {
            return i;
        }
    }
    return null;
}

function openAction(enrichmentid, obj, type) {
	switch(type)
	{
		case 0:
			for(var i in data.spreads) {
				if(data.spreads[i].id == obj && spreadIndex != i){
					unFocus.History.addHistory(getPageNumber(i));
					break;
				}
			}			
			break;
		case 1:
			var url = defaulturl + enrichmentid + '/redirect.ashx?redirectid=' + obj;
			window.open(url);
			break;
		case 2:
			var url = defaulturl + enrichmentid + '/redirect.ashx?redirect=' + obj;
			window.open(url);
			break;
	}
}

function openArticle(id) {
	var url = defaulturl + id + '/default.aspx';
	width = 750;
	height = 500;
	vleft = (screen.availWidth/2)-(width/2);
	vtop = (screen.availHeight/2)-(height/2);
	
	var win = window.open(url, "republishercontent", 'top=' + vtop + ',left=' + vleft + ',width=' + width + ',height=' + height + ',fullscreen=no,toolbar=no,status=yes,menubar=no,scrollbars=no,resizable=yes,directories=no,location=no')
	win.focus();
}

function openHTML(id) {
	var url = defaulturl + id + '/default.aspx';
	width = 750;
	height = 500;
	vleft = (screen.availWidth/2)-(width/2);
	vtop = (screen.availHeight/2)-(height/2);
	
	var win = window.open(url, "republishercontent", 'top=' + vtop + ',left=' + vleft + ',width=' + width + ',height=' + height + ',fullscreen=no,toolbar=no,status=yes,menubar=no,scrollbars=no,resizable=yes,directories=no,location=no')
	win.focus();
}

function openInfomercial(id) {
	openArticle(id);
}

function openHelp(id, url) {
	width = 750;
	height = 500;
	vleft = (screen.availWidth/2)-(width/2);
	vtop = (screen.availHeight/2)-(height/2);
	
	var win = window.open(url, "republisherhelp", 'top=' + vtop + ',left=' + vleft + ',width=' + width + ',height=' + height + ',fullscreen=no,toolbar=no,status=yes,menubar=no,scrollbars=no,resizable=yes,directories=no,location=no')
	win.focus();
}

function openPrint(url){
	height = 500;
	width = 700;
	vleft = (screen.availWidth/2)-(width/2);
	vtop = (screen.availHeight/2)-(height/2);
	var win = window.open(url, 'print', 'width='+width+',height='+height+',top=' + vtop + ',left=' + vleft + ',,fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no').focus();
}

function mediaFile(id) {
	openArticle(id);
}

function smartZoom(id) {
	id = 'region' + id;
	var region = $(id).obj;
	var left = (region.screenleft + zoom.element.offsetLeft);
	var top = (region.screentop + zoom.element.offsetTop);

	var ratio = Math.max(zoom.height/viewHeight, zoom.width/viewWidth);
	zoom.zoom(left/ratio, top/ratio);
}	

/* cookies */
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 NavigationButton(title, image, click) {
	this.element = document.createElement('a');
	this.element.title = title;
	
	this.imagesrc = image;
	
	this.img = document.createElement('img');
	this.img.src = imageurl + 'buttons/' + this.imagesrc + '.gif';
	this.img.border = '0';
	
	this.element.appendChild(this.img);
	this.element.obj = this;
	
	this.enabled = true;
	this.click = click;	
	
	try{this.element.style.cursor = 'pointer';}
	catch(e){this.element.style.cursor = 'hand';}	
	
	this.element.onclick = click;
	
	this.element.onmouseover = function() {
		if(this.obj.enabled) {
			this.obj.img.src = imageurl + 'buttons/' + this.obj.imagesrc + '_hover.gif';;
		}
	}	
	
	this.element.onmouseout = function() {
		if(this.obj.enabled) {
			this.obj.img.src = imageurl + 'buttons/' + this.obj.imagesrc + '.gif';
		}
	}
}

NavigationButton.prototype.setHref = function(pagenumber)
{
    pagenumber = parseInt(pagenumber);
    if (isNaN(pagenumber))
        this.element.href = 'javascript://';
        
    else if (pagenumber >= 0 && pagenumber <= pageNumbers.length)
    {
     //   this.element.href = '#' + pagenumber;
        //do it with the onclick also, so we don't have to wait for the timer event to trigger
        this.element.onclick= function() {unFocus.History.addHistory(pagenumber); return false;};
    }
}

NavigationButton.prototype.setEnabled = function(enabled) {
	this.enabled = enabled;
	try{this.element.style.cursor = enabled ? 'pointer' : 'auto';}
	catch(e){this.element.style.cursor = enabled ? 'hand' : 'auto';}
		
	this.img.src = imageurl + 'buttons/' + this.imagesrc + (enabled ? '' : '_dis') + '.gif';	
}
