var uselightbox = 1;
runinit();
var scrolldirection = 1;

//	alert(parent.frames.length);
//	alert(window.parent.main.document);
//	alert(document.cookie);
//	alert(navigator.appName + ' | ' + navigator.appVersion + ' | ' + navigator.appCodeName + ' | ' + navigator.userAgent);


function runinit(){
	if (document.cookie.indexOf('style=') != -1) {
		mystyle = readCookie('style');
		setActiveStyleSheet(mystyle);
	}
	if (window.name == 'left') {
		if (document.cookie.indexOf('uselightbox=') != -1) {
			uselightbox = readCookie('uselightbox');
		}
		styleswitcher();
		if (document.cookie.indexOf('style=') != -1) stylemenuactive(mystyle); //don't call before styleswitcher() has run
		togglelb(1);
	}
}


function styleswitcher(){
	document.write('<style type="text/css">');
	document.write('span.styleswitcher { display: none; position: absolute; bottom: 15px; left: 0px; line-height: 15px; font-size: 12px; background: #FFFFFF; opacity: 0.7; margin: 0px; padding: 3px; z-index: 10; border-width: 1px ; border-color: #000000; border-style: solid ; background-image: url("backbw.jpg")} html>body span.styleswitcher {background-image: none}');
	document.write('span.styleswitcher a { color: #000000; text-decoration: none; white-space: nowrap ; font-weight: normal }');
	document.write('span.styleswitcher a:hover { color: #000000; text-decoration: none; background-color: #BBBBBB }');
	document.write('span.styleswitchericon { position: absolute; bottom: 4px; left: 4px; background: #FFFFFF; width: 56px; height: 15px; margin: 0px; color: #000000; font-size: 12px; padding-left: 2px; z-index: 10; border-width: 1px; border-color: #000000; border-style: solid}');
	document.write('</style>');
	document.write('<span class="styleswitchericon" onclick="openclosestyleswitch(1,1)" onmouseover="openclosestyleswitch(1,0)">pick style</span>');
	document.write('<span id="styleswitcher" class="styleswitcher" onclick="openclosestyleswitch(0,1)">');
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("stylesheet") != -1 && a.getAttribute("title")) {
			document.write('<a id="' + a.getAttribute("title") + '" href="javascript:setActiveStyleSheet(\'' + a.getAttribute("title") + '\',1);">' + a.getAttribute("title") + '</a><br>');
		}
	}
	document.write('<br>');
	document.write('<a id="uselightbox" href="javascript:togglelb()">use lightbox</a><br>');
	document.write('</span>');
}

function stylemenuactive(title) { // show the current stylesheet as active in the menu. don't call this before styleswitcher()
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("stylesheet") != -1 && a.getAttribute("title")) {
			if (a.disabled == false) {
				document.getElementById(a.getAttribute("title")).style['color'] = '#666666';
				document.getElementById(a.getAttribute("title")).style['font-style'] = 'italic';
			} else {
				document.getElementById(a.getAttribute("title")).style['color'] = '#000000';
				document.getElementById(a.getAttribute("title")).style['font-style'] = 'normals';
			}
		}
	}
}

function openclosestyleswitch(open,close) {   // call with (1,1) for toggle, (1,0) to open only, (0,1) to close only
	if (document.getElementById('styleswitcher').style['display'] != 'inline' && open) {
		document.getElementById('styleswitcher').style['display'] = 'inline' ;
	} else if (document.getElementById('styleswitcher').style['display'] == 'inline' && close) {
		document.getElementById('styleswitcher').style['display'] = 'none' ;
	}
}

function setActiveStyleSheet(title,allframes) {
	var f, i, a;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if(a.getAttribute("rel").indexOf("stylesheet") != -1 && a.getAttribute("title")) {
				a.disabled = true; // not doing if/else since IE wont understand turning on a stylesheet without turning it off first..
				if(a.getAttribute("title") == title) a.disabled = false;
			}
		}
	if (allframes && window.parent.frames.length != 0) {	// load style in all frames if called with (title,1)
		for (f=0; f < window.parent.frames.length ; f++) {
			for(i=0; (a = window.parent.frames[f].document.getElementsByTagName("link")[i]); i++) {
				if(a.getAttribute("rel").indexOf("stylesheet") != -1 && a.getAttribute("title")) {
					a.disabled = true;
					if(a.getAttribute("title") == title) a.disabled = false;
				}
			}
		}
	}
	createCookie('style', title, 7);
	if (allframes && window.name == 'left') stylemenuactive(title); // show the new stylesheet as active in the menu
}


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 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 showhide(showorhide,elem){
	if (showorhide == 'show') {
		var displayhideme='inline';
		var displayshowhiddenlink='none';
	} else {
		var displayhideme='none';
		var displayshowhiddenlink='inline';
	}
	var elements = document.getElementsByTagName(elem);
	for(var i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			if(node.attributes.item(j).nodeName == 'class') {
				if(node.attributes.item(j).nodeValue.slice(0,6) == 'hideme') {
					eval('node.style.display=displayhideme');
				}
			}
		}
	}
	document.getElementById('hidehidden').style['display'] = displayhideme;
	document.getElementById('showhidden').style['display'] = displayshowhiddenlink;
}		//document.styleSheets[0].cssRules[1]].style.display='inline')


function scrollpage(scrollpixels,secondrun){
	var position=document.documentElement.scrollTop;
	var scrolltot = 0;
	var scrollstep = 0;
	var scrollthis = 0;
	while (scrolltot  < scrollpixels) {
		if ((scrolltot + scrollstep + 1) * 2 < scrollpixels ) {
			scrollstep++;	// speedup for first half of scrolldistance
			scrollthis=scrollstep;
		} else if (scrolltot * 2 < scrollpixels) {
			scrollthis=(scrollpixels - scrolltot * 2); // middle
		} else {
				scrollthis=scrollstep;
				scrollstep--; //slowdown for second half
		}
		scrollthis=scrollthis-10; // -10 : will scroll 10 pixels one-by-one before speeding up and 30 after slowing down
		if (scrollthis < 1) scrollthis = 1;
		scrolltot = scrolltot + scrollthis;
		self.scrollBy(0,scrollthis * scrolldirection);
	}
	if (secondrun != 1) {if (position == document.documentElement.scrollTop ) { // switch direction at top/bottom
		scrolldirection = -scrolldirection ;
		scrollpage(scrollpixels,1);
	}}
}

function togglelb(notoggle) {
	if (!notoggle) {
		if (uselightbox == 1) {
			uselightbox = 0;
		} else {
			uselightbox = 1;
		}
	}
	if (uselightbox == 0) {
		document.getElementById('uselightbox').style.color = '#666666';
		document.getElementById('uselightbox').style.textDecoration = 'line-through';
	} else {
		document.getElementById('uselightbox').style.color = '#000000';
		document.getElementById('uselightbox').style.textDecoration = 'none';
	}
	createCookie('uselightbox', uselightbox, 7);
}

function newHTTP() {if (navigator.appName == "Microsoft Internet Explorer") { return new ActiveXObject("Microsoft.XMLHTTP"); } else { return new XMLHttpRequest();}}
function JSONparse(jsonstr) {if (typeof JSON != 'undefined') {return JSON.parse(jsonstr)}else{return jsonParse(jsonstr)}} // use native JSON for firefox 3.1+, otherwise json_sans_eval.js
// json_sans_eval_min.js
var jsonParse=(function(){var number='(?:-?\\b(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b)';var oneChar='(?:[^\\0-\\x08\\x0a-\\x1f\"\\\\]'+'|\\\\(?:[\"/\\\\bfnrt]|u[0-9A-Fa-f]{4}))';var string='(?:\"'+oneChar+'*\")';var jsonToken=new RegExp('(?:false|true|null|[\\{\\}\\[\\]]'+'|'+number+'|'+string+')','g');var escapeSequence=new RegExp('\\\\(?:([^u])|u(.{4}))','g');var escapes={'"':'"','/':'/','\\':'\\','b':'\b','f':'\f','n':'\n','r':'\r','t':'\t'};function unescapeOne(_,ch,hex){return ch?escapes[ch]:String.fromCharCode(parseInt(hex,16));};var EMPTY_STRING=new String('');var SLASH='\\';var firstTokenCtors={'{':Object,'[':Array};return function(json){var toks=json.match(jsonToken);var result;var tok=toks[0];if('{'===tok){result={};}else if('['===tok){result=[];}else{throw new Error(tok);};var key;var stack=[result];for(var i=1,n=toks.length;i<n;++i){tok=toks[i];var cont;switch(tok.charCodeAt(0)){default:cont=stack[0];cont[key||cont.length]=+(tok);key=void 0;break;case 0x22:tok=tok.substring(1,tok.length-1);if(tok.indexOf(SLASH)!==-1){tok=tok.replace(escapeSequence,unescapeOne);};cont=stack[0];if(!key){if(cont instanceof Array){key=cont.length;}else{key=tok||EMPTY_STRING;break;}};cont[key]=tok;key=void 0;break;case 0x5b:cont=stack[0];stack.unshift(cont[key||cont.length]=[]);key=void 0;break;case 0x5d:stack.shift();break;case 0x66:cont=stack[0];cont[key||cont.length]=false;key=void 0;break;case 0x6e:cont=stack[0];cont[key||cont.length]=null;key=void 0;break;case 0x74:cont=stack[0];cont[key||cont.length]=true;key=void 0;break;case 0x7b:cont=stack[0];stack.unshift(cont[key||cont.length]={});key=void 0;break;case 0x7d:stack.shift();break;}};if(stack.length){throw new Error();};return result;};})();

// javascript:getthumbs('2009-10h Chile; Valparaiso')
function getthumbs(foldername,destid,titleid) {
	if (!destid)	{ destid = 'thumbarea' }
	if (!titleid)	{ titleid = 'maintitle' }
	var http = newHTTP();
	http.open("GET", "./folderlist.php?dir="+foldername);
	http.onreadystatechange=function() {
		if (http.readyState == 4) {
			var result = JSONparse(http.responseText);
			document.getElementById('map_canvas').style.height = '220px';
			map.removeControl(bigzoomcontrol);
			map.addControl(smallzoomcontrol);
			document.getElementById(titleid).innerHTML = result.title;
			document.getElementById('prevlink').href = 'javascript:getthumbs("' + result.prevdir + '")';
			document.getElementById('nextlink').href = 'javascript:getthumbs("' + result.nextdir + '")';
			document.getElementById('prevlink').style.display = 'inline' ;
			document.getElementById('nextlink').style.display = 'inline' ;
			var filelist = result.list;
			var newHtml = '';
			for (var i=0, ln=filelist.length; i<ln; i++) {
				newHtml += "<table class='indexthumbsh'><tr><td>"+
				"<a rel='lytebox[photos]' class='indexthumbsh' href='./photo.php?dir="+foldername+"&file="+filelist[i].src+"' title=\""+filelist[i].desc+"\">"+
				"<img class='indexthumbsh' src='./"+foldername+"/tn/tn_"+filelist[i].src+"'></a>"+
				"<td class='shr'><img src='0.gif'><tr><td class='shb'><img src='0.gif'><td class='shbr'></table>\n";
			}
			document.getElementById(destid).innerHTML = newHtml;
			initLytebox();
		}
	}
	http.send(null);
}
