// shared.js script, version 2.1, 08-13-03, Hot Pepper, Inc., http://www.hotpepper.com

// global vars ======================================================
var loc = location.pathname.split('/');
loc.length = loc.length - 1; // removes filename 

var d = location.pathname.replace(/\/[^\/]*/g,'../'); // return one depth too deep, see next line for fix
d = d.substring(0,(d.length-3)); // turns this: ../../ into this ../

function vp_width() {// returns viewport width
	var vp_width = document.body.clientWidth;// for IE and iPhone
	if(document.documentElement.clientWidth) {var vp_width = document.documentElement.clientWidth;}
	else if (window.innerWidth) {var vp_width = window.innerWidth;}
	//alert(vp_width);
	return vp_width;
	}
// /global vars ======================================================

// img preloads =====================================================
/*
hh = new Image(); hh.src = "/pix/hh.gif"; hh_roll = new Image(); hh_roll.src = "/pix/hh_roll.gif";
*/	
// /img preloads =====================================================

// popup window functions ==========================================
function popFlex(URL,winName,W,H,scroll) {
	if (scroll == "no")
		{scroll = "scrollbars=no";}
	else if (scroll == "yes")
		{scroll = "scrollbars";}
	window.open(URL, winName,"top=0,left=30,width="+W+",height="+H+",resizable=no,"+scroll+"");
	}	
// /popup window functions ==========================================

function load_functions() {
	/*
	NOTE: this function MUST be called from body onload, ex: <body onload="load_functions();">
	this avoids render timing issues in Safari
	*/

	// functions to be run on every page:
	var test_width = vp_width();
	//if (test_width > 480) {detect_height();}// if in smart phone, do NOT resize columns!!!!
	if (test_width <= 480) {update_orientation();}// if in smart phone, resize big graphics
	
	//auto_width();
	
	// functions run on a per/page basis. loadfunctions is an array declared in /folder/pagescript.js, each array object represents a function declared either in /shared.js or /folder/pagescript.js
	if (typeof loadfunctions !== 'undefined') {
		for (var n=0; n < loadfunctions.length; n++) {
			eval(loadfunctions[n]+'();');
			}
		}
	}

function update_orientation() {
	window.scrollTo(0, 1);// turns off address bar in iPhone
	
	//alert(vp_width());
	var width = (vp_width()-6);// 3px pad on left and right
	
	document.getElementById('shell').style.width = width+'px';//alert(document.getElementById('shell').style.width);
	
	//move_nav();// in /_includes.php/nav.php
	if (typeof in_wordpress != 'undefined' && in_wordpress == true) {img_resize_ajax('img_resize',width);}
	else {img_resize(width);}
	img_resize2(width);
	}

var resized = new Array();
function img_resize(width) {
	var n = -1;
	for(var k in document.images) {
		var v = document.images[k];
		if (typeof v.width == 'undefined') {continue;}
		n++;
		//alert(v.width);
		resize = false;
		for(var i = 0; i < resized.length;i++) {
			if (resized[i] == v.src) {
				resize = true;
				break;
				}
			}
		if (parseInt(v.width) > width) {resize = true;}
		if (resize) {
			// calc new height according to width =====
			var orig_w = v.width;
			var orig_h = v.height;
			
			var x_ratio = (width/orig_w);
			var new_w = width;//alert(new_w);
			var new_h = Math.ceil(x_ratio * orig_h);//alert(new_h);
			
			v.width = new_w;
			v.height = new_h;
			resized[n] = v.src;
			}// /if (resize) {
		}// /for(var k in document.images) {
	//alert(resized);
	}
var resized2 = new Array();
function img_resize2(width) {
	//alert(width+' -- '+parseInt((width*0.50)));return;
	var n = -1;
	for(var k in document.images) {
		var v = document.images[k];
		if (typeof v.width == 'undefined') {continue;}
		n++;
		//alert(v.width);
		var resize = false;
		for(var i = 0; i < resized2.length;i++) {
			if (resized2[i] == v.src) {
				resize = true;
				break;
				}
			}
		if (parseInt(v.width) < width && parseInt(v.width) > parseInt((width*0.50))) {resize = true;}
		if (resize) {
			// calc new height according to width =====
			var orig_w = v.width;
			var orig_h = v.height;
			
			var x_ratio = (parseInt((width*0.50))/orig_w);
			var new_w = parseInt((width*0.50));//alert(new_w);
			var new_h = Math.ceil(x_ratio * orig_h);//alert(new_h);
			
			v.width = new_w;
			v.height = new_h;
			resized2[n] = v.src;
			}// /if (resize) {
		}// /for(var k in document.images) {
	//alert(resized2);
	}
function img_resize_ajax(process,width) {
	for(var k in document.images) {
		var v = document.images[k];
		if (typeof v.width == 'undefined') {continue;}
		img_resize_ajax_async_request(process,document.images[k],width,v.src);
		}
	}
function img_resize_ajax_async_request(process,obj,width,src) {
	var xhr = createXMLHttpRequest();
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4) {// request is finished
			if(xhr.status == 200) {
				if (xhr.responseText != '') {
					//alert(xhr.responseText);// return text
					img_resize_ajax_async_response(obj,xhr.responseText);// CANNOT return value!!!!!
					//alert(xhr.responseXML);// return xml obj, NOTE: on process.php MUST set the header like: ob_start(); print $xml; header("Content-type: application/xhtml+xml"); ob_end_flush();
					//my_ajax_function(xhr.responseXML);// CANNOT return value!!!!!
					}
				}
			else {
				//alert('XMLHttpRequest error: '+xhr.responseText);
				}
			}
		}
	// send with GET =====
	var url = '/_includes.php/process_img_resize.php?process='+process+'&width='+width+'&src='+src;
	xhr.open('GET',url,true);
	xhr.send(null);
	}
function img_resize_ajax_async_response(obj,response) {
	//alert(response);
	eval('var data = '+response+';');//alert(data);
	//for(var k in data) {alert(data[k]);}
	obj.width = data['width'];
	obj.height = data['height'];
	}

var n = 1;
function crossfade(speed) {
	dissolve(divs_to_fade[n],100,'up',speed);
	n++;
	if (n == divs_to_fade.length) {n = 0;}
	dissolve(divs_to_fade[n],1,'down',speed);
	}
function dissolve(obj_id,o_v,updown,speed) {
	var obj = document.getElementById(obj_id);
	if (obj == null) {return;}
	if (obj.filters) {obj.style.filter = 'alpha(opacity='+o_v+')';}
	obj.style.KHTMLOpacity = o_v/100;
	obj.style.MozOpacity = o_v/100;
	obj.style.opacity = o_v/100;
	if (updown == 'down') {
		if (o_v <= 100) {
			o_v += 5;
			window.setTimeout('dissolve("'+obj_id+'",'+(o_v)+',"'+updown+'","'+speed+'")',speed);
			}
		}
	if (updown == 'up') {
		if (o_v >= 0) {
			o_v -= 5;
			window.setTimeout('dissolve("'+obj_id+'",'+(o_v)+',"'+updown+'","'+speed+'")',speed);
			}
		}
	}

// AJAX functions =============================================================
function createXMLHttpRequest() {
	try{return new ActiveXObject("Msxml2.XMLHTTP");}
	catch (e) {}
	try{return new ActiveXObject("Microsoft.XMLHTTP");}
	catch (e) {}
	try{return new XMLHttpRequest();}
	catch (e) {}
	alert('XMLHttpRequest not supported');
	return null;
	}

function ajax_sync_response_example(table,table_col,table_id) {
	// use this most of the time, stops the js/browser till the server returns the data, kinda like an alert
	// setting the last argument in xhr.open() below to false makes it synchronous
	var xhr = createXMLHttpRequest();
	if (xhr) {
		var url = '/admintools/process.php?process=ajax_validate_row_exists&table='+table+'&table_col='+table_col+'&table_id='+table_id;
		xhr.open('GET',url,false);
		xhr.send(null);
		//alert(xhr.responseText);// return text
		return xhr.responseText;
		//alert(xhr.responseXML);// return xml obj, NOTE: on process.php MUST set the header like: ob_start(); print $xml; header("Content-type: application/xhtml+xml"); ob_end_flush();
		//return xhr.responseXML;
		}
	else {
		alert('An AJAX connection could not be established.');
		}
	}
function ajax_async_response_example(table,table_col,table_id) {
	// do not use this UNLESS the js/browser needs to continue while the server does its task
	// setting the last argument in xhr.open() below to true makes it synchronous, MUST test the response and then  
	var xhr = createXMLHttpRequest();
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4) {// request is finished
			if(xhr.status == 200) {
				if (xhr.responseText != '') {
					//alert(xhr.responseText);// return text
					my_ajax_function(xhr.responseText);// CANNOT return value!!!!!
					//alert(xhr.responseXML);// return xml obj, NOTE: on process.php MUST set the header like: ob_start(); print $xml; header("Content-type: application/xhtml+xml"); ob_end_flush();
					//my_ajax_function(xhr.responseXML);// CANNOT return value!!!!!
					}
				}
			else {alert('XMLHttpRequest error: '+xhr.responseText);}
			}
		}
	// send with GET =====
	var url = '/admintools/process.php?process=ajax_validate_row_exists&table='+table+'&table_col='+table_col+'&table_id='+table_id;
	xhr.open('GET',url,true);
	xhr.send(null);

	// send with POST =====
/*
	var url = '/admintools/process.php';
	var params = 'process=ajax_validate_row_exists&table='+table+'&table_col='+table_col+'&table_id='+table_id;
	xhr.open('POST',url,true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", params.length);
	xhr.setRequestHeader("Connection", "close");
	xhr.send(params);
*/
	}

function ajax_check_unique(inputid,table,col,rowid,value) {
	//alert(inputid+','+table+','+col+','+rowid+','+value);return false;
	trim(value);
	if (value == '') {return;}
	var xhr = createXMLHttpRequest();
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4) {// request is finished
			if(xhr.status == 200) {
				if (xhr.responseText != '') {
					//alert(xhr.responseText);
					ajax_print_msg(inputid,xhr.responseText);
					}
				}
			else {alert('XMLHttpRequest error: '+xhr.responseText);}
			}
		}
	// send with GET =====
	xhr.open('GET','/admintools/process.php?process=ajax_check_unique&table='+table+'&col='+col+'&id='+rowid+'&value='+value,true);
	xhr.send(null);
	// send with POST =====
/*
	var url = '/admintools/process.php';
	var params = 'process=ajax_check_unique&table='+table+'&col='+col+'&id='+rowid+'&value='+value;
	xhr.open('POST',url,true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", params.length);
	xhr.setRequestHeader("Connection", "close");
	xhr.send(params);
*/
	}
function ajax_print_msg(inputid,msg) {
	alert(msg);
	document.getElementById(inputid).focus();
	return false;
	}
// /AJAX functions =============================================================
