// Global Cofiguration Variables
var isCSS 			= false;
var isW3C 			= false;
var isIE4 			= false;
var isNN4 			= false;
var isIE6 			= false;
var isGecko 		= false;
var isOpera 		= false;

// Function:	initialize
// Purpose:		Checks browser context
function initialize() {
	if(document && document.images) {
        isCSS		= (document.body && document.body.style) ? true : false;
        isW3C		= (isCSS && document.getElementById) ? true : false;
        isIE4		= (isCSS && document.all && readIEVer() >= 4.0) ? true : false;
        isNN4		= (document.layers) ? true : false;
        isGecko		= (isCSS && navigator && navigator.product && navigator.product == "Gecko");
        isOpera		= (isCSS && navigator.userAgent.indexOf( "Opera") != -1 );
		isIE6CSS	= (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
		isIE6		= ( isIE6CSS && readIEVer() >= 6.0 );
    }
}

// Function: 	montage
// Purpose:		This function is the only funtion that needs to be called from outside this file.
// Variables	- di_src:			an array of image src paths (numerically keyed)
//				- di_cap:			an array of image captions (keyed to match src)
//				- di_href:			an array of image hrefs (keyed to match src)
//				- selection_type:	1=random dynamic; 2=sequential dynamic; 3=random static
//				- filter:			the number of the filter to use (-1=no filter; 0-22 are filters; 23= random filter)
//				- blend_duration:	the length of time in seconds that it takes for the filter transition to occur
//				- reveal_duration:	the length of time in seconds that it takes for the new image to reveal itself
//				- hold_duration:	the length of time in seconds until the next transition
//				- di_height:		the height of the montage
//				- di_width:			the width of the montage
//				- td_id:			if this variable is supplied it must be the id of the td that you wish the montage to be the background of
function montage( unique_id, di_src, di_cap, di_href, selection_type, filter, blend_duration, reveal_duration, hold_duration, di_height, di_width, stop, td_id, new_window ) {
	
	initialize();
	
	// create unique_id for non-background images
	//unique_id = Math.round(10000*Math.random());
	
	// add non animated version check here
	//var m_montage	= hasBroadband();
	
	/* THIS IF BLOCK WAS BREAKING FIREFOX
	if( m_montage == false || di_src.length <= 1 || !( isIE4 || isW3C ) )
	{
		if( td_id ) {// is background image
			swap_bgimage(-1, 3, filter, blend_duration, reveal_duration, hold_duration, di_src, di_cap, td_id, stop);
		} else {// is not a background image
			swap_image(unique_id, -1, 3, filter, blend_duration, reveal_duration, hold_duration, di_src.length, stop);
		}
		return;
	}*/
	
	// background image check
	if( td_id ) {// is a background image
		if (document.getElementById) {
			var tdobj = document.getElementById(td_id);
			if(filter > -1)// if there if a filter, apply it
				tdobj.style.filter = "blendTrans(duration="+blend_duration+") revealTrans(duration="+reveal_duration+",transition="+filter+")";
			swap_bgimage(-1, selection_type, filter, hold_duration, di_src, di_cap, td_id, stop);
		}
	} else {// montage isn't a background
		document.write( "<div id='container"+unique_id+"' style='position:relative; width:"+di_width+"px;height:"+di_height+"px'>" );//setup image container
		
		for( i = 0; i < di_src.length; i++ ) {// setup image divs
			document.write( "<div id='montage"+unique_id+"_"+i+"' style='display:none'>" );
			if( di_href[i] != "" ) {
				if(new_window) {
					document.write( "<a href='"+di_href[i]+"' target='_blank'><img src='"+di_src[i]+"' alt='"+di_cap[i]+"' border='0' id='monimg"+i+"'  /></a>" );
				} else {
					document.write( "<a href='"+di_href[i]+"'><img src='"+di_src[i]+"' alt='"+di_cap[i]+"' border='0' id='monimg"+i+"'  /></a>" );
				}
			} else {
				document.write( "<img src='"+di_src[i]+"' alt='"+di_cap[i]+"' border='0' id='monimg"+i+"'  />" );
			}
			document.write( "</div>" );
		}
		document.write( "</div>" );
		
		swap_image(unique_id, -1, selection_type, filter, blend_duration, reveal_duration, hold_duration, di_src.length, stop);
	}
}

// Function:	swap_image
// Purpose:		To swap which image is being displayed by the montage
// Variables:	- unique_id:	this is the unique_id generated in montage()
//				- current_image:	the key of the current image
//				- selection_type:	the selection type of the montage
//				- filter:			the used by the montage
//				- blend_duration:	the length of time in seconds that it takes for the filter transition to occur
//				- reveal_duration:	the length of time in seconds that it takes for the new image to reveal itself
//				- hold_duration:	the length of time in seconds until the next transition
//				- image_count:		the number of image in the montage (i.e. the length of src)
function swap_image(unique_id, current_image, selection_type, filter, blend_duration, reveal_duration, hold_duration, image_count, stop) {
	// get image container
	var monContainer = document.getElementById("container"+unique_id);
	
	// get the key of the next image
	if(selection_type == 1) {// random dyanmic image
		var next_image = generate_random_new_number(image_count-1, current_image);
	} else if(selection_type == 3) {// random static image
		var next_image = generate_random_new_number(image_count-1);
	} else {// sequential dynamic image
		if(current_image == image_count-1) {
			var next_image = 0;
		} else {
			var next_image = current_image+1;
		}
	}
	
	// if the browser supports filter and a filter has been selected
	if( (readIEVer() >= 4.0) && (filter > -1) )
	{
		monContainer.style.filter = "blendTrans(duration="+blend_duration+") revealTrans(duration="+reveal_duration+",transition="+filter+")";
	
		monContainer.filters(0).apply();
		monContainer.filters(1).apply();
		
		if(current_image > -1)// -1 means it's the first time through
			document.getElementById("montage"+unique_id+"_"+current_image).style.display = "none";
		current_image = next_image;
		document.getElementById("montage"+unique_id+"_"+current_image).style.display = "block";
		
		monContainer.filters(0).play();
		monContainer.filters(1).play();
	}
	else
	{
		if(current_image > -1)// -1 means it's the first time through
			document.getElementById("montage"+unique_id+"_"+current_image).style.display = "none";
		current_image = next_image;
		document.getElementById("montage"+unique_id+"_"+current_image).style.display = "block";
	}
	
	if(selection_type != 3){
		if((selection_type == 2) && (stop == 1) && (current_image == image_count-1))
			//do nothing
			stop = 1;
		else
			setTimeout("swap_image('"+unique_id+"', "+current_image+", "+selection_type+", "+filter+", "+blend_duration+", "+reveal_duration+", "+hold_duration+", "+image_count+", "+stop+")", hold_duration*1000);
	}
}

// Function:	swap_bgimage
// Purpose:		The same as that of swap_image, only that it operates on a background image ontage
// Variables:	- current_image:	the key of the current image
//				- selection_type:	the selection type of the montage
//				- filter:			the used by the montage
//				- hold_duration:	the length of time in seconds until the next transition
//				- di_src:			this variable can either be the array of image src paths or a comma delimited list of image src paths
//				- di_cap:			this variable can either be the array of image captions or a comma delimited list of image captions
//				- td_id:			the id of the td that the montage is the background of
function swap_bgimage(current_image, selection_type, filter, hold_duration, di_src, di_cap, td_id, stop) {
	if(typeof(di_src) == "string") {
		di_src = di_src.split(",");
		di_cap = di_cap.split(",");
	}
	
	if(selection_type == 1) {
		var next_image = generate_random_new_number(di_src.length-1, current_image);
	} else if(selection_type == 3) {
		var next_image = generate_random_new_number(di_src.length-1);
	} else {
		if(current_image == di_src.length-1) {
			var next_image = 0;
		} else {
			var next_image = current_image+1;
		}
	}
	
	if (document.getElementById) {
		var bgimage_obj = document.getElementById(td_id);
		if( (readIEVer() >= 4.0) && (filter > -1) ){
			bgimage_obj.filters(0).apply();
			bgimage_obj.filters(1).apply();
			bgimage_obj.background = di_src[next_image];
			bgimage_obj.filters(0).play();
			bgimage_obj.filters(1).play();
		} else {
			bgimage_obj.style.background = "url("+di_src[next_image]+")";
		}
		current_image = next_image;
	}
	
	var src_string = "";
	var cap_string = "";
	var comma = "";
	for(var i = 0; i < di_src.length; i++) {
		src_string = src_string + comma + String(di_src[i]);
		cap_string = cap_string + comma + String(di_cap[i]);
		comma = ",";
	}
	
	if(selection_type != 3){
		if((selection_type == 2) && (stop == 1) && (current_image == di_src.length-1))
			//do nothing
			stop = 1;
		else
			setTimeout("swap_bgimage("+current_image+", "+selection_type+", "+filter+", "+hold_duration+", '"+src_string+"', '"+cap_string+"', '"+td_id+"', '"+stop+"')", hold_duration*1000);
		}
}

// Function:	generate_random_new_number
// Purpose:		to generate a random number between 0-'maxium' other than 'except'
// Variables:	- maximum:	sets the upper limit of the random number generation
//				- except:	this number will not be generated
function generate_random_new_number(maximum, except) {
	var mininum = 0;
	var randnum = except;
	while(randnum == except) {
		randnum = (Math.round((Math.random()*(maximum-mininum))+mininum))
	}
	return randnum;
}

function readIEVer() {
	var agent	= navigator.userAgent;
	var offset	= agent.indexOf( "MSIE" );
	if( offset < 0 ) {
		return 0;
	}
	return parseFloat( agent.substring( offset + 5, agent.indexOf( ";", offset ) ) );
}

function hasBroadband() {
	if( readIEVer() < 5.0 )	{
		return false;
	}
	try	{
		document.body.addBehavior ("#default#clientCaps");
		return ( typeof(document.body.connectionType) != "undefined" && document.body.connectionType == "lan" );
	}
	catch( e ) {
		return false;
	}
}

/*
the_name = the name of the snapin
div_id = the id of the div to show
*/
function change_slide(the_name, div_id)
{
	var containing_div = document.getElementById(the_name);
	var div_to_show = document.getElementById(div_id);
	eval("var div_to_hide_id = "+the_name+"_current_div; var filter = "+the_name+"_filter; var selection_method = "+the_name+"_slide_selection; var slide_count = "+the_name+"_slide_count; var hold_duration = "+the_name+"_hold_duration;");
	var div_to_hide = document.getElementById(div_to_hide_id);
	
	if( (readIEVer() >= 4.0) && (filter > -1) )
	{
		eval("var blend_duration = "+the_name+"_blend_duration; var reveal_duration = "+the_name+"_reveal_duration;");
		containing_div.style.filter = "blendTrans(duration="+blend_duration+") revealTrans(duration="+reveal_duration+",transition="+filter+")";
		
		containing_div.filters(0).apply();
		containing_div.filters(1).apply();

		div_to_hide.style.display = "none";
		div_to_show.style.display = "block";
		
		containing_div.filters(0).play();
		containing_div.filters(1).play();
	}
	else
	{
		div_to_hide.style.display = "none";
		div_to_show.style.display = "block";
	}
	
	eval(the_name+"_current_div = '"+div_id+"'");
	
	if(selection_method == 1)//random dynamic
	{
		var pieces = div_id.split("_");
		var num = parseInt(pieces[pieces.length-1]);
		
		var next_num = generate_random_new_number(slide_count-1, num);
		var next_id = the_name+"_"+next_num;
		
		setTimeout("change_slide('"+the_name+"', '"+next_id+"')", hold_duration);
	}
	else if(selection_method == 4)//manual
	{
		var next_element = document.getElementById("next_arrow");
		var prev_element = document.getElementById("prev_arrow");
		
		var pieces = div_id.split("_");
		var num = parseInt(pieces[pieces.length-1]);
		var prev_num  = num-1;
		var next_num = num+1;
		
		if(prev_num < 0)
		{
			prev_num = slide_count-1;
		}
		
		if(next_num >= slide_count)
		{
			next_num = 0;
		}

		pieces[pieces.length-1] = next_num;
		var next_href = pieces.join("_");
		
		pieces[pieces.length-1] = prev_num;
		var prev_href = pieces.join("_");
		
		if(prev_element != null && typeof(prev_element) != "undefined")
		{
			prev_element.href = "javascript:change_slide('"+the_name+"', '"+prev_href+"')";
		}
		
		if(next_element != null && typeof(next_element) != "undefined")
		{
			next_element.href = "javascript:change_slide('"+the_name+"', '"+next_href+"')";
		}
	}
	else//sequential dynamic(don't need to check for random static since this function will never be called if it is that selection)
	{
		eval("var stop_show = "+the_name+"_stop_show;");
		
		var pieces = div_id.split("_");
		var num = parseInt(pieces[pieces.length-1]);
		
		if((num+1) < slide_count)
		{
			pieces[pieces.length-1] = num+1;
		}
		else
		{
			if(!stop_show)
			{
				pieces[pieces.length-1] = 0;
			}
			else
			{
				return -1;//no more slides to show, so return to stop
			}
		}
		
		var next_id = pieces.join("_");
		setTimeout("change_slide('"+the_name+"', '"+next_id+"')", hold_duration);
	}
}

/*
the_name = the name of the snapin
div_id = the first slide to display
*/
function slide_show_setup(the_name, div_id)
{
	var div_to_show = document.getElementById(div_id);
	div_to_show.style.display = "block";
	
	eval("var selection_method = "+the_name+"_slide_selection; var slide_count = "+the_name+"_slide_count; var hold_duration = "+the_name+"_hold_duration;");
	
	if(selection_method == 1)//random dynamic
	{
		var pieces = div_id.split("_");
		var num = parseInt(pieces[pieces.length-1]);
		
		var next_num = generate_random_new_number(slide_count-1, num);
		var next_id = the_name+"_"+next_num;
		
		setTimeout("change_slide('"+the_name+"', '"+next_id+"')", hold_duration);
	}
	else if(selection_method == 2)//sequential dynamic
	{
		eval("var stop_show = "+the_name+"_stop_show;");
		
		var pieces = div_id.split("_");
		var num = parseInt(pieces[pieces.length-1]);
		
		if(slide_count > 1)
		{
			pieces[pieces.length-1] = num+1;
		}
		else
		{
			if(!stop_show)
			{
				//don't stop show, so start at the beginning!
				pieces[pieces.length-1] = 0;
			}
			else
			{
				return;
			}
		}
		
		var next_id = pieces.join("_");
		setTimeout("change_slide('"+the_name+"', '"+next_id+"')", hold_duration);
	}
	else if(selection_method == 4)//manual
	{
		var next_element = document.getElementById("next_arrow");
		var prev_element = document.getElementById("prev_arrow");
		
		var pieces = div_id.split("_");
		var num = parseInt(pieces[pieces.length-1]);
		var prev_num  = num-1;
		var next_num = num+1;
		
		if(prev_num < 0)
		{
			prev_num = slide_count-1;
		}
		
		if(next_num >= slide_count)
		{
			next_num = 0;
		}

		pieces[pieces.length-1] = next_num;
		var next_href = pieces.join("_");
		
		pieces[pieces.length-1] = prev_num;
		var prev_href = pieces.join("_");
		
		if(prev_element != null && typeof(prev_element) != "undefined")
		{
			prev_element.href = "javascript:change_slide('"+the_name+"', '"+prev_href+"')";
		}
		
		if(next_element != null && typeof(next_element) != "undefined")
		{
			next_element.href = "javascript:change_slide('"+the_name+"', '"+next_href+"')";
		}
	}
	else//random static
	{
		//since we already have a random slide displayed, we don't need to do anything.
	}
}

