/*
*/
(function($) {
/**
 * Workmedia 10/2009
 *
 * $('div.class').wm_newsticker({
 *   title_tag: 'dt',
 *   body_tag: 'dd',
 *   ...
 * })
 * you can also use the metadata plugin
 *
 **/
	$.fn.wm_newsticker = function(options){
		// build main options before element iteration
		var opts = $.extend({}, $.fn.wm_newsticker.defaults, options);
		// iterate each matched element
		return this.each(function(){
			$this = $(this);
			// build element specific options
			var $o = $.meta ? $.extend({}, opts, $this.data()) : opts;
			// create news ticker
			$this.css($o.style);
			// create links
			$this.num_items = $(this).find($o.title_tag).length;
			var $links = $('<div class="newsticker_links">').css($o.links_style);
			for($i=1; $i<=$this.num_items; $i++){
				$link = $(this).find($o.title_tag).eq($i-1).find('a:first').attr('href');
				$links.append('&nbsp;<a href="' + $link + '"> ' + $i + ' </a>	 ');
			}
			$this.after($links);
			// stript text and keep image
			$this.find($o.body_tag).each(function(){
				$img = $(this).find('img:first').css('width','300px');
				//clear content and replace by img
				$(this).
					html($img).
					css({
						'position': 'absolute',
						'z-index': '1000',
						'top': '10px',
						'right': '10px'
					});
			});
			// format title
			$this.find($o.title_tag).
				css({
					'margin-right': '320px',
					'z-index': '1001',
					'position': 'absolute',
					'top': '10px',
					'left': '10px'
				})
			// hide all news
			$this.find($o.title_tag).hide();
			$this.find($o.body_tag).hide();
			// set timmer to change news
			$.fn.wm_newsticker.show_news($o, 0);
			$.fn.wm_newsticker.show_next($o);
			// mouse hover efects
			$this.hover(
				function(){
					// stop timer
					clearInterval($this.timer)
				},
				function(){
					// start timer
					clearInterval($this.timer);
					$.fn.wm_newsticker.show_next($o);
				}
			);
			$this.next().hover(
				function(){
					// stop timer
					clearInterval($this.timer);
				},
				function(){
					// start timer
					clearInterval($this.timer);
					$.fn.wm_newsticker.show_next($o);
				}
			);
			$this.next().find('a').hover(
				function(){
					// stop timer
					clearInterval($this.timer);
					$id = $.trim($(this).html())-1;
					$.fn.wm_newsticker.show_news($o, $id);
				}
			);
		});
	};
	$.fn.wm_newsticker.defaults = {
		title_tag: 'dt',
		body_tag: 'dd',
		style:{
			'width': '100%',
			'height': '150px',
			'overflow': 'hidden',
			'position': 'relative',
			'margin': '0',
			'padding': '0'
		},
		links_style:{
			'border': '2px solid #666',
			'border-width': '0 2px 2px 2px',
			'width': '100%'
		}
	};
	$.fn.wm_newsticker.show_next = function($o){
		$this.timer = setTimeout(
			function(){
				$id = $this.active_news+1;
				if($id>=$this.num_items){
					$id = 0;
				}
				$.fn.wm_newsticker.show_news($o, $id);
				$.fn.wm_newsticker.show_next($o);
			},
			3000
		);
	}
	$.fn.wm_newsticker.show_news = function($o, $id){
			// resize
			$this.find('dt:visible').each(function(){
				if($(this).height()>$this.height()){
					$this.height( $(this).height()+10 );
				}
			});
			// hide curent
			$this.find($o.title_tag).eq($this.active_news).fadeOut('fast');
			$this.find($o.body_tag).eq($this.active_news).fadeOut('fast');
			$this.next().find('a').css('background','#fff');
			// show next
			$this.find($o.title_tag).eq($id).fadeIn('fast');
			$this.find($o.body_tag).eq($id).fadeIn('fast');
			$this.next().find('a').eq($id).css('background','#ccc');
			// set curent 
			$this.active_news = $id;
		}
})(jQuery);
