// Easy Slide - jQuery plugin for easy 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_slide: function(firstname,secondname,watermark,iscover,speed,autoplay,playtime,clickable,mytarget, link2) {
			//firstname -- 图片div块
			//secondname -- 轮换区域ID
			//watermark -- 是否显示水印
			
			if (!mytarget)
			{
				//默认为在新窗口打开
				mytarget='_blank';
			}
	
			var mywidth, myheight, timer, myend, myfirst, myfirstlink, myfirstsrc;
	 
			if (!speed){
				speed=1000;	//显示速度为1秒
			}
		
			if (!playtime){
				playtime=6000;	//轮播时间
			}
			
			//图片div的ID
			myfirst=$('#'+firstname+' img:eq(0)').clone();	//克隆每一张图片的一个副本
			myfirstlinkClone = $('#'+firstname+' a:eq(0)').clone();
			// myfirstlink=$(myfirst).attr('rel');	
			myfirstlink=$(myfirstlinkClone).attr('href'); //获得图片的引用地址属性
			// alert(myfirstlink);
			myfirstsrc=$(myfirst).attr('src');	//获得图片地址
				
			var myImage = new Image();	//图片对象
			myImage.src=$(myfirst).attr('src');	//设置图片源地址
			//mywidth = 300;
			mywidth=myImage.width;	//图片宽度
			myheight=myImage.height;	//图片高度

			var mylink = new Array();
			var chk,mysrc,myrel,mytitle,mycover;
			$('#'+secondname).append('<div id='+secondname+'show class=showbg ><div id='+secondname+'link align=center valign=top></div></div>');
			var q;
			function autop(){
				if (!q) {
					q=1;
				}
				myend=$('#'+secondname+'link a').size();
				//#showherelink	a的元素个数
				myend=myend-1;
				if (q > myend) {
					q=0;
				}
				//alert(q);
						$('#'+secondname+'link a').eq(q).click();						
						q=q+1;
			}
			
	$('#'+firstname+' img').each(function(e){
		var f=e+1;
		chk=$(this).attr('title');	//图片标题
		myrel=$(this).parent().attr('href');
		// alert(myrel);
		mysrc=$(this).attr('src');
			if (chk) {
				mytitle=chk;
			}
				if(chk===undefined){
						mytitle=$(this).attr("src");
						mytitle = mytitle.replace(new RegExp(/\.*\w*\//g),"");	//运用正则表达式提取标题
				}
				
				if(link2) {
					mylink[e]='<a href=# rel=\"'+mysrc+'\" link=\"'+myrel+'\" title=\"'+mytitle+'\" no=\"'+f+'\" onclick=\"return false;\" >'+f+'</a>';
				} else {						
				mylink[e]='<a href=# rel=\"'+mysrc+'\" link=\"'+myrel+'\" title=\"'+mytitle+'\" no=\"'+f+'\" onclick=\"return false;\" >'+f+'</a>';	
				}
				//alert(e);
				if (clickable!==1)
				{
					$('#'+secondname+'link').hide();
				}
				$('#'+secondname+'link').html(mylink.join(" "));
				
				if(link2) {
					$('#'+secondname+'link a').addClass("link2");
					//alert(link2);
				} else {
					$('#'+secondname+'link a').addClass("link_nor");
				}
					
			});
				
			if(link2) {
				$('#'+secondname+'link a:eq(0)').addClass("link2_act");
			} else {
			$('#'+secondname+'link a:eq(0)').addClass("link_act");	//第一个链接加上效果，和鼠标移上的效果一样
			}
			var mydis=$(mylink[0]).attr("title");
			
			//$('#'+secondname+'dis').html(mydis);	//图片描述
				
			if (watermark==1) {
			//加上水印，id为showherecc
			$('#'+secondname+'show').append('<em id='+secondname+'cc class=opa><h3>'+mydis+'</h3></em>');
			}
			
			if (myfirstlink) {
				//给图片加上链接
				$('#'+secondname+'show').append('<a href='+myfirstlink+' target='+mytarget+'><img src='+myfirstsrc+'></a>');
			} else {		
				$('#'+secondname+'show').append(myfirst);	//将副本插入			
			}
			
			$('#'+secondname+'show img').hover(function(){	
				clearTimeout(timer);	//当鼠标移上时，停止轮换
				$(this).fadeTo("slow",0.8);				
				$('#'+secondname+'cc').append('<em>Pause Now</em>');}, function(){	//设置鼠标移出时执行的操作
					$(this).fadeTo("slow",1);
					$('#'+secondname+'cc').html('<h3>'+mydis+'</h3>');
					clearTimeout(timer);
					timer=setTimeout(autop,100,1);			
				});	//end hover				
		
			$('#'+secondname+'link a').click(function(){
				var mm=this.rel;
				var nn=this.title;
				var kk=$(this).attr('link');
				var mynowplaying=$(this).text();
					mynowplaying=parseInt(mynowplaying,10);	//转化为10进制
					clearTimeout(timer);
					timer=setTimeout(autop,playtime,mynowplaying);
					$('#'+secondname+'link a').removeClass("link_act");
					if(link2) {
						$('#'+secondname+'link a').removeClass("link2_act");
						$(this).addClass("link2_act");
					} else {
						$(this).addClass("link_act");
					}
					$('#'+secondname+'show img:eq(0)').fadeTo("slow", 0.33, function(){
						$('#'+secondname+'show img:eq(0)').remove();
						if (iscover==1){
							$('#'+secondname+'show').append('<div id='+secondname+'cover class=gray ></div>');
							$('#'+secondname+'cover').css({width:mywidth,height:myheight});
						}
						if (kk!== 'undefined') {
							$('#'+secondname+'show').append('<a href='+kk+' target='+mytarget+' ><img src='+mm +' style="display:none"></a>');
						} else {
							$('#'+secondname+'show').append('<img src='+mm +' style="display:none">');
						}
						if (iscover==1){
							$('#'+secondname+'cover').animate({ 
								height: "0px" }, speed, function(){ $('#'+secondname+'cover').remove();
							});
						}

						$('#'+secondname+'show img').fadeIn('slow');				
						$('#'+secondname+'dis').html(nn);
						//watermark
						if (watermark==1){
							$('#'+secondname+'cc').html('<h3>'+nn+'</h3>');}
							$('#'+secondname+'show img').hover(function(){	
								clearTimeout(timer);
								$(this).fadeTo("slow",0.8);				
								$('#'+secondname+'cc').append('<em>Pause Now</em>');
							},function(){
								$(this).fadeTo("slow",1);
								$('#'+secondname+'cc').html('<h3>'+nn+'</h3>');
			 					myend=$('#'+secondname+'link a').size();
								if (mynowplaying>myend)
								{
									mynowplaying=0;
								}
								clearTimeout(timer);
								timer=setTimeout(autop,100,mynowplaying);			
							});						
						});
					});
					if(autoplay===1)
					{	
						//启动自动播放，每隔playtime进行播放
					 	timer=setTimeout(autop,playtime);
					}
				}
			});
})(jQuery);	
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); 
}; 