﻿/** 
/** zoommerMa to funkcja pozwalająca na powiększanie zdjęć w zadany sposób
/** @version 1.0
/** @autor Łukasz Staliś
/** @date 16-11-2009
/** @company MediaAmbassador
/** @rights All rights reserved
/**	
/** need template like this:
/**			
			<a class="jqzoom clearfix" href="to big image" > //must have no style 
				<img style="float: left;" src="to normal image">
			</a>
**/


jQuery.fn.extend({
	zoommerMa: function(type,data){
		var $ = jQuery;
		var manager= $().zoommerMa.manager?$().zoommerMa.manager:($().zoommerMa.manager = new Array());
		var name = "zoommerMa";
		if(!manager[name]){
			
			type = jQuery.extend({
				//all
				id: name,
				selector: $(this).selector,
				style: "default", //default, urban
				position:"right", //left,right,top, bottom
				width:400, 
				height:400,
				offsetTop:0,
				offsetLeft:0,
				disabled: false,
				loadingTxt:'Trwa ładowanie...',
				appendDOM: 'body',
				//all trigers
				afterInit:function(obj,callback){ callback(); },
				beforeShow:function(obj,callback){ callback(); },
				afterShow:function(obj){},
				beforeHide:function(obj,callback){ callback(); },
				afterHide:function(obj){},
				beforeDestruct:function(obj,callback){ callback(); },
				afterDestruct:function(obj){}			
			},type);
			
			if(type.style=="urban")manager[name]= new zoommerUrbanClass(type);
			
		} else {
			var obj = manager[name];
			switch(type){
				case 'show':obj.showHandler();break;
				case 'hide':obj.hideHandler();break;
				case 'reload':obj.reload(data);break;
				case 'disable':obj.disable(data);break;
				case 'enable':obj.enable(data);break;
				case 'destruct':obj.destruct(data);break;
			};
			
			return $("#"+name);
			
		};
		
		function zoommerUrbanClass(sets){
			var parent = this;
			this.id = sets.id;
			this.selector = sets.selector;
			this.loadingTxt = sets.loadingTxt;
			this.disabled= sets.disabled;
			this.appendDOM = sets.appendDOM;
			//protected vars
			this.linkRef = $(this.selector);
			this.imgRef = this.linkRef.children("img");
			this.zoomRef = null;
			this.zoomImgRef = null;
			this.infoRef = null;
			this.active = false;
			this.href = '';
			this.src = '';
			this.scale = 1;
			this.iterator = 1;
			this.imgWidth = 0;
			this.imgHeight = 0;
			this.zoomWidth = 0;
			this.zoomHeight = 0;
			this.lastPosX = 0;
			this.lastPosY = 0;
			this.lock =false;
			//methods
			this.disable = function(data){
				if(this.disabled==false){
					//parent.linkRef.unbind("mouseover", parent.showHandler);
					this.disabled = true;
				};
			};
			this.enable = function(data){
				if(this.disabled==true){
					//parent.linkRef.bind("mouseover", parent.showHandler);
					this.disabled = false;
				};
			};
			this.show = function(data){
				function showCallback(){
					parent.zoomRef.bind('mousemove',parent.moveHandler);
					parent.zoomRef.stop().show(0).animate({opacity:1},300,function(){
						parent.infoRef.hide(0);
					});					
					parent.afterShow(parent);
				};
				this.beforeShow(this,showCallback);
			};
			this.hide = function(data){
				function showCallback(){
					parent.zoomRef.unbind('mousemove',parent.moveHandler);
					parent.zoomRef.stop().animate({opacity:0},350,function(){
						parent.zoomRef.hide(0);
						parent.infoRef.hide(0);
					});			
					parent.afterHide(parent);
				};
				this.beforeHide(this,showCallback);
			};
			this.move = function(x,y){
				if(this.lock)return;
				if(this.lastPosX==x &&this.lastPosY==y)return;
				this.lock =true;
				var off=this.linkRef.offset();
				
				var fromTop=(off.top-y)*this.iterator;
				var fromLeft=(off.left-x)*this.iterator;
				
				if(fromTop>0)fromTop=0; else if(fromTop<(this.imgHeight-this.zoomHeight))fromTop = (this.imgHeight-this.zoomHeight); 
				if(fromLeft>0)fromLeft=0; else if(fromLeft<(this.imgWidth-this.zoomWidth))fromLeft = (this.imgWidth-this.zoomWidth); 
				
				this.zoomImgRef.css({top:parseInt(fromTop),left:parseInt(fromLeft)});
				this.lock =false;
			};
			this.destruct = function(){
				//parent.linkRef.unbind("mouseover", parent.showHandler);
				//parent.zoomRef.unbind("mouseout", parent.hideHandler);
				parent.zoomRef.unbind('mousemove',parent.moveHandler);
				$(window).unbind('resize',this.reflesh);
				$(this.zoomRef).remove();
				$(this.infoRef).remove();
				$().zoommerMa.manager[this.id] = null;				
			};
			//events Method
			this.afterInit = sets.afterInit;
			this.beforeShow = sets.beforeShow;
			this.afterShow = sets.afterShow;
			this.beforeHide = sets.beforeHide;
			this.afterHide = sets.afterHide;
			this.beforeDestruct = sets.beforeDestruct;
			this.afterDestruct = sets.afterDestruct;
			//event Handlers
			this.moveHandler = function(e){
					parent.move(e.pageX, e.pageY);					
					return false;
			};
			this.showHandler = function(){
				if(parent.active==true)return;
				parent.active=true;
				if(parent.src!=parent.imgRef.attr('src')){
					var img= new Image();
					img.onload=function(){
						parent.imgWidth=this.width;
						parent.imgHeight=this.height;
						parent.src = this.src;
						
						parent.zoomRef.css({
							width:this.width,
							height:this.height	
						});
						if(parent.href!=parent.linkRef.attr('href'))parent.reload(); else parent.show();
					};
					img.src= parent.imgRef.attr('src');
					
				} else if(parent.href!=parent.linkRef.attr('href')){
					parent.reload();
				} else {
					parent.show();
				};
				return false;
			};
			this.reload = function(){
				var img=new Image();
				img.onload=function(){
					parent.href=this.src;
					parent.scale=parent.imgWidth/this.width;
					parent.iterator = (1-parent.scale)/parent.scale;
					parent.zoomHeight=this.height;
					parent.zoomWidth=this.width;
					parent.zoomImgRef.attr('src',parent.href);
					
					if(parent.active==true)parent.show(); else parent.infoRef.hide(0);		
				};
				parent.infoRef.show(0);
				img.src=parent.linkRef.attr('href');
			};
			this.hideHandler = function(){
				if(parent.active==false)return;
				parent.active=false;
				parent.hide();				
				return false;
			};
			this.construct = function(sets){
				
				var off=parent.linkRef.offset();
				
				$('<div id="'+this.id+'" class="zoommerMa"><img src=""></div>').appendTo(this.appendDOM).css({dispaly:"none",opacity:0, top: off.top, left: off.left});
				
				$('<p class="zoommerLoading">'+this.loadingTxt+'</p>').appendTo(this.linkRef).css({display:'none'});
				
				//this.imgRef.css({zIndex:100});
				
				this.zoomRef = $("#"+this.id);
				this.zoomImgRef = $("#"+this.id+" img");
				
				this.infoRef = $(this.selector+" .zoommerLoading");
				
				//if(this.disabled==false)parent.linkRef.bind("mouseover", parent.showHandler);
				//parent.zoomRef.bind("mouseout", parent.hideHandler);
				
				function initCallback(){
					$(window).resize(function(){
						var off=parent.linkRef.offset();
						$('#zoommerMa').css({ top: off.top, left: off.left});
					});
				};			
				this.afterInit(this,initCallback);
			};
			this.construct(sets);
		};
		
		
		
	}
});
