var imageLoad={



	loaderFade: '',



	imageFade: {},



	loadedImages : [],



	timeout: 10000,



	timeoutRef:false,



	loadCompleted: false,



	currentDiv: '',

	windowPos:'',



	getMousePosition: function(e){

		if(window.pageYOffset){

			imageLoad.windowPos = window.pageYOffset;

		}else if(document.body.scrollTop){

			imageLoad.windowPos = document.body.scrollTop;

		}else if(document.documentElement.scrollTop){

			imageLoad.windowPos = document.documentElement.scrollTop;

		}

		if($type(imageLoad.windowPos) != 'number'){

			imageLoad.windowPos = 0;

		}

		

	

	},



	createContainer: function(div_id, title, caption){



		var div = new Element('div',{ 'class' : 'image-container', 'id' : div_id});



		var p_close = new Element('p', { 'class' : 'image-container-close'} );



		var a_close = new Element('a', { 'href' : 'javascript:void(0)', 'events' : { 'click' : imageLoad.closeContainer.bindAsEventListener(div) }  } );



		a_close.setText('close');



		var div_clear = new Element('div', {'style' : 'clear:both'} );



		var h4 = new Element('h4');



		var p_caption = new Element('p', {'class' : 'image-caption'} );



		a_close.injectInside(p_close);



		p_close.injectTop(div);



		h4.injectAfter(p_close);



		div_clear.injectAfter(h4);



		p_caption.injectAfter(div_clear);



		h4.setText( title );



		p_caption.setHTML( caption );



		var body = $$('body');



		div.setStyle('visibility', 'hidden');



		div.injectAfter('wrapper');



		return div;



	},







	loadImage: function(e, divId){



	       div = $(divId);

	       imageLoad.getMousePosition(e);

	 			



               var imageName = this.src.replace('_thumb', '');



	       var imgs = div.getElementsByTagName('img');



	       if( imgs.length > 0 ){	



                                // $('image-caption').setText( this.title );



                               // imageLoad.currentDiv.setStyle('width', imageLoad.loadedImages[0].width);

				

                                $('loader-image').setStyle('display', 'none');



				imageLoad.loadCompleted = true;



				imageLoad.complete.attempt('', div);



                }else{





				$('loader-image').setStyle('top', imageLoad.windowPos + 100);



				$('loader-image').setStyle('display', 'block');



				imageLoad.loadedImages = new Asset.images([imageName], {onComplete: imageLoad.complete.bindAsEventListener(div)} );



				imageLoad.timeoutRef = window.setTimeout(imageLoad.onError, imageLoad.timeout );



		                //$('image-caption').setText( this.title );



                               



               }



	},







	complete: function(){



	



         imageLoad.loadCompleted = true;   



		 if( imageLoad.timeoutRef != false ){



			 window.clearTimeout( imageLoad.timeoutRef );



			 imageLoad.timeoutRef = false;



		 }



		while( imageLoad.loadedImages[0].width > (window.getWidth - 20) || imageLoad.loadedImages[0].height > (window.getHeight - 20)){



			imageLoad.loadedImages[0].width =- 10;



			imageLoad.loadedImages[0].height =- 10;



		}



		var caption = this.getElementsByClassName('image-caption')[0];



		var imgs = this.getElementsByTagName('img');



		if(imgs.length == 0){



			imageLoad.loadedImages[0].injectBefore(caption);



		}

		this.setStyle('top', imageLoad.windowPos + 100); 

		



         	this.setStyle('width', imgs[0].width);



		$('loader-image').setStyle('display', 'none');



		imageLoad.imageFade[this.id].start(0, 1);

		imageLoad.windowPos = 0;



	},



	



	onError: function(){



		if(imageLoad.loadCompleted == false){



			$('loader-image').setStyle('display', 'none');



			alert("Error: could not find Image");



		}



	},







	closeContainer: function(){



		imageLoad.loadCompleted = false;



               // $('loader-image').setStyle('display', 'none');



		imageLoad.imageFade[this.id].start(1, 0);



               //$('image-caption').getPrevious().remove()



	},	



	







	init: function(){



		var img = $$('img');



		var title = '';



		var caption = '';



		for(var i=0; i<img.length; i++){



			if( img[i].src.contains('_thumb') ){



				if( img[i].title.contains('::') ){



					img_pieces = img[i].title.split('::');



					for( var j=0; j<img_pieces.length; j++){



						switch(j){



							case 0:



								img[i].title = img_pieces[j];



								break;



							case 1:



								title = img_pieces[j];



								break;



							case 2:



								caption = img_pieces[j];



								break;



						}



					}



				}



				var div = imageLoad.createContainer( img[i].src.replace('://', ''), title, caption);



				img[i].addEvent('click', imageLoad.loadImage.bindAsEventListener(img[i], div.id) );



				img[i].setStyle('cursor', 'pointer');



				var t = div.getElementsByClassName('image-container-close')

                            t[0].addEvent('click', imageLoad.closeContainer.bindAsEventListener(div));



				imageLoad.loaderFade = $('loader-image').effect('opacity', {duration:500, transition: Fx.Transitions.linear});



				imageLoad.imageFade[div.id] = div.effect('opacity', {duration:500, transition: Fx.Transitions.linear});



				div.makeDraggable();



			}



				



		}



			



		



	}



}







window.addEvent('domready', imageLoad.init);
