// -------------------------------------------
// GENERAL JAVASCRIPT FUNCTIONS
// BKWLD.com
// -------------------------------------------

//open a centered popup
function popup(url,width,height,title,scrollbar,resizable,center) {
	
	//set defaults
	if (width == null) width = 600;
	if (height == null) height = 450;
	if (title == null) title = 'auto_popup';
	if (scrollbar == null) scrollbar = false;
	if (resizable == null) resizable = false;
	if (center == null) center = true;

	//set props
	scrollbar = scrollbar == true ? 'yes' : 'no';
	resizable = resizable == true ? 'yes' : 'no';
	winProperties = 'height='+height+',width='+width+',toolbar=no,location=no,scrollbars='+scrollbar+',titlebar=no,menubar=no,resizable='+resizable+',status=no';
	
	//open and move
	win = window.open(url,title, winProperties);
	if (!win) alert('Please disable your popup blocker to use this site.');
	else if (center) {
		if (self.screen) {
			sw = self.screen.width;
			sh = self.screen.height;
		} else return;
		win.moveTo((sw-width)/2,(sh-height)/2);
	}
	
}


//get element ref
function getElement(name){
	var dom = document.getElementById;
	var iex = document.all;
	var ns4 = document.layers;
	var el = dom ? document.getElementById(name) : iex ? document.all[name] : ns4 ? eval(nest+"document."+name) : false;
	el.css = ns4 ? el : el.style;
	return el;
}

//write within tags of an element
function writeTo(id,text,append) {
	var el = getElement(id);
	switch(append) {
		case 'top newline':
		case 'debug':
			text += '<br>';
		case 'top':
		case 'left':
			el.innerHTML = text + el.innerHTML
			break;
		case 'bottom':
		case 'right':
		case true:
			el.innerHTML += text;
			break;
		default:
			el.innerHTML = text;
			break;
	}
}

//swap the visiiblity of an element
var old_swap_vis_id = null;
function swapVis(id) {
	var el = getElement(id);
	if (old_swap_vis_id) old_swap_vis_id.css.display="none";
	el.css.display="";
	old_swap_vis_id = el;
}

//get browser size
//http://www.quirksmode.org/viewport/compatibility.html
function getWindowSize() {
	var x,y;
	
	// all except Explorer
	if (self.innerHeight) {
		x = self.innerWidth;
		y = self.innerHeight;
	// Explorer 6 Strict Mode
	} else if (document.documentElement && document.documentElement.clientHeight) {
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	// other Explorers
	} else if (document.body) {
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return new Array(x,y);
}

//get a variable from the get
//courtesy of Ben
function getQueryVariable (variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
}

// DYNAMIC RESIZING OF FLASH MOVIES DONE USING THIS:
// http://hossgifford.com/downloads.htm
// I did make some changes to it though
function resizeSWF(id,nWidth,nHeight,parent_div) {
	
	//defaults
	if (nWidth == null || !nWidth) nWidth = '';
	if (nHeight == null || !nHeight) nHeight = '';
	if (parent_div == null) parent_div = id+'_div';
	nWidth = nWidth.toString();
	nHeight = nHeight.toString();
	
	//resize width
	if (nWidth.length > 0) {
		if (nWidth.substr(nWidth.length-2) != 'px' && nWidth.substr(nWidth.length-1) != '%') nWidth += "px";
		document.getElementById(id).style.width = nWidth;
		document.getElementById(parent_div).style.width = nWidth;
	}
	
	//redize height
	if (nHeight.length > 0) {
		if (nHeight.substr(nHeight.length-2) != 'px' && nHeight.substr(nHeight.length-1) != '%') nHeight += "px";
		document.getElementById(id).style.height = nHeight;
		document.getElementById(parent_div).style.height = nHeight;
	}
}

//stop 100% flash movie from shrinking below a certain size
function swfSizeMin(swf_id,min_w,min_h,parent_div) {

	//set it up to update
	window.onresize = function() {
		swfSizeMin(swf_id,min_w,min_h,parent_div)
	}
	
	//get window size
	var sizes = getWindowSize();
	var win_w = sizes[0];
	var win_h = sizes[1];

	//figure out what to set flash size to
	if (win_w < min_w) var w = min_w;
	else var w = "100%";
	if (win_h < min_h) var h = min_h;
	else var h = "100%";

	//set the size
	resizeSWF(swf_id,w,h,parent_div);

}
