(function( $ ){
		
	var movingElements = 0;
		
	var timerInterval;
	var timerStart = 0;
	var timerFramelength = 10;
	var timerDuration = 4000;
	var timerActive = false;
	var timerPaused = false;
		
	var methods = {
		init : function (){
			$list = this.find('.banner_menu li');
			var count = $list.length;
			var h = this.innerHeight();
			var item_h = $list.outerHeight();
			var item_gap = 7;
			var increment = item_h + item_gap;
			var y = h - (count * increment - item_gap);
			$list.each(
				function(index){
					$(this).css("position","absolute").css("top",y+index*increment);
				}
			);
			$root = $(this);
			$list.click(
				function(){
					$root.pabBanner('jumpto',$(this));
				}
			);
			
			this.find('.banner_main').hover(
				function(){
					$root.pabBanner('enterbanner');
				},
				function(){
					$root.pabBanner('exitbanner');					
				}
			);
			
			$root.pabBanner('starttimer');
				
			return this;
		},
		enterbanner : function () {
			this.pabBanner('pausetimer');
		},
		exitbanner : function () {
			this.pabBanner('unpausetimer');			
		},
		showbanner : function (src,href){
			var current_src = $root.find(".banner_main img.bottom_image").attr("src");
			var $top_image = $root.find(".banner_main img.top_image");
			var $botttom_image = $root.find(".banner_main img.bottom_image");
			
			$root.find(".banner_main>a").attr("href",href);
			$top_image.attr("src",current_src);
			$top_image.css("opacity","1");
			$botttom_image.attr("src",src);
			$top_image.animate(
				{
					'opacity' : '0'
				},
				1000,
				'linear',
				function(){
					$top_image.attr("src",src);					
				}
			);
		},
		starttimer : function () {
			timerActive = true;
			if (!timerPaused){
				timerStart = new Date().getTime();
				window.clearInterval(timerInterval); // Just in case
				$root = this;
				timerInterval = window.setInterval(
					function(){
						$root.pabBanner('ticktimer');
					},
					timerFramelength
				);
			}
			return this;
		},
		pausetimer : function () {
			if (timerActive){
				pauseDuration = new Date().getTime() - timerStart;
			} else {
				pauseDuration = 0;
			}	
			window.clearInterval(timerInterval);
			timerPaused = true;
			return this;
		},
		unpausetimer : function () {
			timerPaused = false;
			if (timerActive){
				timerStart = new Date().getTime() - pauseDuration;
				window.clearInterval(timerInterval); // Just in case
				$root = this;
				timerInterval = window.setInterval(
					function(){
						$root.pabBanner('ticktimer');
					},
					timerFramelength
				);
			}
			return this;
		},
		cleartimer : function (){
			timerActive = false;
			window.clearInterval(timerInterval);
			$timebar = this.find('.timebar');
			$timebar.css("width","0%");
		},
		ticktimer : function(){
			var now = new Date().getTime();
			var percent = Math.floor( 10000 * (now-timerStart) / timerDuration) / 100; // round down to 2 decimal places
			if (percent>=100){
				percent = 100;
			}
			$timebar = this.find('.timebar').css("width",percent+"%");
			if (percent == 100){
				this.pabBanner('cleartimer');
				this.pabBanner('step');
			}
		},
		jumpto : function (target){						
			$list = this.find('.banner_menu li');
			var index = $list.index(target);
			var count = $list.length;
			if (index == count-1){
				return this;
			}
			
			$first = $list.first();
			$last = $list.last();
			$last.after(target);
			return $(this).pabBanner('slide');
		},
		slide : function (){				
			$root.pabBanner('cleartimer');
			
			$list = this.find('.banner_menu li');
			
			var count = $list.length;
			var h = this.innerHeight();
			var item_h = $list.outerHeight();
			var item_gap = 7;
			var increment = item_h + item_gap;
			var y = h - (count * increment - item_gap);
			
			movingElements = count;
			
			$root = $(this);
			$list.each(
				function(index){
					if (index==count-1){
						$(this)
							.css("z-index",1)
							.css("background-color","#00A2D0");
						$root.pabBanner('showbanner',$(this).attr("data-src"),$(this).attr("data-href"));
					} else {
						$(this).css("background-color","#32CFFF");
					}
					$(this).animate(
						{
							top: (y+index*increment)+'px'
							// width: (index==count-1 ? 130 : 120) + 'px'
						},
						1500,
						'swing',
						function(){
							$(this).css("z-index",0);
							if (--movingElements == 0){
								$root.pabBanner('starttimer');
							}
						}
					);					
				}
			);
								
			return this;
		},
		step : function(){		
			$list = this.find('.banner_menu li');
			$last = $list.last();
			
			$list.first().before($last);
			return this.pabBanner('slide');		
		}
	};
	
	$.fn.pabBanner = function(method) {
		// Method calling logic
		if ( methods[method] ) {
			return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.pabBanner' );
			return null;
		} 
	};
})( jQuery );

$(document).ready(function () {
	$('#banner').pabBanner('init');
});
