/**
 * jQuery Slider plugin
 * Designed to handle the swapping in and out of slides in a slider (ad box for instance)
 * @name jquery.slider-0.2.js
 * @author Immix Productions
 * @version 0.2
 * @date February 7th 2011
 * @category jQuery plugin
 * @copyright (c) 2011 Immix Productions
 * 
 * REQUIRED
 * jquery.imgpreload.min.js by Dimas Begunoff - http://www.farinspace.com
 * 
 * CHANGELOG
 * 0.2 - added preloader and 'waterfall' transition, fixed 'slide-left' bugs.
 */
(function($) {
	$.fn.extend({
		slider: function(options){
	
		var obj = null;		
		var advert_timer = false;
		var current_slide = 0;
		var slide_count = 0;
		var images_loaded = false;
		var strike = 0;
		
		var defaults = {
			autostart:true, 										 // true or false
			loading_image:'/images/loading.gif', // required, must be a valid path
			slide_height:'auto',   						   // auto or pixel number (may be required by ie6)
			easing:null, 									   	   // null or name of easing function
			speed:600, 										       // in milliseconds
			delay:6500,													 // in milliseconds
			strike_timeout:2000, 								 // in milliseconds
			transition:'fade', 									 // fade or slide-left
			nav_buttons_img:false, 							 // false if no nav buttons, or path to sprite image
			nav_button_size:'15px', 						 // in pixels
			nav_top:false, 											 // top coordinates for navigation buttons. false or pixels
			nav_left:false,	 										 // left coordinates for navigation buttons false or pixels
			nav_bottom: '13px', 								 // bottom coordinates for navigation buttons false or pixels
			nav_right: '17px'	  	 							 // right coordinates for navigation buttons false or pixels
		};
		var options = $.extend(defaults, options);
		
		return this.each(function(){
			obj = $(this);
			
			init_slider();
		});
		
		function init_slider(){
			obj.css({
				'display':'block',
				'position':'relative',
				'overflow':'hidden'
			});
			
			// loading animation
			if (options['loading_image']) {
		  	obj.append('<div class="loading"><img src="' + options['loading_image'] + '" /></div>');
		  	obj.find(".loading").css({
		  		'position': 'absolute',
		  		'top': '0px',
		  		'left': '0px',
		  		'bottom': '0px',
		  		'right': '0px',
		  		'width': '100%',
		  		'height': '100%',
		  		'background-color': '#000000',
		  		'z-index': '1000'
		  	}).find('img').hide().load(function(){
		  		$(this).css({
		  			'position': 'absolute',
		  			'display': 'block',
		  			'top': '50%',
		  			'left': '50%',
		  			'margin-top': '-' + ($(this).height() / 2) + 'px',
		  			'margin-left': '-' + ($(this).width() / 2) + 'px'
		  		}).show();
		  	});
		  }
			
			if (options['nav_buttons_img'] != false) {
		  	obj.append('<div class="nav"></div>');
		  	obj.find(".nav").css({
		  		'position': 'absolute',
		  		'width': 'auto',
		  		'height': '15px',
		  		'z-index': '700'
		  	});
		  	
		  	if (options['nav_top']) 
		  		obj.find(".nav").css("top", options['nav_top']);
		  	if (options['nav_left']) 
		  		obj.find(".nav").css("left", options['nav_left']);
		  	if (options['nav_bottom']) 
		  		obj.find(".nav").css("bottom", options['nav_bottom']);
		  	if (options['nav_right']) 
		  		obj.find(".nav").css("right", options['nav_right']);
		  }
			
			var preload_imgs = [];
			obj.find('li img').each(function() {
				preload_imgs.push($(this).attr('src'));
			});
			imgpreload(preload_imgs, function() {
				images_loaded = true;
			});
			
			obj.find("> li").each(function(index){
				if(options['slide_height'] == 'auto')
					options['slide_height'] = $(this).css('height');
				
				if(options['transition'] == "fade" || options['transition'] == "slide-left")
					$(this).hide();
				
				obj.find(".nav").append('<div id="slider_nav_' + (index + 1) + '"></div>');
				
				if (options['nav_buttons_img'] != false) {
					obj.find(".nav #slider_nav_" + (index + 1)).click(function(){
						clearTimeout(advert_timer);
						change_advert_slide(parseInt($(this).attr("id").substring(11)));
					});
				}
				
				if(options['transition'] == "fade") {
					$(this).css({
						'position':'absolute',
						'top':'0px',
						'left':'0px',
						'z-index':'500'
					});
				} else if(options['transition'] == "waterfall") {
					var zindex = (500 + index);
					$(this).css({
						'position':'absolute',
						'top':(options['slide_height'] * index) + 'px',
						'left':'0px',
						'z-index':zindex
					});
					slide_count = (index + 1);
					$(this).find("ul > li").each(function(index2){
						$(this).attr("slide", slide_count);
						
						$(this).css({
							'position':'absolute',
							'top':'0px',
							'left':'0px',
							'z-index':(zindex - index2)
						});
						
						if(slide_count < obj.find("> li ul > li").length)
							slide_count += obj.find("> li").length;
					});
				} else if(options['transition'] == "slide-left") {
					$(this).css({
						'position':'absolute',
						'top':'0px',
						'left':($(this).width() * index) + 'px',
						'z-index':'500'
					});
				}
			});
			
			obj.find(".nav div").css({
				'position': 'relative',
				'float': 'left',
				'width': options['nav_button_size'],
				'height': options['nav_button_size'],
				'margin-left': '4px',
				'background-image': 'url("' + options['nav_buttons_img'] + '")',
				'background-position': '3px 0px',
				'background-repeat': 'no-repeat',
				'cursor': 'pointer'
			});
			
			obj.find(".nav div").css({
	  		'background-position': '0px 0px'
	  	});
			$(obj.find("li").get(0)).show();
			current_slide = 1;
			obj.find(".nav #slider_nav_" + current_slide).css({
	  		'background-position': '-' + options['nav_button_size'] + ' 0px'
	  	});
			
			
			setTimeout(start_slider, 800);
		}		
		
		function start_slider() {
			if(images_loaded) {
				obj.find('.loading').fadeOut(200, function() {
					$(this).hide();
				});
				
				if (options['transition'] == "waterfall") {
			  	if (options['autostart'] && obj.find("li").length > 1) {
						current_slide = 0;
			  		setTimeout(function() { change_advert_slide(null, null); }, options['delay'] + 200);
			  	}	  
			  }
			  else {
			  	if (options['autostart'] && obj.find("li").length > 1) {
						current_slide = 1;
			  		setTimeout(function() { change_advert_slide(1, null); }, 200);
			  	}
			  }
				
			//	setInterval(check_slider, options['strike_timeout']);
			} else
				setTimeout(start_slider, 200);
		}
		
		function check_slider() {
	/*		if (!advert_timer) {
		  	strike++;
		  } else {
				strike = 0;
			}
			
			if (strike == 3) {
		  	advert_timer = setTimeout(function() { change_advert_slide(null, null); }, options['delay']);
				
				strike = 0;
		  }*/
		}
		
		function change_advert_slide(id, animate) {
			advert_timer = false;
			
			if (current_slide > 0 && id == current_slide) {
		  	advert_timer = setTimeout(function() { change_advert_slide(null, null); }, options['delay']);
		  	return false;
		  }
				
			if (id == null || id <= 0) 
	  		id = (current_slide = current_slide + 1);
			else
				current_slide = id;
	  	
			if(options['transition'] == "waterfall") {
				if (current_slide > slide_count) {
					id = (current_slide = 1);
				}
	  	} else if (id > obj.find("li").length) {
	  		id = (current_slide = 1);
		  }
			
			obj.find(".nav div").css({
	  		'background-position': '0px 0px'
	  	});
			
	  	obj.find(".nav #slider_nav_" + current_slide).css({
	  		'background-position': '-' + options['nav_button_size'] + ' 0px'
	  	});
			
			if (options['transition'] == "fade") {
				clearTimeout(advert_timer);
				advert_timer = setTimeout(function() { change_advert_slide(null, null); }, (options['delay'] + options['speed']));
				
		  	if (obj.find("li:visible").length > 0) {
		  		obj.find("li:visible").stop(true, true).fadeOut(options['speed']);
		  	}		
		  	
		  	$(obj.find("li").get(id - 1)).stop(true, true).fadeIn(options['speed']);
				
		  } else if(options['transition'] == "waterfall") {
									
		  	obj.find('li[slide="' + id + '"]').animate({
					top:'+=' + options['slide_height']
					
				}, {duration:options['speed'], easing:options['easing'], complete:function(){	
				
						if ($(this).siblings('li[slide="' + (id - obj.find("> li").length) + '"]').length > 0) 
							zindex = $(this).siblings('li[slide="' + (id - obj.find("> li").length) + '"]').css('z-index') - 1;
						else {							
							zindex = $(this).css('z-index');
							$(this).siblings('li').each(function() {
								if($(this).css('z-index') < zindex)
									zindex = ($(this).css('z-index') - 1);
							});
						}
					$(this).css({
					'z-index': zindex
					}).animate({
						'top':'-=' + options['slide_height']
					}, 0);
					
					if (Math.round($(this).attr("slide") / obj.find("> li").length) == ($(this).attr("slide") / obj.find("> li").length)) {
						clearTimeout(advert_timer);
				  	advert_timer = setTimeout(function() { change_advert_slide(null, null); }, options['delay']);
						
						if($(this).attr("slide") == (obj.find("li").length - obj.find("> li").length)) {
							obj.find("> li").each(function(index) {
								var zindex = (500 + index);
								$(this).css({
									'z-index':zindex
								});
								$(this).find("ul > li").each(function(index2){									
									$(this).css({
										'z-index':(zindex - index2)
									});
								});
							});											
						}
				  }
				  else 
				  	change_advert_slide();
				}});			
			} else if (options['transition'] == 'slide-left') {
		  	if (obj.find("li:visible").length > 0) {
					advert_timer = setTimeout(function() { change_advert_slide(null, null); }, (options['delay'] + options['speed']));
		  		obj.find("li:visible").stop().animate({left:'-' + $(this).width() + 'px'}, {duration:options['speed'], easing:options['easing'], complete:function(){
		  	}});
		  	}		  	
				$(obj.find("li").get(id - 1)).show().stop().css({left:$(this).width() + "px"}).animate({left:'0px'}, {duration:options['speed'], easing:options['easing']});
	  	}
		}
	}
	});
})(jQuery);	
