
var thisURL = document.location.href;
var splitURL = thisURL.split("#");
var photoId = splitURL[1] - 1;


var photoId = (!photoId)? 0 : photoId;


var borderSize = 10;


var photoDir = "photos/voluntarios/";


var photoArray = new Array(
	
	new Array("1dia mund.jpg", "500", "375", "Dia Mundial del Medio Ambiente - 2009"),
	new Array("1moholi.jpg", "500", "375", "MoHoli"),
    new Array("1monitoreo ecol.jpg", "500", "375", "Voluntarios Monitoreo Ecologico - 2008"),
	new Array("1plastica.jpg", "500", "375", "Amistad que nace en los acuarios"),
	new Array("1somoslosotros.jpg", "500", "375", "Trabajando en equipo y entre amigos"),
	new Array("1stand.jpg", "500", "375", "Stand - Verano 2009"),
	new Array("1stand2.jpg", "500", "375", "Armando el stand "),
	new Array("1trio.jpg", "500", "375", "Trio Acuariano"),
	new Array("1vol 1dia.jpg", "500", "375", "Voluntarios Febrero 2009"),
	new Array("1vol humedal.jpg", "500", "375", "Junto a los niños en Campiche"),
	new Array("1vols.jpg", "500", "375", "Voluntarios Enero 2009"),
	new Array("2minga.jpg", "500", "375", "Minga de limpieza de playas"),
	new Array("2minga1.jpg", "500", "375", "Minga de limpieza de playas"),
	new Array("3stand_2.jpg", "500", "375", "Voluntarias trabajando en el stand"),
	new Array("19despe.jpg", "500", "375", "Avistando en las reinserciones"),
	new Array("acuario dia mund.jpg", "500", "375", "Acuarios en el Dia Mundial del Medio Ambiente"),
	new Array("amigas.jpg", "500", "375", "Amigas participan en monitoreo de nutrias"),
	new Array("caminand.jpg", "500", "375", "De regreso despues de un dia de trabajo"),
	new Array("cara de papa.jpg", "500", "375", "Pensando en proyectos"),
	new Array("carits.jpg", "500", "375", "Caritas"),
	new Array("charla11.jpg", "500", "375", "Registro"),
	new Array("chinchiconce.jpg", "500", "375", "Chinchimén Concepción"),
	new Array("conce.jpg", "500", "375", "Directorio Chinchimén Concepción"),
	new Array("escuela.jpg", "500", "375", "Felices dias en la escuela de Maitencillo"),
	new Array("frisco lolo etc.jpg", "500", "375", "Amigos de Maitencillo"),
	new Array("GABRIEL.jpg", "500", "375", "Bajando por una quebrada en Quirilluca"),
	new Array("greenpeace.jpg", "500", "375", "Greenpeace"),
	new Array("hamigos.jpg", "500", "375", "Almorzando en una tarde de Verano"),
	new Array("k25.jpg", "500", "375", "Sonrisas"),
	new Array("maestr.jpg", "500", "375", "Maestriando"),
	new Array("noche.jpg", "500", "375", "Amigos en la oscuridad"),
	new Array("nosevende.jpg", "500", "375", "Maitencillo NO SE VENDE"),
	new Array("otros copy.jpg", "500", "375", "Visita de amigos"),
	new Array("paso.jpg", "500", "375", "Paso de Nutrias"),
	new Array("quirilluca noseqhacen.jpg", "500", "375", "Conteo de Nutrias"),
	new Array("reunion.jpg", "500", "375", "Primeras reuniones"),
	new Array("salud.jpg", "500", "375", "Festejo"),
	new Array("tvn.jpg", "500", "375", "TVN"),
	new Array("voss.jpg", "500", "375", "Amigos Territoreando")
	);


var photoNum = photoArray.length;


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; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});



var Slideshow = Class.create();

Slideshow.prototype = {
	initialize: function(photoId) {
		this.photoId = photoId;
		this.photo = 'Photo';
		this.photoBox = 'Container';
		this.prevLink = 'PrevLink';
		this.nextLink = 'NextLink';
		this.captionBox = 'CaptionContainer';
		this.caption = 'Caption';
		this.counter = 'Counter';
		this.loader = 'Loading';
	},
	getCurrentSize: function() {
		
		this.wCur = Element.getWidth(this.photoBox) - borderSize;
		this.hCur = Element.getHeight(this.photoBox) - borderSize;
	},
	getNewSize: function() {
		
		this.wNew = photoArray[photoId][1];
		this.hNew = photoArray[photoId][2];
	},
	getScaleFactor: function() {
		this.getCurrentSize();
		this.getNewSize();
		
		this.xScale = (this.wNew / this.wCur) * 100;
		this.yScale = (this.hNew / this.hCur) * 100;
	},
	setNewPhotoParams: function() {
	
		Element.setSrc(this.photo,photoDir + photoArray[photoId][0]);
		
		Element.setHref(this.prevLink, "#" + (photoId+1));
		Element.setHref(this.nextLink, "#" + (photoId+1));
	},
	setPhotoCaption: function() {
	
		Element.setInnerHTML(this.caption,photoArray[photoId][3]);
		Element.setInnerHTML(this.counter,((photoId+1)+'/'+photoNum));
	},
	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});
		
		Element.setWidth(this.captionBox,this.wNew-(-borderSize));
	},
	showPhoto: function(){
		new Effect.Fade(this.loader, {delay: 0.5, duration: 0.3});
	
		new Effect.Appear(this.photo, {duration: 0.5, queue: 'end', afterFinish: function(){Element.show('CaptionContainer');Element.show('PrevLink');Element.show('NextLink');}});
	},
	nextPhoto: function(){
		
		(photoId == (photoArray.length - 1)) ? photoId = 0 : photoId++;
		this.initSwap();
	},
	prevPhoto: function(){
		
		(photoId == 0) ? photoId = photoArray.length - 1 : photoId--;
		this.initSwap();
	},
	initSwap: function() {
		
		Element.show(this.loader);
		Element.hide(this.photo);
		Element.hide(this.captionBox);
		Element.hide(this.prevLink);
		Element.hide(this.nextLink);
		
		this.setNewPhotoParams();
		this.resizePhotoBox();
		this.setPhotoCaption();
	}
}


var myrules = {
	'#Photo' : function(element){
		element.onload = function(){
			var myPhoto = new Slideshow(photoId);
			myPhoto.showPhoto();
		}
	},
	'#PrevLink' : function(element){
		element.onmouseover = function(){
			soundManager.play('beep');
		}
		element.onclick = function(){
			var myPhoto = new Slideshow(photoId);
			myPhoto.prevPhoto();
			soundManager.play('select');
		}
	},
	'#NextLink' : function(element){
		element.onmouseover = function(){
			soundManager.play('beep');
		}
		element.onclick = function(){
			var myPhoto = new Slideshow(photoId);
			myPhoto.nextPhoto();
			soundManager.play('select');
		}
	},
	a : function(element){
		element.onfocus = function(){
			this.blur();
		}
	}
};


Behaviour.addLoadEvent(init);
Behaviour.apply();
function init() {
	var myPhoto = new Slideshow(photoId);
	myPhoto.initSwap();
	soundManagerInit();
}