// generic functions

function getURL(url) {
	location.href=url
}

function setVisible (id,bool) {
	var id = document.getElementById(id);
	//alert(id);
	if (id) id.style.display = (bool) ? 'block' : 'none';
}

// top navigation bar

var timeout=null;
var defaultState="";
var otherState="";

function changeNavigation(p1,p2) {
	if (timeout!=null) { clearTimeout(timeout); timeout=null; }
	if (defaultState == "") { defaultState = p1; otherState=p2; }
	setVisible(p2,true);
	setVisible(p1,false);
}

function changeNavBack(obj,event) {
	// check contains
	if (mouseLeaves(obj,event)) {
		if (timeout!=null) { clearTimeout(timeout); timeout=null; }
		if (defaultState != "") { timeout=setTimeout('changeNavigation("'+otherState+'","'+defaultState+'")', 5000) }
	}
}

function mouseLeaves(element,evt) {
	if (typeof(evt.toElement)!= 'undefined' && evt.toElement && typeof(element.contains)!= 'undefined') {
		return !element.contains(evt.toElement);
	} else if (typeof(evt.relatedTarget)!= 'undefined' && evt.relatedTarget) {
		return !contains(element, evt.relatedTarget);
	} else {
		// crap browser alert! crap browser alert!
		return false;
	}
}

function contains (container, containee) {
	while (containee) {
		if (container == containee) {
			return true;
		}
		containee = containee.parentNode;
	}
	return false;
}

// for archive pages

function initArchiveNavigation() {
	// hide prev button
	url = document.URL;
	searchid = -1;
	if (url.lastIndexOf("contentId=-1") == -1) {
		pos = url.lastIndexOf("contentId=");
		searchid = url.slice(pos + 10, url.length);
	}
	if (searchid > -1) {
		foundpos = -1;
		for (var i = 0; i <= itemcount; i++) {
			url = document.getElementById("li" + i).firstChild.href;
			pos = url.lastIndexOf("contentId=");
			foundid = url.slice(pos + 10, url.length);
			foundpos++;
			if (foundid == searchid) {
				
				cursor = Math.floor(foundpos / perpage);
				offset = cursor;
				setArchivePage(0);
				break;
			}			
		}
		
		//alert(Math.floor(foundpos / perpage)-1);
		//$offset = Math.floor(foundid / perpage) ;
		//setArchivePage(1);
	} else {
		setArchivePage(0);
	}
}

function setArchivePage(step) {
	offset += step;
	setVisible("btn_prev1",(offset > 0));
	setVisible("btn_next1",(offset < Math.ceil(itemcount / perpage)-1));
	for (var i = 0; i <= itemcount; i++) {
		setVisible("li" + i,(i<((offset + 1)*perpage))&& (i>(offset*perpage)-1));
	}
	
}