/*
 * Copyright (c) OutWit Technologies 2007-2017.
 * All rights reserved.
 */

var step=0.0;

var EffectManager ={

	image:null,
	callback:null,
	fadeInActive:null,
	fadeOutActive:null,
	waveActive:null,

	init:function( element, callbackFunction ){
		// Collision

		coreKit.timerService.cancelTimer(EffectManager.fadeInActive);
		EffectManager.fadeInActive=null;
		coreKit.timerService.cancelTimer(EffectManager.fadeOutActive);
		EffectManager.fadeOutActive=null;

		this.image = element;
		this.callback = callbackFunction;
	},
	
	fadeIn:function(){
		try{
			if(EffectManager.image.style.opacity<1){
				EffectManager.image.style.opacity=parseFloat(EffectManager.image.style.opacity) + 0.3;
				EffectManager.fadeInActive=coreKit.timerService.setTimer(5, null, EffectManager.fadeIn);
			}
		}
		catch(e){
			alert("FadeIn:" + e);
		}
			
	},
	
	fadeOut:function( opacity ){
		try{

			if(parseFloat(EffectManager.image.style.opacity)>0.0){
				EffectManager.image.style.opacity=parseFloat(EffectManager.image.style.opacity) - 0.3;
				EffectManager.fadeOutActive=coreKit.timerService.setTimer(5, null, EffectManager.fadeOut);
			}
			else{
				EffectManager.callback();
			}
		}
		catch(e){
			alert("fadeOut:" + e);
		}
	},
	
	zoomIn:function(){
		try{
			if(EffectManager.image){
				var width = parseInt(EffectManager.image.width);
				var maxWidth = parseInt(EffectManager.image.style.maxWidth);
				var height = parseInt(EffectManager.image.height);
				var maxHeight = parseInt(EffectManager.image.style.maxHeight);
				
				if(width<maxWidth && height<maxHeight){
					width += width/100*10;
					height+= height/100*10;
					
					//Prevent to overhead max size
					while(width>maxWidth || height>maxHeight){
						width -= width/100*1;
						height-= height/100*1;
					}
					
					EffectManager.image.style.width = width + "px";
					EffectManager.image.style.height = height + "px";
				}
			}
		}
		catch(e){
			alert("zoomIn:" + e);
		}
	},
	
	zoomOut:function(){
		try{
			if(EffectManager.image){
				var width = parseInt(EffectManager.image.width);
				var minWidth = parseInt(EffectManager.image.style.minWidth);
				var height = parseInt(EffectManager.image.height);
				var minHeight = parseInt(EffectManager.image.style.minHeight);
				
				
				if(width>minWidth && height>minHeight){
					width -= width/100*10;
					height-= height/100*10;
					
					EffectManager.image.style.width = width + "px";
					EffectManager.image.style.height = height + "px";
				}
			}
		}
		catch(e){
			alert("zoomOut:" + e);
		}
	},
	
	
	reflect:function(canvas, image, width, height){
		try{

			canvas.width = width;
			height = Math.min(60, height);
			canvas.height = height;
			var ctx = canvas.getContext("2d");
			ctx.save();
			ctx.fillStyle = '#acacac';
			ctx.scale(1, 0.4);
			ctx.fillRect(0,0,width,height);
			ctx.translate(0,height-1);
			ctx.scale(1,-1);
			ctx.drawImage(image, 0, 3,width-1, height);
			ctx.restore();
			ctx.globalCompositeOperation = "destination-out";
			var gradient = ctx.createLinearGradient(0, 0, 0, 20);

			gradient.addColorStop(1, "rgba(255, 255, 255, 1.0)");
			gradient.addColorStop(0, "rgba(255, 255, 255, 0.8)");
			ctx.fillStyle = gradient;
			
			ctx.fillRect(0, 0, width, height);
			
			
		}
		catch(e){
			//alert("reflect:" + e);
		}
	},
	
	wave:function(canvas, image, width, height){
			try{

				height = parseInt(height);
				width = parseInt(width);

				height = Math.max(canvas.height, height);
				canvas.height = height;
				canvas.width = width;

				var ctx = canvas.getContext("2d");
				ctx.fillStyle = "#cacaca";
				ctx.fillRect (0, height, width, height);
				ctx.scale(1,0.4);
				for(var i=0.0;i<height;i+=1.0)
						ctx.drawImage(image,0,i,40,1,Math.sin(2*(step+i/20.0))*(i/1.3),i,width,1);

				ctx.globalCompositeOperation = "destination-out";
				var gradient = ctx.createLinearGradient(0, 0, 0, 50);

				gradient.addColorStop(1, "rgba(255, 255, 255, 1.0)");
				gradient.addColorStop(0, "rgba(255, 255, 255, 0.8)");
				ctx.fillStyle = gradient;
			
				ctx.fillRect(0, 0, width, height);
				step+=0.1;
	        	EffectManager.waveActive = coreKit.timerService.setTimer(35, null, EffectManager.wave, null, canvas, image, width, height);
			}
			catch(e){
				
			}


	},

	glow:function(canvas, image, width, height){
		try{
			canvas = document.createElement("canvas");
			
			canvas.style.height = image.style.maxHeight;
			canvas.style.width = image.style.maxWidth;
			canvas.height = parseInt(image.style.maxHeight);
			canvas.width = parseInt(image.style.maxWidth);
			canvas.style.marginLeft = image.style.marginLeft;
			canvas.setAttribute("src",image.getAttribute("src"));
			
			canvas.addEventListener("mouseover",function(){
				var ctx = this.getContext("2d");
				canvas.cloneImg = this.parentNode.cloneNode(this);
				addMask(ctx,0,0,this.width,this.height,6,1,0);
			},false);
			
			canvas.addEventListener("mouseout",function(){
				canvas.parentNode.replaceChild(canvas.cloneImg, this);
			},false);
			
			canvas.addEventListener("click",function(){
				var ctx = this.getContext("2d");
				ctx.rotate(45 * Math.PI / 180);
			},false);
			
			var ctx = canvas.getContext("2d");
			ctx.save();
			
			ctx.clearRect(0,0,canvas.width,canvas.height);
			ctx.globalCompositeOperation = "source-over";
			ctx.drawImage(image,0,0,canvas.width,canvas.height);
			ctx.globalCompositeOperation = "destination-out";
			
			image.parentNode.replaceChild(canvas,image);
			
		}
		catch(e){
			//alert("glow:" + e);
		}
	}
}
