// 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, imgheight, thumbheight, slidebar,popupwidth,popupheight){
	if (!mytarget)
	{
		mytarget='_blank';
	}
	
	var mywidth,myheight,timer,myend,myfirst,myfirstlink,myfirstsrc	;
	 
		if (!speed){speed=1000;}
		if (!playtime){playtime=6000;}
		myfirst=$('#'+firstname+' img:eq(0)').clone();
		myfirstlink=$(myfirst).attr('rel');
		myfirstsrc=$(myfirst).attr('src');
		var myImage = new Image();
		active =-1;
		myImage.src=$(myfirst).attr('src');
		mywidth=imgheight;//myImage.width;
		//myheight=myImage.height;
		var myIMG = new Array();
		var showpop = new Array();
		var mylink = new Array();
			var chk,mysrc,myrel,mytitle,mycover;
			//$('#'+secondname).append('<table border=0 width='+mywidth+'><tr><td width='+mywidth+' id='+secondname+'show class=showbg colspan="3"></td></tr><tr><td align=center valign=top><div id=slide><img src=\"img/rewind.png\" width=\"24\" height=\"24\" /><div id='+secondname+'link class = slidercontrol></div><img src=\"img/forward.png\" width=\"24\" height=\"24\" /></div></td></tr></table>');
			if (slidebar==1)
			{
				$('#'+secondname).append('<table border=0 width='+mywidth+'><tr><td width='+mywidth+' id='+secondname+'show   colspan="3"></td></tr><tr><td align=center valign=top><img id = rewind src=\"images/rewind.png\" height=\"'+thumbheight+'\" /></td><td align=center valign=top><div id=slide><div id='+secondname+'link class = slidercontrol></div></div></td><td align=center valign=top><img id = forward src=\"images/forward.png\"  height=\"'+thumbheight+'\" /></td> </tr><tr><td colspan="3"><span style=\"font-style:italic\">Click large image to see zoom view.</span></td></tr></table>');
				$('#slide div').addClass('slidercontrol');				
				$('#slide').addClass("slide");
				sliderheight = thumbheight+4;
				sliderwidth = mywidth-60;
				$('#slide').css({width: sliderwidth,height:sliderheight});				
			}
			else
			{
				$('#'+secondname).append('<table border=0 width='+mywidth+'><tr><td width='+mywidth+' id='+secondname+'show class=showbg></td></tr><tr></td><td align=center valign=top><div id=slide><div id='+secondname+'link></div></div></td></td></tr><tr><td><span style=\"font-style:italic\">Click large image to see zoom view.</span></td></tr></table>');
			}
			$.easing.elasout = function(x, t, b, c, d) {
				var s=1.70158;var p=0;var a=c;
				if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
				if (a < Math.abs(c)) { a=c; var s=p/4; }
				else var s = p/(2*Math.PI) * Math.asin (c/a);
				return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
			};
		function reset_o(){//before each animation, reset to (0,0), skip this.
				$paneOptions.stop().stop();
				$paneOptions[0].scrollLeft = $paneOptions[0].scrollTop = 0;
			};
		
		var $paneOptions = $('#slide');		
			
			
			function autop(q){
						if (!q)
						{
						q=1;
						}
					 myend=$('#'+secondname+'link a').size();
					
						myend=myend-1;
							if (q > myend)
							{
								q=0;
							}
							//alert(q);
						$('#'+secondname+'link a').eq(q).click();						
						//$paneOptions.stop().scrollTo( 'a:eq('+active+')', 1000, { axis:'x' } );
						if (autoplay===1)
						{
							q=q+1;					
						}
					}
			
	
	$('#'+firstname+' img').each(function(e){
		var f=e+1;
		chk=$(this).attr('title');
		myrel=$(this).attr('rel');
		mysrc=$(this).attr('src');
			if (chk)
			{
				mytitle=chk;
					}
				if(chk===undefined){
						mytitle=$(this).attr("src");
						mytitle = mytitle.replace(new RegExp(/\.*\w*\//g),"");
							}
				mylink[e]='<a href=\"javascript:void(0)\" rel=\"'+mysrc+'\" link=\"'+myrel+'\" title=\"'+mytitle+'\" no=\"'+f+'\" ><img src=\"resizer.php?imgfile='+mysrc+'&max_width='+thumbheight+'&max_height='+thumbheight+'\" border =0></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");
			$paneOptions.stop().scrollTo( 'a:eq(0)', 1000, { axis:'x' } );
			var mydis=$(mylink[0]).attr("title");
			$('#'+secondname+'dis').html(mydis);
			if (watermark==1)
			{$('#'+secondname+'show').append('<em id='+secondname+'cc class=opa><h3>'+mydis+'</h3></em>');
						}
						
				if (myfirstlink)
				{					
					$('#'+secondname+'show').html('<a href=\"javascript:void(0)\"><img id = imgthum src=\"resizer.php?imgfile='+myfirstsrc+'&max_width='+mywidth+'&max_height='+mywidth+'\" border = 1 width='+mywidth+'></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);			
		});	
			
		var newrewind = -1;
		if (newrewind == -1)
		{
			newrewind = active-1;	
		}
		else
		{
			newrewind = newrewind-1;	
		}
		
		var newforward = -1;
		if (newforward == -1)
		{
			newforward = newforward + 1;	
		}
		else
		{
			newforward = newforward + 1;	
		}
		$('#rewind').click(function(){//only scroll horizontally
				if (newrewind<0)
				{
					newrewind=0;
				}				
				$paneOptions.stop().scrollTo( 'a:eq('+newrewind+')', 1000, { axis:'x' } );
			});	
		$('#forward').click(function(){//only scroll horizontally
				var newact = active+1;
				myend=$('#'+secondname+'link a').size();
				myend=myend-1
				if (newforward>myend)
				{
					newforward=myend;
				}
				$paneOptions.stop().scrollTo( 'a:eq('+newforward+')', 1000, { axis:'x' } );				
			});		
		$('#'+secondname+'show').click(function(){	
			//alert($('#'+secondname+'show a').attr('link'));
			//open($('#'+secondname+'show a').attr('link'),'_top','width=700,height=500,top=' + args.t + ',left=' + args.l + ',scrollbars,resizable')
			//open($('#'+secondname+'show a').attr('link'),'_top','width=700,height=500,scrollbars,resizable');
			var myWidth = 0, myHeight = 0;
			if( typeof( window.innerWidth ) == 'number' ) 
			{
				//Non-IE
				myWidth = window.innerWidth;
				myHeight = window.innerHeight;
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
			{
				//IE 6+ in 'standards compliant mode'
				myWidth = document.documentElement.clientWidth;
				myHeight = document.documentElement.clientHeight;
			} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
			{
				//IE 4 compatible
				myWidth = document.body.clientWidth;
				myHeight = document.body.clientHeight;
			}
			var popheight = (myHeight - popupheight) / 2;
			var popwidth  = (myWidth - popupwidth) / 2;
			window.open($('#'+secondname+'show a').attr('link'), 'popup', 'location=0,status=0,scrollbars=0,width='+popupwidth+', height='+popupheight+',top='+popheight+',left'+popwidth).focus();
		});
		$('#'+secondname+'link a').click(function(){		
			
			var mm=this.rel;
			var nn=this.title;
			var kk=$(this).attr('link');
			var mynowplaying=$(this).attr('no');
			mynowplaying=parseInt(mynowplaying,10);
			active =  mynowplaying;
			$paneOptions.stop().scrollTo( 'a:eq('+(mynowplaying-1)+')', 1000, { axis:'x' } );
			if (autoplay==1)
			{
				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});
					}
					if (kk!== 'undefined')
					{
						//$('#'+secondname+'show').append('<a href=\"javascript:void(0)" no=\"'+kk+'\"><img src='+mm +' style="display:none" width='+mywidth+'></a>');						
						$('#'+secondname+'show').html('<a href=\"javascript:void(0)\" link = \"'+kk+'\"><img src='+mm +' style="display:none" width='+mywidth+'></a>');
						//$('#'+secondname+'show').append('<a href=\"javascript:window.open('+kk+', \'popup\', \'location=0,status=0,scrollbars=0,width='+popupwidth+'\')\"><img src='+mm +' style="display:none" width='+mywidth+'></a>');
						//window.open('link', 'popup', 'location=0,status=0,scrollbars=0,width=210,height=165,top=')
					}
					else{
						$('#'+secondname+'show').html('<img src=\"resizer.php?imgfile='+mm+'&max_width='+mywidth+'&max_height='+mywidth+'\" style="display:none" width='+mywidth+'>');
					}
				

				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;
			}
			if (autoplay==1)
			{
				clearTimeout(timer);			
				timer=setTimeout(autop,100,mynowplaying);
			}
		});						
	});
});

	if (autoplay==1)
	{	
	 	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); 
}; 

 
 
