function findDimensions () {
	var image= new Image();
	image.onload = function(){
		$('bgImage').src = image.src;
		if ($('bgImageOld').src == "")
			$('bgImageOld').src = image.src;
		handleDimension(image.width, image.height);
	}
	if ($('bgImage').src.indexOf('/-fullscreen.jpg') != -1 || $('bgImage').src.indexOf("noImage.gif-fullscreen.jpg") != -1){
		image.src="images/default.jpg";
	}
	else{
		image.src=$('bgImage').src;
	}
}

function getDim(){
	var winObj = new Object(); 
	if (self.innerHeight) {
		winObj["x"] = self.innerWidth;
		winObj["y"] = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		// Explorer 6 Strict Mode
		winObj["x"] = document.documentElement.clientWidth;
		winObj["y"] = document.documentElement.clientHeight;
	} else if (document.body) { 
		// other Explorers
		winObj["x"] = document.body.clientWidth;
		winObj["y"] = document.body.clientHeight;
	}
	return winObj;
}

function handleDimension(width, height, id){
	var id = (id == null) ? 'bgImage': id;
	var winObj = getDim();
	var x = winObj["x"];
	var y = winObj["y"];
	var mod = height/width;
	var modX = x/width;
	var modY = y/height;
	iwidth=x;
	iheight=x*mod;
	$(id).setAttribute("width", iwidth);
	$(id).setAttribute("height",iheight);
	$(id).style.top = "0";
	if (y > iheight){
		$(id).style.top = (y-iheight)/2+"px";
		iheight = y;
	}
	$("navigation").style.height = iheight+"px";
	try{
		$("content").style.height = iheight+"px";
	}
	catch (e) {}
	$("extra").style.height = iheight+"px";
	$('BG').style.height = iheight+"px";
	$(id).show();
	if (id == 'bgImage' && document.getElementById('bgImageOld')){
		if ($('bgImageOld').src != $('bgImage').src){
			$('bgImageOld').fade({duration: 0.6, afterFinish: function(){
				newoldImage = new Image();
				newoldImage.onload = function(){
					$('bgImageOld').src=newoldImage.src;
					$('bgImageOld').setAttribute("width", $('bgImage').width);
					$('bgImageOld').setAttribute("height",$('bgImage').height);
					$('bgImageOld').style.top = $('bgImage').style.top;
					$('bgImageOld').show();
				}
				newoldImage.src=$('bgImage').src;
			}});
		}
		else{
			if ($('bgImageOld').width != $('bgImage').width || $('bgImageOld').height != $('bgImage').height || $('bgImageOld').style.top != $('bgImage').style.top){
				$('bgImageOld').hide();
				$('bgImageOld').setAttribute("width", $('bgImage').width);
				$('bgImageOld').setAttribute("height",$('bgImage').height);
				$('bgImageOld').style.top = $('bgImage').style.top;
				$('bgImageOld').show();
			}
		}
	}
	checkHeight();
}


function test(evt){
	var x,y;
	if (self.innerHeight) {
		x = self.innerWidth;
		y = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		// Explorer 6 Strict Mode
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	} else if (document.body) { 
		// other Explorers
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	var iwidth=  $("bgImage").width;//1155;
	var iheight= $("bgImage").height;//740;
	if (x == iwidth && y == iheight)
		return;
	var mod = iheight/iwidth;
	var modX = x/iwidth;
	var modY = y/iheight;
	iwidth=x;
	iheight=x*mod;
	$("bgImage").setAttribute("width", iwidth);
	$("navigation").style.height = iheight+"px";
	try{
		document.getElementById("content").style.height = iheight+"px";
	}
	catch (e) {
		// TODO: handle exception
	}
	document.getElementById("extra").style.height = iheight+"px";
	document.getElementById("bgImage").setAttribute("height",iheight)
	$('BG').style.height = iheight+"px";
	$('bgImage').show();
}


function showHide(id){
	if ($(id).style.display == 'none'){
		$(id).appear();
		new Effect.Move(id, { duration: 1, y: 0,mode: 'absolute'});
		if (id == 'content'){
			new Effect.Morph('galleryDiv', { style: 'width: 10%; margin-left: 30%', duration: 1});
			$('gallery').hide();
			$('moveButtons').appear();
		}
	}
	else
	{
		new Effect.Move(id, { duration: 1, y: $('bgImage').height, mode: 'absolute'});
		$(id).fade();
		if (id == 'content'){
			new Effect.Morph('galleryDiv', { style: 'width: 39%; margin-left: 1%', duration: 1});
			$('moveButtons').hide();
			$('gallery').appear();
		}
	}
}

function hide(id){
	var dim = getDim();
	var down = dim["y"];
	new Effect.Move(id, { duration: 1, y: down, mode: 'absolute'});
	$(id).fade();
	if (id == 'content'){
		new Effect.Morph('galleryDiv', { style: 'width: 39%; margin-left: 1%', duration: 1});
		$('moveButtons').hide();
		$('gallery').appear();
	}
}

function moveUp(id){
	var newId = $('content').firstChild.id;
	new Effect.Move(newId, { duration: 0.5, y: -300,mode: 'relative'});
}

function moveDown(id){
	var newId = $('content').firstChild.id;
	if ($(newId).style.top.replace("px","") > -300){
		new Effect.Move(newId, { duration: 0.5, y: 0,mode: 'absolute'});
	}
	else{
		new Effect.Move(newId, { duration: 0.5, y: 300,mode: 'relative'});
	}
	
}

function setFooter(){
	document.getElementById("projectfooter").style.bottom = "0";
}

function checkHeight(){
	if (!document.getElementById("gallery")){
		if (parseInt($('content').firstChild.offsetHeight) > parseInt($('BG').offsetHeight) && $('content').style.display != 'none'){
		try{
			
			$('galleryDiv').appear();
		}catch(e){}
		}
		else{
			try{
				$('galleryDiv').hide();
			}
			catch(e){}
		}
	}
}

function changeImage(oldPath,newPath){
	if (oldPath == newPath){
		return;
	}
	if (document.getElementById('bgImageOld')){
		//$('bgImageOld').show();
		findDimensions();
		//Element.remove('bgImageOld');
	}
	else{
		oldImage = new Image();
		oldImage.onload = function(){
			oldImage.id = "bgImageOld";
		    oldImage.style.zIndex = "4";
		    oldImage.style.position = "absolute";
		    $('BG').appendChild(oldImage);
		    findDimensions();
	    }
		oldImage.src=oldPath+"-fullscreen.jpg";
	}
}
