/*
OVERVIEW 3/25/09
Mark Faulk
Image galleries are defined in the database. 

3 global js vars are created - xgallery will be a 2D Array that will hold the contents of the gallery. curimg remembers the index # in xgallery of which image is currently being displayed. And sl is the slider control.

The grabgallery function receives a gallery id. It then AJAX's to grabgallery.asp to retrieve the contents of the gallery. The data returned from grabgallery is stored in the javascript array xgallery. xgallery is a 2D-array where index 0 is the image caption and index 1 is the path to the image. Control passes to update gallery then the data is returned.

updategallery sets xgallery to have a value of the 2D javascript array returned from grabgallery. It then calls showbgeagal

showbgeagal sets curimg (global js var), which holds the current image being displayed in this gallery, to 0. It then creates several divs:
bgeagallerywrapper - a fixed-position wrapper spanning the width and height of the document window.
	inside bgeagallerywrapper are two divs:
	bgeagalimgbox - simple box that holds the image
	bgeagalbar - bar that displays the caption, forward/back controls, close button, and slider control
	
bgeagallerywrapper is inserted into the DOM before the body's firstChild.

Then it checks to see if the browser is not ie6. If so, it will create a background mask that grays out the background. This is then inserted into the DOM, and is z-indexed to be below the bgeagallerywrapper div.

Next, it creates the innerHTML for the bgeagalimgbox and bgeagalbar

Last, it creates the slider control and calls the showimg function, which loads/changes images

showimg receives 2 vars, goto and direction. goto is a boolean, and if true it was called from the showbgeagal function or the slider control. It initializes the curimg var to 0 (the first index in xgallery) if called from showbgeagal, or the value in the slider control if called from the slider control. The direction var is used when showimg is called from the left/right arrows to ++ or -- the curimg var or to set curimg directly to a slider control value.

showimg then updates the caption and the x/y (x of y) and calls the function that centers the image.
*/

var xgallery=new Array()
var curimg
var sl
var galleryhtml=''
var galpostfn=null

function grabgallery(idx,fn)
{
	//calls getfiles.asp to retrieve filelist for creating a new gallery
	createRequest()
	
	var url='/includes/popupimagegallery/grabgallery.asp'
	var sel=''
	sel='idx='+escape(idx)

	galpostfn=fn
	//alert(sel)
	request.open("POST",url,true)

	request.onreadystatechange = function (){updateGallery()}
	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
	request.send(sel)
}

function updateGallery()
{
	//alert('frmname='+frmname+'\nrequest.readyState='+request.readyState+'\nrequest.responseText='+request.responseText)
	
	if(request.readyState==4)
	{
		var resp=request.responseText
		//document.getElementById('results').innerHTML=resp
		//alert(resp.replace(/\]/g,']\n'))
		if(resp=='')
		{
			alert('An error has occurred.')
		}
		else
		{
			resp=unescape(resp)
		}
		//alert(resp)
		if (request.status==200)
		{
			if(resp.indexOf('Error:')>-1)
			{
				alert(resp)
			}
			else
			{
				resp=unescape(resp)
				xgallery=eval(resp)
				//showbgeagal(xgallery)
				if(gebid('bgeagalcontainer'))
				{
					closebgeagal()	
				}
				getgalhtml()

			}
			//document.getElementById('files').innerHTML=resp.replace(/\+/g,' ')
		}
		else
		{
			alert("An error has occurred. The server said: "+request.status)
			//document.getElementById('files').innerHTML=resp.replace(/\+/g,' ') //'<span style="color:#FF0000">An error occurred</span>'
		}
	}
	//document.getElementById(ETFform+'linkbutton').innerHTML=rememberlinkbtn
}

function getgalhtml()
{
	//calls getfiles.asp to retrieve filelist for creating a new gallery
	createRequest()
	
	var url='/includes/popupimagegallery/galleryhtml.asp'
	var sel=''
	//alert(sel)
	request.open("POST",url,true)

	request.onreadystatechange = xshow
	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
	request.send(sel)
}

function xshow()
{
	if(request.readyState==4)
	{
		var resp=request.responseText
		//alert(resp)
		if(resp=='')
		{
			alert('An error has occurred.')
		}
		else
		{
			resp=unescape(resp)
		}
		//alert(resp)
		if (request.status==200)
		{
			galleryhtml=resp
			showbgeagal(xgallery)
		}
		else
		{
			alert("An error has occurred. The server said: "+request.status)
			//document.getElementById('files').innerHTML=resp.replace(/\+/g,' ') //'<span style="color:#FF0000">An error occurred</span>'
		}
	}
}


function showbgeagal(xgallery)
{
	curimg=0
	//alert(xgallery.length+'\n'+xgallery[1].length+'\n'+xgallery[1])
	
	var xdiv=document.createElement('div')
	xdiv.setAttribute("id", "bgeagalcontainer")
	xdiv.setAttribute("class", "bgeagalcontainer")
		
	document.body.insertBefore(xdiv, document.body.firstChild)
	gebid('bgeagalcontainer').className='bgeagalcontainer'
	
	xdiv.innerHTML=galleryhtml
	
	createmask()

	//gebid('bgeagalcontainer').style.width=

	var imghtml=''
	var barhtml=''
	
	imghtml+='<img src="/assets/'+unescape(xgallery[curimg][1])+'" id="bgeagalimg" onload="posgalimg()">\n'

	gebid('bgeagalimgbox').innerHTML=imghtml


	gebid('bgeagalcaption').innerHTML=unescape(xgallery[curimg][0])
	
	

/*	barhtml+='<div id="barofwrapper" style="width:200px;">\n'
	barhtml+='<div class="baritemarrow" id="bararrow"><a href="javascript:showimg(false,-1)"><img src="/assets/images/homepage/LeftArrowButtonOff.gif" style="border:0px;"></a></div>\n'
	barhtml+='<div id="barof" style="position:relative;bottom:0px;float:left;margin:0px;padding:0px;width:70px;text-align:center;">'+(curimg+1)+' / '+xgallery.length+'</div>\n'
	barhtml+='<div class="baritemarrow"><a href="javascript:showimg(false,1)"><img src="/assets/images/homepage/RightArrowButtonOff.gif" style="border:0px;"></a></div>\n'
	barhtml+='</div>\n'*/
	
	gebid('barof').innerHTML=(curimg+1)+' / '+xgallery.length
	
	
/*
	var vals='['
	for(var x=0;x<xgallery.length;x++)
	{
		vals+=x+', '
	}
	vals=vals.substr(0,vals.length-2)
	vals+=']'
	//alert(vals)
	if(!((navigator.appName=='Microsoft Internet Explorer') && (navigator.appVersion.indexOf('MSIE 6.0')>0)))
	{
		//alert($R(0,xgallery.length)+'\n ' + xgallery.length+'\n '+vals)
		sl = new Control.Slider('bgeagalhandle','bgeagalslider',{axis:'horizontal',alignX: 0,alignY: 0,minimum:0,maximum:xgallery.length,range: $R(0,(xgallery.length-1)),values:eval(vals),sliderValue: 0,increment:5,onSlide:function(value){;showimg(true,value)},onChange:function(value){showimg(true,value)}});
	}
*/

	/*if(isie())
	{
		alert('yay')
		window.attachEvent('onkeydown',function(){alert('test')})
	}*/

	var igevt=function(evt){checkclosegal(evt)}
	/*var igevt=function(evt){
		alert(evt.keyCode)
	}*/
	
	if(!isie())
	{
		
		window.addEventListener('keydown',igevt, false)
		//window.addEventListener('keypress',igevt, false)
	}
	else
	{
		
		document.body.attachEvent('onkeydown',igevt)
	}
	
	if(galpostfn!=null)
	{
		eval(galpostfn)
	}
	showimg(true,0)
}

var igevt=null

function checkclosegal(evt)
{
	
	if(evt.keyCode==27)
	{
		closebgeagal()
		if(!isie())
		{
			window.removeEventListener('keydown',igevt, false)
		}
		else
		{
			document.body.detachEvent('onkeydown',igevt)
		}
	}

}

function showimg(goto,direction)
{
	//alert(goto+' dir='+direction)

	if(goto==true)
	{
		curimg=direction
	}
	else
	{
		curimg+=direction
		if(curimg < 0)
		{
			curimg=xgallery.length-1
		}
		else if(curimg>(xgallery.length-1))
		{
			curimg=0
		}
/*		if(!((navigator.appName=='Microsoft Internet Explorer') && (navigator.appVersion.indexOf('MSIE 6.0')>0)))
		{
			sl.setValue(curimg)
		}*/
	}

	gebid('bgeagalimgbox').style.visibility='hidden'

	var imgsrc=unescape(xgallery[curimg][1])
	if(imgsrc.indexOf('/assets')==-1)
	{
		imgsrc='/assets'+imgsrc
	}
	gebid('bgeagalimg').src=imgsrc
	
	gebid('bgeagalcaption').innerHTML=unescape(xgallery[curimg][0])
	//gebid('barof').innerHTML=''
	gebid('barof').innerHTML=(curimg+1)+' / '+xgallery.length


	//centerelement('bgeagalwrapper')
}

function posgalimg()
{


	
	//check to see if the image is too tall
	if(isie())
	{
		wih=document.documentElement.clientHeight 
	}
	else
	{
		wih=window.innerHeight
	}
	
	gebid('bgeagalimg').style.height=''
	//alert(gebid('bgeagalwrapper').clientHeight+'\n'+wih)
	
	if(gebid('bgeagalwrapper').clientHeight > wih)
	{
		if(isie())
		{
			//alert('yay')
			gebid('bgeagalimg').style.height=wih-400+'px'
		}
		else
		{
			gebid('bgeagalimg').style.height=wih-(parseInt(getstyle('bgeagalwrapper','padding-top'))+parseInt(getstyle('bgeagalwrapper','padding-bottom'))+parseInt(gebid('bgeagalheaderimg').clientHeight)+parseInt(gebid('bgeagalcaption').clientHeight)+50)+'px'
		}
	}
	else
	{
		gebid('bgeagalimg').style.height=''
	}



	if(isie())
	{
		//gebid('galww').style.width=gebid('bgeagalimg').clientWidth+'px'
		//gebid('bgeagalnavwrapper').style.width=gebid('galww').style.width
		/*gebid('galww').style.width=''*/
		
	}
	
	
	//adjust for close btn
	gebid('bgeagalwrapper').style.width=gebid('galww').clientWidth+gebid('bgeagalclosebtn').clientWidth+'px'
	
	centerelement('bgeagalwrapper');
	
	
	gebid('bgeagalimgbox').style.visibility='visible';
	if(getstyle('bgeagalcontainer','visibility')=='hidden')
	{
		gebid('bgeagalcontainer').style.visibility='visible'
	}
}

function closebgeagal()
{
	/*var childnode
	var removednode*/
	if(gebid('bgeagalcontainer'))
	{
		removebox('bgeagalcontainer')
		/*childnode=gebid('bgeagalcontainer')
		removednode=document.body.removeChild(childnode)*/
	}
	
	if(gebid('bgeapagemask'))
	{
		removebox('bgeapagemask')
		/*childnode=gebid('bgeapagemask')
		removednode=document.body.removeChild(childnode)*/
	}

}
