/* Javascript Photo Gallery
		Version: 1.0

		Check the subversion log for changes
		Please update version number when making changes
		
		Make sure to create a global variable called JSGallerySettings to initiate config
		ie:
		var JSGallerySettings = JSGalleryConfig(3000,'defaultGalleryName');
			parameters = (slideshowSpeed in milliseconds, default gallery name as string)
 */


var JSGallerySettings = new JSGalleryConfig(3000,'');
// speed measured in milliseconds  3000 = 3 seconds

var JSGalleryPaginate = null;
var JSGalleryGalleries = new Array();
var JSGalleryPhotolist = new Array();
var JSGalleryTimer = null;
var JSGalleryCurrentGallery = null;
var JSGalleryCurrentPhoto = 0;


function JSGalleryConfig(ssspeed,defaultGallerystr)
{
	this.slideshowSpeed = ssspeed;
	this.defaultGallery = defaultGallerystr;
}

function JSGalleryGallery(namestr,idstr,photos,image,defaultGallery) {
    this.galleryname = namestr;
    this.galleryid = idstr;
    this.photos = photos;
    this.image = image;
    this.defaultGallery = defaultGallery;
}

function JSGalleryPhoto(pathstr,captionstr,altstr,thumbstr,titlestr,idstr) {
    this.fullpath = pathstr;
    this.thumbpath = thumbstr;
    this.alttext = altstr;
    this.caption = captionstr;
    this.titletext = titlestr;
    this.idnum = idstr;
}

function initJSGallery()
{
	getJSWidth()
  var loadAjax = ajaxRequester();

  if( loadAjax != null)
  {
		var strQueryString = 'index.cfm/event/loadgalleries'
		loadAjax.open("GET", strQueryString, true);
		loadAjax.onreadystatechange = function (aEvt) {
			if( loadAjax.readyState == 4)
			{ //The load is complete when readyState equals 4
				if( loadAjax.status != "200" &&  loadAjax.status != undefined)
				{
					var x = loadAjax.responseText;
					document.getElementById('gallerycont').innerHTML = x + "<br />" + strQueryString;
				} else {
          var xmlDoc; 
          if(window.ActiveXObject)
          {
              //Internet Explorer
              xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
              xmlDoc.async = false;
              xmlDoc.loadXML( loadAjax.responseText);
          }
          else if(document.implementation && document.implementation.createDocument)
          {
              //Mozilla
              xmlDoc =  loadAjax.responseXML;
          }
          
          var count = $(xmlDoc).find('Count').text()
					var galleryTags = xmlDoc.documentElement.getElementsByTagName('Gallery')
					for(var i = 0; i < galleryTags.length; i++)
					{
						var namestr = $(galleryTags[i]).find('Name').text()
						var idstr = $(galleryTags[i]).find('Id').text()
						var photostr = $(galleryTags[i]).find('Photos').text()
						var imgstr = $(galleryTags[i]).find('Image').text()
						isDefault = false
						if(JSGallerySettings.defaultGallery == namestr) isDefault = true
						JSGalleryGalleries[i] = new JSGalleryGallery(namestr,idstr,photostr,imgstr,isDefault)
					}

					for(var i = 0; i < JSGalleryGalleries.length; i++)
					{
						if(JSGalleryGalleries[i].name == true)
							JSGalleryCurrentGallery = i
						else
							JSGalleryCurrentGallery = 0
					}
					loadJSGallery(JSGalleryCurrentGallery)
				}
			}
		}
		loadAjax.send(null);
	}
}

function loadJSGallery(gallerynum)
{
	hideJSGalleries()
	pauseJSShow()
	var galleryid = JSGalleryGalleries[gallerynum].galleryid
  var loadAjax = ajaxRequester();

  if( loadAjax != null)
  {
		var strQueryString = 'index.cfm/event/loadgallery/galleryid/' + galleryid
		loadAjax.open("GET", strQueryString, true);
		loadAjax.onreadystatechange = function (aEvt) {
			if( loadAjax.readyState == 4)
			{ //The load is complete when readyState equals 4
				if( loadAjax.status != "200" &&  loadAjax.status != undefined)
				{
					var x = loadAjax.responseText;
					document.getElementById('gallerycont').innerHTML = x + "<br />" + strQueryString;
				} else {
          var xmlDoc; 
          if(window.ActiveXObject)
          {
              //Internet Explorer
              xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
              xmlDoc.async = false;
              xmlDoc.loadXML( loadAjax.responseText);
          }
          else if(document.implementation && document.implementation.createDocument)
          {
              //Mozilla
              xmlDoc =  loadAjax.responseXML;
          }
          var count = Number($(xmlDoc).find('Count').text())
					var photos = xmlDoc.documentElement.getElementsByTagName('Photo')

          var container = document.getElementById('photobox');
          container.innerHTML = ""
					
					JSGalleryPhotolist = new Array()
					
					for(var i = 0; i < count; i++)
					{
						var imglink = document.createElement('p')
						imglink.className ='imagepage'
						imglink.innerHTML = (i + 1)
						imglink.id = 'photolist_' + i
						imglink.onclick = new Function ("evt",'loadJSImage(' + i + ')')
						$(imglink).hoverIntent(showJSThumb,hideJSThumb)
						container.appendChild(imglink)
						
						var pathstr = photos[i].getAttribute("path")
						var captionstr = photos[i].getAttribute("caption")
						var altstr = photos[i].getAttribute("alttext")
						var thumbstr = photos[i].getAttribute("thumb")
						var titlestr = photos[i].getAttribute("title")
						var idstr = photos[i].getAttribute("id")
						JSGalleryPhotolist[i] = new JSGalleryPhoto(pathstr,captionstr,altstr,thumbstr,titlestr,idstr)
					}
					loadJSImage(0)
					JSslideshow()
				}
			}
		}
		loadAjax.send(null);
	}
}

function showJSGalleries()
{
	$('#gallerybox').empty()
	var gallerybg = document.createElement('div')
	gallerybg.className = 'gallerybg'
	document.getElementById('gallerybox').appendChild(gallerybg)
	
	var scrollbox = document.createElement('div')
	scrollbox.className = 'galleryScroll'
	document.getElementById('gallerybox').appendChild(scrollbox)
	
	for(var i = 0; i < JSGalleryGalleries.length; i++)
	{
		var gallerydiv = document.createElement('div')
		gallerydiv.className = 'gallery'
		gallerydiv.onmouseover = new Function ("evt",'this.className = "gallery-c"')
		gallerydiv.onmouseout = new Function ("evt",'this.className = "gallery"')
		gallerydiv.onclick = new Function ("evt",'loadJSGallery(' + i + ')')
		
		var thumb = document.createElement('img')
		var thumbsrc = JSGalleryGalleries[i].image
		if(thumbsrc == '')
		{
			thumbsrc = 'images/JSGallery/noImageThumb.jpg'
		}
		thumb.src = thumbsrc
		thumb.alt = JSGalleryGalleries[i].galleryname

		var galleryTitle = document.createElement('p')
		galleryTitle.innerHTML = JSGalleryGalleries[i].galleryname
		
		gallerydiv.appendChild(thumb)
		gallerydiv.appendChild(galleryTitle)
		scrollbox.appendChild(gallerydiv)

	}
	$('#gallerybox').fadeIn()
	$('#gallerybox div.gallerybg').fadeTo(0,.7)
	document.getElementById('galleryicon').onclick = new Function ("evt",'hideJSGalleries()')
}

function hideJSGalleries()
{
	$('#gallerybox').fadeOut()
	document.getElementById('galleryicon').onclick = new Function ("evt",'showJSGalleries()')
}

function loadJSImage(imagenum)
{
	if(imagenum > (JSGalleryPhotolist.length - 1)) return;
	if(imagenum < 0) return;
	$('#mainphoto').empty()
	var pathstr = JSGalleryPhotolist[imagenum].fullpath
	var mainimg = document.createElement('img')
	mainimg.src = pathstr
	mainimg.style.display = 'none'
	$(mainimg).hoverIntent(showJSCaption,hideJSCaption)
	$('#mainphoto').append(mainimg)
	$(mainimg).fadeIn()
	for(var i = 0; i < JSGalleryPhotolist.length; i++)
	{
		document.getElementById('photolist_' + i).className = 'imagepage'
		if(i == imagenum) document.getElementById('photolist_' + i).className = 'imagepage-c'
	}
	JSGalleryCurrentPhoto = imagenum
}

function showJSCaption()
{
	var captiondiv = document.getElementById('captionCont')
	captiondiv.innerHTML = ''
	
	var transdiv = document.createElement('div')
	transdiv.className = 'captionbg'
	
	var countp = document.createElement('p')
	countp.className = 'photoCaptionCount'
	countp.innerHTML = 'Image ' + (JSGalleryCurrentPhoto + 1) + ' of ' + JSGalleryPhotolist.length

	var capp = document.createElement('p')
	capp.className = 'photoCaption'
	capp.innerHTML = JSGalleryPhotolist[JSGalleryCurrentPhoto].caption
	
	captiondiv.appendChild(transdiv)
	captiondiv.appendChild(countp)
	captiondiv.appendChild(capp)
	
	$(transdiv).fadeTo(0,.4)
	$(captiondiv).slideDown()
}

function hideJSCaption()
{
	$('#captionCont').slideUp()
}

function moveJSImage(count)
{
	if((JSGalleryCurrentPhoto + count) < 0) JSGalleryCurrentPhoto = JSGalleryPhotolist.length
	if((JSGalleryCurrentPhoto + count) > (JSGalleryPhotolist.length - 1)) JSGalleryCurrentPhoto = -1
	loadJSImage(JSGalleryCurrentPhoto + count)
}

function prevJSPage()
{
	var totpages = Math.ceil(JSGalleryPhotolist.length / JSGalleryPaginate)
	var pagediv = document.getElementById('photobox')
	var temp = pagediv.style.top.split('p')
	var curtop = Number(temp[0])
	var curpage = (Math.abs(curtop) / 15) + 1
	if(curpage == 1) return;
	var newtop = curtop + 15
	if(JSGalleryPhotolist.length > JSGalleryPaginate) $(pagediv).animate({top: newtop + "px"})
}

function nextJSPage()
{
	var totpages = Math.ceil(JSGalleryPhotolist.length / JSGalleryPaginate)
	var pagediv = document.getElementById('photobox')
	var temp = pagediv.style.top.split('p')
	var curtop = Number(temp[0])
	var curpage = (Math.abs(curtop) / 15) + 1
	if(curpage == totpages) return;
	var newtop = curtop - 15
	if(JSGalleryPhotolist.length > JSGalleryPaginate) $(pagediv).animate({top: newtop + "px"})
}

function JSslideshow()
{
	clearInterval(JSGalleryTimer)
	JSGalleryTimer = setInterval('moveJSImage(1)',JSGallerySettings.slideshowSpeed)
	document.getElementById('playpause').src = 'images/JSGallery/pause.gif'
	document.getElementById('playpause').onclick = new Function ('evt','pauseJSShow()')
}

function pauseJSShow()
{
	clearInterval(JSGalleryTimer)
	document.getElementById('playpause').src = 'images/JSGallery/play.gif'
	document.getElementById('playpause').onclick = new Function ('evt','JSslideshow()')
}

function getJSGalleryPosition(obj)
{
  var curleft = 0
  var curtop = 0
  
  if (obj.offsetParent)
  {
      do
      {
	    curleft += obj.offsetLeft;
		curtop += obj.offsetTop;
	} while (obj = obj.offsetParent);
  }
  return {x:curleft, y:curtop}
}

function showJSThumb()
{
	var elpos = getJSGalleryPosition(document.getElementById(this.id))
	var imgnum = this.id.split('_')
	var thiswidth = this.offsetWidth
	var thumbcont = document.createElement('div')
	thumbcont.className = 'thumbcont'
	thumbcont.id = 'thumbcont'
	thumbcont.style.display = 'none'
	thumbcont.style.height = '0px'
	thumbcont.style.top = (elpos.y + 3) + 'px'
	thumbcont.style.left = (elpos.x - (thiswidth / 2)) + 'px'

	var thumbox = document.createElement('div')
	thumbox.className = 'thumbbox'
	
	var thumbimg = document.createElement('img')
	thumbimg.className = 'thumbimg'
	thumbimg.src = JSGalleryPhotolist[imgnum[1]].thumbpath
	thumbimg.alt = 'Thumbnail'
	
	thumbcont.appendChild(thumbox)
	thumbox.appendChild(thumbimg)
	document.body.appendChild(thumbcont)

	$(thumbcont).animate({height: '58px', top: (elpos.y - 55) + 'px'},200)
}

function hideJSThumb()
{
	var x = document.getElementById('thumbcont')
	x.parentNode.removeChild(x)
}

function getJSWidth()
{
	// This function calculates the width of the pages and sets pagination size accordingly
	// styles of the controls with paddings and margins = 180 pixels
	
	var parent = document.getElementById('gallerycont')
	var pwidth = parent.offsetWidth
	var contwidth = 180
	var remainder = pwidth - contwidth
	document.getElementById('photobox').style.width = remainder + 'px'
	JSGalleryPaginate = Math.floor(remainder / 25)
}