var myUrl = window.location.toString();
var myUrlLen = myUrl.length;

var IE = navigator.userAgent.indexOf('MSIE')!=-1;
var IE6 = navigator.userAgent.indexOf('MSIE 6')!=-1;
var FF = navigator.userAgent.indexOf('Firefox')!=-1;
var myY ="";
var myX ="";

/* Example
if(FF){
	//filter js for ff
	alert("ff")
}
*/

function $(){
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}


// addevent prototype
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}
	
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);
// there is a conflict with the UNLOAD and with the LOAD of this JS with the resizing template *****************************************
// END addevent

/*
Sweet Titles (c) Creative Commons 2005
http://creativecommons.org/licenses/by-sa/2.5/
Author: Dustin Diaz | http://www.dustindiaz.com
*/
var sweetTitles = { 
	xCord : 0,					// @Number: x pixel value of current cursor position
	yCord : 0,					// @Number: y pixel value of current cursor position
	tipElements : ['a','abbr','acronym','img'],	// @Array: Allowable elements that can have the tooltip
	obj : Object,					// @Element: That of which you're hovering over
	tip : Object,					// @Element: The actual tooltip itself
	active : 0,					// @Number: 0: Not Active || 1: Active
	init : function() {
		if ( !document.getElementById ||
			!document.createElement ||
			!document.getElementsByTagName ) {
			return;
		}		
		var i,j;
		this.tip = document.createElement('div');
		this.tip.id = 'tooltip';
		document.getElementsByTagName('body')[0].appendChild(this.tip);
		this.tip.style.top = '0';
		this.tip.style.visibility = 'hidden';
		var tipLen = this.tipElements.length;
		for ( i=0; i<tipLen; i++ ) {
			var current = document.getElementsByTagName(this.tipElements[i]);
			var curLen = current.length;
			for ( j=0; j<curLen; j++ ) {
				addEvent(current[j],'mouseover',this.tipOver);
				addEvent(current[j],'mouseout',this.tipOut);
				current[j].setAttribute('tip',current[j].title);
				current[j].removeAttribute('title');
			}
		}
	},
	updateXY : function(e) {
		if ( document.captureEvents ) {
			sweetTitles.xCord = e.pageX;
			sweetTitles.yCord = e.pageY;
		} else if ( window.event.clientX ) {
			sweetTitles.xCord = window.event.clientX+document.documentElement.scrollLeft;
			sweetTitles.yCord = window.event.clientY+document.documentElement.scrollTop;
		}
	},
	tipOut: function() {
		if ( window.tID ) {
			clearTimeout(tID);
		}
		if ( window.opacityID ) {
			clearTimeout(opacityID);
		}
		sweetTitles.tip.style.visibility = 'hidden';
	},
	checkNode : function() {
		var trueObj = this.obj;
		if ( this.tipElements.inArray(trueObj.nodeName.toLowerCase()) ) {
			return trueObj;
		} else {
			return trueObj.parentNode;
		}
	},
	tipOver : function(e) {
		sweetTitles.obj = this;
		tID = window.setTimeout("sweetTitles.tipShow()",700); //time of the tooltip to show
		sweetTitles.updateXY(e);
	},
	tipShow : function() {
		var scrX = Number(this.xCord);
		var scrY = Number(this.yCord);
		var tp = parseInt(scrY+15);
		var lt = parseInt(scrX+10);
		var anch = this.checkNode();
		var addy = '';
		var access = '';

		if ( anch.nodeName.toLowerCase() == 'a' ) {
//			addy = (anch.href.length > 32 ? anch.href.toString().substring(0,32)+"..." : anch.href);
			//check the string of text after www.life-is-simple.co.uk of the URL
			if(anch.href.toString().indexOf("www.life-is-simple.co.uk") != -1){
				//if you are a link with the URL of life is simple
				var myendaddress = anch.href.toString().substring(32, anch.href.length);
				addy = (myendaddress.length > 30 ? myendaddress.substring(0, 30)+"..." : myendaddress);
			}else if(anch.href.toString().indexOf("life-is-simple.co.uk") != -1){
				var myendaddress = anch.href.toString().substring(28, anch.href.length);
				addy = (myendaddress.length > 30 ? myendaddress.substring(0, 30)+"..." : myendaddress);
			}else{
				addy = (anch.href.length > 39 ? anch.href.toString().substring(7,39)+"..." : anch.href.toString().substring(7,anch.href.length));
			}
			var access = (anch.accessKey ? ' <span class="accessk">Accesskey ['+anch.accessKey+']</span> ' : '' );

		} else {
			addy = anch.title;
			if(anch.href){
				if(IE && anch.href == anch.src){
					//do nothing because is an image not a link
				}else{
					addy = anch.firstChild.nodeValue;
				}
			}

		}

// TO TEST WITH ANCHOR ELEMENTS IF IT WORKS AND IMAGES*******************************
//		var verifyAsh = anch.getAttribute('tip').indexOf("#")=-1;
//		if(!verifyHref || !verifyAsh){

		var memorise = "";
		if(anch.href){
			var verifyHref = anch.href.indexOf('javascript:')!=-1;
			if(!verifyHref){
				//check that is not a javscript in the link
				memorise = "<p>"+anch.getAttribute('tip')+"<em>"+addy+access+"</em></p>";
			}else{
				if(access == ""){
					memorise = "<p>"+anch.getAttribute('tip')+"</p>";
				}else{
					memorise = "<p>"+anch.getAttribute('tip')+"<br /><em>"+access+"</em></p>";
				}
			}
		}else{
			//trouble with IE it doesn't work I don't know why
			memorise = "<p>"+anch.getAttribute('tip')+addy+"</p>";
		}


		this.tip.innerHTML = '<div id="tooltip-top"></div><div class="center">'+memorise+'</div><div id="tooltip-bottom"></div>'

		if(parseInt(document.documentElement.clientHeight+document.documentElement.scrollTop) < parseInt(this.tip.offsetHeight+tp) ) {
			//horizontal fix
			this.tip.style.top = parseInt(tp-(this.tip.offsetHeight+10))+'px';

			if (parseInt(document.documentElement.clientWidth+document.documentElement.scrollLeft) < parseInt(this.tip.offsetWidth+lt) ) {
				//bottom-right
				this.tip.style.left = parseInt(lt-(this.tip.offsetWidth+10))+'px';
				$("tooltip-top").style.backgroundImage= "url(http://www.life-is-simple.co.uk/img/tooltip-top3.png)"
				$("tooltip-top").style.height ="11px";
				$("tooltip-bottom").style.backgroundImage= "url(http://www.life-is-simple.co.uk/img/tooltip-bottom3.png)"
				$("tooltip-bottom").style.height ="22px";

			} else {
				//bottom-left
				this.tip.style.left = lt+'px';
				$("tooltip-top").style.backgroundImage= "url(http://www.life-is-simple.co.uk/img/tooltip-top3.png)"
				$("tooltip-top").style.height ="11px";
				$("tooltip-bottom").style.backgroundImage= "url(http://www.life-is-simple.co.uk/img/tooltip-bottom2.png)"
				$("tooltip-bottom").style.height ="22px";
			}

		}else{
			//vertical fix
			this.tip.style.top = tp+'px';

			if (parseInt(document.documentElement.clientWidth+document.documentElement.scrollLeft) < parseInt(this.tip.offsetWidth+lt) ) {
				//top-right 
				this.tip.style.left = parseInt(lt-(this.tip.offsetWidth+10))+'px';
				$("tooltip-top").style.backgroundImage= "url(http://www.life-is-simple.co.uk/img/tooltip-top2.png)"
			} else {
				//top-left
				this.tip.style.left = lt+'px';
			}
		}

		if(anch.getAttribute('tip') != ''){
			this.tip.style.visibility = 'visible';
			//this.tip.style.opacity = '.1';        DISABLED OPACITY
			//this.tipFade(0);
		}

	}/* I disabled this function because it craete problems to the smoothness of the animation of the slide,
	tipFade: function(opac) {
		var passed = parseInt(opac);
		var newOpac = parseInt(passed+10);
		if ( newOpac < 80 ) {
			this.tip.style.opacity = '.'+newOpac;
			this.tip.style.filter = "alpha(opacity:"+newOpac+")";
			opacityID = window.setTimeout("sweetTitles.tipFade('"+newOpac+"')",20);
		}else{ 
			this.tip.style.opacity = '.80';
			this.tip.style.filter = "alpha(opacity:80)";
		}
	}*/
};
function pageLoader() {
	sweetTitles.init();
}
// Se è IE6 non attivare sweet-titles (ho un problema con la trasparenza su background che non riesco a risolvere, stranamente sembra funzionare con una parte dei png, problema di cache?)
if(!IE6){
addEvent(window,'load',pageLoader);
}
// END Sweet titles

// added some modifications by Alessio Carone "Change style in resize of the window"
// NOTE: some screen show a different value of width at the same resolution (maybe is caused by video motherboard?)

wraphandler = {
	init: function() {
		if (!document.getElementById) return;
		// set up the appropriate wrapper
		wraphandler.setWrapper();
		// and make sure it gets set up again if you resize the window

		wraphandler.addEvent(window,"resize", wraphandler.setWrapper);
	},

	setWrapper: function() {

// INTERESTING FEATURE SLIDE SOMETHING TO THE RESIZE OF THE LAYOUT TO THINK ABOUT NEW IDEAS TO HOW USE IT
// collapse2._slideit(); 

		var isie= navigator.userAgent.indexOf('MSIE') != -1; //check if the browser is IE

		// width stuff from ppk's evolt.org/article/document_body_doctype_switching_and_more/17/30655/index.html
	    var theWidth = 0;
		if (window.innerWidth) {
			theWidth = window.innerWidth
		} else if (document.documentElement && document.documentElement.clientWidth) {
			theWidth = document.documentElement.clientWidth
		} else if (document.body) {
			theWidth = document.body.clientWidth
		}
		if (theWidth != 0) {
			if (theWidth < 1250) {
				//check if the user is in portfolio
				if(myUrlLen > 32){
					if(myUrl.indexOf('portfolio.htm') != -1){
						$('changeCss').href = "/css/800-portfolio.css";
					}else{
						//if is not portfolio
						$('changeCss').href = "/css/800.css";
					}
				}else{
					$('changeCss').href = "/css/800.css";
				}

//				$('wrapper').className = 'altwrapper';
//				$('wrapper').className += ' altwrapper2';
			}else if(theWidth >= 1251){
				if(myUrlLen > 32){
					if(myUrl.indexOf('portfolio.htm') != -1){
						$('changeCss').href = "/css/1280-portfolio.css";
					}else{
						//if is not portfolio
						$('changeCss').href = "/css/1280.css";
					}
				}else{
					$('changeCss').href = "/css/1280.css";
				}

			}
		}
		
		if(isie== true){
			theWidth += 20; //there is a gap in px width in IE. It doesn't count the sidebar width.
		}

		$('value').innerHTML = "width: "+theWidth+" px"; 			// VARIABLE THAT TELLS HOW MUCH IS THE WIDTH OF THE BROWSER
		addEvent(window,'load',pageLoader);
	},

	// addEvent stuff from John Resig's ejohn.org/projects/flexible-javascript-events
	addEvent: function( obj, type, fn ) {
		if ( obj.attachEvent ) {
			obj['e'+type+fn] = fn;
			obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
			obj.attachEvent( 'on'+type, obj[type+fn] );
		} else {
			obj.addEventListener( type, fn, false );
		}
	}
}
wraphandler.addEvent(window,"load",wraphandler.init);

/*END CSS style in resize of the window*/


/*	BROWSE DETECT
	http://www.quirksmode.org/js/detect.html 	

    *  Browser name: BrowserDetect.browser
    * Browser version: BrowserDetect.version
    * OS name: BrowserDetect.OS

*/

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

/* For the moment I disabled this functionality to hide or show the side bar on the left*/

function hideshow(id){
	if($(id).style.display == "none"){
		$(id).style.display = "block";
		if(id == 'side-box'){
			$("mylink").innerHTML = "Hide more info";
		}
	}else{
		$(id).style.display = "none";
		if(id == 'side-box'){
			$("mylink").innerHTML = "More info";
		}
	}
}


var storage = new Array("additional","additional1","additional2","additional3","additional4","additional5","additional6","additional7");

function starthide(){
	for(var i = 0; i<=storage.length; i++){
		if($(storage[i])){
			$(storage[i]).style.display = "none";
		}
	}
}
addEvent(window,"load",starthide);

var myVar;
var txt=new Array();
txt[0]='<a href="http://www.flickr.com/photos/dthprod/212461272/" target="_blank">Oslo Fylke at sunrise</a>';
txt[1]='<a href="http://www.flickr.com/photos/55671677@N00/95734560/" target="_blank">Nynäsviken</a>';
txt[2]='<a href="http://commons.wikimedia.org/wiki/Image:British_Museum_Reading_Room_Panorama_Feb_2006.jpg" target="_blank">British Museum in London</a>';
txt[3]='<a href="http://www.flickr.com/photo_zoom.gne?id=406108908&size=l" target="_blank">Louvre at night in Paris</a>';

var thx=new Array();
thx[0]='<a href="http://www.lavoricreativi.com/forum" target="_blank" title="For talking and inspiring me about design and how to work in the design business">All my friend of Lavori Creativi&#39;s forum</a>';
thx[1]='<a href="http://www.connexion.co.uk" target="_blank" title="To have believed and supported my skills and passions">James Stratton</a>';
thx[2]='<span title="For sustain me even if I am so far away">My mother and my father</span>';
thx[3]='<a href="http://www.maedastudio.com" title="for his thoughts about simplicity">John Maeda</a>';

var img=new Array();
img[0]='00';
img[1]='01';
img[2]='02';
img[3]='03';

function randomHead(){
    var num=Math.floor(Math.random() * img.length);
    document.write('<link rel="stylesheet" type="text/css" href="/css/'+img[num]+'.css" />');
	myVar=num;
}
randomHead();
