var lastElm   = '';
var animation = false;
var imgLast;
var imgCur    = 0;

var xPlodeItems = Array();

var pApplet;


function manageClick(event) {
	if (!event) var e = window.event;
	
	var xPos = (!event.x) ? event.layerX : event.x;
	
	if (event.x) {
		if (event.layerX < event.x) xPos = event.layerX;
	}
	
	if(xPos < $('im'+imgCur).getDimensions().width/2) {
		pIm();
	} else {
		nIm();
	}
	manageArrows(event);
}

function manageArrows(event) {
	if (!event) var e = window.event;
	
	var xPos = (!event.x) ? event.layerX : event.x;
	
	if (event.x) {
		if (event.layerX < event.x) xPos = event.layerX;
	}
	
	if(xPos < $('im'+imgCur).getDimensions().width/2) {
		$('im'+imgCur).style.cursor = "w-resize";
	} else {
		$('im'+imgCur).style.cursor = "e-resize";
	}
}
 
function reveal() {
	var screenSize = document.viewport.getDimensions();
	document.body.style.overflow = "hidden";
	
	
	if ($('backgroundImage') == null) {
		new Effect.Parallel([
			new Effect.Move('menu', 			{sync: true, x: -100, y: 0, mode: 'relative'}),
			new Effect.Opacity('hole', 			{sync: true, from: 1.0, to: 0.0}),
			new Effect.Move('content', 			{sync: true, x: screenSize.width-200, y: 0, mode: 'relative'}),
			new Effect.Morph('test', 			{sync: true, style:'left:50px; width:'+(screenSize.width-100)+'px;'})
		], {
			duration:1.0,
			afterFinish: function() {
				if (!pApplet) {
					$('canvas').setAttribute('style', 'width:100%; height:100%;');
					pApplet = Processing(canvas, $('script').text);	
				}
			}
		});
	
	} else {
		new Effect.Parallel([
			new Effect.Move('menu', 			{sync: true, x: -100, y: 0, mode: 'relative'}),
			new Effect.Move('backgroundImage',	{sync: true, x: screenSize.width-200, y: 0, mode: 'relative'}),
			new Effect.Opacity('hole', 			{sync: true, from: 1.0, to: 0.0}),
			new Effect.Move('content', 			{sync: true, x: screenSize.width-200, y: 0, mode: 'relative'}),
			new Effect.Morph('test', 			{sync: true, style:'left:50px; width:'+(screenSize.width-100)+'px;'})
		], {
			duration:1.0,
			afterFinish: function() {
				if (!pApplet) {
					$('canvas').setAttribute('style', 'width:100%; height:100%;');
					pApplet = Processing(canvas, $('script').text);	
				}
			}
		});
	}
}

function hide() {
	var screenSize = document.viewport.getDimensions();
		
	if ($('backgroundImage') == null) {
		new Effect.Parallel([
			new Effect.Move('menu', 			{sync: true, x: 100, y: 0, mode: 'relative'}),
			new Effect.Move('content', 			{sync: true, x: 150, y: 0, mode: 'absolute'}),
			new Effect.Opacity('hole', 			{sync: true, from: 0.0, to: 1.0}),
			new Effect.Morph('test', 			{sync: true, style:'left:150px; width:0px;'})
		], {
			duration:1.0,
			afterFinish: function() {document.body.style.overflow = "auto";}
		});
	
	} else {
		new Effect.Parallel([
			new Effect.Move('menu', 			{sync: true, x: 100, y: 0, mode: 'relative'}),
			new Effect.Move('backgroundImage',	{sync: true, x: 150, y: 0, mode: 'absolute'}),
			new Effect.Opacity('hole', 			{sync: true, from: 0.0, to: 1.0}),
			new Effect.Move('content', 			{sync: true, x: 150, y: 0, mode: 'absolute'}),
			new Effect.Morph('test', 			{sync: true, style:'left:150px; width:0px;'})
		], {
			duration:1.0,
			afterFinish: function() {document.body.style.overflow = "auto";}
		});
	}	
}


function xPlode() {
	var divs = $$('a');
	for (var i = 0; i < divs.length; i++) {
		var div = $(divs[i]);
		var position 	= div.viewportOffset();
		
		xPlodeItems.push({item: div, left:position.left, top:position.top});
	}


	for (var i = 0; i < xPlodeItems.length; i++) {
		var div = xPlodeItems[i].item;
		try {
			document.body.appendChild(div);
			div.absolutize();
			div.style.left = xPlodeItems[i].left+"px";
			div.style.top  = xPlodeItems[i].top+"px";
			div.style.marginLeft = "0px";
			div.style.marginRight = "0px";
			div.style.marginBottom = "0px";
			div.style.marginTop = "0px";
		} catch (e) {
			alert(e);
		}
	}	
	new PeriodicalExecuter(xPlodeMove, 0.1);
}

function xPlodeMove() {
	for (var i = 0; i < xPlodeItems.length; i++) {
		var div 		= xPlodeItems[i].item;
		var position 	= div.viewportOffset();
		
		var rnd         = Math.floor(Math.random()*2);
		var ampl		= Math.floor(Math.random()*10);
		div.style.left 	= (rnd == 0) ? (position.left + ampl)+"px" : (position.left - ampl)+"px";
		var rnd         = Math.floor(Math.random()*2);
		var ampl		= Math.floor(Math.random()*10);
		div.style.top 	= (rnd == 0) ? (position.top + ampl)+"px" : (position.top - ampl)+"px";
	}
	
}

function getPageSize() {

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) { 
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	} else { // Explorer Mac...would also work in Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	} 

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){ 
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	//arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return {width:pageWidth, height:pageHeight, windowWidth:windowWidth,windowHeight:windowHeight}; //arrayPageSize;
}

function pIm() {
	$('im'+imgCur).toggle();
	var n = (imgCur > 0) ? imgCur - 1 : imgLast;
	$('im'+n).toggle();	
	$('work_infos').hide();
	imgCur--;
	if (imgCur < 0) imgCur = imgLast;
}

function nIm() {
	$('im'+imgCur).toggle();
	var n = (imgCur < imgLast) ? imgCur + 1 : 0;
	$('im'+n).toggle();
	$('work_infos').hide();
	imgCur++;
	if (imgCur > imgLast) imgCur = 0;
}


document.observe("dom:loaded", function() {
	var works = $$('.work_data');
	
	for (var i = 0; i < works.length; i++) {
		var image = works[i].next();
		image.absolutize();
		
		$(works[i]).observe('mouseout', function(event) {
			var image = this.next();
			image.hide();
		});
		
		$(works[i]).observe('mousemove', function(event) {
			var works = $$('.work_data');
	
			for (var i = 0; i < works.length; i++) {
				var image = works[i].next().hide();
			}
			
			var image = this.next();
			
			image.show();
			
			var scroll = jQuery(document).scrollTop();
			var top = this.cumulativeOffset()[1]-scroll;

			var h 	= jQuery(window).height();
			var iH  = jQuery(image).find('img').height();
			
			if (top+iH+75 > h ) top = h-iH-75;
			image.style.top = top+scroll+'px';
			
			image.style.left = Event.pointerX(event)-140+'px';
			image.show();
		});
	}
});


function accordion(event) {
	document.title = e.target;
/*	$(elm).absolutize();
	$(elm).show();
	/*if (!animation && elm != lastElm) {
		if (lastElm != '') {
			if ($(lastElm).visible()) Effect.SlideUp(lastElm, {duration: 0.1});		
		}
		if (!$(elm).visible()) Effect.SlideDown(elm, {duration: 0.1, afterFinish: function() { 
				animation = false; 
			}
		});
		lastElm 	= elm;
		animation 	= true;
	}*/
}

var iRatio;	
var dRatio;
var rRatio;
var dSize;
var resizing = false;
var timer;

function updatePicture() {
	$('backgroundImageLow').show();
	$('backgroundImageHigh').show();
	if (dRatio > iRatio) {
		$('backgroundImageHigh').src = "/img.php?file="+dFile+"&w="+dSize.width;
	} else {
		$('backgroundImageHigh').src = "/img.php?file="+dFile+"&h="+dSize.height;
	}
	resizing = false;
}

function resize(doRatio) {
		
	$('backgroundImage').style.height = "0px";
	
	dSize = getPageSize(); //$('body').getDimensions(); //document.viewport.getDimensions();

	if (BrowserDetect.browser == 'Explorer' && BrowserDetect.version == 6) {
	    $('content').style.width = (dSize.windowWidth-150)+"px";	
	}

    var iSize = $('backgroundImageLow').getDimensions();
        dRatio = dSize.width / dSize.height;

    $('backgroundImage').style.height = dSize.height+"px";	    
    if(doRatio) iRatio = iSize.width / iSize.height;
	   
    if (dSize.width < limit) {
    	$('backgroundImage').style.width = (limit-150)+"px";
    } else {
    	$('backgroundImage').style.width = ($('content').getDimensions().width)+"px";//"100%";    
    }
      		
    if (dRatio > iRatio) {
    	$('backgroundImageLow').style.width   = dSize.width+"px";
    	$('backgroundImageLow').style.height  = (dSize.width/iRatio)+"px";
    	$('backgroundImageHigh').style.width  = dSize.width+"px";
    	$('backgroundImageHigh').style.height = (dSize.width/iRatio)+"px";
    } else {
    	$('backgroundImageLow').style.width   = (dSize.height*iRatio)+"px";
    	$('backgroundImageLow').style.height  = dSize.height+"px";				
    	$('backgroundImageHigh').style.width  = (dSize.height*iRatio)+"px";
    	$('backgroundImageHigh').style.height = dSize.height+"px";				
    }
    
    if (resizing) $('backgroundImageHigh').hide();
    
    if(doRatio) updatePicture(dSize);
    
    //resizePage(doRatio);
    
    resizing = true;
    
    clearTimeout(timer)
    timer = setTimeout(updatePicture,500);
}

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

