/**
 *
 */
(function($){
	$.fn.AdwiseSlider = function(options) {
		
		var defaults = {
			containerBackground: '.container_background',
			containerContent: '.container_content',
			containerPaging: '.container_paging',
			containerPanel: '.panel',
			
			// paging settings
			paging: true,
			holderPaging: '.paging',
			pagingItem: '',
			pagingActiveClass: 'active',
			
			// navigation settings, for previous and next
			navigation: false,
			navItem: '<a href="javascript:return false;"></a>',
			navPrevText: 'Previous',
			navPrevClass: 'panel_nav prev',
			navNextText: 'Next',
			navNextClass: 'panel_nav next',
			
			// transition effects
			pause: 7500,
			transition: 1300,
			transitionEffect: 'easeInOutExpo',
			
			// handles
			stopOnAction: true,
			doActionOnleave: true,
			autoPlay: true,
			fullwidth:false
		};
		
		var opts = jQuery.extend(defaults, options);
		var base = this;
		
		/**
		 * Initialize the slider
		 */
		this.init = function() {
			if(opts.pause < opts.transition) {
				alert('The pause is lower then the transition. The slider has been killed!');
				return false;
			}
			
			// figure out how many panels there are
			this.backgrounds = jQuery(opts.containerBackground, base);
			this.backgroundPanels = jQuery(opts.containerPanel, this.backgrounds);
			this.contents = jQuery(opts.containerContent, base);
			this.contentPanels = jQuery(opts.containerPanel, this.contents);
			
			// init slide
			this.preInitSlide();
			
			this.currentslide = 0;
			this.nextslide = 1;
			
			// when need to create pagings
			if(opts.paging) {
				this.paging = jQuery(opts.holderPaging, jQuery(opts.containerPaging, base));
				this.createPaging();
			}
			
			// when need to create navigations
			if(opts.navigation) {
				this.navigations = jQuery(opts.containerPaging, base);
				this.createNavigation();
			}
			
			// start sliding
			this.contentPanels.not(':first').css('display', 'none');
			
			jQuery(":first", this.paging).addClass(opts.pagingActiveClass);
			
			if(opts.autoPlay) {
				this.timer = setInterval(function() { base.startSlide() }, opts.pause);
			}
		}
		
		/**
		 * Creates the paging navigation
		 */
		this.createPaging = function() {
			for(var i=0; i<this.panelsize; i++) {
				var singleItem = jQuery(opts.pagingItem);	
					singleItem.html((i+1));
				this.paging.append(singleItem);
			}
			
			this.paging.children().each(function(index) {
				jQuery(this).mouseover(function() {
					base._gotoTab(index);
				});
			});
		}
		
		/**
		 * Creates the navigation next and previous buttons
		 */
		this.createNavigation = function() {
			var prevItem = jQuery(opts.navItem);
				prevItem.html(opts.navPrevText);
				prevItem.addClass(opts.navPrevClass);
				prevItem.click(function() {
					if(opts.autoPlay && opts.stopOnAction) {
						clearInterval(base.timer);
					}
					
					base.nextslide = (base.currentslide - 1);
					if(base.currentslide == 0) {
						base.nextslide = (base.panelsize - 1);
					}
					base.startSlide();
				});
			
			var nextItem = jQuery(opts.navItem);
				nextItem.html(opts.navNextText);
				nextItem.addClass(opts.navNextClass);
				nextItem.click(function() {
					if(opts.autoPlay && opts.stopOnAction) {
						clearInterval(base.timer);
					}
					
					base.startSlide();
				});
			
			this.navigations.append(prevItem);
			this.navigations.append(nextItem);
			this.navigations.prev = prevItem;
			this.navigations.next = nextItem;
		}
		
		/**
		 * The startpoint of the slider, initializes all
		 */
		this.startSlide = function() {
			this._slideBackgrounds();
			this._fadeContents();
			
			if(opts.paging) {
				this._setPaging();
			}
			
			this._setSlidePosition();
		}
		
		/**
		 * Will slide the backgrounds for a single time 
		 */
		this._slideBackgrounds = function() {
			this.backgrounds.animate({
					left: "-" + (base.nextslide * base.slidewidth)
				},
				opts.transition, 
				opts.transitionEffect
			);
		}
		
		/**
		 * Will fade out the current content part 
		 * and fade in the next content part
		 */
		this._fadeContents = function() {
			this.contentPanels.eq(this.currentslide).fadeOut(1000);
			this.contentPanels.eq(this.nextslide).fadeIn(1000);
		}
		
		/**
		 * Wil setup the paging with active class
		 */
		this._setPaging = function() {
			this.paging.children().removeClass(opts.pagingActiveClass);
			jQuery(":eq(" + this.nextslide + ")", this.paging).addClass(opts.pagingActiveClass);
		}
		
		/**
		 * Will update the slide positions
		 */
		this._setSlidePosition = function() {
			this.currentslide = this.nextslide;
			this.nextslide++;
			if(this.nextslide == this.panelsize) {
				this.nextslide = 0;
			}
		}
		
		/**
		 * The callback for the onclicks in the slider
		 * Will stop the auto-sliding and will goto the
		 * slide wich is clicked
		 */
		this._gotoTab = function(index) {
			if(opts.stopOnAction) {
				clearInterval(this.timer);
			}
			this.nextslide = index;
			this.startSlide();
		}
		
		/**
		 *
		 */
		this.preInitSlide = function() {
			base.panelsize = base.backgroundPanels.size();
			if(base.backgroundPanels.size() < base.contentPanels.size()) {
				base.panelsize = base.contentPanels.size();
			}
			
			if(opts.fullwidth){
				base.slidewidth = jQuery(window).width();
			}else{
				base.slidewidth = jQuery(this).width();
			}
			base.totalwidth = base.slidewidth * base.panelsize;
			
			base.backgrounds.width(base.totalwidth);
			base.backgroundPanels.each(function(index) {
				jQuery(this).width(base.slidewidth);
			});
		}
		
		// ----------------
		// init for every slide in the list
		this.each(function() {
			base.init();
			
			jQuery(window).resize(function() {
				base.preInitSlide();
				var newLeftPos = base.currentslide * base.slidewidth;
				base.backgrounds.css('left', "-" + newLeftPos + "px");
			});
		});
		
		return this;
	};  
})(jQuery);
