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

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

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

	Table of Contents
	-----------------
	Configuration

	Functions
	- getPageScroll()
	- getPageSize()
	- pause()
	- getKey()
	- listenKey()
	- showLightbox()
	- hideLightbox()
	- initLightbox()
	- addLoadEvent()

	Function Calls
	- addLoadEvent(initLightbox)

*/



//
// Configuration
//

// If you would like to use a custom loading image or close button reference them in the next two lines.
//var loadingImage = '/img/loading.gif';
var loadingImage = '';
var closeButton = '';
var objKeyboardMsg = document.createElement("div");




//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}



//
// 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 if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
xScroll = document.documentElement.scrollWidth;
yScroll = document.documentElement.scrollHeight;
} else { // Explorer Mac...would also work in 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(pageWidth,pageHeight,windowWidth,windowHeight) 
return arrayPageSize;
}

//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

//
// getKey(key)
// Gets keycode. If 'x' is pressed then it hides the lightbox.
//

function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();

	if(key == 'x'){ hideLightbox(); }
}


//
// listenKey()
//
function listenKey () {	document.onkeypress = getKey; }


//
// showLightbox()
// Preloads images. Pleaces new image in lightbox then centers and displays.
//
function showLightbox(objLink)
{
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var objLightboxFrame = document.getElementById('lightboxFrame');
	var objCaption = document.getElementById('lightboxCaption');
	var objImage = document.getElementById('lightboxImage');
	var objLoadingImage = document.getElementById('loadingImage');
	var objLightboxDetails = document.getElementById('lightboxDetails');
	var FlashWidth = 480; // these values should be overwritten by rel2 and rel3 properties in <a> tag
	var FlashHeight = 300;

	rel2 = objLink.getAttribute('rel2');
	rel3 = objLink.getAttribute('rel3');
	if(rel2 && rel2 != '') FlashWidth = rel2;
	if(rel3 && rel3 != '') FlashWidth = rel3;

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// center loadingImage if it exists
	if (objLoadingImage) {
		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
		objLoadingImage.style.display = 'block';
	}

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';

	wb = document.getElementById('w-ban');
	if (wb)
	{
		wb.style.visibility = 'hidden';
	}
	wb = document.getElementById('l-ban');
	if (wb)
	{
		wb.style.visibility = 'hidden';
	}
	wb = document.getElementById('top-banner');
	if (wb)
	{
		wb.style.visibility = 'hidden';
	}

	if ((objLink.href.indexOf('.htm') > 0) || (objLink.href.indexOf('.swf') > 0))
	{
		objKeyboardMsg.innerHTML = innHTML + innHTML2;

		if (objLink.href.indexOf('.htm') > 0)
			objLightboxFrame.innerHTML = '<iframe frameborder="0" scrolling="none" marginwidth="0" marginheight="0" src="' + objLink.href + '" width="' + FlashWidth + '" height="' + FlashHeight + '"></iframe>';
		else
		{
			FlashWidth = objLink.getAttribute('rel2');
			FlashHeight = objLink.getAttribute('rel3');

			objLightboxFrame.innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + FlashWidth + '" height="' + FlashHeight + '" id="Viewer" align="middle">'
			+ '<param name="allowScriptAccess" value="always" />'
			+ '<param name="movie" value="' + objLink.href + '" />'
			+ '<param name="menu" value="false" />'
			+ '<param name="quality" value="high" />'
			//+ '<param name="scale" value="noscale" />'
			+ '<param name="salign" value="lt" />'
			+ '<embed src="' + objLink.href + '" menu="false" quality="high" scale="noscale" salign="lt" width="' + FlashWidth + '" height="' + FlashHeight + '" name="Viewer" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
			+ '</object>';
		}
		// center lightbox and make sure that the top and left values are not negative
		// and the image placed outside the viewport
		var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - FlashHeight) / 2);
		var lightboxLeft = ((arrayPageSize[0] - 20 - FlashWidth) / 2);

		objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";

		objLightboxDetails.style.width = FlashWidth + 'px';

		if(objLink.getAttribute('title')){
			objCaption.style.display = 'block';
			//objCaption.style.width = FlashWidth + 'px';
			objCaption.innerHTML = objLink.getAttribute('title');
		} else {
			objCaption.style.display = 'none';
		}
		if (navigator.appVersion.indexOf("MSIE")!=-1){
			pause(250);
		}
		if (objLoadingImage) {	objLoadingImage.style.display = 'none'; }
		selects = document.getElementsByTagName("select");
        	for (i = 0; i != selects.length; i++) {
                	selects[i].style.visibility = "hidden";
        	}
		objLightbox.style.display = 'block';
		arrayPageSize = getPageSize();
		objOverlay.style.height = (arrayPageSize[1] + 'px');
		listenKey();
		return false;

	}
	else
	{
		// preload image
		imgPreload = new Image();

	imgPreload.onload=function(){
		objImage.src = objLink.href;

		// center lightbox and make sure that the top and left values are not negative
		// and the image placed outside the viewport
		var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imgPreload.height) / 2);
		var lightboxLeft = ((arrayPageSize[0] - 20 - imgPreload.width) / 2);

		objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";


		objLightboxDetails.style.width = imgPreload.width + 'px';

		if(objLink.getAttribute('title')){
			objCaption.style.display = 'block';
			//objCaption.style.width = imgPreload.width + 'px';
			objCaption.innerHTML = objLink.getAttribute('title');
		} else {
			objCaption.style.display = 'none';
		}
		if (navigator.appVersion.indexOf("MSIE")!=-1){
			pause(250);
		}
		if (objLoadingImage) {	objLoadingImage.style.display = 'none'; }
		selects = document.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++) {
                selects[i].style.visibility = "hidden";
        }
		objLightbox.style.display = 'block';
		arrayPageSize = getPageSize();
		objOverlay.style.height = (arrayPageSize[1] + 'px');
		listenKey();
		return false;
	}

	}

	if ((objLink.href.indexOf('.htm') < 0) || (objLink.href.indexOf('.swf') < 0))
	{
		imgPreload.src = objLink.href;

		rel2 = objLink.getAttribute('rel2');
		rel3 = objLink.getAttribute('rel3');
		rel4 = objLink.getAttribute('rel4');
		if (objLink.getAttribute('relname'))
		{
			relname = objLink.getAttribute('relname');
			if (relname && relname != '')
			{
				relname = "<div class=\"light_name\">"+relname+"</div>";
				innHTML = innHTML.replace(/<div class="light_name">(.*?)<\/div>/gi, relname);
			}
		}



		reld = objLink.getAttribute('reld');
		if (rel2 && rel2 != '')
		{
			innHTMLall = innHTML + '<div style="text-align:center">' + rel2 + countHTML  + '</div>' + innHTML2;
			if (reld)
			innHTMLall += '<div style="text-align:center">' + reld + '</div>';
		}
		else
		{
			innHTMLall = innHTML + innHTML2;
			if (reld)
			innHTMLall += '<div style="text-align:center">' + reld + '</div>';
		}
		if (rel3 && rel3 != '')
			innHTMLall = innHTMLall + innHTML3 + rel3 + innHTML4;
		if (rel4 && rel4 != '')
			innHTMLall = innHTMLall + innHTML5 + rel4 + innHTML6;

		objKeyboardMsg.innerHTML = innHTMLall;
	}
}

function hideLightbox()
{
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
	document.onkeypress = '';

	wb = document.getElementById('w-ban');
	if (wb)
	{
		wb.style.visibility = '';
	}
	wb = document.getElementById('l-ban');
	if (wb)
	{
		wb.style.visibility = '';
	}
	wb = document.getElementById('top-banner');
	if (wb)
	{
		wb.style.visibility = '';
	}

}

function initLightbox()
{
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");

	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightbox")){
			anchor.onclick = function () {showLightbox(this); return false;}
		}
	}
	var objBody = document.getElementsByTagName("body").item(0);
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {hideLightbox(); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var imgPreloader = new Image();
	imgPreloader.onload=function(){
		var objLoadingImageLink = document.createElement("a");
		objLoadingImageLink.setAttribute('href','#');
		objLoadingImageLink.onclick = function () {hideLightbox(); return false;}
		objOverlay.appendChild(objLoadingImageLink);
		var objLoadingImage = document.createElement("img");
		objLoadingImage.src = loadingImage;
		objLoadingImage.setAttribute('id','loadingImage');
		objLoadingImage.style.position = 'absolute';
		objLoadingImage.style.zIndex = '150';
		objLoadingImageLink.appendChild(objLoadingImage);
		imgPreloader.onload=function(){};
		return false;
	}
	imgPreloader.src = loadingImage;
	var objLightbox = document.createElement("div");
	objLightbox.setAttribute('id','lightbox');
	objLightbox.style.display = 'none';
	objLightbox.style.position = 'absolute';
	objLightbox.style.zIndex = '100';
	objBody.insertBefore(objLightbox, objOverlay.nextSibling);

	var objLightboxFrame = document.createElement("div");
	objLightboxFrame.setAttribute('id','lightboxFrame');
	objLightbox.style.display = 'none';
	objLightbox.style.position = 'absolute';
	objLightbox.style.zIndex = '100';
	objLightbox.appendChild(objLightboxFrame);

	var objLink = document.createElement("a");
	objLink.setAttribute('href','#');
	objLink.setAttribute('title','Закрыть');
	objLink.onclick = function () {hideLightbox(); return false;}
	objLightboxFrame.appendChild(objLink);
	var imgPreloadCloseButton = new Image();
	imgPreloadCloseButton.onload=function(){
		var objCloseButton = document.createElement("img");
		objCloseButton.src = closeButton;
		objCloseButton.setAttribute('id','closeButton');
		objCloseButton.style.position = 'absolute';
		objCloseButton.style.zIndex = '200';
		objLink.appendChild(objCloseButton);
		return false;
	}
	imgPreloadCloseButton.src = closeButton;
	var objImage = document.createElement("img");
	objImage.setAttribute('id','lightboxImage');
	objLink.appendChild(objImage);
	var objLightboxDetails = document.createElement("div");
	objLightboxDetails.setAttribute('id','lightboxDetails');
	objLightbox.appendChild(objLightboxDetails);
	var objCaption = document.createElement("div");
	objCaption.setAttribute('id','lightboxCaption');
	objCaption.style.display = 'none';
	objLightboxDetails.appendChild(objCaption);
	objKeyboardMsg.setAttribute('id','keyboardMsg');
    	    objKeyboardMsg.innerHTML = (typeof(innHTML) != "undefined" ? innHTML : '') + (typeof(countHTML) != "undefined" ? countHTML : '') + innHTML2 + innHTML3 + innHTML4;
	objLightboxDetails.appendChild(objKeyboardMsg);
}
