// ********************************************************
// This library requires the Prototype JavaScript Framework
// by Sam Stephenson <sam@conio.net>
// ********************************************************

var myphotos = [];
var photopath = "/media/photos/" ;
var largepath = "/media/large/"  ;
var imgs = [] ;
var sid = 0 ;
var galleryLinks = [] ;
var captions = []
var objView

//Set up the EventObservers
Event.observe(window, 'load', clickHandler, false);

function clickHandler()
{
	objView = $("photoView") ;
	if($("photo")!=null)
	{
		$("photo").onclick = showLarge ;
	}
	galleryLinks = $A(document.getElementsByClassName("galleryLink"));	
	captions = $A(document.getElementsByClassName("gallery")) ;
	galleryLinks.each(function(gL){
		gL.onclick = showPhoto;
	}) ;
	captions = $A(document.getElementsByClassName("gallery"));
	//Preload images
	
	galleryLinks.each(function(gL2, i){
	imgs[i] = new Image() ;
	//imgs[i].src =  gL2.id.substring('gallery-'.length) + ".jpg" ;
	imgs[i].src = photopath + gL2.id.substring('gallery-'.length) + ".jpg" ;
	});
	
	if ($("photo") != null && sid==0)
	{
		var s = $("photo").href ;
		var pttrn = /.*\/photos\/view.aspx\?sid=(\d+).*/ ;
		var s1 = s.gsub(pttrn, '#{1}') ;
		sid = isNaN(s1)==false ? s1 : 0 ;
	}
	
}
// =============
// Gallery view
// =============
function showPhoto()
{	
	sid = this.id.substring('gallery-'.length) ;	
	var a1 = galleryLinks.pluck('id');
	var a2 = captions.pluck('alt');
	var gId = this.id ;	
	var i = a1.indexOf(gId) ;
	var myurl = photopath + sid + ".jpg";	
	
	objView.src = myurl ;	
	if($("lbl")!=null) $("lbl").innerHTML = a2[i] ;
	location.href="#page_top" ;
	if($("photoView")!=null) $("photoView").width=150;
	return false ;
}
// =================
// Large photo view
// =================
function showLarge()
{
	var strUrl = this.href ;	
	
	if(sid != null)
	{		
		strUrl = strUrl.replace(/\?sid=\d*/i, "?sid="+sid+"") ;
		if (!strUrl.match(/\?sid=0/i))	location.href = strUrl ;
		return false ;
	}
	else
	{
		return true ;
	}
}