// JavaScript Document

PhotoGallery.prototype.currentpos=0;
PhotoGallery.prototype.currentthumbpos=0;
PhotoGallery.prototype.moveamount=150;
PhotoGallery.prototype.thumbnailnum;
PhotoGallery.prototype.maxthumbvisible = 3; //Define how many thumbnails will be visible at one time in the thumbbox--for now this should only be 3 since it's actually the css that controls the size of the viewable box
PhotoGallery.prototype.current_imgid=0;
PhotoGallery.prototype.moving = false;
PhotoGallery.prototype.finishedsizing=0;
PhotoGallery.prototype.preloadimg="no"//Preload images ("yes" or "no"):
PhotoGallery.prototype.myloadedimage = new Array();
PhotoGallery.prototype.myloadedimage[0]=1;
PhotoGallery.prototype.imggallery=new Array()
//PhotoGallery.prototype.firstimagestart=1;
PhotoGallery.prototype.nextorprev=0;
// setting reference
PhotoGallery.prototype._oScope = 0;
PhotoGallery.prototype.name;
PhotoGallery.prototype.direction = 0; // vertical / 1 horizontal
//PhotoGallery.prototype.fadeout=1;


function PhotoGallery( name, imgwrapper, thumbwrapper, prevbutton, nextbutton ) {
	this.name = name;
	this.imgwrapper = imgwrapper;
	this.thumbwrapper = thumbwrapper;
	this.prevbutton = prevbutton;
	this.nextbutton = nextbutton;
	this._oScope = this;
}


PhotoGallery.prototype.areweready = function(loadarea, imgindex, img_id) {
	jslog.debug('areweready called');
	if ( this.finishedsizing++ ) { 
		modifyimage(loadarea, imgindex, img_id);
		this.finishedsizing = 0;
	}
}


PhotoGallery.prototype.modifyimage = function(loadarea, imgindex, img_id ){
	loadimagenow = function(){
		if (document.getElementById) {
			var imgobj=document.getElementById(loadarea);
			// Mootools 1.11
			//var fader = new Fx.Style(loadarea,'opacity', {duration:fadespeed});
			// Mootools 1.2
			var fader = new Fx.Morph(loadarea,'opacity', {duration:fadespeed});
			fader.set(0);
			imgobj.innerHTML = this.returnimgcode( this.imggallery[imgindex] );
			fader.start(0,1);
			this.current_imgid=img_id;
			this.myloadedimage[imgindex]=1;
		}
		return false;
	}.bind(this);
	if(this.myloadedimage[imgindex]==null){	
		new Asset.image(this.imggallery[imgindex][0], { onload: function() { loadimagenow(); } });
	}else{
		loadimagenow();
	}
}

PhotoGallery.prototype.returnimgcode = function( theimg ) {
	var imghtml = "";
	if (theimg[1]!="")
		imghtml='';
	
	if(typeof theimg[4] != "undefined") {
	   imghtml+='<img src="'+theimg[0]+'" width="'+theimg[1]+'px" height="'+theimg[2]+'px" border="0" alt="'+theimg[4]+'" />';
	} else {
	   imghtml+='<img src="'+theimg[0]+'" width="'+theimg[1]+'px" height="'+theimg[2]+'px" border="0"/>';
	}
	
	if (theimg[1]!="")
		imghtml+='';
	return imghtml;
}

PhotoGallery.prototype.checkbutton = function(mynum){
	if ( mynum == 0 ) {
		this.mm_shl(this.prevbutton,'hidden');
		this.mm_shl(this.nextbutton,'visible');
	} else if ( (mynum+1)*this.maxthumbvisible < this.thumbnailnum) {
		this.mm_shl(this.prevbutton,'visible');
		this.mm_shl(this.nextbutton,'visible');
	} else {
		this.mm_shl(this.prevbutton,'visible');
		this.mm_shl(this.nextbutton,'hidden');
	}
}

PhotoGallery.prototype.mm_shl = function() { //v6.0
  //jslog.debug('mm_shl called');
	var obj,args=arguments;
	if( (obj = this.MM_findObj(args[0]))!=null) {
		if (obj.style) {
			obj=obj.style;
		}
		obj.visibility=args[1];
	}
}


PhotoGallery.prototype.MM_findObj = function(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

PhotoGallery.prototype.addposition = function(addwidth){
	if ( !this.moving ) {
		 // if animagic is still moving the image..don't update the current position till it's done
		if(addwidth=="minus"){
			this.currentpos-=1;
		}else if(addwidth=="plus"){
			this.currentpos+=1;
		}
	}
	return this.currentpos;
}

PhotoGallery.prototype.movethumbs = function(way){
	thumbwrapper = this.thumbwrapper.toString();
	if(way=='plus'){
		move=(this.currentthumbpos+this.moveamount);
		// Mootools 1.11
		//var movethumbs = new Fx.Styles(thumbwrapper, {duration: transspeed, transition: Fx.Transitions.quadOut});
		// Mootools 1.2
		var movethumbs = new Fx.Morph(thumbwrapper, {duration: transspeed, transition: Fx.Transitions.Quad.easeInOut});
		if( this.direction == 0 ) {
			movethumbs.start({ top: [this.currentthumbpos, move]});
		} else if( this.direction == 1 ) {
			movethumbs.start({ left: [this.currentthumbpos, move]});
		}
		this.currentthumbpos+=this.moveamount;
	}else if(way=='minus'){
		move=(this.currentthumbpos-this.moveamount);
		// Mootools 1.11
		//var movethumbs = new Fx.Styles(thumbwrapper, {duration: transspeed, transition: Fx.Transitions.quadOut});
		// Mootools 1.2
		var movethumbs = new Fx.Morph(thumbwrapper, {duration: transspeed, transition: Fx.Transitions.Quad.easeInOut});
		if( this.direction == 0 ) {
			movethumbs.start({ top: [this.currentthumbpos, move]});
		} else if( this.direction == 1 ) {
			movethumbs.start({ left: [this.currentthumbpos, move]});
		}
		this.currentthumbpos-=this.moveamount;		
	}
}




// JavaScript Document
PhotoGallery.prototype.initGallery = function( tempgallery, count, first_id, startwidth, startheight ) {
	if(tempgallery){
	this.imggallery = tempgallery;
	if(this.preloadimg=="yes"){
		for (x=0; x<this.imggallery.length; x++){
			var myimage=new Image()
			myimage.src=this.imggallery[x][0]
		}
	}
	this.thumbnailnum = this.imggallery.length;
	this.current_imgid = first_id;
	currentwidth=startwidth;
	currentheight=startheight;
	if(this.thumbnailnum>this.maxthumbvisible){
		minuspos = this.addposition('minus');
		pluspos = this.addposition('plus');
		if(this.direction==0) {
			lefthtml = "<a href=\"javascript:" + this.name + ".checkbutton(" + this.name + ".addposition('minus'));" + this.name + ".movethumbs('plus');\"><img src='fileadmin/templates/images/gallery-up-arrow.png' width='81' height='15' border='0' /></a>";
			righthtml = "<a href=\"javascript:" + this.name + ".checkbutton(" + this.name + ".addposition('plus'));" + this.name + ".movethumbs('minus');\"><img src='fileadmin/templates/images/gallery-down-arrow.png' width='81' height='15' border='0' /></a>";
		} else if(this.direction==1) {
			lefthtml = "<a href=\"javascript:" + this.name + ".checkbutton(" + this.name + ".addposition('minus'));" + this.name + ".movethumbs('plus');\"><img src='fileadmin/templates/images/gallery-left-arrow.gif' width='15' height='81' border='0' /></a>";
			righthtml = "<a href=\"javascript:" + this.name + ".checkbutton(" + this.name + ".addposition('plus'));" + this.name + ".movethumbs('minus');\"><img src='fileadmin/templates/images/gallery-right-arrow.gif' width='15' height='81' border='0' /></a>";
		}
		$(this.prevbutton).setHTML(lefthtml);
		$(this.nextbutton).setHTML(righthtml);
		this.checkbutton(0);
	}
	this.setloadersize('imgloader', 0, first_id );
	}
}


function test(){
	jslog.debug('test log ' + loadarea);
}


PhotoGallery.prototype.setloadersize = function(loadarea, imgindex, img_id ) {	
	if( this.imgwrapper != '' ) {
		name = this.name.toString();
		ref = this._oScope;
		// Mootools 1.11
		//setstyle = new Fx.Styles( 'imgloader', { duration: transspeed, onComplete: function() { ref.modifyimage( loadarea, imgindex, img_id ); } } );
		// Mootools 1.2
		setstyle = new Fx.Morph( 'imgloader', { duration: transspeed } );
		setstyle.addEvents({ onComplete: function() { ref.modifyimage( loadarea, imgindex, img_id ); } });
		width = this.imggallery[imgindex][1];
		height = this.imggallery[imgindex][2];
		setstyle.start({
			'width': width,
			'height': height	
		});
	}
}

PhotoGallery.prototype.getstarted = function(width, height, loadarea, imgindex, img_id, current_imgid){
 
 	try {

	if(typeof this.name != 'undefined')	{
		name = this.name.toString();
		//console.log("this is being run");
	}


	this.checknext(img_id);
	ref = this._oScope;
	// Mootools 1.11
	//resizeDivHeight = new Fx.Style( 'imgloader', 'opacity', { duration:500, onStart: function() { ref.setloadersize( 'imgloader', imgindex, img_id ); } }  /*, onComplete: function(){ modifyimage( loadarea, imgindex, img_id );*/  );
	// Mootools 1.2
	resizeDivHeight = new Fx.Morph( 'imgloader', 'opacity', { duration:500  }  /*, onComplete: function(){ modifyimage( loadarea, imgindex, img_id );*/  );
	resizeDivHeight.addEvents({onStart: function() { ref.setloadersize( 'imgloader', imgindex, img_id ); } })
	resizeDivHeight.start( 1,0 );

	} catch(e) {
		alert(e.message);
	}
}

PhotoGallery.prototype.loadfirstimage = function(currentwidth,currentheight){
	function setfirstimage() {
		var newHTML = "<img src='"+tempgallery[0][0]+"' />";
		$('imgloader').setHTML(newHTML);
		currentheight=this.imggallery[0][2];
		currentwidth=this.imggallery[0][1];
	}
	new Asset.image(this.imggallery[0][0], {onload: setfirstimage});
}


PhotoGallery.prototype.nextimage = function(current_imgid){
	newimgid = Number(current_imgid)+1;
	newwidth =this.imggallery[newimgid][1]
	newheight =this.imggallery[newimgid][2]
	newimgindex =this.imggallery[newimgid][8]
	newimgid = this.imggallery[newimgid][8]
	cwidth=this.imggallery[current_imgid][1]
	cheight=this.imggallery[current_imgid][2]
	checknext(newimgid);
	this.nextorprev=1;
	getstarted(Number(newwidth), Number(newheight), 'imgloader',Number(newimgindex) ,Number(newimgid) , Number(current_imgid), Number(cwidth), Number(cheight))
}

PhotoGallery.prototype.previmage = function(current_imgid){
	newimgid = Number(current_imgid)-1;
	newwidth =this.imggallery[newimgid][1]
	newheight =this.imggallery[newimgid][2]
	newimgindex =this.imggallery[newimgid][8]
	newimgid = this.imggallery[newimgid][8]
	cwidth=this.imggallery[current_imgid][1]
	cheight=this.imggallery[current_imgid][2]
	checknext(newimgid);
	this.nextorprev=1;
	getstarted(Number(newwidth), Number(newheight), 'imgloader',Number(newimgindex) ,Number(newimgid) , Number(current_imgid), Number(cwidth), Number(cheight))
}


PhotoGallery.prototype.checknext = function(mynum){
	thumbmax=(Number(this.thumbnailnum)-1);
	if ( mynum < 1 ) {
		this.mm_shl('prev','hidden');
		this.mm_shl('next','visible');
	} else if ( mynum <  thumbmax ) {
		this.mm_shl('prev','visible');
		this.mm_shl('next','visible');
	} else {
		this.mm_shl('prev','visible');
		this.mm_shl('next','hidden');
	}
}

