var curgal=''
var curimg=0
var sl

function createmask()
{
	var newmask=document.createElement("div")
		
	newmask.setAttribute("id", "backgroundmask")
	newmask.setAttribute("class", "backgroundmask")
	
	document.body.insertBefore(newmask, document.body.firstChild)
	document.getElementById('backgroundmask').className='backgroundmask'	

}

function creategal(gn,num)
{
	curgal=gn
	curimg=num
	//alert(curgal+'\n'+curimg)
	
	var newgalwrapper=document.createElement("div")

	newgalwrapper.setAttribute("id", "gallerywrapper")
	newgalwrapper.setAttribute("class", "gallerywrapper")

	newgalwrapper.innerHTML='<div id="galimgbox" class="galimgbox"></div>'
	newgalwrapper.innerHTML+='<div id="galbar" class="galbar">'
	newgalwrapper.innerHTML+='</div>'
	
	document.body.insertBefore(newgalwrapper, document.body.firstChild)
	document.getElementById('gallerywrapper').className='gallerywrapper'

	document.getElementById('gallerywrapper').style.width=document.body.clientWidth

	var imghtml=''
	var barhtml=''
	
	imghtml+='<img src="" id="galimg" onload="centerimg();document.getElementById(\'galimgbox\').style.visibility=\'visible\';">'



	barhtml+='<div style="display:table;width:900px;background-color:transparent;">'
	
	barhtml+='<div id="caption" class="baritem" style="width:390px;text-align:left;">&nbsp;</div>'
	
	barhtml+='<div id="barofwrapper" class="baritem" style="width:140px;">'

	barhtml+='<div class="baritemarrow" id="bararrow"><a href="javascript:showimg(2,-1)"><img src="/images/imggal/arrowleft.png" style="border:0px;"></a></div>'
	barhtml+='<div id="barof" style="position:relative;bottom:0px;float:left;margin:0px;padding:0px;width:70px;text-align:center;">'+num+' / '+eval(curgal)+'</div>'
	barhtml+='<div class="baritemarrow"><a href="javascript:showimg(2,1)"><img src="/images/imggal/arrowright.png" style="border:0px;"></a></div>'

	barhtml+='</div>'
	
	barhtml+='<div id="closebutton" class="baritem" style="text-align:right;width:310px;"><a href="javascript:closegal()"><img src="/images/imggal/close.png" style="border:0px;width:25px;position:relative;right:3px;"></a></div>'
	
	barhtml+='</div>'
	
	if(!((navigator.appName=='Microsoft Internet Explorer') && (navigator.appVersion.indexOf('MSIE 6.0')>0)))
	{
		barhtml+='<div id="galsliderwrapper" class="galsliderwrapper"><div id="galslider"><div id="galhandle"></div></div></div>'
	}

	createmask()
	document.getElementById('galimgbox').innerHTML=imghtml
	document.getElementById('galbar').innerHTML=barhtml

	var vals='['
	for(var x=0;x<eval(curgal);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('galhandle','galslider',{axis:'horizontal',alignX: 0,alignY: 0,minimum:0,maximum:eval(curgal),range: $R(1,(eval(curgal))),values:eval(vals),sliderValue: 0,increment:5,onSlide:function(value){;showimg(1,value)},onChange:function(value){showimg(1,value)}});
	}

	
	showimg(1,curimg)

}

function showimg(switchto,num)
{
	//alert(curgal+'\n'+num)
	/*
	switchto - 
		1= absolute
		2= relative
	num- exact number (absolute) or relative direction to move curimg
	*/
	document.getElementById('galimgbox').style.visibility='hidden'
	if(switchto==1)
	{
		curimg=num
	}
	else if(switchto==2)
	{
		curimg+=num
	}
	
	//this if statement handles wrapping
	if(curimg < 1)
	{
		curimg=eval(curgal)
	}
	else if(curimg>(eval(curgal)))
	{
		curimg=1
	}

	document.getElementById('barof').innerHTML=(curimg)+' / '+eval(curgal)
	document.getElementById('galimg').src='/images/'+curgal+curimg+'.jpg'
	//document.getElementById('galimg').style.visibility='visible'
}

function centerimg()
{

	if((navigator.appName=='Microsoft Internet Explorer') && (navigator.appVersion.indexOf('MSIE 6.0')>0))
	{
		//ie6

		//alert(document.documentElement.scrollTop)
		document.getElementById('galimgbox').style.position='absolute'
		document.getElementById('galimgbox').style.top=document.documentElement.scrollTop+120+'px'
		document.getElementById('galimgbox').style.left=(document.body.clientWidth/2)-(document.getElementById('galimgbox').clientWidth/2)+'px'

		document.getElementById('galbar').style.position='absolute'
		document.getElementById('galbar').style.top=document.documentElement.scrollTop+20+'px'
//		document.getElementById('galbar').style.left=(document.body.clientWidth/2)-(document.getElementById('galbar').clientWidth/2)+'px'

		document.getElementById('closebutton').style.width='300px'
	}
	else if(navigator.appName=='Microsoft Internet Explorer')
	{
		document.getElementById('galimgbox').style.width=document.getElementById('galimg').clientWidth+'px'
		document.getElementById('galimgbox').style.left="50%"
		document.getElementById('galimgbox').style.marginLeft='-'+(document.getElementById('galimgbox').clientWidth/2)+'px'
		//document.getElementById('galimgbox').style.left=(document.body.clientWidth/2)-(document.getElementById('galimgbox').clientWidth/2)+'px'
		/*document.getElementById('galimgbox').style.top=((document.body.clientHeight-50)/2)-(document.getElementById('galimgbox').clientHeight/2)+'px'*/
	}
	else
	{
		document.getElementById('galimgbox').style.width=document.getElementById('galimg').style.width
		document.getElementById('galimgbox').style.left="50%"
		document.getElementById('galimgbox').style.marginLeft='-'+(document.getElementById('galimgbox').clientWidth/2)+'px'

		//document.getElementById('galimgbox').style.left=(document.body.clientWidth/2)-(document.getElementById('galimgbox').clientWidth/2)+'px'
		/*document.getElementById('galimgbox').style.top=((window.innerHeight-50)/2)-(document.getElementById('galimgbox').clientHeight/2)+'px'*/
	}
	
}

function closegal()
{
	var childnode=document.getElementById("gallerywrapper")
	var removednode=document.body.removeChild(childnode)

	childnode=document.getElementById("backgroundmask")
	removednode=document.body.removeChild(childnode)
}