var browser=navigator.appName;
var opacity_limit = 100;
var counter=0;
var cnt = 0;
var fade_in_progress = false;

function get_function_name(strfunc){
	
	// thanks to http://www.sitepoint.com/forums/showthread.php?t=519952
	var callerfunc=/^function\s+([a-z0-9_\-\$]+)/im.exec(strfunc)[1];
	return callerfunc;
}

function funct(a){ // input is arguments
	// constructs a function name and comma seperated parameters list. Parameter strings are quoted ie, 'out'
	var params=new Array();
	for(var i=0; i<a.length; i++){
		params[i] = (typeof(a[i]) == 'string') ? "'"+a[i]+"'" : a[i];
	}
	// construct fname(p1,pn) 
	var name = get_function_name(a.callee);
	return name + "(" + params.join(",")  + ")";
}

function setOpacity(obj,opacity){
	// Set the opactity depending upon the browser
	opacity = (opacity == 100)?99.999:opacity;
	switch (browser){
		case "Microsoft Internet Explorer":
			obj.style.filter = "alpha(opacity:"+opacity+")";
			break;
		case "Opera":		// guess
		case "Safari":		// guess
		case "Netscape":	// FireFox
			obj.style.opacity = opacity/100;
			break;
		default:			// what the hey
			obj.style.opacity = opacity/100;
	}
}

function fade(objId,opacity,direction,op_step,st_delay) {
    
  // alert(objId + " " + opacity + " " + direction + " " + op_step + " " + st_delay);
  	counter++; // debugging
	if (document.getElementById) {
		var obj = document.getElementById(objId);
		// if (!obj) {alert('error');}
		if ( (opacity >= 0) && (opacity <= opacity_limit) ) {
			setOpacity(obj, opacity);
			if (direction == 'in') {
				opacity += op_step;
			}else{
				opacity -= op_step;
			}
			// var s = "fade('"+objId+"',"+opacity+",'"+direction+"',"+op_step+","+st_delay+")";
			var this_function = funct(arguments);
      		window.setTimeout(this_function,st_delay);
		}else {
			fade_in_progress=false;
			cnt++; //debugging
			// document.getElementById("X").innerHTML='end of fade ' + cnt;
			return true;
		}
	}
	// document.getElementById("next").innerHTML='<b>exit fade() </b> ' + counter;
	return false;	// here when returning from the calling function once and then (I guess) handle by the setTimeout()
}


function schedule_display_buttons(){	
	var base=0;
	var a=Array();
	a[1]=0; 						// Initial opacity
	a[2]="'in'";					// Direction with added single quotes
	a[3]=1;							// Opacity step 
	a[4]=2;						// Step delay
	for (var i in button_ids){						// fires off a time for each button
		base=base+250;
		a[0]="'" + button_ids[i] + "'"; // Id with added single quotes
		var f = "fade" + "(" + a.join(",")  + ")";
		window.setTimeout(f, base);
	}
}

function schedule_display_main_page(){	
	var base=1000;
	var a=Array();
	a[0]="'" + "front_page_container" + "'";	// Id with single quotes
	a[1]=0; 								// Initial opacity
	a[2]="'in'";							// Direction with added single quotes
	a[3]=1;									// Opacity step 
	a[4]=2;									// Step delay
	var f = "fade" + "(" + a.join(",")  + ")";
	window.setTimeout(f, base);
}
