var EnlargeImage = Class.create({

	initialize: function() {		
		this.imageList();
		this.insertHTML();		
	},
	
	insertHTML: function() {
		var objBody = $$('body')[0];
		if (!$('overlay')) {
		    objBody.appendChild(Builder.node('div', {id:'overlay'}));
		}
		objBody.appendChild(Builder.node('div', {id:'enlargeImage'}, [
		  Builder.node('div', {id:'enlargeImageInner'}, [
		  	Builder.node('div', {id:'enlargeImageTop'}, [
		  		Builder.node('div', {id:'enlargeImageTopLeft'}),
		  		Builder.node('div', {id:'enlargeImageTopRight'}, [
		  			Builder.node('span', {id:'enlargeImageTopRightClose'}, 'Close X')
		  		])
		  	]),
		  	Builder.node('div', {id:'enlargeImageBottom'}, [
		  		Builder.node('img', {id:'enlargeImageImg'}),
		  		Builder.node('div', {id:'enlargeImageLoading'})
	  		]),
	  		Builder.node('div', {id:'enlargeImageClear'})
		  ])
		]));		
		
		var th = this;
    (function(){
      var ids = 'overlay enlargeImage enlargeImageInner enlargeImageTop enlargeImageTopLeft enlargeImageTopRight enlargeImageTopRightClose enlargeImageBottom enlargeImageImg enlargeImageLoading';   
      $w(ids).each(function(id){ th[id] = $(id); });
    }).defer();
    
    $('overlay').hide().observe('click', (function() { this.end(); }).bind(this));
		$('enlargeImage').hide().observe('click', (function(event) { if (event.element().id == 'enlargeImage') this.end(); }).bind(this));
		$('enlargeImageTopRightClose').observe('click', (function() { this.end(); }).bind(this));		
	},	
	
	imageList: function() {
		document.observe('click', (function(event){
			var target = event.findElement('a[rel=enlargeImage]');
			if (target) {
				event.stop();
				this.start(target);	
			}
		}	).bind(this));
	},
	
	start: function(link) {
		
		var arrayPageSize    = this.getPageSize();
    var arrayPageScroll  = document.viewport.getScrollOffsets();
		var enlargeImageTop  = arrayPageScroll[1] + (document.viewport.getHeight() / 10);
		var enlargeImageLeft = arrayPageScroll[0];
		
    $('overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
    
		new Effect.Appear(this.overlay, { duration: 0.2, from: 0.0, to: 0.8 });		
		
		this.enlargeImageTopLeft.update(link.title);
			
		this.enlargeImageBottom.setStyle({height: 200 + 'px'});
		this.enlargeImageImg.hide();
		this.enlargeImageLoading.show();
				
		var preloader = new Image();
		this.enlargeImageImg.hide();
		preloader.onload = (function(){
		  this.enlargeImageLoading.hide();
			this.enlargeImageImg.src = link.href + '?width=800';		  
		  this.changeHeight(preloader.height);		  
		}).bind(this);
		preloader.src = link.href + '?width=800';

		this.enlargeImage.setStyle({ top: enlargeImageTop + 'px', left: enlargeImageLeft + 'px' }).show();		
		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });
	},
	
	changeHeight: function(height) {
		
		var heightCurrent = this.enlargeImageBottom.getHeight();
		if (heightCurrent == 0) {
			heightCurrent = 200;	
		}
		var heightNew = height;
		var yScale = (heightNew / heightCurrent) * 100;
		var hDiff = heightCurrent - heightNew;
		if (hDiff != 0) {		
		  new Effect.Scale(this.enlargeImageBottom, yScale, {scaleX: false, scaleContent: false, duration: 0.2 }); 			
		}
		new Effect.Appear(this.enlargeImageImg, { duration: 0.2, queue: 'end'});        
	},
	
	end: function() {
		this.enlargeImageImg.hide(); 
		this.enlargeImage.hide();
		
		new Effect.Fade(this.overlay, { duration: 0.2 });	
		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
	 
	},
	
	getPageSize: function () {
		var xScroll, yScroll, windowWidth, windowHeight;		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else {
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}			
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		return [pageWidth,pageHeight];
	}
	
});

document.observe('dom:loaded', function () { new EnlargeImage(); });