// Easy News - jQuery plugin for News Slide by Michael Lo
// http://www.ezjquery.com
// Copyright (c) 2007 Michael Lo
// Dual licensed under the MIT and GPL licenses.
// http://www.opensource.org/licenses/mit-license.php
// http://www.gnu.org/licenses/gpl.html
// free for anyone like Jquery. Enjoy!
(function($) {
jQuery.extend({
init_plus: function(option){

 option = $.extend({
    firstname:"",
	secondname:"",
	thirdname:"",
	fourthname:"",
	newstitle: "",
	newsswitch: "",
	readthisarticle: "",	
	playingtitle:"Now Playing:",
	nexttitle:"Next News:",
	prevtitle:"Prev News:",
	readThisArticleText: "read this article",
	readAllArticlesText: "read all articles",	
	startingArticle: "",
	newsspeed:6000,
	isauto:1,
	imagedir:"",
	newscountname:"",
	disablenewscount:false
  }, option);
	
	var firstname=option.firstname;
	var secondname=option.secondname;
	var thirdname=option.thirdname;
	var fourthname=option.fourthname;
	var newstitle=option.newstitle;	//vu added specificially where we want to display the current news item title
	var newsswitch=option.newsswitch;	//vu added specificially where we want to display numbers for the articles
	var readthisarticle=option.readthisarticle;	//vu added specificially where we want to display numbers for the articles
	var readallarticles=option.readallarticles;	//vu added specificially where we want to display numbers for the articles
	var startingArticle = option.startingArticle; // vu added to start the rotation from any article
	var newsspeed=option.newsspeed;
	var isauto=option.isauto;
	var playingtitle=option.playingtitle;
	var nexttitle=option.nexttitle;
	var prevtitle=option.prevtitle;
	var readThisArticleText=option.readThisArticleText;
	var readAllArticlesText=option.readAllArticlesText; // vu added for all articles
	var imagedir=option.imagedir;
	var newscountname=option.newscountname;
	var disablenewscount=option.disablenewscount;
	if (newscountname){var news_sp=1;}if (disablenewscount===true){var news_dis=1;}
	if (!startingArticle) { startingArticle = 0};
	var myprevimg=$('#news_prev').attr('src'); if (!myprevimg){myprevimg=imagedir+'prev.gif';}
	var mynextimg=$('#news_next').attr('src'); if (!mynextimg){mynextimg=imagedir+'next.gif';}
	var mypauseimg=$('#news_pause').attr('src'); if (!mypauseimg){mypauseimg=imagedir+'pause.gif';}

	var myprevimg0=$('#news_prev0').attr('src'); if (!myprevimg0){myprevimg0=imagedir+'prev0.gif';}
	var mynextimg0=$('#news_next0').attr('src'); if (!mynextimg0){mynextimg0=imagedir+'next0.gif';}
	var mypauseimg0=$('#news_pause0').attr('src'); if (!mypauseimg0){mypauseimg0=imagedir+'pause0.gif';}


	var activechk,activechkmore,mysize,myfirst,myfirst_explain,active,timer;
	var numberedArticles = '';	//vu
	var docId = '';	// this contains the document id for the read more link
	
	var pageVar = '';	// this will keep us on the correct language page if we click on a news article number
	
	if (parent.document.URL.indexOf("?id=") != -1) { 
		var lengthOfSubstring = parent.document.URL.indexOf("&") - parent.document.URL.indexOf("?id=") - 4 ;
		pageVar = 'id=' + parent.document.URL.substr(parent.document.URL.indexOf("?id=") + 4, lengthOfSubstring) + '&';
	}
	
	mysize=$('#'+firstname+' .news_style').size();
	myfirst=$('#'+firstname+' .news_style').eq(startingArticle).html();
	myfirst_title = $('#'+firstname+' .news_style').eq(startingArticle).attr('rel');
	myfirst_explain=$('#'+firstname+' .news_style').eq((startingArticle + 1)).attr('rel');
	active=parseInt(startingArticle);

	
	$('#'+secondname).append('<table border=0 id=mm'+firstname+' class=news_move cellspacing="0" cellpadding="0"><tr id=insidetr'+secondname+'></tr></table>');
	$('#insidetr'+secondname).append('<td class=mytable id='+secondname+active+'>'+myfirst+'</td>');
	
	// if newscountname is set and disablenewscount is false, show where we are						
	if (news_sp===1 && news_dis!=1) {$('#'+newscountname).html('1/'+mysize);}
	// if newscountname is not set and disablenewscount is false, show where we are	
	if (news_sp!=1 && news_dis!=1){$('#'+thirdname).html('&nbsp;&nbsp;'+playingtitle+'1/'+mysize+'&nbsp;&nbsp;<br>');}
	
	$('#'+thirdname).append(nexttitle+myfirst_explain);
	
	// vu  start customising
	// put the title of the first article in the newstitle id
	$('#'+newstitle).append(myfirst_title);	
	// figure out the number of Articles
	for (i=1; i <= mysize; i++) {
		docId = $('#'+firstname+' .news_style').eq(i-1).attr('id'); 
		parentId = $('#'+firstname+' .news_style').eq(i-1).attr('parent');
		if (i == (parseInt(startingArticle) + 1)) {
			numberedArticles += '<span class="activeNumberedArticle">' + i + '</span>';
			readArticle = '<a href="index.php?id=' + docId + '">' + readThisArticleText + '</a>';
			allArticles = '<a href="index.php?id=' + parentId + '">' + readAllArticlesText + '</a>';
		}
		else {
			numberedArticles += '<a href="index.php?' + pageVar + 'startingArticle=' + (i - 1) + '"><span class="numberedArticle">' + i + '</span></a>';
		}
		
	}	
	// put the numbers in the appropriate id
	$('#'+newsswitch).html(numberedArticles);		
	$('#'+readthisarticle).html(readArticle);
	$('#'+readallarticles).html(allArticles);
	
	//$('#'+thirdname).html('&nbsp;&nbsp;'+playingtitle+'1/'+mysize+'&nbsp;&nbsp;<br>');
	//$('#'+thirdname).append(nexttitle+myfirst_explain);

/////////////////////////////////////////Next Click////////////////////////////////////////////////////////////////////////////////
	$('#'+fourthname+' #news_next').click(function(){
		clearTimeout(timer);
		$(this).attr({src:mynextimg0});
		$('#'+fourthname+' #news_prev').attr({src:myprevimg});
		$('#'+fourthname+' #news_pause').attr({src:mypauseimg});
		var need_to_delete='#'+secondname+active;
		
		active=active+1;
		if (active==mysize)	{active=0;}
		var mynum=active+1;
		var nextnum=mynum;
		var temp=$('#'+firstname+' .news_style').eq(active).html();
		if (nextnum==mysize){nextnum=0;}
		var mynow_explain=$('#'+firstname+' .news_style').eq(nextnum).attr('rel');
		var mynow_title=$('#'+firstname+' .news_style').eq(active).attr('rel'); //vu add in the title
		var numberedArticles = '';	
			
		$('#insidetr'+secondname).append('<td class=mytable id='+secondname+active+'>'+temp+'</td>');
		
		//$('#'+thirdname).html('&nbsp;&nbsp;'+playingtitle+''+mynum+'/'+mysize+'&nbsp;&nbsp;<br>');
		//$('#'+thirdname).append(nexttitle+mynow_explain);
		if (news_sp===1 && news_dis!=1) {$('#'+newscountname).html(mynum+'/'+mysize);$('#'+thirdname).html(nexttitle+mynow_explain);}				
		if (news_sp!=1 && news_dis!=1){$('#'+thirdname).empty().html('&nbsp;&nbsp;'+playingtitle+''+mynum+'/'+mysize+'&nbsp;&nbsp;<br>');$('#'+thirdname).append(nexttitle+mynow_explain);}				
		if (news_dis===1) {$('#'+thirdname).html(nexttitle+mynow_explain);}	
		
		
		$('#'+newstitle).html(mynow_title); //vu add in the title
		// figure out the number of Articles
		for (i=1; i <= mysize; i++) {
			docId = $('#'+firstname+' .news_style').eq(i-1).attr('id'); 
			parentId = $('#'+firstname+' .news_style').eq(i-1).attr('parent');
			if (i == (active + 1)) {
				numberedArticles += '<span class="activeNumberedArticle">' + i + '</span>';
				readArticle = '<a href="index.php?id=' + docId + '">' + readThisArticleText + '</a>';
				allArticles = '<a href="index.php?id=' + parentId + '">' + readAllArticlesText + '</a>';
			}
			else {
				numberedArticles += '<a href="index.php?' + pageVar + 'startingArticle=' + (i - 1) + '"><span class="numberedArticle">' + i + '</span></a>';
			}
		}	
		// put the numbers in the appropriate id
		$('#'+newsswitch).html(numberedArticles);		
		$('#'+readthisarticle).html(readArticle);
		$('#'+readallarticles).html(allArticles);
						
		var whatdist=$('#'+secondname+active).css("width");
		whatdist=parseInt(whatdist,10);
		$('#mm'+firstname).animate({left:-whatdist},1000,function(){
			$(need_to_delete).remove();
			$('#mm'+firstname).css({left:'0'});								
		});
		if (isauto==1)
		{
			timer=setTimeout(autonext,newsspeed,active);
		}				

	});
/////////////////////////////////////////prev click///////////////////////////////////////////////////////////////////////////////////
	$('#'+fourthname+' #news_prev').click(function(){
		clearTimeout(timer);
		$(this).attr({src:myprevimg0});
		$('#'+fourthname+' #news_next').attr({src:mynextimg});
		$('#'+fourthname+' #news_pause').attr({src:mypauseimg});
		var need_to_delete='#'+secondname+active;
		active=active-1;
		if (active<0){active=mysize-1;}
		var mynum=active+1;
		var myprevnum=mynum-2;
		if (myprevnum<0){myprevnum=mysize-1;}							
					
		var temp=$('#'+firstname+' .news_style').eq(active).html();
		var mynow_explain=$('#'+firstname+' .news_style').eq(myprevnum).attr('rel');
		var mynow_title=$('#'+firstname+' .news_style').eq(active).attr('rel'); //vu add in the title
		
		$('#insidetr'+secondname).prepend('<td class=mytable id='+secondname+active+'>'+temp+'</td>');
		
		var whatdist=$('#'+secondname+active).css("width");
		whatdist=parseInt(whatdist,10);
		$('#mm'+firstname).css({left:-whatdist});
		
		//$('#'+thirdname).html('&nbsp;&nbsp;'+playingtitle+''+mynum+'/'+mysize+'&nbsp;&nbsp;<br>');
		//$('#'+thirdname).append(prevtitle+mynow_explain);
		if (news_sp===1 && news_dis!=1) {$('#'+newscountname).html(mynum+'/'+mysize);$('#'+thirdname).html(prevtitle+mynow_explain);}				
		if (news_sp!=1 && news_dis!=1){$('#'+thirdname).empty().html('&nbsp;&nbsp;'+playingtitle+''+mynum+'/'+mysize+'&nbsp;&nbsp;<br>');$('#'+thirdname).append(prevtitle+mynow_explain);}				
		if (news_dis===1){$('#'+thirdname).html(prevtitle+mynow_explain);}
		
		$('#mm'+firstname).animate({left:0},1000,function(){
			$(need_to_delete).remove();
		});
		if (isauto==1)
		{
		timer=setTimeout(autoprev,newsspeed,active);
		}

	});
//////////////////////////Pause Click//////////////////////////////////////////////////////////////
	$('#'+fourthname+' #news_pause').toggle(function(){
		$(this).attr({src:mypauseimg0});
		$('#'+fourthname+' #news_next').attr({src:mynextimg});
		$('#'+fourthname+' #news_prev').attr({src:myprevimg});
		clearTimeout(timer);
	}, function(){
		timer=setTimeout(autonext,1,active);
		timer=setTimeout(autonext,newsspeed,active);
	});
	
//////////////////////////Addtion Function//////////////////////////////////////////////////////////////
	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); 
	}; 

//////////////////////////Auto Next//////////////////////////////////////////////////////////////
	function autonext(q){
		if (!q){q=0;}
		myend=$('#'+firstname+' .news_hide_style').size();
		myend=myend-1;
		if (q >= myend){q=0;}
		$('#'+fourthname+' #news_next').eq(q).click();
		q=q+1;					
	}
//////////////////////////Auto Prev//////////////////////////////////////////////////////////////
	function autoprev(q){
		if (!q){q=0;}
		myend=$(".news_hide_style").size();
		myend=myend-1;
		if (q >= myend){q=0;}
		$('#'+fourthname+' #news_prev').eq(q).click();
		q=q+1;					
	}
//////////////////////////Init AutoPlay//////////////////////////////////////////////////////////////
	if (isauto==1) {
		timer=setTimeout(autonext,newsspeed,1);
	}

}
});
})(jQuery);
