﻿  var xmlhttp = false;

	try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}

	
	var first = true; // is image first in list?;
	var last = false; // is image last in list? if there is only one image in list, it is first and last on same time;
	var previous = ''; // translation of "previous image";
	var next = ''; // translation of "next image";
	var cur = -1; // current image ID;
	var pStart = 1; // pager first num in list;
	var images = []; // as whole gallery is stored in one record, then it's better to send it whole at once.
	var catPath = '/gal_slide.php'; 

	// getPageSize adopted from LyteBox script - http://www.dolem.com/lytebox/
	function getPageSize(){	
		var xScroll, yScroll, windowWidth, windowHeight;
		if (window.innerHeight && window.scrollMaxY) {
			xScroll = document.scrollWidth;
			yScroll = self.innerHeight + self.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else {
			xScroll = document.getElementsByTagName("html").item(0).offsetWidth;
			yScroll = document.getElementsByTagName("html").item(0).offsetHeight;
			xScroll = (xScroll < document.body.offsetWidth) ? document.body.offsetWidth : xScroll;
			yScroll = (yScroll < document.body.offsetHeight) ? document.body.offsetHeight : yScroll;
		}
		if (self.innerHeight) {
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) {
			windowWidth = document.getElementsByTagName("html").item(0).clientWidth;
			windowHeight = document.getElementsByTagName("html").item(0).clientHeight;
			windowWidth = (windowWidth == 0) ? document.body.clientWidth : windowWidth;
			windowHeight = (windowHeight == 0) ? document.body.clientHeight : windowHeight;
		}
		var pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;
		var pageWidth = (xScroll < windowWidth) ? windowWidth : xScroll;
		return new Array(pageWidth, pageHeight);
	}
	
	function closeSlide(){
		cur = 0;
		document.getElementById('semi-tr-bg-black').style.display = 'none';
		document.getElementById('slide').style.display = 'none';
	}
	
	function showSlide(id){
		var pgSize = getPageSize();
		var trBg = document.getElementById('semi-tr-bg-black');
		trBg.style.display = 'block';
		trBg.style.width = pgSize[0] + 'px';
		trBg.style.height = pgSize[1] + 'px';
		document.getElementById('slide').style.display = 'block';
		requestXml(catPath + '?catid=' + id + '&cur=' + cur);
	}
	
	function retrieveContent(){
		var response = null;

		if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200 || xmlhttp.status == 302){ // HTTP 302 - Found; in case of Apache response on URL rewrite;
				if(xmlhttp.getResponseHeader("Content-Type") == 'text/xml'){
					response = xmlhttp.responseXML;
				}else{
					alert('Invalid response data!');
					return false;
				}
				var result = '';
				//for (var property in response.childNodes[0]) {
				//	result += property + ': ' /*+ response[property] + '\r\n'*/;
				//}
				//alert(result);
				//alert(response.childNodes[0].textContent + ' ' + response.childNodes[0].tagName);
				//alert(response.getElementsByTagName('images').length);
				var xmlImages = response.getElementsByTagName('images')[0].getElementsByTagName('image');
				var src = '';
				var alt = '';
				var id = 0;
				if(xmlImages.length == 1){
					last = true;
				}
				images = [];
				for(var c = 0; c < xmlImages.length; c++){
					//alert(xmlImages[c].getElementsByTagName('id').length);
					id = xmlImages[c].getElementsByTagName('id')[0].childNodes[0].nodeValue;
					src = xmlImages[c].getElementsByTagName('src')[0].childNodes[0].nodeValue;
					alt = xmlImages[c].getElementsByTagName('alt')[0].childNodes[0].nodeValue;
					images[c] = new Array(id, src, alt);
				}
				
				var xmlTransl = response.getElementsByTagName('translations')[0];
				previous = xmlTransl.getElementsByTagName('previous')[0].childNodes[0].nodeValue;
				next = xmlTransl.getElementsByTagName('next')[0].childNodes[0].nodeValue;
				
				//cur = 0;
				changeContent(0);
			}
		}
	}
	
	function emptyEvent(){
		return false;
	}
	
	function requestXml(reqStr){
		xmlhttp.open("GET", reqStr);
		
		xmlhttp.onreadystatechange=retrieveContent;
		xmlhttp.send(null);
	}
	
	function changeContent(id){
		var imgHldr = document.getElementById('slide-content');
		var img = imgHldr.getElementsByTagName('img')[0];
		cur = id;
		if(cur<images.length){
			img.src = 'images/' + images[cur][1];
			img.alt = images[cur][2];
			//cur++;
		}
		if(cur > 0){
			first = false;
		}else{
			first = true;
		}
		if(cur < images.length - 1){
			last = false;
		}else{
			last = true;
		}
		// te ielikt formulu nobīdes aprēķināšanai.
		pStart = cur - 4; // floor(9/2) == 4;
		if(pStart < 0){
			pStart = 0;
		}
		document.getElementById('navigation').innerHTML = renderNavigation();
	}

	// quick and dirty rewrite from my own PHP pager!
	function renderNavigation(){
		var a = '<ul>';
		if(!first){
			a += '<li class="fl"><a href="#" onclick="changeContent(' + (cur-1) + '); return false;">' + previous + '</a> ..</li>';
		}
		var count = 9;
		if(images.length - pStart < count){
			count = images.length - pStart;
		}else if(images.length < count){
			count = images.length;
		}

		for(var i = pStart; i<pStart+count; i++){
			cssClass = '';
			if(i == cur){
				cssClass = ' class="active"';
			}
			if((cur == pStart + count-1) && (i == pStart + count-1) ){
			  adds = '';
			}else{
			  adds = '..';
			}
			a += '<li' + cssClass + '><a href="#" onclick="changeContent(' + i + '); return false;">' + (i+1) + '</a> ' + adds + '</li>';
		}
		if(!last){
			a += '						<li class="fl"><a href="#" onclick="changeContent(' + (cur+1) + '); return false;">' + next + '</a></li>';
		}
		a += '					</ul>';
		return a;
	}
