(function($){ 
     $.fn.extend({  
         intro_teasers: function(options) {       
            $.fn.intro_teasers.defaults = {
					open_selector: 'div.thumbs_holder_outer > div.thumbs_holder > a.open',
					open_class: 'is_open',
					startscreen_class: 'startscreen',
					details_tobackground_class: 'details_background',
					thumbs_selector: 'div.thumbs_holder_outer > div.thumbs_holder > ul.thumbs li.item',
					thumbs_width: 6.667,
					animation_offset: 1.9,
					autorun_duration: 10000,
					autorun: true,
					panel_selector: 'div.thumbs_holder_outer > div.thumbs_holder',
					item_selector:	'ul.details > li.item'
			};
			
			// build main options before element iteration
			var opts = $.extend({}, $.fn.intro_teasers.defaults, options);
			
			return this.each(function() {
				
				var $base_container = $(this);
				var $is_open = true;
				var $items_count = $base_container.find(opts.thumbs_selector).size();
				var $animation_width =  ($items_count * opts.thumbs_width) + opts.animation_offset;
				var $panel_el = $base_container.find(opts.panel_selector);
				var $current_pos =  1;
				var $has_startscreen = false;
				var $start_el = $(opts.item_selector+"."+opts.startscreen_class);
				var $details_holder_el = $base_container.find(opts.item_selector).parent();
				
				if ($start_el.size() > 0) $has_startscreen = true;
				
				if  (opts.autorun && $items_count > 1 && $has_startscreen == false) {
					TIMEOUT = window.setTimeout(function() {
						openNextItem();
					}, opts.autorun_duration);
				}
				
				if ($has_startscreen == false) {
					startSlides();
				} else {
					$start_el.find('A.a_startscreen').click(function(e){
						startSlides();
						return false;
					});
					if  (opts.autorun) {
						TIMEOUT = window.setTimeout(function() {
							startSlides();
						}, opts.autorun_duration);
					}	
				}	
				
				
				$base_container.find(opts.open_selector).click(function(e){
					this.blur();
					if ($is_open == false) {
						open_thumbs();
						$(this).addClass(opts.open_class);
					} else {
						close_thumbs();
						$(this).removeClass(opts.open_class);	
					}	
					return false;
				});
				
				$base_container.find(opts.thumbs_selector+" A").click(function(e){
					show_details_by_pos( extractId($(this).attr('class'), 'pos'));
					close_thumbs();
					return false;
				});
				
				function startSlides() {
					$panel_el.show();
					show_details_by_pos(1);
					close_thumbs();
				}
				
				function open_thumbs() {
					setDetailsToBackground();
					$panel_el.animate({
										right: 0
									  }, 100, function() {
												$is_open = true;
										});
				}
				
				function close_thumbs() {
					$panel_el.animate({
										right: "-"+$animation_width+"em"
									  }, 100, function() {
												$is_open = false;
												setDetailsToForeground();
										});
				}
				
				function show_details_by_pos(pos) {
					
					if  (opts.autorun) window.clearTimeout(TIMEOUT);		
					
					$base_container.find(opts.item_selector).hide();
					
					var $item = $base_container.find(opts.item_selector+".pos_"+pos);
					var $item_bg = $item.find('.bg');
					var $item_content = $item.find('.content');
					$item_bg.css("opacity", 0.5);
					$item_content.hide();
					$item.show();
					$current_pos = pos;	
					$item_bg.animate({ 
										opacity: 1
									 }, 1000, function() { 
										$item_content.show();										
										if  (opts.autorun) {
											TIMEOUT = window.setTimeout(function() {
												openNextItem();
											}, opts.autorun_duration);
										}
										
									 });
								 
				}
				
				function openNextItem() {
					var next_pos = parseInt($current_pos) + 1;
					if (next_pos > $items_count) next_pos = 1;
					show_details_by_pos(next_pos);
				}
				
				function setDetailsToBackground() {
					$details_holder_el.addClass(opts.details_tobackground_class);
				}
				
				function setDetailsToForeground() {
					$details_holder_el.removeClass(opts.details_tobackground_class);
				}
				function extractId(txt,token){
					var parts = txt.split(" ");
					var val = "";
					for (var i=0; i<parts.length; i++) {
						var part = parts[i];
						var start = part.indexOf(token+"_");
						if (start != -1) {
							val = part.substring(start+(token.length+1), part.length);
						}

					}
					return val;
				}

            });
        } 
    }); 
})(jQuery);


