/*
* construction.js
*
* Daniel Boesswetter <daniel@daniel-boesswetter.de>
* (c) 2003
*
* Fri Jul 25 14:45:30 WEDT 2003
*
*/

cursor_width = 250;
cursor_height = 250;
background_image_url = "img/digger02.jpg";
//image_width = 1000;
image_width = 1280;
//image_height = 1280;
//image_height = 1000;
image_height = 960;
mouse_x = 0;
mouse_y = 0;
mouse_state = 0;

visibleDiv = null;

function document_width() {
	return isNaN( window.innerWidth )
		? document.body.offsetWidth - 22
		: window.innerWidth;
}

function document_height() {
	return isNaN( window.innerHeight )
		? document.body.offsetHeight - 5
		: window.innerHeight;

}

function cursor() {
	return document.getElementById( 'cursor' );
}

function body() {
	return document.getElementById( 'body' );
}

function h1() {
	return document.getElementById( 'h1' );
}

function h2() {
	return document.getElementById( 'h2' );
}

function v1() {
	return document.getElementById( 'v1' );
}

function v2() {
	return document.getElementById( 'v2' );
}

function about() {
	return document.getElementById( 'about' );
}

function projects() {
	return document.getElementById( 'projects' );
}

function legal() {
	return document.getElementById( 'legal' );
}

function contact() {
	return document.getElementById( 'contact' );
}

function init() {
	cursor().style.width = cursor_width;
	cursor().style.height = cursor_height;
	//body().style.backgroundImage = "url(" + background_image_url + ")";
	adjust( 0, 0 );
}

function adjust( x, y ) {

	/* check boundaries */
	xmin = cursor_width / 2;
	ymin = cursor_height / 2;
	xmax = document_width() - cursor_width/2;
	ymax = document_height() - cursor_height/2;

	if ( !( xmin <= x && x <= xmax ) ) x = x < xmin ? xmin : xmax;
	if ( !( ymin <= y && y <= ymax ) ) y = y < ymin ? ymin : ymax;
	if ( !( xmin <= mouse_x && mouse_x <= xmax ) ) mouse_x = mouse_x < xmin ? xmin : xmax;
	if ( !( ymin <= mouse_y && mouse_y <= ymax ) ) mouse_y = mouse_y < ymin ? ymin : ymax;

	w = cursor_width;
	h = cursor_height;
	if ( mouse_state ) {
		/* mouse button pressed: div's size */
		w =  cursor_width + Math.abs( x - mouse_x );
		h = cursor_height + Math.abs( y - mouse_y );
		cursor().style.width = w;
		cursor().style.height = h;

	}

	/* mouse button not pressed or moving in negative direction:
	** div's position */
	if ( !mouse_state || x < mouse_x ) {
		dx = x - cursor_width / 2;
		cursor().style.left = dx;
	}

	if ( !mouse_state || y < mouse_y ) {
		dy = y - cursor_height / 2;
		cursor().style.top = dy;
	}

	/* image displacement */
	px = -(dx + ( image_width - document_width() )/2);
	py = -(dy + ( image_height - document_height() )/2);
	pos = px + "px " + py + "px";
	cursor().style.backgroundPosition = pos;

	/* horizontal & vertical bars */
	v1().style.left = cursor().style.left;
	h1().style.top = cursor().style.top;

	if ( mouse_state ) {
		v2().style.left = Math.max( x, mouse_x ) + cursor_width/2 - 1;
		h2().style.top = Math.max( y, mouse_y ) + cursor_height/2 - 1;
	} else {
		v2().style.left = x + cursor_width/2 - 1;
		h2().style.top = y + cursor_height/2 - 1;
	}

	/* content divs */
	if ( visibleDiv ) {
		visibleDiv.style.left = v2().style.left;
		visibleDiv.style.top = h1().style.top;

         }
}

function show( what ) {
	var div = document.getElementById( what );
         if ( div ) {
         	visibleDiv = div;
		div.style.visibility = "visible";
         }
}

function unshow() {
         if ( visibleDiv ) visibleDiv.style.visibility = "hidden";
}

function mouseDown( event ) {
	/* button pressed */
	mouse_state = 1;
	/* save mouse state */
	mouse_x = event.clientX;
	mouse_y = event.clientY;
}

function mouseUp( event ) {
	/* unpressed */
	mouse_state = 0;
	cursor().style.width = cursor_width;
	cursor().style.height = cursor_height;
	adjust( event.clientX, event.clientY );
}
