/***/
window.addEvent('domready', function() {
/***/
var imageZoom = new Class({
	initialize: function(){
		this.images = document.getElements('img[class^=test]');
		this.preview = $('Preview');
		this.loader = $('previewLoader');
		this.marginFromPic = 10; //10 px margin from the edge of picture
		this.prevPadding = 10; //5+5 in css is 5px
		this.show = $('showzoom');
		this.oldTitle = '';
		
		
		/**positioning pictures for ie
		if(window.ie){this.images.each(function(element){element.setStyle('margin', '0px');});}
		else{this.images.each(function(element){element.setStyles({'margin' : 'auto','right' : '6px'});});}*/
		/**end of positioning pictures for ie*/
		
		this.showPreviewEvnt();
		if(this.show.checked) this.imgEvnt();
	},
	prevImages: function(othis){
		var no = Math.floor(Math.random()*1001);
		var bb = 0;
		var windowSizeY = window.getSize().size.y; //y size of window
		var windowSizeX = window.getSize().size.x; // x size of window
		var windowPositionY = window.getSize().scroll.y; //actual position of y size
		var maxBottom = windowPositionY + windowSizeY;
		var dimm = this.getCoordinates(); //get dimmension of thumbnail
		var imageURL = 'http://images.robertharding.com/zoom/'+this.alt; //setup path to file
		othis.loader.setStyles({
			'top' : (dimm.top - 15),
			'left' : (dimm.left + (dimm.width/2)),
			'visibility' : 'visible'
		});
		
		
		var img = new Asset.image(imageURL,{
			id : 'prevPicture_'+no,
			onload: function(){
				//preview.empty();

				var colImgs = othis.preview.getElements('img');
				colImgs.each(function(el){
					if(el.id != 'prevPicture_'+no) el.remove();
				});
				img.injectTop('Preview');
				
				bb++;
		if(bb == 1){
			var loadedDim = this.getCoordinates(); //get dimmension of preview image
			this.setStyles({
				'width':(loadedDim.width * 1)+'px',//for seanie 80% of picture width - now at 100% since zooms archive installed 28-2-2011
				'height':(loadedDim.height * 1)+'px' //for seanie 80% of picture height
			});
		}
		var loadedDim = this.getCoordinates(); //get dimmension of preview image
		var loadedWidth = loadedDim.width;
		othis.preview.setStyle('width',loadedWidth+'px');
		
		
		/*var DescSize = $('prevDescr').getCoordinates().height + 10; //10 is a margin-top */
		
		if($defined($('prevDescr'))) var DescSize = $('prevDescr').getCoordinates().height + 10; //10 is a margin-top
		else var DescSize = 0;
		
		/*var DescSize = title.getCoordinates().height;*/
		
		var loadedHeight = 0 + loadedDim.height + DescSize; 
		var newPrevLeft=0;var newPrevTop=0;var isOnMiddle=0;var cannotFit=0; //reset last values of preview div top and left
		
		
		///setup preview on the right from the picture
		var rightPrevEnd = dimm.left + dimm.width + loadedWidth + othis.marginFromPic; // x position of preview pic end 
		var bottomPrevEnd = dimm.top + loadedHeight; // y position of preview pic end 
		//////////////////////////
		
		
		
		//left positioning
		if(windowSizeX < rightPrevEnd){ //means can not put the picture on the right hand side - no space for it
			///setup preview on the left from the picture
			var leftPrevEnd = dimm.left - othis.marginFromPic - othis.prevPadding - loadedWidth;
			if(leftPrevEnd > 0 && (leftPrevEnd + loadedWidth) < dimm.left){
				//means then there is a space on the left hand side
				newPrevLeft = leftPrevEnd;
			}
			else{
				//no space on the left and right hand side //propably need to be spaced on the top or bottom of the picture
				if(loadedWidth > windowSizeX){ //can not load preview because there is no space for it
					//alert('can not fit preview');
					//cannotFit = 1;
					newPrevLeft = 0;
				}
				else{ //fit the picture in the middle of page
					newPrevLeft = (windowSizeX - loadedWidth) /2;
					isOnMiddle = 1;
				}
			}
		}
		else{//put the picture on the right hand side
			newPrevLeft = dimm.left + dimm.width + othis.marginFromPic;
		}
		///end of left positioning
		
		
		
		//top positioning
		if(maxBottom < bottomPrevEnd){ //means there is not enough space on the bottom
			if(isOnMiddle == 1){//picture is on the midde of thumbnail
				var topPrevStart = dimm.top - loadedHeight - othis.prevPadding - othis.marginFromPic;
				if(topPrevStart > windowPositionY) newPrevTop = topPrevStart;
				else{//can not fit preview
					newPrevTop = topPrevStart;
				}
			}
			else{
				//move the picture to the top //check is the window height is not more then pic height
				newPrevTop = maxBottom - loadedHeight - othis.prevPadding - othis.marginFromPic;
			}
		}
		else{ //there is a space on the bottom
			if(isOnMiddle == 1){ //picture is on the midde of thumbnail
				var bottomPrevEnd = dimm.bottom + loadedHeight + othis.prevPadding + othis.marginFromPic;
				if(maxBottom > bottomPrevEnd){
					newPrevTop = dimm.bottom + othis.marginFromPic;
				} 
				else{
					var topPrevEnd = dimm.bottom + loadedHeight + othis.prevPadding + othis.marginFromPic; 
					if(windowSizeY > topPrevEnd){
						newPrevTop = dimm.bottom + othis.marginFromPic;
					} 
					else{
						var topPrevEnd = dimm.top - loadedHeight - othis.prevPadding - othis.marginFromPic;
						if(topPrevEnd > windowPositionY) newPrevTop = topPrevEnd;
						else{//can not fit preview
							newPrevTop = topPrevEnd;
						} 
					}
				}
			}
			else { 
				if (dimm.top < 0) {
					newPrevTop = 0;
					} else	newPrevTop = dimm.top;
			}
		}
		//end of top positioning
		
		//setup the position of the preview width and height
		othis.preview.setStyles({
			'visibility':'visible',
			'height':loadedHeight+'px',
			'width': loadedWidth+'px',
			'top': newPrevTop,
			'left': newPrevLeft,
			'border' : '1px solid #000',
			'padding' : '6px'
		});
		
		var rhText = new Element('div',{
			styles: {
				'position':'absolute',
				'top':(loadedDim.height - 18)+'px',
				'right': '10px',
				'color': '#fff',
				'font-size': '11px'
			}
		});
		rhText.setHTML('Robert Harding<br />World Imagery');
		rhText.injectInside(othis.preview.id);
		
		othis.loader.setStyle('visibility','hidden');
		
			}, //end of onload function
			onabort:function(){
				othis.preview.empty();
			},
			onerror: function(){
				othis.preview.empty();
			}
		
		}); //end of load picture
	},
	showPreviewEvnt: function(){
		this.show.addEvent('click',function(){
			if(this.show.checked){
				this.images.removeEvents('mouseenter');
				this.images.removeEvents('mouseleave');
				new Ajax('switchzoom.php',{
					method:'get',
					data: {
						'setShow':'1'
					}
				}).request();
				this.imgEvnt();
			}
			else{
				this.images.removeEvents('mouseenter');
				this.images.removeEvents('mouseleave');
				new Ajax('switchzoom.php',{
					method:'get',
					data: {
						'setShow':'0'
					}
				}).request();
			}
		}.bind(this));
	},
	imgEvnt: function(){
		var f1 = '';var othis = this;
		this.images.each(function(element){
			element.addEvents({
				'mouseenter': function(e){
					new Event(e).stop();
					othis.preview.empty();
					var title= new Element('div',{
						'class': 'prevDecr',
						'id':'prevDescr'
					});
					title.empty().appendText(this.title);
					othis.oldTitle = this.title;
					element.title = '';
					title.injectInside(othis.preview.id);
					f1 = othis.prevImages.delay(200,element,[othis]);
				},
				'mouseleave':function(){
					$clear(f1);
					element.title = othis.oldTitle;
					othis.preview.empty().setStyles({
						'visibility':'hidden',
						'height':'0px',
						'border' : 'none',
						'padding' : '0px'
					});
				}
			});
		});
	}
});

var b = new  imageZoom();

/***/	
});/**end of domready load*/
/***/
