(function($) {
    var AsuraLayeredSlider = function(element,options){
    	return AsuraLayeredSlider.fn.create(element,options);
    }
    AsuraLayeredSlider.fn = AsuraLayeredSlider.prototype = {
    	version : "1.0.2.0",
    	create:function(element,options){
    		this.options = $.extend(true, $.fn.AsuraLayeredSlider.defaults, options);
    		
    		this.vars = {
    			slideCount:0,
    			lastSlide:0,
    			currentSlide:0,
    			residue:0,
    			fxs:[],
    			transitting:false
    		};
    		this.panel = {
    			timer:null
    		};
    		
    		this.panel.showScreen = $(element);
    		$(element).addClass("asura-layered-slider");
    		$(element).wrap("<div class='slide-wrapper'/>");
    		this.slider = $(element).parent();
    		this.items = $(element).children();
    		
    		this.setSlideSize();
    		if(this.options.pauseOnHover){
	        	this.setPauseOnHover();
	        }
    		
    		this.panel.timer = new AsuraRotateTimer(element,{
				duration:this.options.duration,
				oncomplete:$.proxy(function() {
				    try{
				    	this.vars.residue = 0;
				    	this.vars.lastSlide = this.vars.currentSlide;
	                	this.vars.currentSlide++;
	                	this.transit(); 
	                }catch(e){}
				    this.panel.timer.startTimer();
			    
				},this)
			});
			this.setHoverNavigation();
    		if(this.options.autoplay && this.items.length > 1){
	        	this.panel.timer.startTimer();
	        }
    	},
    	setSlideSize : function() {
    		var max_image_width = 0, max_image_height = 0;
    		var max_ratio = 0;
    		
    		this.items.each($.proxy(function(i,v){
    			this.vars.slideCount++;
				$(v).css({zIndex:i+5,left:i*this.options.slideDistance+'px'});
    			$(v).attr("rel",i);
    		},this));
    		if(this.vars.slideCount == 0)
    			return;
    		
    		
			if(this.vars.currentSlide >= 0 && this.vars.currentSlide < this.items.length){
				if(this.items.eq(parseInt(this.vars.currentSlide)).children("img")[0].complete){
					this.afterLoadImage();
				}
				else{
					this.items.eq(parseInt(this.vars.currentSlide)).children("img").load(
						$.proxy(this.afterLoadImage,this));
				}
				
			}
    		for(var i = 0; i < this.items.length; i++){
				if(i != this.vars.currentSlide){
					if(this.items.eq(i).children("img")[0].complete){
						this.items.eq(i).children("img").siblings().width(this.items.eq(i).children("img").width()+'px');
					}
					else{
						this.items.eq(parseInt(i)).children("img").load(
							function(){
								$(this).siblings().width($(this).width()+'px');
							});
					}
				}
			}
    	},
		afterLoadImage : function(){
			max_image_width = $("img",this.items.eq(this.vars.currentSlide)).width();
			max_image_height = $("img",this.items.eq(this.vars.currentSlide)).height();
			this.slider.width(parseInt(max_image_width+this.options.slideDistance*(this.items.length-1))+'px');
			this.slider.height(max_image_height+'px');
			this.items.eq(this.vars.currentSlide).children("img").siblings().width(max_image_width+'px');
			for(var i = this.vars.currentSlide+1; i < this.items.length; i++){
				this.items.eq(i).css({left:parseInt((i-1)*this.options.slideDistance+max_image_width)+'px'});
			}
		},
    	setClickSelector : function() {
    		var this_ = this;
    		this.items.click(function(e){
    			if(this_.vars.transitting)
    				return;
    			this_.stopProgress();
    			this_.vars.lastSlide = this_.vars.currentSlide;
    			this_.vars.currentSlide = $(this).attr("rel");
    			this_.transit();
    		});
    	},
    	setHoverNavigation : function(){
			
			var this_ = this;
    		this.items.bind("mouseenter",function(e){
    			this_.stopProgress();
    			this_.vars.lastSlide = this_.vars.currentSlide;
    			this_.vars.currentSlide = $(this).attr("rel");
				this_.transit();				
    		});
    	},
    	setPauseOnHover : function(){
	    	this.slider.hover(
				$.proxy(function(){
	                this.vars.paused = true;
	                this.pauseProgress();
	            },this), 
	            $.proxy(function(){
	                this.vars.paused = false;
	                this.vars.running = false;
	                //Restart the timer
	                if(this.options.pauseOnHover)
	                	this.startProgress();
	            },this));
	            
	    },
    	stopProgress : function() {
    		this.panel.timer.stopTimer();
    	},
    	pauseProgress : function(){
	    	this.panel.timer.pauseTimer();
	    },
    	startProgress : function() {
    		this.panel.timer.startTimer();
    	},
    	
    	transit : function(){
    		if(this.vars.currentSlide == this.vars.slideCount){ 
				this.vars.currentSlide = 0;
				if(this.options.slideshowEnd)
					this.options.slideshowEnd.call(this);
			}
			else if(this.vars.currentSlide < 0)
				this.vars.currentSlide = this.vars.slideCount - 1;
			this.items.removeClass('current-slide');
			
	    	this.items.stop();
	    	this.vars.transitting = true;
	    	var currItem = this.items.eq(this.vars.currentSlide);
	    	var descr = currItem.children("div");
	    	
	    	if(descr.length > 0){
	    		descr.css("top",currItem.height());
	    	}
			
	    	this.items.eq(this.vars.currentSlide).animate({left:this.vars.currentSlide*this.options.slideDistance+'px'},
	    			this.options.transSpeed,$.proxy(function(){
	    				this.vars.transitting = false;
	    				if(descr.length > 0){							
	    					//descr.animate({top:currItem.height()-descr.height()},200,function(){$(this).css("top","");});							
							descr.animate({top:currItem.height()-descr.height()},200);
							
	    		    	}
	    			},this));
			var image_width = this.items.eq(this.vars.currentSlide).children("img").width();
			
			for(var i = 0; i < this.vars.currentSlide; i++){
				
				this.items.eq(i).animate({left:(i*this.options.slideDistance)+'px'},this.options.transSpeed);
				this.items.eq(i).find("div").animate({top:currItem.height()-descr.height()+80},200);
				
			}
			
			for(var i = parseInt(this.vars.currentSlide)+1; i < this.items.length; i++){				
				this.items.eq(i).animate({left:((i-1)*this.options.slideDistance+image_width)+'px'},this.options.transSpeed);	
				this.items.eq(i).find("div").animate({top:currItem.height()-descr.height()+80},200);								
				
			}
			
			
    	}
    	
    };
    $.fn.AsuraLayeredSlider = function(options) {
    
        return this.each(function(key, value){
            var element = $(this);
            if (element.data('asuralayeredslider')) {
            	return element.data('asuralayeredslider');
            }
            
            var asuraLayeredSlider = new AsuraLayeredSlider(this, options);
            element.data('asuralayeredslider', asuraLayeredSlider );
        });

	};
	$.fn.AsuraLayeredSlider.defaults = {
		autoplay:true,
		pauseOnHover:true,
		duration:3000,
		transSpeed:300,
		slideDistance:30
	};
})(jQuery);

(function($) {
    AsuraRotateTimer = function(element,options){
    	return AsuraRotateTimer.fn.create(element,options);
    };
    
	function time() {
        return (new Date).getTime()
    }
	
    var lastTime = time();

    AsuraRotateTimer.fn = AsuraRotateTimer.prototype = {
    	version : "1.0.0.0",
    	create : function(element,options){
    		var defaults = {
    			width:32,
    			height:32,
    			radius:12,
    			duration:3000,
    			oncomplete:null
    		};
    		this.options = $.extend(true,defaults,options);
    		this.vars = {
    			supportCanvas:true,
    			startTime:0,
    			lastPauseResidue:0,
    			pause:true
    		};
    		
    		this.timer = null;
			
    		lastTime = this.vars.startTime = time();
		    
		    setInterval($.proxy(this.refreshTimer,this), 16);
		    setInterval($.proxy(this.refreshTimer,this), 30)
    	},
    	
    	stopTimer : function(){
    		this.vars.lastPauseResidue = 0;
    		this.vars.pause = true;
    	},
    	pauseTimer : function(){
    		this.vars.lastPauseResidue = time()-this.vars.startTime+this.vars.lastPauseResidue;
    		this.vars.pause = true;
    	},
    	startTimer : function(onComplete){
    		this.vars.startTime = time();
    		this.vars.pause = false;
    	},
    	refreshTimer : function(){
    		var h = time();
	        if (h - lastTime >= 30) {
	        	if(this.vars.pause == false){
		            var elapsetime = h-this.vars.startTime+this.vars.lastPauseResidue;
					
	                if(elapsetime >= this.options.duration){
	                	this.vars.lastPauseResidue = 0;
	                	this.vars.pause = true;
	                	if(this.options.oncomplete)
	                		this.options.oncomplete();
	                }
	            }
				
				lastTime = h;
	        }
		}
    };
})(jQuery);

