jQuery.extend({
init_news: function(option){

 option = $.extend({
    firstname:"",
	secondname:"",
	thirdname:"",
	fourthname:"",
	nexttitle:"Next News:",
	newsspeed:20000,
	mouseover:true,
	clickable:true,
	effectspeed:300,
	imagedir:"",
	mynewscontent:'news_content',
	mynewstitle:'news_title',
	mytotaltitles:5,
	mytotitleitem:""
  }, option);
  
	var firstname=option.firstname;
	var secondname=option.secondname;
	var thirdname=option.thirdname;
	var fourthname=option.fourthname;
	var newsspeed=option.newsspeed;
	var nexttitle=option.nexttitle;
	var mouseover=option.mouseover;
	var clickable=option.clickable;
	var effectspeed=option.effectspeed;
	var imagedir=option.imagedir;
		
	var mynewscontent=option.mynewscontent;
	var mynewstitle=option.mynewstitle;
	var mynewstitle_selected=option.mynewstitle_selected;
	var mytotaltitles=option.mytotaltitles;
	var mytitleitem=option.mytitleitem;
	
	var news_content=option.news_content;
	var news_holder=option.news_holder;
		
	effectspeed=parseInt(effectspeed,10);
	var mynextimg=$('#news_next').attr('value'); if (!mynextimg){mynextimg=imagedir+'next';}
	var mypauseimg=$('#news_pause').attr('value'); if (!mypauseimg){mypauseimg=imagedir+'pause';}
	var mynextimg0=$('#news_next0').attr('value'); if (!mynextimg0){mynextimg0=imagedir+'next0';}
	var mypauseimg0=$('#news_pause0').attr('value'); if (!mypauseimg0){mypauseimg0=imagedir+'pause0';}

	var activechk,mysize,myfirst,myfirst_explain,active,timer;
			mysize=$('#'+firstname+' .'+mytitleitem).size();
			
			/**
			 * Initiate new (Start from first one)
			 */
			active=mysize-1;
			timer=setTimeout(autonext,1,1);
			
			/**
			 * Show
			 */
			$('#'+fourthname+' #news_next').click(function(){
			
				clearTimeout(timer);
				$(this).attr({value:mynextimg0});
				$('#'+fourthname+' #news_pause').attr({value:mypauseimg});
				
				active=active+1;
				if (active==mysize){active=0;}
				
				var mynow=$('#'+firstname+' .'+mytitleitem).eq(active).children('div.'+mynewscontent).html();
				var news_text=$('#'+firstname+' .'+news_content).eq(active).html();
				/**
				 * Next titles
				 */
				var nextnum=-1; // Set nextnum to active
				var next_title = [mytotaltitles];// Set titles array
				
				for (var i=0;i<mytotaltitles;i++) {
					nextnum=nextnum+1; // Increase to the next one.
					nextnum=nextnum%mysize;
					next_title[nextnum]=$('#'+firstname+' .'+mytitleitem).eq(nextnum).children('div.'+mynewstitle).html();
				}

				
				$('#'+secondname).fadeOut(effectspeed,function(){
					$('#'+secondname).empty();
					$('#'+secondname).html(mynow);
					$('#'+news_holder).empty();
					$('#'+news_holder).html(news_text);
					
					$('#'+thirdname).empty();
					var top;
					for (var i=0;i<mytotaltitles;i++) {
						top_s = 72 * i + 5;
						top = top_s + 5;
						if(i == active && i == 0) {
							$('#'+thirdname).append('<div style="height:67px; margin-bottom:5px;">'+'<div style="top:5px;" class="'+mynewstitle_selected+'_block bg0'+'_s'+'">'+next_title[i]+'</div>'+'</div>');
						} else if(i == active) {
							$('#'+thirdname).append('<div style="height:67px; margin-bottom:5px;">'+'<div style="top:'+top_s+'px;" class="'+mynewstitle_selected+'_block bg0'+'_s'+'">'+next_title[i]+'</div>'+'</div>');
						} else if(i == active-1) {
							$('#'+thirdname).append('<div style="margin:0px; top:'+top+'px;" class="'+mynewstitle+'_block bg0'+'">'+next_title[i]+'</div>');
						} else {
							$('#'+thirdname).append('<div style="top:'+top+'px;" class="'+mynewstitle+'_block bg0'+'">'+next_title[i]+'</div>');
						}
					}
					
					$('#'+secondname).fadeIn(effectspeed);
					
					/**
					 * Select other news item from short info.
					 */
					if (clickable===true)
					{		
						$('#'+thirdname+' .'+mynewstitle+'_block').click(function(){
							clearTimeout(timer);
							var index = ($('#'+thirdname+' .'+mynewstitle+'_block').index(this)) % mysize;
							timer=setTimeout(autonext,1,index);
							active=index-1;
							autonext();
						});
					}
						
				});
					
				timer=setTimeout(autonext,newsspeed,active);
			});

			
			/**
			 * Stop on mouse over
			 */
			if (mouseover===true)
			{					
				$('#'+secondname).hover(function(){
					clearTimeout(timer);
					activechk=$('#'+fourthname+' #news_next').attr('value');
					$('#'+fourthname+' #news_pause').attr({value:mypauseimg0});
					$('#'+fourthname+' #news_next').attr({value:mynextimg});
				},function(){
					$('#'+fourthname+' #news_pause').attr({value:mypauseimg});
						if (activechk==mynextimg0){timer=setTimeout(autonext,20000,active);}
				});
				
			}
			
			/**
			 * Time out
			 */
			var _st = window.setTimeout; 
			window.setTimeout = function(fRef, mDelay) { 
				if(typeof fRef == 'function'){ 
					var argu = Array.prototype.slice.call(arguments,2); 
					var f = (function(){ fRef.apply(null, argu); }); 
					return _st(f, mDelay); 
				} 
				return _st(fRef,mDelay); 
			}; 

			/**
			 * Display next automatically
			 */
			function autonext(){
				$('#'+fourthname+' #news_next').eq(0).click();
			}

			timer=setTimeout(autonext,newsspeed,1);


}
});