// JavaScript Document
var picture_array = new Array();
var loader_image = new Image();
var slide_image = new Image();
var picture_on;
var difh;
var difw;
loader_image.src = "/images/image_loader.gif";

function show_picture(picpath,picid,picw,pich,mtype) {
	popup = document.getElementById('picture_window');
	popup.innerHTML = '<img src="' + loader_image.src + '">';
	popup.style.display = 'block';
	center_screen(popup, true);
	if (mtype == 1) {
		var loader = new ImageLoader(picpath);
		loader.loadEvent = function(url, image){
			difh = popup.offsetHeight;
			difw = popup.offsetWidth;
			popup.innerHTML = '';
			popup.style.height = difh + 'px';
			popup.style.width = difw + 'px';
			resize_window(image.src, picw, pich, 1);
			if (picture_array[picid]) {
				document.getElementById('picture_info_text').innerHTML = picture_array[picid][2];
			}
			picture_on = picid;
			center_screen(popup, true);
		}
		loader.load();
	} else {
		difh = popup.offsetHeight;
		difw = popup.offsetWidth;
		popup.innerHTML = '';
		popup.style.height = difh + 'px';
		popup.style.width = difw + 'px';
		resize_window(picpath, picw, pich, 2);
		picture_on = picid;
	}
}

function resize_window(src, picw, pich, mtype) {
	popup = document.getElementById('picture_window');
	if (difw < picw) {
		difw = (difw + Math.ceil((picw - difw) / 10));
		if (difw > picw) difw = picw;
		popup.style.width = difw + 'px';
		center_screen(popup, true);
		mytime = setTimeout("resize_window('" + src + "', " + picw + ", " + pich + ", " + mtype + ");", 10);
	} else if (difh < pich) {
		difh = (difh + Math.ceil((pich - difh) / 10));
		if (difh > pich) difh = pich;
		popup.style.height = difh + 'px';
		center_screen(popup, true);
		mytime = setTimeout("resize_window('" + src + "', " + picw + ", " + pich + ", " + mtype + ");", 10);
	} else {
		if (mtype == 1) {
			content = '<img src="' + src + '" onmouseover="javascript:on_picture();" onmouseout="javascript:off_picture();" border="0">';
		} else {
			content = '<br><br><br><object width="560" height="340" onmouseover="javascript:on_picture();" onmouseout="javascript:off_picture();"><param NAME="wmode" VALUE="transparent"><param name="movie" value="http://www.youtube.com/v/' + src + '?fs=1&amp;hl=en_US&amp;color1=0x006699&amp;color2=0x54abd6"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + src + '?fs=1&amp;hl=en_US&amp;color1=0x006699&amp;color2=0x54abd6" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>';
		}
		popup.innerHTML = content;
		center_screen(popup, true);
	}
}
		

function close_picture() {
	document.getElementById('picture_window').style.display = 'none';
	document.getElementById('picture_window').style.width = 'auto';
	document.getElementById('picture_window').style.height = 'auto';
	button_prev = document.getElementById('picture_prev');
	button_next = document.getElementById('picture_next');
	button_close = document.getElementById('picture_close');
	info = document.getElementById('picture_info_div');
	button_prev.style.display = 'none';
	button_next.style.display = 'none';
	button_close.style.display = 'none';
	info.style.display = 'none';
}

function on_picture() {
	popup = document.getElementById('picture_window');
	button_prev = document.getElementById('picture_prev');
	button_next = document.getElementById('picture_next');
	button_close = document.getElementById('picture_close');
	picture_info = document.getElementById('picture_info_div');
	if (picture_array.length > 1) {
		button_next.style.display = 'block';
		button_next.style.left = (popup.offsetLeft + popup.offsetWidth - button_next.offsetWidth - 6) + 'px';
		button_next.style.top = (popup.offsetTop + 50) + 'px';
		button_prev.style.display = 'block';
		button_prev.style.left = (popup.offsetLeft + 6) + 'px';
		button_prev.style.top = (popup.offsetTop + 50) + 'px';
	}
	button_close.style.display = 'block';
	button_close.style.left = (popup.offsetLeft + (popup.offsetWidth / 2) - (button_close.offsetWidth / 2)) + 'px';
	button_close.style.top = (popup.offsetTop + 6) + 'px';
	picture_info.style.display = 'block';
	picture_info.style.width = (popup.offsetWidth - 12) + 'px';
	picture_info.style.left = (popup.offsetLeft + 6) + 'px';
	picture_info.style.top = (popup.offsetTop + popup.offsetHeight - picture_info.offsetHeight - 6) + 'px';
}

function off_picture() {
	button_prev = document.getElementById('picture_prev');
	button_next = document.getElementById('picture_next');
	button_close = document.getElementById('picture_close');
	info = document.getElementById('picture_info_div');
	button_prev.style.display = 'none';
	button_next.style.display = 'none';
	button_close.style.display = 'none';
	info.style.display = 'none';
}

function next_picture() {
	close_picture();
	var next_id;
	if (picture_array.length > 1) {
		next_id = picture_on + 1;
		if (next_id >= picture_array.length) {
			next_id = 0;
		}
		show_picture(picture_array[next_id][0],next_id,picture_array[next_id][3],picture_array[next_id][4],picture_array[next_id][5]);
	}
}

function prev_picture() {
	close_picture();
	var next_id;
	if (picture_array.length > 1) {
		next_id = picture_on - 1;
		if (next_id < 0) {
			next_id = picture_array.length - 1;
		}
		show_picture(picture_array[next_id][0],next_id,picture_array[next_id][3],picture_array[next_id][4],picture_array[next_id][5]);
	}
}

function center_screen(div, verticalalign) {	
	var wwidth = 0, wheight = 0, wleft = 0, wtop = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		wwidth = window.innerWidth;
		wheight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		wwidth = document.documentElement.clientWidth;
		wheight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		wwidth = document.body.clientWidth;
		wheight = document.body.clientHeight;
	}
	if( typeof( window.pageYOffset ) == 'number' ) {
		wtop = window.pageYOffset;
		wleft = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		wtop = document.body.scrollTop;
		wleft = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		wtop = document.documentElement.scrollTop;
		wleft = document.documentElement.scrollLeft;
	}

	var divtop = (wheight - div.offsetHeight) / 2;
	var divleft = (wwidth - div.offsetWidth) / 2;
	
	div.style.left = divleft + wleft + 'px';
	if (verticalalign)
		div.style.top = divtop + wtop + 'px';
}

window.onresize = function(){
	center_screen(document.getElementById('picture_window'), false);
}

window.onscroll = function() {
	center_screen(document.getElementById('picture_window'), false);
}

function addListener(element, type, expression, bubbling) {
	bubbling = bubbling || false;
	if(window.addEventListener)	{
		element.addEventListener(type, expression, bubbling);
		return true;
	} else if(window.attachEvent) {
		element.attachEvent('on' + type, expression);
		return true;
	} else return false;
}

var ImageLoader = function(url){
	this.url = url;
	this.image = null;
	this.loadEvent = null;
}

ImageLoader.prototype = {
	load:function() {
		this.image = document.createElement('img');
		var url = this.url;
		var image = this.image;
		var loadEvent = this.loadEvent;
		addListener(this.image, 'load', function(e){
			if(loadEvent != null) {
				loadEvent(url, image);
			}
		}, false);
		this.image.src = this.url;
	},
	getImage:function(){
		return this.image;
	}
}
