/*
	Multifaceted Lightbox
	by Greg Neustaetter - http://www.gregphoto.net

	INSPIRED BY (AND CODE TAKEN FROM)
	==================================
	The Lightbox Effect without Lightbox
	PJ Hyett
	http://pjhyett.com/articles/2006/02/09/the-lightbox-effect-without-lightbox


	Lightbox JS: Fullsize Image Overlays
	by Lokesh Dhakar - http://www.huddletogether.com

	For more information on this script, visit:
	http://huddletogether.com/projects/lightbox/

	Licensend under:
	Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
	(basically, do anything you want, just leave my name and link)

*/

var Lightbox = {
	lightboxType : null,
	lightboxCurrentContentID : null,

	showBoxString : function(content, boxWidth, boxHeight){
		this.setLightboxDimensions(boxWidth, boxHeight);
		this.lightboxType = 'string';
		var contents = $('boxContents');
		contents.innerHTML = content;
		this.showBox();
		return false;
	},


	showBoxImage : function(href, boxWidth, boxHeight) {
		this.lightboxType = 'image';
		this.setLightboxDimensions(boxWidth, boxHeight);
		var contents = $('boxContents');
		contents.style.lineHeight = boxHeight+'px';
		contents.style.background ='white';
		var objImage = document.createElement("img");
		objImage.setAttribute('id','lightboxImage');
		contents.appendChild(objImage);
		imgPreload = new Image();
		imgPreload.onload=function(){
			objImage.src = href;
			Lightbox.showBox();
		}
		imgPreload.src = href;
		return false;
	},

	showBoxByID : function(id, boxWidth, boxHeight) {
		this.lightboxType = 'id';
		this.lightboxCurrentContentID = id;
		this.setLightboxDimensions(boxWidth, boxHeight);
		var element = $(id);
		var contents = $('boxContents');
		contents.appendChild(element);
		Element.show(id);
		this.showBox();
		return false;
	},

	showBoxByAJAX : function(href, boxWidth, boxHeight, onComplete) {
		this.lightboxType = 'ajax';
		this.setLightboxDimensions(boxWidth, boxHeight);
		var contents = $('boxContents');
		
		var myAjax;
		if (onComplete)
			myAjax = new Ajax.Updater(contents, href, {method: 'get', onComplete: onComplete});
		else	myAjax = new Ajax.Updater(contents, href, {method: 'get'});
		
		this.showBox();
		return false;
	},

showBoxByIFrame : function(href, boxWidth, boxHeight,scrolling) {
		if(scrolling=='' || scrolling ==undefined || scrolling == "undefined") scrolling = "no";
		this.lightboxType = 'iframe';
		this.setLightboxDimensions(boxWidth, boxHeight);
		var contents = $('boxContents');

		var objIFrame = document.createElement("iframe");
		objIFrame.setAttribute('id', 'iframeMappa');
		objIFrame.setAttribute('border', '0');
		objIFrame.setAttribute('frameborder', 'no');
		objIFrame.setAttribute('framespacing', '0');
		objIFrame.setAttribute('scrolling', scrolling);
		objIFrame.setAttribute('marginwidth', '0');
		objIFrame.setAttribute('marginheight', '0');
		objIFrame.setAttribute('background', 'red');
		objIFrame.setAttribute('src', href);
		objIFrame.setAttribute('allowtransparency','allowtransparency');
		objIFrame.style.visibility = "hidden";
		objIFrame.style.width=boxWidth+'px';
		objIFrame.style.height=boxHeight+'px';

		var objLoading = document.createElement("div");
		objLoading.setAttribute('id', 'iframeLoading');
		objLoading.innerHTML = 'Trwa ładowanie <br /><br /><img src="/vimages/default/ajax-loader.gif";/>';

		

		contents.appendChild(objLoading);

		try
		{	with (objLoading.style)
			{	position = 'absolute';
				top = (parseInt((boxHeight / 2)) - 10) + 'px';
				width = '99%';
				textAlign = 'center';
			}
		}
		catch (e) {}

		contents.appendChild(objIFrame);

		Event.observe(objIFrame, 'load', this.onIFrameLoad, false);

		this.showBox();
		return false;
	},

	onIFrameLoad : function(e)
	{
		if (this)
		{	if (this.style)
				this.style.visibility = "visible";

			if (this.parentNode.childNodes.item(0).id == "iframeLoading")
				this.parentNode.removeChild(this.parentNode.childNodes.item(0));
			try
			{	this.contentWindow.document.body.style.border = "0";
				this.contentWindow.document.body.style.background ="transparent";
			}
			catch (e) {}

		}
	},

	setLightboxDimensions : function(width, height) {
		var windowSize = this.getPageDimensions();
		
		if(width) {
			if(width < windowSize[0]) {
				 $('boxContents').style.width= width + 'px';
			} else {
				$('boxContents').style.width = (windowSize[0] - 50) + 'px';
				
			}
		}
		if(height) {
			if(height < windowSize[1]) {
				$('boxContents').style.height = height + 'px';
			} else {
				$('boxContents').style.height = (windowSize[1] - 50) + 'px';
			}
		}
		$('box').style.height=height+20+'px';
		$('box').style.width=width+20+'px';

		
	},


	showBox : function() {
		
		
		
		if (/MSIE 6./i.test(navigator.userAgent))
			document.styleSheets[0].addRule("select", 'display:none');

		Element.show('overlay');
		this.center('box');

		Event.observe(document, 'keydown', this.onGlobalKeyDown, false);

		return false;
	},


	hideBox : function(){
		var contents = $('boxContents');
		if(this.lightboxType == 'id') {
			var body = document.getElementsByTagName("body").item(0);
			Element.hide(this.lightboxCurrentContentID);
			body.appendChild($(this.lightboxCurrentContentID));
		}

		$('box').style.width = null;
		$('box').style.height = null;
		Element.hide('box');
		Element.hide('overlay');
		contents.innerHTML = '';

		document.getElementsByTagName("body").item(0).setStyle({
			overflow: '',
			marginRight: ''
		});

		if (/MSIE 6./i.test(navigator.userAgent))
			document.styleSheets[0].addRule("select", 'display:block');

		this.showScroll();

		Event.stopObserving(document, 'keydown', this.onGlobalKeyDown);

		return false;
	},

	hideContents : function() {
		Element.hide('boxContents');
	},

	showContents : function() {
		Element.show('boxContents');
	},

	onGlobalKeyDown: function(e)
	{
		var keycode = null;
		if (e == null)
			keycode = event.keyCode;
		else
		{	if (e.keyCode)
				keycode = e.keyCode;
			else	keycode = e.which;
		}

		switch (keycode)
		{
			case 27:
				Lightbox.hideBox();
				break;

			case 13:
				return;

			default:
				break;
		}
	},

	// taken Lightbox.from lightbox js, modified argument return order
	getPageDimensions : function(){
		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;
		}
		arrayPageSize = new Array(windowWidth,windowHeight,pageWidth,pageHeight)
		return arrayPageSize;
	},

	center : function(element){
		try{
			element = document.getElementById(element);
		}catch(e){
			return;
		}

		var windowSize = this.getPageDimensions();
		var window_width  = windowSize[0];
		var window_height = windowSize[1];

		$('overlay').style.height = windowSize[3] + 'px';

		element.style.position = 'fixed';
		element.style.zIndex   = 99;

		var scrollY = 0;

		if ( document.documentElement && document.documentElement.scrollTop ){
			scrollY = document.documentElement.scrollTop;
		}else if ( document.body && document.body.scrollTop ){
			scrollY = document.body.scrollTop;
		}else if ( window.pageYOffset ){
			scrollY = window.pageYOffset;
		}else if ( window.scrollY ){
			scrollY = window.scrollY;
		}

		var elementDimensions = Element.getDimensions(element);
		var setX = ( window_width  - elementDimensions.width  ) / 2;
		var setY = ( window_height - elementDimensions.height ) / 2;

		if (/MSIE 7./i.test(navigator.userAgent))
		{	
				element.style.position = 'fixed';

		}
		if (/MSIE 6./i.test(navigator.userAgent))
		{	element.style.position = 'absolute';
 			document.getElementsByTagName("body").item(0).setStyle({
				overflow: 'hidden',
 				marginRight: '16px'
			});
			setY += scrollY;
 		}

		setX = ( setX < 0 ) ? 0 : setX;
		setY = ( setY < 0 ) ? 0 : setY;

		var offsetTop = (window_height * 8) / 100;

		element.style.left = setX + "px";
		element.style.top  = setY - offsetTop + "px";

		Element.show(element);

	},

	init : function() {

		var lightboxtext = '<div id="box" style="display:none;">';
		lightboxtext += '<div id="close" onclick="Lightbox.hideBox()" style="text-align:right;"><img src="/vimages/default/close.png" alt="X" title="" height="25px" width="25px" style="position:relative;top:22px;right:-8px;z-index: 99;" onClick="Lightbox.hideBox()" /></div><div class="empty"></div>';
		lightboxtext += '<table  cellspacing="0" cellpadding="0" border="0" class="pop_dialog_table"><tr><td class="pop_topleft "></td><td class="pop_borderTop"></td><td class="pop_topright"></td></tr><tr><td class="pop_borderLateral"></td><td class="pop_content"><div id="boxContents"></div></td><td class="pop_borderLateral"></td></tr><tr><td class="pop_bottomleft"></td><td class="pop_borderTop"></td><td class="pop_bottomright"></td></tr></table></div>';

		lightboxtext += '</div><div class="empty"></div>';

		lightboxtext += '<div id="overlay" style="display:none" onClick="Lightbox.hideBox()"></div>';

		var body = document.getElementsByTagName("body").item(0);
		body.setAttribute ('overflow', 'hidden');

		new Insertion.Bottom(body, lightboxtext);
	},
	
	hideScroll: function () {
		var body = document.getElementsByTagName("body").item(0);
		body.setAttribute ('scroll', 'NO');
	},

	showScroll: function (){
		var body = document.getElementsByTagName("body").item(0);
		body.setAttribute ('scroll', 'YES');

	}

	
}

	

/*-----------------------------------------------------------------------------------------------*/

Event.observe(window, 'load', Lightbox.init, false);


//
// function lightboxInit()
// {	Lightbox.init();
// }
