// Listaet kartinki. Beret faily iz massiva
// i vyzivaet funkciyu showBox
// Polu4aet argument:
// 0 - ukazyvat osnovnuyu kartinku,
// 1 - predidushuyu
// 2 - sleduyushuyu
//
var maxi;
var im;
var allImages = new Array();
var allPrompts = new Array();


function showBoxesArray(whatshow) {
	var arrows;
	if(!maxi)
		{
		im=0;
		maxi=allImages.length;
		}
	if((whatshow==2)&&(im<(maxi-1)))im++;
	if((whatshow==1)&&(im))im--;
	if(!whatshow)im=0;
	if(!im)arrows=2;
	else if(im==(maxi-1))arrows=1;
	else arrows=3;
	showBox(allImages[im], 0, allPrompts[im], arrows);
	}


// Pokazyvaet box s kartinkoi ili html
// dla image type = 0
// dla html type = 1
// theTarget - ssylka na to 4to nugno otkryt
// showarrows = 3 - pokazivat strelki
//            = 1 - tolko levuyu
//            = 2 - tolko pravuyu
//
function showBox(theTarget, type, title, showarrows) {
	var pageCoords = getPageCoords();
	theShadow=makeShadow(pageCoords);

	var theLoading = document.createElement('div');
	theLoading.id = 'loading';
	theLoading.style.top = parseInt(pageCoords[2] + (pageCoords[0] - 55) / 2) + 'px';
	//theLoading.onclick = function() { closeBox(); }
	theShadow.appendChild(theLoading);

	// sozdaem element div - 'beloe okno'
	var theBox = document.createElement('div');
	theBox.id = 'litebox';
	theBox.title = "lai aizvērtu, uzklikšķini";

	// etot blok tolko dla video
	if(type)
		{

		theBox.onclick = function() { closeBox(); }
		theBox.style.width = '330px';
		theBox.style.marginTop = parseInt(pageCoords[2] + (pageCoords[0] - 240 - 50) / 2) + 'px';

		var dataPreload=AJAXrequest(theTarget);
		theShadow.removeChild(theLoading);
		theBox.innerHTML+=dataPreload;
		//theBox.appendChild(dataPreload);

		var theCaption = document.createElement('p');
		theCaption.innerHTML = (title) ? title : '';
		theCaption.innerHTML += "<em>klikšķini uz bildes vai nospied ESC, lai aizvērtu logu</em>";
		theBox.appendChild(theCaption);

		theShadow.appendChild(theBox);
		document.onkeypress = getKey;
		}
// to 4to nige - eto dla image
	else 
		{
		var imgPreload = new Image();
		
		imgPreload.onload = function() {
		theBox.style.width = imgPreload.width + 10 + 'px';
		theBox.style.marginTop = parseInt(pageCoords[2] + (pageCoords[0] - imgPreload.height - 50) / 2) + 'px';

		var theImage = document.createElement('img');
		theImage.src = theTarget;
		theImage.alt = title;
		theImage.onclick = function() { closeBox(); }
		theImage.width = imgPreload.width;
		theImage.title = "lai aizvērtu, klikšķini šeit";
		
		
		// Esli peremennaya showarrows != 0 - zna4it nugno pokazat strelki
		if(showarrows)
			{
			leftArrow = document.createElement('img');
			leftArrow.src = "/images/litebox/thumb-prev.gif";
			leftArrow.style.styleFloat = "left";
			leftArrow.onclick = function() {closeBox();showBoxesArray(1);}

			rightArrow = document.createElement('img');
			rightArrow.src = "/images/litebox/thumb-next.gif";
			rightArrow.style.styleFloat = "right";
			rightArrow.onclick = function() {closeBox();showBoxesArray(2);}
			}

		var theCaption = document.createElement('p');
		theCaption.innerHTML = (theImage.alt) ? theImage.alt : '';
		theCaption.innerHTML += "<em>klikšķini šeit vai spied ESC, lai aizvērtu logu</em><br>";
		theShadow.removeChild(theLoading);
		theBox.appendChild(theImage);
		if((showarrows==1)||(showarrows==3))theBox.appendChild(leftArrow);
		if(showarrows>1)theBox.appendChild(rightArrow);
		theBox.appendChild(theCaption);
		theShadow.appendChild(theBox);

		document.onkeypress = getKey;
		return false;
			}
		imgPreload.src = theTarget;
		}
}


// funkciya polu4eniya AJAX danyh
function AJAXrequest(url) {
		// polu4aem obyekt XMLHTTPRequest
			this.http = get_http();
			this.working = false;

		// Zapros
		if (!this.working && this.http) {
			var http = this.http;

			this.http.open("GET", url, false);
			this.working = true;
			this.http.send(null);

		if(!this.http){
			alert("Error creating XMLHTTP object!");
			}
	return (http.responseText);
	}
}


// kross-brouzernaya realizaciya polu4eniya obyekta XMLHTTP 
function get_http(){
    var xmlhttp;
    /*@cc_on
    @if (@_jscript_version >= 5)
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new 
                ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlhttp = false;
            }
        }
    @else
        xmlhttp = false;
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}


// Delaet zatenenie ekrana
function makeShadow(pageCoords) {
	var theBody = document.getElementsByTagName('body')[0];
	var theShadow = document.createElement('div');
	theShadow.id = 'shadow';
	theShadow.style.height = (pageCoords[1] + 'px');
	theShadow.className = 'on';
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
		}
	theBody.insertBefore(theShadow, theBody.firstChild);
	return(theShadow);
}

function getPageCoords() {
	var coords = [0, 0, 0]; // height of window, document, scroll pos
	// all except IE
	if (window.innerHeight) {
		coords[0] = window.innerHeight;
		coords[2] = window.pageYOffset;
	}
	// IE 6 Strict
	else if (document.documentElement && document.documentElement.clientHeight != 0) {
		coords[0] = document.documentElement.clientHeight;
		coords[2] = document.documentElement.scrollTop;
	}
	else if (document.body) {
		coords[0] = document.body.clientHeight;
		coords[2] = document.body.scrollTop;
	}

	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight;
	if (test1 > test2) {
		coords[1] = document.body.scrollHeight;
	} else {
		coords[1] = document.body.offsetHeight;
	}
	if (coords[1] < coords[0]) coords[1] = coords[0];

	return coords;
}

// Zakryvaet vsplyvayushee okno
function closeBox() {
	var theBody = document.getElementsByTagName('body')[0];
	var theBox = document.getElementById('litebox');
	if (theBox) theBox.style.display = 'none';
	var theShadow = document.getElementById('shadow');
	if (theShadow) theShadow.style.display = 'none';
	theBody.removeChild(theShadow);

	selects = document.getElementsByTagName('select');
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = 'visible';
	}
	document.onkeypress = '';
	return false;
}

function getKey(e) {
	var arrowImg;

	if (!e) var e = window.event;
	var keycode = e.keyCode ? e.keyCode : e.which;

	switch (keycode) {
  	case 27: // esc
		case 32: // spacebar
			closeBox();
			break;
	}
	if (arrowImg) { closeBox(); showBox(arrowImg); }
	return false;
}

