//07.09.2011
// add: if no preloader id show slides direct
// add: if no more then 1 teaser dont show arrows


jQuery(document).ready(function($){

	$.fn.teaser = function(opt){
	
		opt = $.extend( {}, $.fn.teaser.opt, opt );
	
		return this.each(function(){
		
			var target	= $(this);
			var total	= $(target).children('a').size();
			var items_arr = new Array();
			var timer;
			var fade_time = opt.fade_time * 1000;
			var pause = opt.pause * 1000;
			var c_index = 0;
			
			var on_pause = 0;
			
			function real_count(tar){
				
				var l = 0;
				$.each(tar, function(){
					l++;
				});
				
				return l;
			};

			function collect_data(){

				$(target).children('a').each(function(index){

					//force cache
					
					var img_src =  $(this).children('img').attr('src') + '?' + (new Date()).getTime();				
					var href = $(this).attr('href');
					
					$(this).children('img').attr('src', img_src);
					
					var that = this;
					
					$(this).children('img').load(function(){

						items_arr.push(
							{
								href	:	href,
								bg		:	img_src,
								realInd	:	index
							}
						);

						
						$(that).remove();

						if(real_count(items_arr) == total){	//All items are ready so Go go go!
							prepare_slides();						
						}
							
					}); //End load()
					
				});
			}
			
			function prepare_slides(){
			
				$.each(items_arr, function(i, val){
					
					$(target).append('<div class="'+opt.slides_class+' hidden_slider" id="slider_'+val['realInd']+'" style="display: none; z-index: '+(val['realInd'] + 2)+'; background-image: url('+val['bg']+')"></div>');
	
				});
				
				if(opt.preloader_id) {
					$('#'+opt.preloader_id).fadeOut(500, function(){
						slides_ready();
					});
				}else{
					slides_ready();
				}
				
			}
			
			
			function slides_ready(){			
			
				if(opt.href_id){
					$('#'+opt.href_id).mouseenter(function(){

						if(timer){
							clearTimeout(timer);
						}

						if(real_count(items_arr) > 1){
							$('.'+opt.arrows_class).show();
						}
						
					}).mouseleave(function(){

						play(c_index);

						if(real_count(items_arr) > 1){
							$('.'+opt.arrows_class).hide();
						}
					});
				}
				
				if(real_count(items_arr) > 1){ /* if more then 1 teaser -> show arrows on mouseenter */

					$('.'+opt.arrows_class).mouseenter(function(){
						$('.'+opt.arrows_class).show();
					}).mouseleave(function(){
						$('.'+opt.arrows_class).hide();
					});
				}
				
				play(0); //0 - Begin with first elm (realInd)		
			}
			
			function play(current_index){
			
				//for pause
				c_index = current_index;
				
				//found current elm
				var current;
				$.each(items_arr, function(i, val){

					if(val['realInd'] == current_index){
						current = val;
					}				
				});

				//Bind left arrow action
				$('#'+opt.left_arrow).unbind('click').click(function(evn){

					evn.preventDefault();
					clearTimeout(timer);
					
					$('#slider_'+current['realInd']).stop().css(
						{
							'opacity': 1,
							'z-index': (current['realInd'] + 2)
						}
					).addClass('hidden_slider');



					var prev = (c_index > 0) ? (c_index - 1) : (total - 1);
					play(prev);
				});

				//Bind right arrow action
				$('#'+opt.right_arrow).unbind('click').click(function(evn){
					
					evn.preventDefault();
					clearTimeout(timer);
					

					$('#slider_'+current['realInd']).stop().css(
						{
							'opacity': 1,
							'z-index': (current['realInd'] + 2)
						}
					).addClass('hidden_slider');


					
					var next = ((c_index + 1) == total) ? 0 : (c_index + 1);

					play(next);
				});

				//Bind new link href
				$('#'+opt.href_id).attr('href', current['href']);

							
				$('#slider_'+current['realInd']).css('z-index', (total + 10)).fadeIn(fade_time, function(){
				
				
					$(this).removeClass('hidden_slider');

					$('.hidden_slider').css({'display' : 'none'});

					$(this).addClass('hidden_slider').css('z-index', (current['realInd'] + 2));
					
					//start timer
					clearTimeout(timer);
					
					if(total > 1){
						timer = setTimeout(function(){
						
							if((current_index+1) == total)
								current_index = 0;
							else
								current_index++;
								
								
							play(current_index);
						}, pause);
					}
					
				});
				
			} /* End Fn Play() */

			
			if(total > 0){

				collect_data();
				
				$(window).unload(function() {
					clearTimeout(timer);
				});
			}

		}); /* End of Each Object */
	};

	$.fn.teaser.opt = {
		slides_class	:	'slides',
		preloader_id	:	'',
		fade_time		:	1,
		pause			:	5,
		href_id			:	'',
		arrows_calss	:	'teaser-nav',
		left_arrow		:	'left-arrow',
		right_arrow		:	'right-arrow'
		
	};	
	


});
