// myslide - $ plugin for easy slide by Michael Lo
// http://jquery.000webhost.info
// free for anyone like Jquery. Enjoy! Brady

	jQuery.extend({
init_slide: function(firstname,secondname,watermark,iscover,speed,autoplay,playtime,clickable){
	
	var mywidth,myheight,timer,myend;
	if (!speed){speed=1000;}
	if (!playtime){playtime=6000;}

	var myfirst=$('#'+firstname+' img:eq(0)').clone();
	var myfirstlink=$(myfirst).attr('rel');
	var myfirstsrc=$(myfirst).attr('src');
	var myImage = new Image();
	myImage.src=$(myfirst).attr('src');
	mywidth=myImage.width;
	myheight=myImage.height;
	
	var mylink = new Array()
	var chk,mysrc,myrel,mytitle,mycover;
	$('#'+secondname).append('<table border=0 width='+mywidth+'><tr><td width='+mywidth+' height='+myheight+' id='+secondname+'show class=showbg ></td></tr><tr><td id='+secondname+'link align=center valign=top></td></tr><tr><td align=center><span id='+secondname+'dis></span></td></tr></table>');
	
	// For each hidden image
	$('#'+firstname+' img').each(function(e){
		var f=e+1;
		chk=$(this).attr('title');
		myrel=$(this).attr('rel');
		mysrc=$(this).attr('src');
		// if title is specified
		if(chk){
			mytitle=chk;
		}
		// if no title specified, title becomes file name
		if(chk==undefined){
			mytitle=$(this).attr("src");
			mytitle = mytitle.replace(new RegExp(/\.*\w*\//g),"");
		}
		
		mylink[e]='<a href=# rel=\"'+mysrc+'\" link=\"'+myrel+'\" title=\"'+mytitle+'\" no=\"'+f+'\" >'+f+'</a>';
		if (clickable!==1){
			$('#'+secondname+'link').hide();
		}
		$('#'+secondname+'link').html(mylink.join(""));
		$('#'+secondname+'link a').addClass("link_nor");					 
	});
				
	$('#'+secondname+'link a:eq(0)').addClass("link_act");
	var mydis=$(mylink[0]).attr("title");
	$('#'+secondname+'dis').html(mydis);
	
	// add watermark
	if (watermark==1){
		$('#'+secondname+'show').append('<em id='+secondname+'cc class=opa><h3>'+mydis+'</h3></em>');
	}				
	
	// if the first image from the hidden div
	// show it right away w/ or w/o link
	if (myfirstlink){
		$('#'+secondname+'show').append('<a href='+myfirstlink+' target="_self"><img src='+myfirstsrc+'></a>');
	}else{		
		$('#'+secondname+'show').append(myfirst);			
	}
	
	// What to do on hover states
	$('#'+secondname+'show img').hover(function(){
		// hover over
		// stop timer, fade to 80%
		clearTimeout(timer);
		$(this).fadeTo("slow",0.8);				
		$('#'+secondname+'cc').append('<em>Pause Now</em>');
	},function(){
		// hover off
		// fade to 100&, restart timer
		$(this).fadeTo("slow",1);
		$('#'+secondname+'cc').html('<h3>'+mydis+'</h3>');
		clearTimeout(timer);
		timer=setTimeout(autop,100,1);			
	});					
	
	// what to do when a link is clicked
	// or images auto transition
	$('#'+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);
		clearTimeout(timer);
		timer=setTimeout(autop,playtime,mynowplaying);
		$('#'+secondname+'link a').removeClass("link_act");
		$(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});
			}
			
			// attach image w/ or w/o link
			if (kk !== 'undefined')	{
				$('#'+secondname+'show').append('<a href='+kk+' target="_self" ><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();
				});
			}
			
			// fade in image
			$('#'+secondname+'show img').fadeIn('slow');				
			$('#'+secondname+'dis').html(nn);
			
			//watermark
			if (watermark==1){$('#'+secondname+'cc').html('<h3>'+nn+'</h3>');}
			
			// hover on/off states
			// same as above
			$('#'+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);			
			});
		});
	});
	
	// start the transitions
	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); 
	}
	if (autoplay==1){	
		timer=setTimeout(autop,playtime,1);
	}

	function autop(q){
		if (!q)	{
			q=0;
		}
		myend=$('#'+secondname+'link a').size();
		myend=myend-1;
		if (q > myend){
			q=0;
		}
		$('#'+secondname+'link a').eq(q).click();
		q=q+1;		
	}
}});