/*
	HEAVYBOX v1.0.7 beta
	--------
	Copyright ©2007 Nick Berlette, Riuv International
	Developed by Nick Berlette
	
	Official website: http://www.heavybox.net
	
	LEGAL NOTICE:
	Go ahead and use it, and feel free to share it. But PLEASE do not claim any of my code as your own. Thanks :)
	
	------------------------------------------------------
	
	------------------
	CONFIGURATIONS, FEEL FREE TO EDIT THESE.
	------------------
*/

var loading = true; // Show "Loading..." page before displaying actual page's contents? true means yes, false means no.
var position = 'fixed'; // Choose between fixed (stays put) or absolute (scrolls with the content)
var top = '150'; // Distance from the top of the page (in pixels)
var left = '150'; // Distance from the left of the page (in pixels)
var key_cheats = true; // Allow use of keys? (for re-positioning, closing, etc.)
var key_interval = '10'; // When using keys for positioning, how many pixels to move in one press? 10 is good.
var key_interval_large = '100'; // For bigger gaps, different keys. It's suggested to leave it at 100.

/*
	------------------
	CONFIGURATIONS DONE, DO NOT EDIT BELOW THIS LINE
	------------------
*/

function $ (element) {
	return document.getElementById(element);	
}
function $$ (tag) {
	return document.getElementsByTagName(tag);	
}
function change_opacity (element, opacity) {
	object = $(element);
	object.style.filter = 'alpha(opacity=' + opacity + ')';
	object.style.MozOpacity = opacity / 100;
	object.style.KhtmlOpacity = opacity / 100;
	object.opacity = opacity / 100;	
}
function fade_out (id) {
	for (i = 0; i <= 100; i++) {
		x = 100 - i;
		setTimeout('change_opacity(\'' + id + '\', ' + x + ')', i + 100);
	}	
}
function fade_in (id) {
	for (i = 0; i <= 100; i++) {
		setTimeout('change_opacity(\'' + id + '\', ' + i + ')', i + 100);
	}	
}
function heavybox_deploy (page) {
	if(loading) {
		$('heavybox_frame').src = 'loading.html';
	}
	change_opacity('heavybox', 0);
	$('heavybox').style.visibility = 'visible';
	setTimeout("fade_in('heavybox')", 400);
	setTimeout("$('heavybox_frame').src = '" + page + "'", 2000);
}
function heavybox_withdraw () {
	fade_out('heavybox');
	setTimeout("$('heavybox').style.visibility = 'hidden'", 500);
	withdraw = function () {
		if(loading) {
			$('heavybox_frame').src = 'loading.html';	
		}
		$('heavybox').style.top = top + 'px';
		$('heavybox').style.left = left + 'px';	
	}
	setTimeout('withdraw()', 1600);
}
function heavybox_write () {
	position = position ? position : 'fixed';
	document.write('<div id="heavybox" style="position: ' + position + '; top: ' + top + 'px; left: ' + left + 'px;"><div id="dragger" class="heavybox-top" onmousedown="drag_object($(\'heavybox\'), event);">');
	document.write('<a href="#" onclick="heavybox_withdraw(); return false;" class="heavybox-right"><img src="img/heavybox-close.gif" alt="X" border="0" /></a>');
	document.write('</div><div class="heavybox-loader">');
	document.write('<iframe src="loading.html" class="heavybox-page" id="heavybox_frame" name="heavybox_frame" frameborder="0" scrolling="auto" width="330" height="215"></iframe>');
	document.write('</div></div>');
}
function key_press (e) {
	if($('heavybox').style.visibility == 'visible') {
		var evt = e ? e : window.event;
		var key = evt.keyCode;
		var ctrl = (evt.ctrlKey) ? true : false;
		var start_x = parseInt($('heavybox').style.left), start_y = parseInt($('heavybox').style.top);
		var original_page = $('heavybox_frame').src;
		key_interval = parseInt(key_interval);
		key_interval_large = parseInt(key_interval_large);
		switch(key) {
			case 33: // Page Up
				$('heavybox').style.top = (start_y - key_interval_large) + 'px';	
			break;
			case 34: // Page Down
				$('heavybox').style.top = (start_y + key_interval_large) + 'px';	
			break;
			case 35: // End, Delete
				case 46:
					heavybox_withdraw();
			break;
			case 37: // Ctrl + Left
				if(ctrl) {
					$('heavybox').style.left = (start_x - key_interval) + 'px';	
				}
			break;
			case 38: // Ctrl + Up
				if(ctrl) {
					$('heavybox').style.top = (start_y - key_interval) + 'px';	
				}
			break;
			case 39: // Ctrl + Right
				if(ctrl) {
					$('heavybox').style.left = (start_x + key_interval) + 'px';	
				}
			break;
			case 40: // Ctrl + Down
				if(ctrl) {
					$('heavybox').style.top = (start_y + key_interval) + 'px';	
				}
			break;
			case 82: // Ctrl + R
				if(ctrl) {
					$('heavybox_frame').src = original_page;
					return false;
				}
			break;
		}
	}
}
function user_agent (string) { 
	return (Math.max(navigator.userAgent.toLowerCase().indexOf(string), 0)); 
}
function coordinates (e, flip) {
	if(flip) {
		if(user_agent('msie')) {
			return event.clientY + document.body.scrollTop;
		} else {
			return e.pageY;	
		}
	} else {
		if(user_agent('msie')) {
			return event.clientX + document.body.scrollTop;	
		} else {
			return e.pageX;	
		}
	}
}
function drag_object (element, e) {
	function drag (e) { 
		if(dragging) { 
			element.style.top = (object_x = coordinates(e, 1) + start_y - mouse_y + 'px'); 
			element.style.left = (object_y = coordinates(e) + start_x - mouse_x + 'px'); 
		} 
	}
	var start_x = parseInt(element.style.left), start_y = parseInt(element.style.top);
	var mouse_x  = coordinates(e), mouse_y = coordinates(e, 1);
	var object_x, object_y;
	var dragging = true;
	document.onmousemove = drag;
	document.onmouseup = function () {
		dragging = false;
	}
}
if(key_cheats) {
	document.onkeypress = key_press;	
}
