// Heavily modified and edited for idiosyncratic www.scientificartstudio.com use by Adam Gillett, 09/20/09
// 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!
// added variable "p" for IE compatability, referenced in [function autop(q)] and [$('#'+secondname+'link a').click(function()] by Adam Gillett, 09/20/09
(function($) {
jQuery.extend({
	init_slide: function(firstname,secondname,watermark,speed,playtime,clickable,mytarget){
//set up variables and formatting
	if (!mytarget) {mytarget='_blank';}
		if (!speed){speed=1000;}
		if (!playtime){playtime=6000;}
		var mywidth,myheight,timer,myend,myfirst,myfirstlink,myfirstsrc;
		//myfirst=$('#'+firstname+' img:eq(0)').clone();
		myfirst=$('<img src="img/palette.gif">');//defined myfirst for idiosyncratic use
		myfirstlink=$(myfirst).attr('rel');
		myfirstsrc=$(myfirst).attr('src');
		mywidth=myfirst.width;
		myheight=myfirst.height;
		p=1;
		var mylink = new Array();
		var chk,mysrc,myrel,mytitle,mycover;
		$('#'+secondname).append('<div class=imgnav id='+secondname+'link width='+mywidth+'><span id=playpause class=link_act><img src=img/play.png alt=click to pause></span></div><div width='+mywidth+' height='+myheight+' id='+secondname+'show class=showbg ></div><div class=imgnav id='+secondname+'dis width='+mywidth+'></div>');
//more set up - called once for slides generation only		
		$('#'+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=# rel=\"'+mysrc+'\" link=\"'+myrel+'\" title=\"'+mytitle+'\" no=\"'+f+'\" >'+f+'</a>';
			if (clickable!==1){$('#'+secondname+'link').hide();}
			$('#'+secondname+'link').append(mylink[e]);
			$('#'+secondname+'link a').addClass("link_nor");
		});
//more set up variables and formatting (lines 45-54)				
		myend=$('#'+secondname+'link a').size();
		$('#'+secondname+'link a:eq(0)').addClass("link_act");
		var mydis=$(mylink[0]).attr("title");
		$('#'+secondname+'dis').html(mydis);
		if (watermark==1){$('#'+secondname+'show').append('<div id='+secondname+'cc class=opa>'+mydis+'</div>');}
		if (myfirstlink){
			$('#'+secondname+'show').append('<a href='+myfirstlink+' target='+mytarget+'><img src='+myfirstsrc+'></a>');
		}else{		
			$('#'+secondname+'show').append(myfirst);			
		}
//called on every timer countdown (starts a fuction for slide changing)		
		function autop(q){
			if (!q) {q=p;}
			if (q >= myend){q=0;}
			$('#'+secondname+'link a').eq(q).click();						
			q=q+1;
		}
//START called by autop & numbered list (contains nestled functions for pausing, resuming, topnavHover, playpauseClick)		
		$('#'+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);
			p=mynowplaying;
			$('#playpause').html('<img src=img/play.png alt=click to pause>');
			var playstate=0;
			clearTimeout(timer);
			timer=setTimeout(autop,playtime,mynowplaying);
			$('#'+secondname+'link a').removeClass("link_act");
			$(this).addClass("link_act");
//called by functions to halt the slideshow			
			function pausing(){
				clearTimeout(timer);
				$('#playpause').html('<img src=img/pause.png alt=click to play>');
				playstate=1;
			}
//called by functions to resume the slideshow			
			function resuming(){
				$('#playpause').html('<img src=img/play.png alt=click to pause>');
				playstate=0;
				clearTimeout(timer);
				timer=setTimeout(autop,playtime*0.5,mynowplaying);	
			}
//for drop-down menu pause / resuming on rollover			
			$('#topnav').hover(function(){
				pausing();
			},function(){
				resuming();			
			});
//for play-pause button pause / resuming on click			
			$('#playpause').click(function(){
				if (playstate==0){
					pausing();
				}else{
					resuming();
				}
			});
//actual slide change w/ callback function			
			$('#'+secondname+'show img:eq(0)').fadeTo(speed, 0.33,function(){
				$('#'+secondname+'show img:eq(0)').remove();
				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">');
				}
				$('#'+secondname+'show img').fadeIn(speed);				
				$('#'+secondname+'dis').html(nn);
				if (watermark==1){$('#'+secondname+'cc').html(nn);}
			});
		});
//END of $('#'+secondname+'link a').click(function() and nestled functions
//more set up - called only once when slideshow starts; CRITICAL for starting all the variables and executions
		autop(myend);
	}
});
})(jQuery);	
//called on every timeout...modification of setTimeout expression...may be self defeating
/*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); 
};*/

 
 
