﻿var prefs = {'stylesheet': readCookie('style') };

prefs.is_iPhone = / iPhone OS /.test(navigator.userAgent);
if ( prefs.is_iPhone ) {
	prefs.stylesheet = 'iphone';
	prefs.like_iPhone = true;
} else {
	prefs.stylesheet = prefs.stylesheet == null ? 'clean' : prefs.stylesheet;
	prefs.like_iPhone = ( prefs.stylesheet == 'iphone' );
}
document.writeln('<link rel="stylesheet" href="/'+prefs.stylesheet+'.css" type="text/css" charset="utf-8" />');
prefs.iframedelta = prefs.stylesheet=='style' ? 140 : ( prefs.stylesheet=='fancy' ? 140 : ( prefs.stylesheet=='basic' ? 250 : ( prefs.stylesheet=='iphone' ? 130 : 200)));
window.onload = function() { fontSizeInit(); fixExternalLinks() }

function fontSizeInit() {
	if ( prefs.like_iPhone ) return;
	prefs.fontRule = document.styleSheets[1].cssRules[0].style;
	prefs.origFontSize = prefs.fontRule.fontSize.toString();
	prefs.fontSize = readCookie('fontSize');
	prefs.fontSize = parseInt(prefs.fontSize) ? parseInt(prefs.fontSize) : 0;
	fontSizeSet();
}

function fixExternalLinks() {
	if (! document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank";
	}
}

function fontSizeSet() {
	if ( prefs.like_iPhone ) return;
	var cursize = prefs.origFontSize;
	var curunit = cursize.replace(/^[0-9.]+([a-z]{2})$/,"$1");
	cursize = cursize.replace(/^([0-9.]+)[a-z]{2}$/,"$1");
	cursize = curunit == 'em' ? parseInt(parseFloat(cursize) * 20) : parseInt(cursize);
	cursize += prefs.fontSize;
	cursize = curunit == 'em' ? String(cursize /20 ) : String(cursize);
	prefs.fontRule.fontSize = cursize+curunit;
}

function fontSizeReset() { fontSizeCheck(0) }

function fontSizeIncrease() { fontSizeCheck( prefs.fontSize +1 ) }

function fontSizeReduce() { fontSizeCheck( prefs.fontSize -1 ) }

function fontSizeCheck(c) {
	if ( prefs.like_iPhone ) return;
	c == 0 ? eraseCookie('fontSize') : writeCookie('fontSize',c);
	prefs.fontSize = c;
	fontSizeSet();
}

function fixSize() { if (! prefs.is_iPhone ) if (getObj('jsframe')) getObj('jsframe').height = (window.innerHeight - prefs.iframedelta) }

function getObj(i) {
	if (document.getElementById) return (document.getElementById(i)) ? document.getElementById(i) : false;
	if (document.all) return (document.all[i]) ? document.all[i] : false;
	return false;
}

function extlink(u) { window.open(u,'extLinksWindow') }

function commonNavCode(curPage) {
	var i,pageMap = [];
	var d, h, s, tgt = document.getElementById('mainContent');
	var aspects = new Array('original','clean','heavy','fancy','vanilla','iphone');
	var aspectNames = new Array('Original','Clean','Heavy','Fancy','Vanilla','iPhone/iPad');
	pageMap[pageMap.length] = {'name':'index','url':'/', 'short':'  ', 'text':'Home Page'};
	pageMap[pageMap.length] = {'name':'email','url':'/mail','text': '@crossbow.it', 'short':'Mail' };
	if ( ! prefs.is_iPhone ) pageMap[pageMap.length] = {'name':'webmail','url':'/webmail','text':'Webmail'};
	if ( ! prefs.is_iPhone ) pageMap[pageMap.length] = {'name':'javascript','url':'/javascript','text':'JavaScript'};
	pageMap[pageMap.length] = {'name':'testi','url':'/testi','text':'Tomi' };
	pageMap[pageMap.length] = {'name':'pillole','url':'/pillole','text':'Pillole' };
	if ( ! prefs.is_iPhone ) pageMap[pageMap.length] = {'name':'macutils','url':'/macutils','text':'Utilities Mac'};
	pageMap[pageMap.length] = {'name':'chi','url':'/chisono','text':'L’ospite', 'short':'/me'};
	if ( ! prefs.is_iPhone ) pageMap[pageMap.length] = {'name':'dvdlist','url':'/dvd','text':'DVD'};
	//pageMap[pageMap.length] = {'name':'dreambox','url':'tv.html','text':'Dreambox'};
	with ( d = document.createElement( 'div' ) ) {
		setAttribute('id', 'siteId' );
		setAttribute('class', 'sideBox' );
		appendChild( document.createTextNode( 'CROSSBOW.it' ) );
		if ( ! prefs.is_iPhone ) {
			with ( h = document.createElement( 'span' ) ) {
				setAttribute( 'id', 'copyright' );
				appendChild( document.createTextNode( '© 2006-'+( (new Date() ).getFullYear() )+' Marco Balestra' ) );
			}
			appendChild( h );
		}
	}
	tgt.parentNode.insertBefore( d, tgt );
	with ( d = document.createElement( 'div' ) ) {
		setAttribute( 'id' , 'navBox' );
		setAttribute( 'class' , 'sideBox' );
		with ( h = document.createElement( 'div' ) ) {
			setAttribute( 'id' , 'navBar' );
			for (i=0; i< pageMap.length; i++) {
				with ( s = document.createElement( 'div' ) ) {
					setAttribute( 'class' , curPage == pageMap[i].name ? 'current' : 'link' );
					if ( prefs.like_iPhone && pageMap[i]['short'] ) {
						appendChild( document.createTextNode( pageMap[i]['short'] ) );
						if ( ! prefs.is_iPhone ) setAttribute( 'title', pageMap[i].text );
					} else {
						appendChild( document.createTextNode( pageMap[i].text ) );
					}
					eval( "s.addEventListener( 'click', function() { location.href='"+pageMap[i].url+"' }, false );" );
				}
				appendChild( s );
			}
		}
		appendChild( h );
	}
	tgt.parentNode.insertBefore( d, tgt );
	if ( prefs.is_iPhone ) {
		addEventListener("load", function() { setTimeout(scrollTo, 0, 0, 1); }, false);
	} else {
		with (  d = document.createElement( 'div' ) ) {
			setAttribute( 'id' , 'aspect' );
			setAttribute( 'class' , 'sideBox' );
			with ( h = document.createElement( 'div' ) ) {
				setAttribute( 'class' , 'label' );
				appendChild( document.createTextNode('Stile:') );
			}
			appendChild( h );
			with ( h = document.createElement( 'div' ) ) {
				setAttribute( 'class' , 'controls' );
				with ( s = document.createElement( 'select' ) ) {
					setAttribute( 'id' , 'aspectSelect' );
					addEventListener( 'change', function(){ writeCookie('style', this.options[this.selectedIndex].value,30);location.reload(); }, false);
					for (i=0; i<aspects.length; i++) s.options[ s.options.length ] = new Option( aspectNames[i], aspects[i] );
					value = prefs.stylesheet;
				}
				appendChild( s );
			}
			appendChild( h );
		}
		tgt.parentNode.appendChild( d );
		with (  d = document.createElement( 'div' ) ) {
			setAttribute( 'id' , 'w3validations' );
			setAttribute( 'class' , 'sideBox' );
			with ( h = document.createElement( 'img' ) ) {
				setAttribute( 'style', 'width:88px;height:31px' );
				setAttribute( 'src', '/img/validxhtml11.png' );
				setAttribute( 'alt', 'Valid XHTML 1.1!' );
				addEventListener('click', function() { extlink( 'http://validator.w3.org/check?uri='+escape(location.href) ); }, false);
			}
			appendChild( h );
			with ( h = document.createElement( 'img' ) ) {
				setAttribute( 'style', 'width:88px;height:31px' );
				setAttribute( 'src', '/img/validcss.gif' );
				setAttribute( 'alt', 'Valid CSS!' );
				addEventListener('click', function() { extlink('http://jigsaw.w3.org/css-validator/validator?uri=http%3A%2F%2Fwww.crossbow.it%2F'+prefs.stylesheet+'.css&amp;usermedium=all'); }, false);
			}
			appendChild( h );
		}
		tgt.parentNode.appendChild( d );
		with ( d = document.createElement( 'div' ) ) {
			setAttribute( 'id', 'fontSize' );
			setAttribute( 'class' , 'sideBox' );
			with ( h = document.createElement( 'div' ) ) {
				setAttribute( 'class' , 'label' );
				appendChild( document.createTextNode('Font:') );
			}
			appendChild( h );
			with ( h = document.createElement( 'div' ) ) {
				setAttribute( 'class' , 'controls' );
				with ( s = document.createElement( 'span' ) ) {
					setAttribute( 'class' , 'control' );
					setAttribute( 'title' , 'riduci' );
					addEventListener( 'click', fontSizeReduce, false);
					appendChild( document.createTextNode('–') );
				}
				appendChild( s );
				with ( s = document.createElement( 'span' ) ) {
					setAttribute( 'class' , 'control' );
					setAttribute( 'title' , 'reset' );
					addEventListener( 'click', fontSizeReset, false);
					appendChild( document.createTextNode('#') );
				}
				appendChild( s );
				with ( s = document.createElement( 'span' ) ) {
					setAttribute( 'class' , 'control' );
					setAttribute( 'title' , 'aumenta' );
					addEventListener( 'click', fontSizeIncrease, false);
					appendChild( document.createTextNode('+') );
				}
				appendChild( s );
			}
			appendChild( h );
		}
		tgt.parentNode.appendChild( d );
	}
}

function writeCookie(name, value, days) {
	var date,expires;
	if (days) {
		date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	} else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var c,ca = document.cookie.split(';');
	var nameEQ = name + "=";
	for(var i=0; i < ca.length; i++) {
		c = ca[i].toString().replace(/^ */,'');
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function eraseCookie(name) { writeCookie(name, "", -1) }
