// -----------------------------------------------------------------------------------
// 
// This page coded by Scott Upton
// http://www.uptonic.com | http://www.couloir.org
//
// This work is licensed under a Creative Commons License
// Attribution-ShareAlike 2.0
// http://creativecommons.org/licenses/by-sa/2.0/
//
// Associated APIs copyright their respective owners
//
// -----------------------------------------------------------------------------------
// --- version date: 11/28/05 --------------------------------------------------------


// get current photo id from URL
var thisURL = document.location.href;
var splitURL = thisURL.split("#");
//var photoId = splitURL[1] - 1;

// if no photoId supplied then set default
var photoId 

// CSS border size x 2
var borderSize = 40;
//var LoadingMovie = false;
// Photo directory for this gallery
var photoDir = "images/users/";

// Define each photo's name, height, width, and caption
	// Source, Width, Height, Caption
// Number of photos in this gallery
var photoNum = $("NumOfPics").value

var photoArray = new Array;
for (i=0;i<photoNum;i++) {
	if ($("DataOfBigPic"+i) != null) {
		photoArray[i] = $("DataOfBigPic"+i).value.split("*****");
		if (!photoId) {
			photoId = i;	
		}
	}
}

var crrWidth,crrHeight,newWidth,newHeight
var CaptionBeforeInquire
/*--------------------------------------------------------------------------*/

// Additional methods for Element added by SU, Couloir
Object.extend(Element, {
	getWidth: function(element) {
   	element = $(element);
   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   	element = $(element);
    	element.style.height = h +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setMove: function(element,src) {
		$(element).innerHTML = "<object width='425' height='355'><param name='movie' value='"+src+"' id='prmMovie'></param><param name='wmode' value='transparent'></param><embed id='embMovie' type='application/x-shockwave-flash' wmode='transparent' width='425' height='355' src='"+src+"'></embed></object>"
		if ($(element).innerHTML == "")	{
			$(element).innerHTML = "<embed id='embMovie' type='application/x-shockwave-flash' wmode='transparent' width='425' height='355' src='"+src+"'></embed>"
		}
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	},
	setInquire: function(hrefSubject) {
		element = $("Caption")
		if (element.scrollHeight<40) {
			element.innerHTML = element.innerHTML + "<BR>"
		}
		// if inquire exists then replace it.
		if (element.innerHTML.indexOf("mailto:")>-1) {
			element.innerHTML = CaptionBeforeInquire;
		} 
		CaptionBeforeInquire = element.innerHTML
		element.innerHTML = element.innerHTML + " <a href='mailto:info@alonsegevgallery.com?subject="+hrefSubject+"&body=Regarding "+hrefSubject+"' >inquire</a>"

	}
});

/*--------------------------------------------------------------------------*/

var Slideshow = Class.create();

Slideshow.prototype = {
	initialize: function(photoId) {
		this.photoId = photoId;
		this.photo = 'Photo';
		this.objMovie = 'objMovie';
		this.photoBox = 'BoxOfBigPic';
		this.prevLink = 'PrevLink';
		this.nextLink = 'NextLink';
		this.captionBox = 'CaptionContainer';
		this.caption = 'Caption';
		this.loader = 'Loading';
	},
	getCurrentSize: function() {
		// Get current height and width, subtracting CSS border size
		this.wCur = Element.getWidth(this.photoBox);
		this.hCur = Element.getHeight(this.photoBox);
	},
	getNewSize: function() {
		// Get current height and width
		this.wNew = parseInt(photoArray[photoId][1])+borderSize;
		this.hNew = parseInt(photoArray[photoId][2]);
	},
	getScaleFactor: function() {
		this.getCurrentSize();
		this.getNewSize();
		// Scalars based on change from old to new
		this.xScale = (this.wNew / this.wCur) * 100;
		this.yScale = (this.hNew / this.hCur) * 100;
	},
	setNewPhotoParams: function() {
		// Set source of new image
		if (photoArray[photoId][0].indexOf("youtube")>0) {
			Element.setMove(this.objMovie,photoArray[photoId][0]);
			LoadingMovie = true;
		} else {
			Element.setSrc(this.photo,photoDir + photoArray[photoId][0]);
			LoadingMovie = false;
		}
		// Set anchor for bookmarking
		Element.setHref(this.prevLink, "#" + (photoId+1));
		Element.setHref(this.nextLink, "#" + (photoId+1));
	},
	setPhotoCaption: function() {
		// Add caption from gallery array
		Element.setInnerHTML(this.caption,"<i>"+photoArray[photoId][3]+"</i>, "+photoArray[photoId][4]+"<BR>"+photoArray[photoId][5]);
	},
	resizePhotoBox: function() {
		this.getScaleFactor();
		new Effect.Scale(this.photoBox, this.yScale, {scaleX: false, duration: 0.3, queue: 'front'});
		new Effect.Scale(this.photoBox, this.xScale, {scaleY: false, delay: 0.5, duration: 0.3});
		if (LoadingMovie) {
			this.showPhoto()
		}
	},
	showPhoto: function(){
		new Effect.Fade(this.loader, {delay: 0.5, duration: 0.3});
		// Workaround for problems calling object method "afterFinish"
		emailData = photoArray[photoId][3]+", "+ photoArray[photoId][4]+", "+photoArray[photoId][5] +" by "+ $("Subject").innerHTML
		if (LoadingMovie) {
			new Effect.Appear(this.objMovie, {duration: 0.5, queue: 'end', afterFinish: function(){Element.show('CaptionContainer');Element.show('PrevLink');Element.show('NextLink');Element.setInquire(emailData)}});
//			$(this.objMovie).style.visibility = "visible"
		} else {
			new Effect.Appear(this.photo, {duration: 0.5, queue: 'end', afterFinish: function(){Element.show('CaptionContainer');Element.show('PrevLink');Element.show('NextLink');Element.setInquire(emailData)}});
		}
		LoadingMovie = false
	},
	nextPhoto: function(){
		// Figure out which photo is next
		(photoId == (photoArray.length - 1)) ? photoId = 0 : photoId++;
		this.initSwap();
	},
	prevPhoto: function(){
		// Figure out which photo is previous
		(photoId == 0) ? photoId = photoArray.length - 1 : photoId--;
		this.initSwap();
	},
	initSwap: function() {
		// Begin by hiding main elements
		Element.show(this.loader);
		Element.hide(this.photo);
		Element.hide(this.objMovie);
		Element.hide(this.captionBox);
		Element.hide(this.prevLink);
		Element.hide(this.nextLink);
		// Set new dimensions and source, then resize
		this.setNewPhotoParams();
		this.resizePhotoBox();
		this.setPhotoCaption();
	}
}

/*--------------------------------------------------------------------------*/

// Establish CSS-driven events via Behaviour script
var myrules = {
	'#Photo' : function(element){
		element.onload = function(){
			var myPhoto = new Slideshow(photoId);
			myPhoto.showPhoto();
		}
	},
	'#PrevLink' : function(element){
		element.onmouseover = function(){
		}
		element.onclick = function(){
			var myPhoto = new Slideshow(photoId);
			myPhoto.prevPhoto();
		}
	},
	'#NextLink' : function(element){
		element.onmouseover = function(){
		}
		element.onclick = function(){
			var myPhoto = new Slideshow(photoId);
			myPhoto.nextPhoto();
		}
	},
	a : function(element){
		element.onfocus = function(){
			this.blur();
		}
	}
};

// Add window.onload event to initialize
Behaviour.addLoadEvent(init);
Behaviour.apply();
function init() {
	if (photoNum>0) {
		var myPhoto = new Slideshow(photoId);
		myPhoto.initSwap();
	}
}

function initPhotos(ParentExID,TotalPhotoNum) {
	photoArray = new Array;
	arrIndex = 0
	for (i=0;i<TotalPhotoNum;i++) {
		if ($("DataOfBigPic"+ParentExID+"_"+i) != null) {
			photoArray[arrIndex] = $("DataOfBigPic"+ParentExID+"_"+i).value.split("*****");
			arrIndex = arrIndex+1
		}
	}	
	photoId = 0
	photoNum = arrIndex
}