var showBox;

$(document).ready( function() {
	$("UL.image A").click( function() {
		showBox = new ShowBox($(this)[0]);
		showBox.init();
		return false;
	});
	$("#menu A").each( function() {
		if ($(this)[0].href.indexOf('#') != -1)
			$(this).addClass("notavailable");
	});
	$("#menu A").click( function() {
		if ($(this)[0].href.indexOf('#') != -1)
	   		return false;
	});
});

function ShowBox(el) {
	this.el = el;
	this.rootWidth = 782;
	this.overlay = obj('overlay');
	this.loading = obj('loading');
	this.DIVimage = obj('image');
	this.DIVclose = obj('close');
	
	this.image = this.DIVimage.childNodes[0];

	this.box = obj('preview');
	this.box.width = 150;
	this.box.height = 113;
}
ShowBox.prototype.init = function() {
	var yScroll = getPageYScroll();
	var pageSize = getPageSize();

	this.overlay.style.left = (0-(pageSize[0]-this.rootWidth)/2)+'px';
	this.overlay.style.width = pageSize[0]-10+'px';
	this.overlay.style.height = pageSize[1]+'px';
	$(this.overlay).show();

	$(this.DIVclose).show();

	this.box.style.top = (pageSize[3]/2-this.box.height/2+yScroll)+'px';
	this.box.style.left = Math.ceil(this.rootWidth/2-this.box.width/2)+'px';
	$(this.box).show();

	var imgPreloader = new Image();
	imgPreloader.onload = function() { showBox.loaded(imgPreloader); }
	imgPreloader.src = this.el.href;
	
	this.overlay.onclick = function() { showBox.hide(); }
	this.DIVclose.onclick = function() { showBox.hide(); }
}
ShowBox.prototype.loaded = function(imgPreloader) {
	this.image.src = imgPreloader.src;
	this.image.style.width = imgPreloader.width+'px';
	this.image.style.height = imgPreloader.height+'px';
	this.resize(imgPreloader.width, imgPreloader.height);
}
ShowBox.prototype.resize = function(imgWidth, imgHeight) {
	if (this.box.style.width == '') this.box.style.width = this.box.width+'px';
	if (this.box.style.height == '') this.box.style.height = this.box.height+'px';
	$(this.box).animate({height: imgHeight, top: parseInt(this.box.style.top)-(imgHeight-this.box.height)/2, width: imgWidth, left: parseInt(this.box.style.left)-(imgWidth-this.box.width)/2}, "normal", null, function() { showBox.showPicure(); });
}
ShowBox.prototype.showPicure = function() {
	$(this.DIVimage).show();
	$(this.loading).hide();
}
ShowBox.prototype.hide = function() {
	$(this.box).hide();
	$(this.overlay).hide();
	$(this.DIVclose).hide();
	$(this.DIVimage).hide();
	this.box.style.width	= '';
	this.box.style.height	= '';
	this.box.style.left		= '';
	$(this.loading).show();
}

function obj(id) {
	return document.getElementById(id);
}

// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
function getPageYScroll() {
	if (self.pageYOffset)
		return self.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
		return document.documentElement.scrollTop;
	else if (document.body) // all other Explorers
		return document.body.scrollTop;
}
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
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 { // Explorer Mac...would also work in Explorer 6 Strict, 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;
	}

	return new Array(pageWidth,pageHeight,windowWidth,windowHeight);
}
