
var imgArray = new Array();
var imgI = 0;
var prevButtonTxt = 'prev';
var nextButtonTxt = 'next';
var closeButtonTxt = 'close';
listId = 'galleryItemsList';


var overlayObj;
var galleryObj,galleryPObj;
var imgObj;
var prevObj, nextObj;
var windowWidth = 0, windowHeight = 0; //window dimension
var scrOfX = 0, scrOfY = 0; 
var pageWidth = 0, pageHeight=0;


function showImg(itemNo) {
	imgI = itemNo;
	imgArray = document.getElementById(listId).innerHTML.split('|');
	
	
		imgPreloader = new Image();
		// once image is preloaded, resize image container
		imgPreloader.onload=function(){
			imgObj.src = imgArray[imgI];
			imgObj.style.width = imgPreloader.width;
			imgObj.style.height = imgPreloader.height;
			galleryObj.style.width = imgPreloader.width;
			galleryObj.style.height = imgPreloader.height;
		}
		imgPreloader.src = imgArray[imgI];
		
		
	getScroll();
	getPageSize();	
	
	galleryPObj.style.top = (scrOfY + 40) + 'px';
	imgObj.src = imgArray[itemNo];
	
	overlayObj.style.visibility = '';
	overlayObj.style.width=pageWidth + 'px';
	overlayObj.style.height=pageHeight + 'px';
	galleryPObj.style.width=pageWidth + 'px';
	galleryPObj.style.display='';
	
	if (itemNo>0 ) {
		//preload prev
		preloadImg(itemNo - 1);
		//show button
		prevObj.style.display = '';
	}
	else {
		//hide button
		prevObj.style.display = 'none';
	}
	if (itemNo<imgArray.length - 1){
		//preload next
		preloadImg(itemNo + 1);
		//show button
		nextObj.style.display = '';
	}
	else {
		//hide button
		nextObj.style.display = 'none';
	}
	
	
}

function preloadImg(itemNo){
if (document.images) {
    var img1 = new Image();
    img1.src = imgArray[itemNo];
}}


function hideGallery(){
	overlayObj.style.visibility = 'hidden';
	galleryPObj.style.display='none';
}
function makeOverlay() {

}
window.onload = function(){
		var objBody = document.getElementsByTagName('body').item(0);
		
		
		//hide-rest div
		var objOverlay = document.createElement('div');
		objOverlay.setAttribute('id','overlay');
		objOverlay.onclick = function() { hideGallery(); return false; }
		objOverlay.style.visibility = 'hidden';
		objBody.appendChild(objOverlay);
		overlayObj = document.getElementById('overlay'); //global 
		
		
		//gallery parent div
		galleryPObj = document.createElement('div');
		galleryPObj.setAttribute('id','ipgallery');
		galleryPObj.style.display = 'none';
		objBody.appendChild(galleryPObj);
		
		//gallery div
		galleryObj = document.createElement('div');
		galleryObj.setAttribute('id','igallery');
		//galleryObj.style.display = 'none';
		galleryPObj.appendChild(galleryObj);
		
		
		
		//img
		imgObj = document.createElement('img');
		imgObj.setAttribute('id','igalleryImg');
		imgObj.setAttribute('src','galeria/blank.gif');
		galleryObj.appendChild(imgObj);
		
		//prev link
		prevObj = document.createElement('div');
		prevObj.setAttribute('id','prevButton');
		prevObj.innerHTML = '<span>' + prevButtonTxt + '</span>';
		prevObj.onclick = function () {showImg(imgI-1); return false;};
		
		galleryObj.appendChild(prevObj);
		
		//next link
		nextObj = document.createElement('div');
		nextObj.setAttribute('id','nextButton');
		nextObj.onclick = function () {showImg(imgI+1); return false;};
		nextObj.innerHTML = '<span>' + nextButtonTxt + '</span>';
		galleryObj.appendChild(nextObj);
		
		
		//close link
		closeObj = document.createElement('div');
		closeObj.setAttribute('id','closeButton');
		closeObj.onclick = function () {hideGallery(); return false;};
		closeObj.innerHTML = '<span>' + closeButtonTxt + '</span>';
		galleryObj.appendChild(closeObj);
		

}

function getWindowSize(){
  //size
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    windowWidth = window.innerWidth;
    windowHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }

}
function getScroll() {
   //scroll
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }

}

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 { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	// 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;
	}
}


