        //Random Transitions Slideshow- By JavaScript Kit (http://www.javascriptkit.com)
//Created: Nov 3rd, 2008
/* array of IE transition strings - some look bad */
/*
var global_transitions=[ 
	"progid:DXImageTransform.Microsoft.Barn()",
	"progid:DXImageTransform.Microsoft.Blinds()",
	"progid:DXImageTransform.Microsoft.CheckerBoard()", 
	"progid:DXImageTransform.Microsoft.Fade()",
	"progid:DXImageTransform.Microsoft.GradientWipe()",
	"progid:DXImageTransform.Microsoft.Inset()",
	"progid:DXImageTransform.Microsoft.Iris()",
	"progid:DXImageTransform.Microsoft.Pixelate(maxSquare=15)", 
	"progid:DXImageTransform.Microsoft.RadialWipe()",
	"progid:DXImageTransform.Microsoft.RandomBars()",
	"progid:DXImageTransform.Microsoft.RandomDissolve()",
	"progid:DXImageTransform.Microsoft.Slide()",
	"progid:DXImageTransform.Microsoft.Spiral()", 
	"progid:DXImageTransform.Microsoft.Stretch()",
	"progid:DXImageTransform.Microsoft.Strips()",
	"progid:DXImageTransform.Microsoft.Wheel()", 
	"progid:DXImageTransform.Microsoft.Zigzag()"
]
*/
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Image data format for slide show
//  data[0]:  	not used
//	data[1]: 	Image URL
//	data[2]:	not used
//	data[3]:	not used
//	data[4]:	copyright notice
//	data[5]:	Title
//	data[6]:	Caption
//
//

var global_transitions=[   // Pruned array of IE transition strings - these look OK
	 "progid:DXImageTransform.Microsoft.Fade()",
	 "progid:DXImageTransform.Microsoft.GradientWipe()",
	 "progid:DXImageTransform.Microsoft.Slide()"
]

//var stop_slideshow = false;
//var tcount = 2;    // ==> medium speed = default value set in .html
//var fcount = 0;
//var img_increment = 0;
//var run_once = false;

function flashyslideshow(setting){
	this.wrapperid=setting.wrapperid
	this.wrapperclass = setting.wrapperclass
	this.imagearray=setting.imagearray
	this.pause=setting.pause
	this.transduration=setting.transduration/1000 //convert from miliseconds to seconds unit to pass into el.filters.play()
	this.currentimg=setting.first_image
	this.run_mode = setting.run_mode
	this.slideshow_controls_id = setting.slideshow_controls_id
	this.slideshow_container_id = setting.slideshow_container_id
	this.show_return_msg = setting.show_return_msg
	this.return_msg_id = setting.return_msg_id
	this.link_bar_id = setting.link_bar_id
	this.image_data_path = setting.data_path
	this.top_text_id = setting.top_text_id
	this.run_random = setting.run_random
	this.start_random = setting.start_random
	this.preload = setting.preload
	this.soldprintsdiv = setting.soldprintsdiv
	this.show_watermark = setting.show_watermark
	this.stop_slideshow = false;
	this.slideshow_run = this.run_mode;
	
 	 this.tcount = 6;    // ==> medium speed = default value set in .html
 	 this.fcount = 0;
	 this.save_tcount = this.tcount;
 	 this.img_increment = 0;
 	 this.run_once = false;
	 this.image_container_size = new this.Imgdata();
	 this.image_max_size = new this.Imgdata();
	 this.image_data = new Array();
	 this.first_loaded = false;
	 this.img_preload_div = document.getElementById("preloaded_images");
	 this.preloadimage = new Image();
	 this.image_cache = new Array();
	 this.show_sold_box = false;		
	 this.watermark_text = '';
	if(this.currentimg > this.imagearray.length-1) { this.currentimg = 0 } 
	if(this.start_random) this.currentimg = this.get_random();
	
	//write transparent image to get CSS into DOM
//	document.write('<div id="'+this.wrapperid+'" class="'+this.wrapperclass+'">');		
//	document.write('<div id="'+this.wrapperid+'_inner"' + '><\/div><\/div>');
//	$('#'+this.wrapperid+'_inner').hide();	
//	this.contentdiv=document.getElementById(this.wrapperid+"_inner");
//	this.contentdiv.innerHTML = '<img style = "border:none;" src = "images\/welcome_for_fp_slideshow.gif">';	
//	$('#'+this.wrapperid+'_inner').fadeIn(1000).delay(2000).fadeOut(2000);
	
//alert('Well?');
	document.write('<div id="'+this.wrapperid+'" class="'+this.wrapperclass+'">');		
	document.write('<div id="'+this.wrapperid+'_inner"' + '>' + '<img style = "border:none;" src = "images\/loading_for_fp_slideshow.gif">'+'<\/div><\/div>');

		 
	//Read CSS size information from CSS
	this.image_container_size.width = parseInt($("#"+this.wrapperid).css("width"));
	this.image_container_size.height = parseInt($("#"+this.wrapperid).css("height"));
	this.image_max_size.width = parseInt($("#"+this.wrapperid + " img").css("max-width"));
	this.image_max_size.height = parseInt($("#"+this.wrapperid + " img").css("max-height"));
	this.contentdiv=document.getElementById(this.wrapperid+"_inner");
	

//	if(this.soldprintsdiv.length) {
//		this.show_sold_box = true;		
//		this.sold_prints_div = document.getElementById('#'+this.soldprintsdiv);	
//	}
	// If run_mode == true then running as rotating slide show so get first image loaded and displayed 
	
	// Preload first image
		this.image_data[this.currentimg] = new this.Imgdata();		
		this.image_data[this.currentimg].width = 0;
		this.image_data[this.currentimg].height = 0;
		this.image_cache[this.currentimg] = new Image();					
		this.image_cache[this.currentimg].src = this.imagearray[this.currentimg][0];

	
	if(this.run_mode) {
		this.first_loaded = this.showImage(this.currentimg);	
		if(!this.first_loaded) {
		   this.tcount = 1;   // if image isn't ready set to retry after short intervals		 
		}
	}
	// Preload remaining images if desired 
/*	if(this.preload) {
		for(var i=0;i<this.imagearray.length;i++) {
		if(i != this.currentimg) {
			this.image_data[i] = new this.Imgdata();		
			this.image_data[i].width = 0;
			this.image_data[i].height = 0;
			this.image_cache[i] = new Image();
			this.image_cache[i].src = this.imagearray[i][0];
			}
		}
	}
*/
	
//=======================================================================================================================
	var effectindex=Math.floor(Math.random()*global_transitions.length) //randomly pick a transition to utilize    
	if (this.contentdiv.filters){                                       //if the filters[] collection is defined on element (only in IE)
		this.contentdiv.style.filter=global_transitions[effectindex] //define transition on element
		this.pause+=setting.transduration                       //add transition time to pause
	} // endif

	this.filtersupport=(this.contentdiv.filters && this.contentdiv.filters.length>0)? true : false //test if element supports transitions and has one defined

	var slideshow = this  // DON'T DELETE THIS ^^%%$^#
	setInterval(function(){slideshow.rotate()}, this.pause); 

} 
// End flashyslideshow()	
// ========================================================================================================  
//=========================================================================================================
//=========================================================================================================	
                      
// addEvent 
flashyslideshow.addEvent=function(target, functionref, tasktype){
	if (target.addEventListener)
		target.addEventListener(tasktype, functionref, false);
	else if (target.attachEvent)
		target.attachEvent('on'+tasktype, function(){return functionref.call(target, window.event)});   // window.event is 'ancient' Netscape property?
},   // end flashyslideshow.addEvent=function

flashyslideshow.setopacity=function(el, degree){ //sets opacity of an element (FF and non IE browsers only)
	if (typeof el.style.opacity!="undefined")
		el.style.opacity=degree
	else
		el.style.MozOpacity=degree
	el.currentopacity=degree
}, // end flashyslideshow.setopacity

////////////////////////////////////////////////////////////////////////////////////////
flashyslideshow.prototype.Imgdata = function()
{
	this.height = 0;
	this.width = 0;
}
////////////////////////////////////////////////////////////////////////////////////////
flashyslideshow.prototype.getSlideHTML=function(index){
	var slideHTML ='<img id = "'+ this.wrapperid + '_img_id" src="'+this.imagearray[index][0]+'" /><br/>'  //src
	
//	slideHTML += '<div class ="'+this.wrapperclass+'_watermark">Watermark</div></div><br/>'
	
	slideHTML+= '<span class = "img_copyright">' + ((this.imagearray[index][3])? this.imagearray[index][3] : '' ) + '<\/span>';   // copy right notice
	if(this.imagearray[index][4] != "") 
	{
	slideHTML+= '<br \/><span class = "img_title">'+ this.imagearray[index][4] + '<\/span>'
	}
	if(this.imagearray[index][5] != "") 
	{
	slideHTML+= '<br \/><span class = "img_caption">'+ this.imagearray[index][5] + '<\/span><br \/><br \/>'
	}
	slideHTML = this.getSpaceDiv(index) + slideHTML;
	return slideHTML //return HTML for the slide at the specified index e.g. <img src="images/1.jpg" /><br />Copyright &copy; Daniel Christie
}
////////////////////////////////////////////////////////////////////////////////////////
flashyslideshow.prototype.oldgetSlideHTML=function(index){
	
	var slideHTML=(this.imagearray[index][1])? '<a href="'+this.imagearray[index][1]+'" target="'+this.imagearray[index][2]+'">\n' : '' // is slide hyperlinked?
	slideHTML+='<img id = "'+ this.wrapperid + '_img_id" src="'+this.imagearray[index][0]+'" />'  //src
	slideHTML+=(this.imagearray[index][1])? '</a><br />' : '<br />' //hyperlink if any
	slideHTML+= '<span class = "img_copyright">' + ((this.imagearray[index][3])? this.imagearray[index][3] : '' ) + '<\/span>';   // copy right notice
	if(this.imagearray[index][4] != "") {
	slideHTML+= '<br \/><span class = "img_title">'+ this.imagearray[index][4] + '<\/span>'
	}
	if(this.imagearray[index][5] != "") {
	slideHTML+= '<br \/><span class = "img_caption">'+ this.imagearray[index][5] + '<\/span><br \/><br \/>'
	}
	slideHTML = this.getSpaceDiv(index) + slideHTML;
	return slideHTML //return HTML for the slide at the specified index e.g. <img src="images/1.jpg" /><br />Copyright &copy; Daniel Christie
}
////////////////////////////////////////////////////////////////////////////////////////
flashyslideshow.prototype.getSpaceDiv = function(index) {
	var space_div = '<div id = "space_div" style = "margin:0; padding:0;width:auto;height:';
	var sdend =  'px;"><\/div>';
	var spacer_height = 0;
	var height = 0;
	if(this.image_data[index].width == 0 )	height = this.image_container_size.height;  // don't have image size data yet so set spacer to 0
	else {
		// image is smaller than container so center vertically
		if(this.image_data[index].width <= this.image_max_size.width && this.image_data[index].height <= this.image_max_size.height )
		{
			height = this.image_data[index].height;
			// Try to deal with thumbnail sized images
			if((this.image_data[index].width < 0.5*this.image_max_size.width )|| (this.image_data[index].height < 0.5*this.image_max_size.height)) {
				height = 1.7*height;
			}
		}
		else {
			// one or both dimensions are larger than max width
			var width_ratio = (this.image_data[index].width / this.image_max_size.width  );
			var height_ratio = (this.image_data[index].height / this.image_max_size.height );

			if (width_ratio > height_ratio ){  
				height = Math.floor(this.image_data[index].height / width_ratio)		
			}
			else   
			{
				height = this.image_max_size.height;
			} 
		}
	}
	if(this.imagearray[index][3] != '') height += 12;
	if(this.imagearray[index][4] != '') height += 25;
	if(this.imagearray[index][5] != '') height += 50;
	spacer_height = Math.floor(0.5*(this.image_container_size.height - height));
	space_div = space_div + spacer_height + sdend;
	return space_div;	
}
////////////////////////////////////////////////////////////////////////////////////////
flashyslideshow.prototype.return_artist_path = function() {

		if(this.image_data[this.currentimg].width != 0)
		{

		return  this.imagearray[this.currentimg][0];

		}
		else
		 return "";
}
////////////////////////////////////////////////////////////////////////////////////////
flashyslideshow.prototype.return_image_number = function() {
		return this.currentimg;
}
flashyslideshow.prototype.return_run_mode = function() {
	return this.slideshow_run;	
}
////////////////////////////////////////////////////////////////////////////////////////
flashyslideshow.prototype.showImage = function(img_number) {

	if(this.image_data[img_number].width != 0) {
		this.contentdiv.innerHTML=this.getSlideHTML(img_number);   //image has already been loaded & we have size infomation so display it
		this.writesoldprint(img_number);
		this.draw_watermark(img_number);
		return true;
	}
	else {
		var preloadimage = new Image();
		preloadimage.src = this.imagearray[img_number][0];
		this.image_data[img_number].width = preloadimage.width;
		this.image_data[img_number].height = preloadimage.height;
		if(this.image_data[img_number].width != 0) {
			this.contentdiv.innerHTML=this.getSlideHTML(img_number);   //image has already been loaded & we have size infomation so display it 	
			this.writesoldprint(img_number);
			this.draw_watermark(img_number);		
			return true;
		}		
	}
	return false;  // image has not been loaded so do nothing & try again next time
}
////////////////////////////////////////////////////////////////////////////////////////
flashyslideshow.prototype.writesoldprint = function(img_number) 
{
	
	if(this.soldprintsdiv.length) {
		var sold = this.imagearray[img_number][1];
		var prints = this.imagearray[img_number][2];
		var spd = $('#'+this.soldprintsdiv);
		if(sold == '' && prints == '')
		{
			spd.css('display','none');
		}
		else
		{
			if(sold != '' && prints != '')
				sold = sold + '<br/>'+prints;
			else 
				sold = sold + prints;			
			spd.html(sold);
			spd.css('display','block');
		}
	}
}
////////////////////////////////////////////////////////////////////////////////////////
// In IE9 and Safari with narrow images (~200px?) this code puts the watermark too far to the left
flashyslideshow.prototype.draw_watermark = function(img_number)
{
	if(this.show_watermark > 0)
	{
		var opacity = 0;
		switch(this.show_watermark) {
			case 1:
				opacity = 0.35;
			break;
			case 2:
				opacity = 0.5;
			break;
			case 3:
				opacity = 0.75;
			break;	
		}
		var jpos = $('#'+this.wrapperid+'_img_id');
		var cr_text = this.imagearray[img_number][3];
		var cindex = cr_text.indexOf(';',0)+1;
		cr_text = cr_text.substr(0,cindex)+'<br/>'+cr_text.substr(cindex);
		var watermark_html = '<div class="'+this.wrapperclass+'_watermark">'+cr_text+'</div>';
		jpos.after(watermark_html);
		var wm = $('div.'+this.wrapperclass+'_watermark');		
		var xval = 1.0*(jpos.position().left + this.image_data[img_number].width) - parseInt(wm.css('width'));
		var yval = 1.0*(jpos.position().top + this.image_data[img_number].height) - parseInt(wm.css('height'));		
		wm.css({'top':yval,'left':xval,'display':'block','opacity':opacity});	
	}
};
////////////////////////////////////////////////////////////////////////////////////////
flashyslideshow.prototype.preload_files = function()
{
	for(var i = 0;i<this.imagearray.length;i++) {

		$('#preloaded_images').css('background-image','url('+ this.imagearray[i][0] + ')')
	}
}
////////////////////////////////////////////////////////////////////////////////////////
flashyslideshow.prototype.show_slideshow = function(img_number,top_anchor_id) {
//	var gallery = document.getElementById(gallery_id);
	var lbar = document.getElementById(this.link_bar_id);
	var tbar = document.getElementById(this.top_text_id);
//	gallery.style.display = "none";
	lbar.style.display = "none";
	tbar.style.display = "none";	
	this.show_viewer(img_number,true);	
	if(top_anchor_id.length)
		goToByTopScroll(top_anchor_id);

}
////////////////////////////////////////////////////////////////////////////////////////
flashyslideshow.prototype.hide_slideshow = function(gallery_id) {
	var gallery = document.getElementById(gallery_id);
	var lbar = document.getElementById(this.link_bar_id);
	var tbar = document.getElementById(this.top_text_id);
			
	gallery.style.display = "block";
	lbar.style.display = "block";
	tbar.style.display = "block";
	this.show_viewer(0,false);

}
////////////////////////////////////////////////////////////////////////////////////////
flashyslideshow.prototype.show_viewer = function(img_number,visible)  {
	
	var button = document.getElementById("pause");
	var prev = document.getElementById("prev");
    var next = document.getElementById("next");	
	var vwindow = document.getElementById(this.slideshow_container_id);
	var rmsg = document.getElementById(this.return_msg_id);
	this.currentimg = img_number;		
	this.showImage(this.currentimg);	
	button.innerHTML = "Play";
	prev.style.color = "black";
	next.style.color = "black";
	if(visible) 
	{
		vwindow.style.display= "block";
		this.show_controls(true);
	}
	else
	{
		vwindow.style.display = "none";
		this.show_controls(false);
	}
	if(this.show_return_msg) { rmsg.style.visibility = "visible"; }
	else {rmsg.style.visibility = "hidden";}

}
////////////////////////////////////////////////////////////////////////////////////////
flashyslideshow.prototype.stopss = function() {
   var button = document.getElementById("pause");
   var prev = document.getElementById("prev");
   var next = document.getElementById("next");
   if(this.stop_slideshow) {
	   this.stop_slideshow = false;
	   button.innerHTML = "Pause";
	   prev.style.color = "#888";
	   next.style.color = "#888";
	   single_step = 0;
	   this.slideshow_run = true;
   }
   else {
	   this.stop_slideshow = true;
	   button.innerHTML = "Resume";
	   prev.style.color = "black";
	   next.style.color = "black";
	   single_step = 1;
	   this.slideshow_run = false;
   }
}
///////////////////////////////////////////////////////////////////////////////////////
flashyslideshow.prototype.set_speed = function(speed){
	this.tcount = speed;
	this.fcount = 0;
}	
///////////////////////////////////////////////////////////////////////////////////////
flashyslideshow.prototype.s_step = function(index_increment) {
	
		if(this.stop_slideshow == true ) this.img_increment = index_increment;
}
///////////////////////////////////////////////////////////////////////////////////////
flashyslideshow.prototype.show_controls = function(show_state) {
		var x = document.getElementById(this.slideshow_controls_id).style;
		if(show_state == true)	x.display = "block";
		else x.display = "none";	
}	
///////////////////////////////////////////////////////////////////////////////////////
flashyslideshow.prototype.get_random = function() {
	
	return Math.floor(Math.random()*this.imagearray.length);
	
}
/////////////////////////////////////////////////////////

flashyslideshow.prototype.rotate=function(){

	var content_div = this.contentdiv;
	
	if(this.preload) {
		for(var i=0;i<this.imagearray.length;i++) {
		if(i != this.currentimg) {
			this.image_data[i] = new this.Imgdata();		
			this.image_data[i].width = 0;
			this.image_data[i].height = 0;
			this.image_cache[i] = new Image();					
			this.image_cache[i].src = this.imagearray[i][0];
			}
		}
	}

	
	
	if(!this.run_mode) {
		this.stop_slideshow = true;
		this.run_once = true;
		this.run_mode = true;
		return;	
	}
	
	if(this.stop_slideshow && this.img_increment ==0 ) return;
	///////// timer ///////
	if(!this.stop_slideshow) {
		if(this.fcount < this.tcount) {
			this.fcount = this.fcount + 1;
			return;}
		else { this.fcount = 0;}
	}
	/////////// single-step ////////////////////////
	if(this.stop_slideshow && this.img_increment != 0)
	{
		if(this.img_increment == 1){    // step one ahead 
		    	this.currentimg=(this.currentimg<(this.imagearray.length-1))? this.currentimg+1 : 0;

			}
		if(this.img_increment == -1) {  // step one back
				this.currentimg = (this.currentimg == 0)? this.imagearray.length-1 : this.currentimg-1; 
		}
	  this.img_increment = 0;	 	
	}
	////////// normal slide show operation
	else {		    	
			this.currentimg=(this.currentimg<this.imagearray.length-1)? this.currentimg+1 : 0 
        	// increments this.currentimg or resets to 0 if current is last in imagearray
	}
																							//  =====> inc's counter <======
	if (this.filtersupport){
		content_div.filters[0].apply()
	}
	else{
		flashyslideshow.setopacity(content_div, 0)
	} 
	
	if(this.run_random) this.currentimg = this.get_random(); 
																	
	if(this.showImage(this.currentimg)) this.tcount = this.save_tcount;
																		
	if (this.filtersupport){
		content_div.filters[0].play(this.transduration)
	}
	else{
		content_div.fadetimer=setInterval(function(){
			if (content_div.currentopacity<1)
				flashyslideshow.setopacity(content_div, content_div.currentopacity+0.1)
			else
				clearInterval(content_div.fadetimer)
		}, 50) 
	} 
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
//////// Utility functions & etc.
//////////////////////////////////////////////////////////////////////////////////////////////////////																			
// background fade function
var numSteps=50;
var startingRed=0;
var startingGreen=0;
var startingBlue=0;
var endingRed=240;
var endingGreen=232;
var endingBlue=212;
var deltaRed=0;
var deltaGreen=0;
var deltaBlue=0;
var currentRed=0;
var currentGreen=0;
var currentBlue=0;
var currentStep=0;
var timerID=0;

////////////////////////////////////////
// fade timer
////////////////////////////////////////
function startFadeDec(startR, startG, startB, 
   endR, endG, endB, nSteps)
{
  	currentRed=startingRed=parseInt(startR, 10);
  	currentGreen=startingGreen=parseInt(startG, 10);
  	currentBlue=startingBlue=parseInt(startB, 10);
  	endingRed=parseInt(endR, 10);
  	endingGreen=parseInt(endG, 10);
  	endingBlue=parseInt(endB, 10);
  	numSteps=parseInt(nSteps, 10);
  	deltaRed=(endingRed-startingRed)/numSteps;
  	deltaGreen=(endingGreen-startingGreen)/numSteps;
	deltaBlue=(endingBlue-startingBlue)/numSteps;
	currentStep=0;
	
  	fade();
}

////////////////////////////////////////
// fade timer
////////////////////////////////////////
function fade()
{

  	currentStep++;
  	// if not done yet, change the backround
  	if (currentStep<=numSteps)
  	{
		// convert to hex	
		var hexRed=decToHex(currentRed);
		var hexGreen=decToHex(currentGreen);
		var hexBlue=decToHex(currentBlue);
	
		var color="#"+hexRed+""+hexGreen+""+hexBlue+"";
//	alert(color);
		
	  	document.bgColor=color;
//  	alert(document.bgColor);

		// increment color
		currentRed+=deltaRed;
		currentGreen+=deltaGreen;
		currentBlue+=deltaBlue;
//	alert("cr="+currentRed+" cg="+currentGreen+" cb="+currentBlue);
		
	  	timerID=setTimeout("fade()", 20); // sets timer so that this function will
                  		   		      // be called every 10 miliseconds
   }
}

////////////////////////////////////////
// convert decimal to hexadecimal number
////////////////////////////////////////
function decToHex(decNum)
{
	decNum=Math.floor(decNum);
	var decString=""+decNum;
	// make sure the number is valid
	for (var i=0; i<decString.length; i++)
	{

		if (decString.charAt(i)>='0' && decString.charAt(i)<='9')
		{
		}
		else
		{
			alert(decString+" is not a valid decimal number because it contains "+decString.charAt(i));
 			return decNum;
		}
	}
	var result=decNum;
	var remainder="";
	// use string because math operation won't work with hex alphabet
	var hexNum="";

	var hexAlphabet=new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
	while (result>0)
	{
		result=Math.floor(decNum/16);
		remainder=decNum%16;
		decNum=result;

/*		if (remainder>=10)
		{
			// use double quotes because Netscape 3 will give error if using single quote
			if (remainder==10)
				remainder="A";
			if (remainder==11)
				remainder="B";
			if (remainder==12)
				remainder="C";
			if (remainder==13)
				remainder="D";
			if (remainder==14)
				remainder="E";
			if (remainder==15)
				remainder="F";
		}*/
		// just append the next remainder to the beginning of the string
		hexNum=""+hexAlphabet[remainder]+""+hexNum;
	};
	// make sure to have at least 2 digits
	if (hexNum.length==1)
		hexNum="0"+hexNum;
	else if (hexNum.length==0)
		hexNum="00";
	return hexNum;
}   

function fadeRandom()
{
	startFadeDec(sR, sG, sB, eR, eG, eB, 30);
}
