var ClickCount=1;
var tpczb=10;
var border_width = 1;
var PictureShow=5;
var cell_spacing =10;
var scale_width =400;
var scale_hight =300;
var PictureHeight;
//////////////////////////////////////////////////////////////////////////////////////
var loadingImage = '../../img/loading.gif';
var closeButton = '../../img/close.gif';
var PictureWidth="" ;
var SmallPicture = new Array();
var BigPictur = new Array();
var title = new Array();
var PictureID = "" ;
var behind="";
var UserDivID="";
var Picture_default = new Array();
var left_ButtonPath ="";
var right_ButtonPath ="";
var borderCss="listimage";
var borderCssNone="listimage_none";
////////////////////////////////////////////////////////////////////////////////////////////////////


function PictureShowView(path)
{	
	View(BigPictur[(path-1)],scale_width,scale_hight);
	document.getElementById(behind).title=title[(path-1)];
}


function PictureSelectStyle(id)
{   
//	for(var i=1;i<=SmallPicture.length;i++)
//	{
//		if(i!=id)
//		{
//         	 document.getElementById("d"+i).className=borderCssNone;
//		}else
//		{
//			document.getElementById("d"+i).className=borderCss;
//		}
//	}
}

function right()  
{
	var old = ClickCount;
	if(ClickCount<SmallPicture.length)
	{
		ClickCount++;
	}
	move(ClickCount);
	PictureSelectStyle(ClickCount);
	PictureShowView(ClickCount);
	if (document.getElementById('thumbnail_' + old) != null)
	{
		document.getElementById("thumbnail_" + old).style.MozOpacity=0.5;
		document.getElementById('thumbnail_' + ClickCount).style.MozOpacity=1;
		document.getElementById("thumbnail_" + old).filters.alpha.opacity=50;
		document.getElementById('thumbnail_' + ClickCount).filters.alpha.opacity=100;
	}
		
}


function left()
{	
	var old=ClickCount;
	if(ClickCount!=1)
	{
		ClickCount--;
	}
	move(ClickCount);
	PictureSelectStyle(ClickCount);
	PictureShowView(ClickCount);
	if (document.getElementById('thumbnail_' + old) != null)
	{
		document.getElementById("thumbnail_" + old).style.MozOpacity=0.5;
		document.getElementById('thumbnail_' + ClickCount).style.MozOpacity=1;
		document.getElementById("thumbnail_" + old).filters.alpha.opacity=50;
		document.getElementById('thumbnail_' + ClickCount).filters.alpha.opacity=100;
	}
}

function move(position)
{
	if (position<PictureShow/2+1)
	{
		document.getElementById('div_center').style.left=(parseInt(tpczb)-3)+'px';
	}
	else if (position>SmallPicture.length-(PictureShow/2)+1)
	{
		if(PictureShow<SmallPicture.length)
		{
			document.getElementById('div_center').style.left=parseInt(parseInt(tpczb)-(SmallPicture.length-PictureShow) * (parseInt(cell_spacing)+parseInt(PictureWidth)))-3+'px';
		}
	}
	else 
	{
		
		if(PictureShow%2==0 && ClickCount==parseInt(SmallPicture.length)-(PictureShow/2)+1)
		{
			document.getElementById('div_center').style.left=parseInt(parseInt(tpczb)-(SmallPicture.length-PictureShow) * (parseInt(cell_spacing)+parseInt(PictureWidth)))-3+'px';
		}else
		{
			document.getElementById('div_center').style.left=parseInt((parseInt(tpczb)-(position - (PictureShow-1)/2-1) *(parseInt(cell_spacing)+parseInt(PictureWidth))))-3+'px';
		}
	}
}
function changeFocus(position)
{
	var old=ClickCount;	
	ClickCount=position;
	move(ClickCount);
	PictureSelectStyle(ClickCount);
	PictureShowView(ClickCount);
	if (document.getElementById('thumbnail_' + old) != null)
	{
		document.getElementById("thumbnail_" + old).style.MozOpacity=0.5;
		document.getElementById('thumbnail_' + position).style.MozOpacity=1;
		document.getElementById("thumbnail_" + old).filters.alpha.opacity=50;
		document.getElementById('thumbnail_' + position).filters.alpha.opacity=100;
	}
}
function main()
{
	PictureHeight = ((parseInt(PictureWidth)*3)/4);
	var ScaleHeight = ((parseInt(PictureWidth)*3)/4)+(parseInt(border_width)*2);
	var HiddenDivWidth = (parseInt(PictureWidth)+parseInt(cell_spacing))*parseInt(PictureShow)+(parseInt(border_width)*2);
	var count_PictureWidth = parseInt(PictureWidth)*parseInt(SmallPicture.length)+(parseInt(SmallPicture.length)-1)*parseInt(cell_spacing)+(parseInt(border_width)*2);
	var html;
	html = "<div  id='div_left' class=\"divcss_left\" style=\"height:"+ScaleHeight+"px;\">";
	html+="<a href='#' onclick='left();return false;'><img  src='"+left_ButtonPath+"' border='0'></a></div>";
	html+="<div id='div_outer' class=\"divcss_outer\" style=\"top:-"+ScaleHeight+"px; width:"+HiddenDivWidth+"px; height:"+ScaleHeight+"px; \">";
	html+="<div id='div_center' class=\"divcss_center\" style=\"width:"+count_PictureWidth+"px; height:"+ScaleHeight+"px;\">";	
	html+="</div></div>";
	html += "<div id='div_right' class=\"divcss_right\" style=\"height:"+ScaleHeight+"px;top:-"+parseInt(ScaleHeight*2)+"px;\">";
	html+="<a href='#' onclick='right();return false;'><img src='"+right_ButtonPath+"'></a></div>";
	document.getElementById(UserDivID).innerHTML=html;
	var PictureCount = SmallPicture.length;
	var val;
	val="<table border='0px' cellpadding='0' class=\"TableCss\" cellspacing=\"0\"><tr>";
	for(var i=0;i<PictureCount;i++)
	{
		val+="<td id = 'd"+(i+1)+"'  height ='"+PictureHeight+"px' class=\"TableCss_td\" style=\"width:"+PictureWidth+"px;\"><a href=\"#\" onclick=\"changeFocus("+ (i+1) + ");return false;\" class=\"TableCss_td_a\"><img  class=\"TableCss_td_a_img\" style=\"width:"+PictureWidth+"px;height:"+PictureHeight+"px;\" id='thumbnail_" + (i+1) + "' src='"+SmallPicture[i]+"' border=\"0\"/></a></td>";
		if (i!=PictureCount-1)
		{
		val+="<td style=\"width:"+cell_spacing+"px;\"></td>";
		}
	}
	val+="</tr></table>";
	document.getElementById('div_center').innerHTML=val;
	document.getElementById('div_right').style.left = (parseInt(cell_spacing)+parseInt(PictureWidth))*parseInt(PictureShow)+(30+parseInt(cell_spacing))+parseInt(border_width*2)+'px';
	var default_m =1;
	
	for(var i=0;i<Picture_default.length;i++)
	{
	    if(Picture_default[i] == "True")
	    {
	        default_m = (i+1);
	    }
	}
	
changeFocus(default_m);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
image111=new Image();

function View(op,lwidth,lheight)
{	
	document.getElementById(behind).href=op;
		if (window.navigator.userAgent.indexOf("MSIE")>=1 ||window.ActiveXObject)//IE
		{
			image111.src=loadingImage;
			image111.onLoad = AlterDisplay(op,lwidth,lheight);
		}else
		{
			image111.onLoad = AlterDisplay(op,lwidth,lheight);
		}

}
function AlterDisplay(op,lwidth,lheight)
{   
	if (image111.complete)
	{
		image111=new Image();
		image111.src=op;
		addEffect();
		image111.onLoad = ViewAsync(lwidth,lheight);
	}
	else
	{
		setTimeout("AlterDisplay('"+op+"',"+lwidth+","+lheight+")",10);
	}
}
function ViewAsync(lwidth,lheight)
{   
	if (image111.complete)
	{
		 var pwidth=image111.width;
 var pheight=image111.height;
 document.getElementById(PictureID).src=image111.src;
 var m,n;
 if(pwidth>lwidth && pheight>lheight)
 {
   n=lheight;
 
    m=pwidth*n/pheight;
	if(m>lwidth)  
	  {
	  	m=lwidth;
	    n=pheight*m/pwidth;
	  }
 }
   else if(pwidth<lwidth && pheight<lheight)
   {
	   m=pwidth;
	   n=pheight;
   }
   else if(pwidth>=lwidth && pheight<lheight)
   {
      m=lwidth;
	  n=m*pheight/pwidth;	 
   }
   else if(pwidth<=lwidth && pheight>lheight)
   {
     n=lheight;
	 m=n*pwidth/pheight;
	}
	else if(pwidth >lwidth && pheight==lheight)
	{
		m=lwidth;
		n=m*pheight/pwidth;
	}else if(pwidth <lwidth && pheight==lheight)
	{
		n=lheight;
		m=n*pwidth/pheight;
	}else if(pwidth==lwidth && pheight==lheight)
	{
		m=pwidth;
		n=pheight;
	}
	 document.getElementById(PictureID).width=m;
	 document.getElementById(PictureID).height=n;

	}
	else
	{
		setTimeout("ViewAsync("+lwidth+","+lheight+")",10);
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}


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){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	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.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}
function getKey(e){
	if (e == null) { 
		keycode = event.keyCode;
	} else {
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();
	
	if(key == 'x'){ hideLightbox(); }
}
function listenKey () {	document.onkeypress = getKey; }

function showLightbox(objLink)
{
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var objCaption = document.getElementById('lightboxCaption');
	var objImage = document.getElementById('lightboxImage');
	var objLoadingImage = document.getElementById('loadingImage');
	var objLightboxDetails = document.getElementById('lightboxDetails');

	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	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';
	}
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	imgPreload = new Image();

	imgPreload.onload=function(){
		objImage.src = objLink.href;
		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';
		document.getElementById('lightbox').style.width=objLightboxDetails.style.width;
		
		if(objLink.getAttribute('title')){
			objCaption.style.display = 'block';
			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;
	}

	imgPreload.src = objLink.href;
	
}


function hideLightbox()
{
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';

	// make select boxes visible
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}

	// disable keypress listener
	document.onkeypress = '';
}

function initLightbox()
{
	
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");

	// loop through all anchor tags
	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);
	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	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();

	// preload and create loader image
	var imgPreloader = new Image();
	
	// if loader image found, create link to hide lightbox and create loadingimage
	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(){};	//	clear onLoad, as IE will flip out w/animated gifs

		return false;
	}

	imgPreloader.src = loadingImage;

	// create lightbox div, same note about styles as above
	var objLightbox = document.createElement("div");
	objLightbox.setAttribute('id','lightbox');
	objLightbox.style.display = 'none';
	objLightbox.style.position = 'absolute';
	objLightbox.style.zIndex = '100';	
	 objLightbox.style.width = imgPreloader.width;

	objBody.insertBefore(objLightbox, objOverlay.nextSibling);
	
	// create link
	var objLink = document.createElement("a");
	objLink.setAttribute('href','#');
	objLink.setAttribute('title','Click to close');
	objLink.onclick = function () {hideLightbox(); return false;}
	objLightbox.appendChild(objLink);

	// preload and create close button image
	var imgPreloadCloseButton = new Image();

	// if close button image found, 
	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;

	// create image
	var objImage = document.createElement("img");
	objImage.setAttribute('id','lightboxImage');
	objLink.appendChild(objImage);
	
	// create details div, a container for the caption and keyboard message
	var objLightboxDetails = document.createElement("div");
	objLightboxDetails.setAttribute('id','lightboxDetails');
	objLightbox.appendChild(objLightboxDetails);

	// create caption
	var objCaption = document.createElement("div");
	objCaption.setAttribute('id','lightboxCaption');
	objCaption.style.display = 'none';
	objLightboxDetails.appendChild(objCaption);

	// create keyboard message
	var objKeyboardMsg = document.createElement("div");
	objKeyboardMsg.setAttribute('id','keyboardMsg');
	objKeyboardMsg.innerHTML = 'Press <a href="#" onclick="hideLightbox(); return false;" class="ql"><kbd>X </kbd></a>to Close';
	objLightboxDetails.appendChild(objKeyboardMsg);


}


function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}

addLoadEvent(initLightbox);	

function addEffect()
{	
	//alert('in');
	if (window.navigator.userAgent.indexOf("MSIE")>=1 ||window.ActiveXObject)
	{
//	alert('is IE');
		var imageInstance = document.getElementById(PictureID);
		//imageInstance.style.filter="progid:DXImageTransform.Microsoft.RevealTrans(Duration=1.0, Transition=23);";
		imageInstance.filters.item(0).apply();
		imageInstance.filters.item(0).transition = 23;
		imageInstance.filters(0).play(1);
	}
}