// shared.js script, version 2.1, 08-13-03

// global vars ======================================================
var in_wordpress = true;

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 =====================================================

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) {update_orientation();}// if in smart phone, resize big graphics
	}

function update_orientation() {
	window.scrollTo(0, 1);// turns off address bar in iPhone
	
	var width = (vp_width()-6);//alert(vp_width());// 3px pad on left and right AND 1px stroke
	
	//if (document.getElementById('') != null) {document.getElementById('').style.width = width+'px';}
	if (document.getElementsByClassName('column_small') != null) {
		for (var n=0;n<document.getElementsByClassName('column_small').length;n++) {document.getElementsByClassName('column_small')[n].style.width = width+'px';}
		}
	if (document.getElementsByClassName('column_medium') != null) {
		for (var n=0;n<document.getElementsByClassName('column_medium').length;n++) {document.getElementsByClassName('column_medium')[n].style.width = width+'px';}
		}
	if (document.getElementsByClassName('column_large') != null) {
		for (var n=0;n<document.getElementsByClassName('column_large').length;n++) {document.getElementsByClassName('column_large')[n].style.width = width+'px';}
		}
	if (document.getElementById('copyright') != null) {document.getElementById('copyright').style.width = width+'px';}
	if (document.getElementById('header-wrap') != null) {document.getElementById('header-wrap').style.width = width+'px';}
	if (document.getElementById('header') != null) {document.getElementById('header').style.width = width+'px';}
	if (document.getElementById('featured') != null) {document.getElementById('featured').style.width = width+'px';}
	if (document.getElementById('featured-alt') != null) {document.getElementById('featured-alt').style.width = width+'px';}
	if (document.getElementById('footer') != null) {document.getElementById('footer').style.width = width+'px';}
	if (document.getElementById('footer_navigation') != null) {document.getElementById('footer_navigation').style.width = width+'px';}
	if (document.getElementById('main') != null) {document.getElementById('main').style.width = width+'px';}
	if (document.getElementById('tagline-center') != null) {document.getElementById('tagline-center').style.width = width+'px';}
	if (document.getElementById('tagline-right') != null) {document.getElementById('tagline-right').style.width = width+'px';}
	if (document.getElementById('wrap') != null) {document.getElementById('wrap').style.width = width+'px';}
	if (document.getElementsByClassName('portfolio') != null) {
		for (var n=0;n<document.getElementsByClassName('portfolio').length;n++) {
			var portfolio = document.getElementsByClassName('portfolio')[n];
			if (portfolio.getElementsByClassName('wrapper') != null) {
				for (var i=0;i<portfolio.getElementsByClassName('wrapper').length;i++) {
					portfolio.getElementsByClassName('wrapper')[i].style.width = width+'px';
					}
				}
			}
		}
	
	if (typeof in_wordpress != 'undefined' && in_wordpress == true) {img_resize_ajax('img_resize',width);}
	else {img_resize(width);}
	img_resize2(width);
	//if (typeof document.getElementById('divid1') != 'undefined') {div_resize(width,document.getElementById('bgimg'),0);}
	//if (typeof document.getElementById('divid2') != 'undefined') {div_resize(width,document.getElementById('bgimg'),1);}
	
	//move_nav();// in /_includes.php/nav.php
	}

var div_resized = new Array();
function	div_resize(width,obj,n) {
	if (obj.style.width != '') {
		var orig_w = parseInt(obj.style.width);//alert(orig_w);
		var orig_h = parseInt(obj.style.height);//alert(orig_h);
		}
	else {
		var orig_w = parseInt(window.getComputedStyle(obj,null).getPropertyValue("width"));//alert(orig_w);
		var orig_h = parseInt(window.getComputedStyle(obj,null).getPropertyValue("height"));//alert(orig_h);
		}
	
	var resize = false;
	for(var k in div_resized) {
		if (div_resized[k] == obj.id) {
			resize = true;
			break;
			}
		}
	if (orig_w > width) {resize = true;}
	if (resize) {
		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);
		
		obj.style.width = new_w+'px';
		obj.style.height = new_h+'px';
		div_resized[n] = obj.id;
		}
	}
var img_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 < img_resized.length;i++) {
			if (img_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;
			img_resized[n] = v.src;
			}// /if (resize) {
		}// /for(var k in document.images) {
	//alert(img_resized);
	}
var img_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 < img_resized2.length;i++) {
			if (img_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;
			img_resized2[n] = v.src;
			}// /if (resize) {
		}// /for(var k in document.images) {
	//alert(img_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 = '/wp-content/themes/delericon_m/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'];
	}


// 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 =============================================================


