var dimStart = 100;
var contStart = 160;
var t = 0;

window.onload=function() {
	var urlvariables = getUrlVars();
	if (urlvariables['page']=='photo') {	
		toggle('subnav_photo');
	}
	if (urlvariables['page']=='graphic') {	
		toggle('subnav_graphic');
	}
	var hidden = readCookie('hidden');
	if (hidden == 'yes') {
		dimStart = 0;
		contStart = 80;
		opf = dimStart / 100;
	} else {
		dimStart = 100;
		contStart = 160;
		opf = 1;
	}
	mainn = document.getElementById('main_nav');
	mainc = document.getElementById('main_content');
	netnn = document.getElementById('network_nav');
	netnn.style.cssText = 'filter: alpha(opacity = '+ dimStart +'); opacity: '+ opf +';';
	mainn.style.cssText = 'filter: alpha(opacity = '+ dimStart +'); opacity: '+ opf +';';
	mainc.style.cssText = 'margin-left: '+ contStart +'px; visibility: visible;';
	
	sevenUp.test();
}

function toggle(obj) {
	el = document.getElementById(obj);
	if ( el.style.display != 'block' ) {
		el.style.display = 'block';
	}
	else {
		el.style.display = '';
	}
}

function objHide() {
	x = setTimeout("dimOut(0,80)", 500);
	createCookie('hidden','yes');
}

function dimOut(dimStop,contStop) {
	clearTimeout(t);
	mainn = document.getElementById('main_nav');
	mainc = document.getElementById('main_content');
	netnn = document.getElementById('network_nav');
	if (dimStart > dimStop) {
		dimStart = dimStart - 5;
		if (contStart > contStop) {
			contStart = contStart - 3;
		}
		opf = dimStart / 100;
		netnn.style.cssText = 'filter: alpha(opacity = '+ dimStart +'); opacity: '+ opf +';';
		mainn.style.cssText = 'filter: alpha(opacity = '+ dimStart +'); opacity: '+ opf +';';
		mainc.style.cssText = 'margin-left: '+ contStart +'px; visibility: visible;';
		dimstr = "dimOut("+ dimStop +","+ contStop +")";
		t = setTimeout(dimstr, 2);
	}
}

function objShow() {
	x = setTimeout("dimIn(100,160)", 500);
	createCookie('hidden','no');
}

function dimIn(dimStop,contStop) {
	clearTimeout(t);
	mainn = document.getElementById('main_nav');
	mainc = document.getElementById('main_content');
	netnn = document.getElementById('network_nav');
	if (dimStart < dimStop) {
		dimStart = dimStart + 5;
		if (contStart < contStop) {
			contStart = contStart + 3;
		}
		opf = dimStart / 100;
		netnn.style.cssText = 'filter: alpha(opacity = '+ dimStart +'); opacity: '+ opf +';';
		mainn.style.cssText = 'filter: alpha(opacity = '+ dimStart +'); opacity: '+ opf +';';
		mainc.style.cssText = 'margin-left: '+ contStart +'px; visibility: visible;';
		dimstr = "dimIn("+ dimStop +","+ contStop +")";
		t = setTimeout(dimstr, 2);
	}
}

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    return vars;
}

// Navegar por teclas!
function keyNavigation(e, picURL, imgStart, imgTotal) {
	if (e.keyCode == 36) { // Home
		if (imgStart > 1) {
			document.location = picURL + "start=1";
		}
	}
	if (e.keyCode == 35) { // End
		if (imgStart < imgTotal) {
			document.location = picURL + "start=" + imgTotal;
		}
	}
	if (e.keyCode == 37) { // Left Shift
		if (imgStart > 0) {
			if (imgStart == 1) {
				imgStart = imgTotal;
			} else {
				imgStart = imgStart - 1;
			}
			document.location = picURL + "start=" + imgStart;
		}
	}
	if (e.keyCode == 39) { // Right Shift
		if (imgStart <= imgTotal) {
			if (imgStart == imgTotal) {
				imgStart = 1;
			} else {
				imgStart = imgStart + 1;
			}
			document.location = picURL + "start=" + imgStart;
		}
	}
	if (e.keyCode == 33) { // Pg Up
		if (imgStart > 1) {
			imgStart = imgStart - 5;
			if (imgStart < 1) { imgStart = 1; }
			document.location = picURL + "start=" + imgStart;
		}
	}
	if (e.keyCode == 34) { // Pg Down
		if (imgStart < imgTotal) {
			imgStart = imgStart + 5;
			if (imgStart > imgTotal) { imgStart = imgTotal; }
			document.location = picURL + "start=" + imgStart;
		}
	}
}

function keyNavigationIndicator(e, imgStart, imgTotal) {
	el = document.getElementById('keynav_indicator');
	h = document.getElementById('main_image').offsetHeight / 2;
	el.style.cssText = 'margin-left: '+ contStart +'px;';
	el.style.top = h + "px";
	if (e.keyCode == 36) { // Home
		if (imgStart > 1) {
			el.innerHTML = '<b>First</b>';
			el.style.display = 'block';
		}
	}
	if (e.keyCode == 35) { // End
		if (imgStart < imgTotal) {
			el.innerHTML = '<b>Last</b>';
			el.style.display = 'block';
		}
	}
	if (e.keyCode == 37) { // Left Shift
		if (imgStart > 0) {
			if (imgStart == 1) {
				el.innerHTML = '<b>Last</b>';
			} else {
				el.innerHTML = '<b>&larr;</b>';
			}
			el.style.display = 'block';
		}
	}
	if (e.keyCode == 39) { // Right Shift
		if (imgStart <= imgTotal) {
			if (imgStart == imgTotal) {
				el.innerHTML = '<b>First</b>';
			} else {
				el.innerHTML = '<b>&rarr;</b>';
			}
			el.style.display = 'block';
		}
	}
	if (e.keyCode == 33) { // Pg Up
		if (imgStart > 1) {
			el.innerHTML = '<b>&larr;&larr;</b>';
			el.style.display = 'block';
		}
	}
	if (e.keyCode == 34) { // Pg Down
		if (imgStart < imgTotal) {
			el.innerHTML = '<b>&rarr;&rarr;</b>';
			el.style.display = 'block';
		}
	}
}

/* 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+";";
}

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);
}