// Easy News - jQuery plugin for News Slide by Michael Lo
// http://www.ezjquery.com
// Copyright (c) 2008 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!

$(document).ready(function(){
	if ($("#divSlideshow").length > 0)
	{
		$("#divSlideshowHiddenContent > div").addClass('news_style');

		$.init_news({
			firstname: "divSlideshowHiddenContent",
			secondname: "divSlideShowContent",
			fourthname: "divSlideshowBtn",
			newsspeed: '6000',
			imagedir: 'resources/pictures/slideshow/'
		});
	}
});

jQuery.extend({
	init_news: function(option){

		option = $.extend({
			firstname:"",
			secondname:"",
			fourthname:"",
			newsspeed:6000,
			mouseover:true,
			effectspeed:600,
			imagedir:""
		}, option);

		var firstname=option.firstname;
		var secondname=option.secondname;
		var fourthname=option.fourthname;
		var newsspeed=option.newsspeed;
		var mouseover=option.mouseover;
		var effectspeed=option.effectspeed;
		var imagedir=option.imagedir;

		effectspeed=parseInt(effectspeed,10);
		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,active,timer;
		mysize=$('#'+firstname+' .news_style').size();
		myfirst=$('#'+firstname+' .news_style').eq(0).html();
		active=0;
		$('#'+secondname).append(myfirst);

		$('#'+fourthname+' #news_next').click(function(){
			clearTimeout(timer);
			$(this).attr({src:mynextimg0});
			$('#'+fourthname+' #news_prev').attr({src:myprevimg});
			$('#'+fourthname+' #news_pause').attr({src:mypauseimg});
			active=active+1;
			if (active==mysize){
				active=0;
			}
			var mynum=active+1;
			var mynow=$('#'+firstname+' .news_style').eq(active).html();
			var nextnum=mynum;
			if (nextnum==mysize){
				nextnum=0;
			}

			$('#'+secondname).fadeOut(effectspeed,function(){
				$('#'+secondname).empty().html(mynow).fadeIn(effectspeed, function() {if ($.browser.msie) this.style.removeAttribute('filter');});
			});

			timer=setTimeout(function(){autonext(active)},newsspeed);
		});
		$('#'+fourthname+' #news_prev').click(function(){
			clearTimeout(timer);
			$(this).attr({
				src:myprevimg0
			});
			$('#'+fourthname+' #news_next').attr({
				src:mynextimg
			});
			$('#'+fourthname+' #news_pause').attr({
				src:mypauseimg
			});
			active=active-1;
			if (active<0){
				active=mysize-1;
			}
			var mynum=active+1;
			var myprevnum=mynum-2;
			if (myprevnum<0){
				myprevnum=myprevnum-1;
			}

			var mynow=$('#'+firstname+' .news_style').eq(active).html();
			$('#'+secondname).fadeOut(effectspeed,function(){
//				$('#'+secondname).empty();
//				$('#'+secondname).html(mynow);
//				$('#'+secondname).fadeIn(effectspeed);
				$('#'+secondname).empty().html(mynow).fadeIn(effectspeed, function() {if ($.browser.msie) this.style.removeAttribute('filter');});
			});


			timer=setTimeout(function(){autoprev(active)},newsspeed);
		});

		$('#'+fourthname+' #news_pause').click(function(){
			$(this).attr({
				src:mypauseimg0
			});
			$('#'+fourthname+' #news_next').attr({
				src:mynextimg
			});
			$('#'+fourthname+' #news_prev').attr({
				src:myprevimg
			});
			clearTimeout(timer);
		});
		//add by request
		if (mouseover===true)
		{
			$('#'+secondname).hover(function(){
				clearTimeout(timer);
				activechk=$('#'+fourthname+' #news_next').attr('src');
				activechkmore=$('#'+fourthname+' #news_prev').attr('src');
				$('#'+fourthname+' #news_pause').attr({
					src:mypauseimg0
				});
				$('#'+fourthname+' #news_next').attr({
					src:mynextimg
				});
				$('#'+fourthname+' #news_prev').attr({
					src:myprevimg
				});
			},function(){
				$('#'+fourthname+' #news_pause').attr({
					src:mypauseimg
				});
				if (activechk==mynextimg && activechkmore==myprevimg){
					timer=setTimeout(function(){autonext(active)},100);
				}
				if (activechk==mynextimg0){
					timer=setTimeout(function(){autonext(active)},100);
				}
				if (activechk==mynextimg && activechkmore==myprevimg0){
					timer=setTimeout(function(){autoprev(active)},100);
				}
			});
		}
		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;
		}
		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;
		}
		timer=setTimeout(function(){autonext(1)},newsspeed);
	}
});
