/*
 * ShowTime (c) by widelldesign 2007.
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 * Basic code extracted from Thickbox by Cody Lindley,
 * which in turn is built on the jQuery library.
 */

//on page load call ShowTime_init
$(document).ready(ST_init);

//add ShowTime to href elements that have a class of .slideshow
function ST_init(){
	$("a.slideshow").click(function(){
	ST_show(this.href,'slideshow');
	this.blur();
	return false;
	});
	$("a.video").click(function(){
	ST_show(this.href,'video');
	this.blur();
	return false;
	});
	$("a.video_vimeo").click(function(){
	ST_show(this.href,'video_vimeo');
	this.blur();
	return false;
	});
	$("a.mp3").click(function(){
	ST_show(this.href,'mp3');
	this.blur();
	return false;
	});	
	
}

function ST_show(url,typ) {

		if (document.getElementById("LightsOut") == null) {
			if (typ == 'slideshow'){
				$("body").append("<div id='LightsOut'></div><div id='SlideShow'><div style='position:absolute;z-index:200;top:27px;left:32px;'><iframe src='" + url + "' frameborder='0' height='390' scrolling='no' width='700'></iframe></div></div>");
			} else if (typ == 'video'){
				$("body").append("<div id='LightsOut'></div><div id='Video'><div style='position:absolute;z-index:200;top:27px;left:32px;'><iframe src='" + url + "' frameborder='0' height='380' scrolling='no' width='480'></iframe></div></div>");
			} else if (typ == 'video_vimeo'){
				$("body").append("<div id='LightsOut'></div><div id='Video_vimeo'><div style='position:absolute;z-index:200;top:9px;left:32px;'><iframe src='" + url + "' frameborder='0' height='350' scrolling='no' width='550'></iframe></div></div>");
			} else if (typ == 'mp3'){
				$("body").append("<div id='LightsOut'></div><div id='Mp3'><div style='position:absolute;z-index:200;top:23px;left:32px;'><iframe src='" + url + "' frameborder='0' height='20' scrolling='no' width='700'></iframe></div></div>");
			}
			
			$("#LightsOut").click(function(){
			$("#Video,#Video_vimeo,#SlideShow,#Mp3,#LightsOut").remove();
			return false;
		});
			document.onkeydown = function(e){
				if ($('#LightsOut').css('display') =='block'){
					if (e == null) { // ie
						keycode = event.keyCode;
					} else { // mozilla
						keycode = e.which;
					}
					if(keycode == 27){ // close
						$("#Video,#Video_vimeo,#SlideShow,#LightsOut").remove();
					}
				}
			}
	
		}

	

	ST_overlaySize();
	ST_position();
	//alert('Fönstrets mått: ' + a[0] +'x' + a[1] + ' Scroll: ' + b[0] + ', ' + b[1] + ' Sidans mått: ' + c[0] +'x' + c[1] + ' Typ: ' + typ);


}

function ST_position() {
	var pagesize = ST_getPageSize();	
	var arrayPageScroll = ST_getPageScrollTop();
	$("#Video").css({left: (arrayPageScroll[0] + (pagesize[0] - 544)/2)+"px", top: (arrayPageScroll[1] + (pagesize[1]-444)/2)+"px" });
	$("#Video_vimeo").css({left: (arrayPageScroll[0] + (pagesize[0] - 600)/2)+"px", top: (arrayPageScroll[1] + (pagesize[1]-444)/2)+"px" });
	$("#SlideShow").css({left: (arrayPageScroll[0] + (pagesize[0] - 765)/2)+"px", top: (arrayPageScroll[1] + (pagesize[1]-453)/2)+"px" });
	$("#Mp3").css({left: (arrayPageScroll[0] + (pagesize[0] - 765)/2)+"px", top: (arrayPageScroll[1] + (pagesize[1]-97)/2)+"px" });
}


function ST_overlaySize(){
	if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {	
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		var deff = document.documentElement;
		var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	} else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
  	}
	$("#LightsOut").css({"height":yScroll +"px", "width":xScroll +"px"});
}

function ST_getPageScrollTop(){
	var yScrolltop;
	var xScrollleft;
	if (self.pageYOffset || self.pageXOffset) {
		yScrolltop = self.pageYOffset;
		xScrollleft = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){	 // Explorer 6 Strict
		yScrolltop = document.documentElement.scrollTop;
		xScrollleft = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScrolltop = document.body.scrollTop;
		xScrollleft = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScrollleft,yScrolltop);
	return arrayPageScroll;
}

function ST_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
	arrayPageSize = new Array(w,h) 
	return arrayPageSize;
}
