// JavaScript Document
/*
Do Not edit below line
________________________________________________________________________________
*/
var i = 0;
var timer_id = null;
var rotate_time = 1000;// this is miliseconds
var speed = Math.round(rotate_time / 100);//tranition time
var div_id = 'main_holder';//This is the div that holds the main pic
var image_id = "main_pic";// This is the main Picture ID
var thumb_div_id = 'thumb_div';
function start() {
	
	//blend_image(0);
	change_image(0, 0);
	make_thumb_table();
	//timerID = setTimeout('start()', rotate_time);
}
/**
######################################################################
**/
function blend_image(count) {
	i = 100;
	timer_id = setTimeout("fade('out', true, "+count+", true);", speed );
	var timer_id2 = null;
	var timer = 0;
	//set the backgrond
	//set new background
	//bg_image = ''+path+'/'+pic_array[count][1];
	//document.getElementById(div_id).style.background = " url('"+bg_image + "') 5px 5px no-repeat";
	//fade in image
	/*
	timer_id = setTimeout("changeOpac(" + i + ",'" + image_id + "')",(speed));
	for(i = 100; i >= 0; i--) {
		
		timer++;
		if(i == 0) {
			clearTimeout(timer_id);
			//switch background to the top image
			imagefile = ''+path+pic_array[count][1];
			document.getElementById(image_id).src = imagefile;
			//set opac to 100 for image
			timer = 0;
			for(a = 1; a <= 100; a++) {
				timer_id2 = setTimeout("changeOpac(" + a + ",'" + image_id + "')",(  speed));
				timer++;
				if(a == 100) {
					clearTimeout(timer_id2);
					//changeOpac(100, image_id);
					//clear background
					//bg_image = ''+path+'/'+pic_array[count][1];
					//document.getElementById(div_id).style.background =	" color:#fff ";
				}
			}
		}
	}
	*/
}
function fade(dir, change, count, fade_in){
	
	if( dir == "in" ){
		changeOpac(i, image_id );
		i = i +2;
	}
	else{
		changeOpac(i, image_id );
		i = i -2;
	}
	//alert("test - i: "+i+"  Speed: "+speed);
	if(i >= 100 || i <= 0){
		//Clear timer
		clearTimeout(timer_id);
		if(change && (i == 100 || i == 0) ){
			//change image
			imagefile = ''+path+pic_array[count][1];
			document.getElementById(image_id).src = imagefile;
			
		}
		if( fade_in ){
			//call the timer 
			//fade('in', false, count, false);
			timer_id = setTimeout("fade('in', false, "+count+", false);", speed );
		}
	}
	else{
		timer_id = setTimeout("fade('"+dir+"', "+change+", "+count+", "+fade_in+");", speed );
	}
}
function change_image(count, open_win){
	//change image
	imagefile = ''+path+pic_array[count][1];
	document.getElementById(image_id).src = imagefile;
	// open new window
	if( open_win == 'y' ){
		openWindow_pic(count);
	}
	isIE = (document.all ? true : false);
	isDOM = (document.getElementById ? true : false);
	//Write the Text count
	text_id = "replace_text";
	num = 1 + count;
	if (isIE) {
		var elt = document.all[text_id];
		elt.innerHTML = "Photo #"+num;
	} else if (isDOM) {
		var elt = document.getElementById(text_id);
		elt.innerHTML = "Photo #"+num;
	} else {
		var elt = document.layers[text_id].document;
		elt.open();
		elt.write("Photo #"+num);
		elt.close();
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}
/**************************************/

/***************/
window.onload = function () {
	start();
}

/*******************
	Make the table
********************/

function make_thumb_table() {
	var thumb_grid;
	var style_class;
	var total_thumb = pic_array.length;// This is count(array);
	var count = 0;
	
	var num_rows = Math.ceil(total_thumb/num_columns);
	
	thumb_grid = '<table id="thumbnails">';
	// Outer loop - tr
	for (var x = 0; x < num_rows; x++) {
		thumb_grid += "<tr>";
		// INner loop columns - td
		for (var y = 0; y < num_columns; y++) {
			//count = (x * 7) + y + dayOfMonthOfFirstSunday - 7;
			if (count < 0 || count >= total_thumb) {
				thumb_grid += '<td class="no_thumb">&nbsp;</td> ';
			}
			// Make the thumb Nail
			else if (count < total_thumb) {
				var color = "blue";
				style_class = '';
				//if (day > 0 && day == count){// day is sent down - current thumb
					//style_class=' class="active" ';
				//}
				//else{
				thumb_grid += '<td'+style_class+'><span class="count">'+(count+1)+'</span><a title="Click to see this Photo" href="javascript:change_image(';
				//blend_image(';
				thumb_grid += count+', \'y\')" >';
				thumb_grid += '<img src="'+path+pic_array[count][0]+'" alt="Stair Photo" /></a>';
				thumb_grid += '</a></td>';
				count++;
			}
		}
		thumb_grid += "</tr>";
	}
	thumb_grid += "</table>";
	//alert("Test"+thumb_grid);
	
	isIE = (document.all ? true : false);
	isDOM = (document.getElementById ? true : false);
	//Write the Table:
	if (isIE) {
		var elt = document.all[thumb_div_id];
		elt.innerHTML = thumb_grid;
	} else if (isDOM) {
		var elt = document.getElementById(thumb_div_id);
		elt.innerHTML = thumb_grid;
	} else {
		var elt = document.layers[thumb_div_id].document;
		elt.open();
		elt.write(thumb_grid);
		elt.close();
	}
}
/*********
	Open window
**********/
function openWindow_pic(count) { //v2.0
	var theURL = openpath + '?count=' + count;
	var features = 'scrollbars=yes,resizable=yes,width=940,height=600';
	window.open(theURL,'StairPartsUSA',features);
}
