var window_x = 0;
var window_y = 0;
var max_width = 1080;
var min_width = 955;
var left_w = 159; //size of top left frame width
var top_h = 239; //size of top left frame height
var mid_w = 710; //size of middle frame width
var mid_h = 437; //size of middle frame height
var right_w = 211; //size of right frame width
var bottom_h = 288; //size of right frame height
var left_percent = 0.147222222; //This is calculated by left frame width divided by full image width
var mid_percent = 0.657407407;
var right_percent = 0.19537037;
var histApp;
var offsetTop;
var sInterval;
var t_height;
var width_diff;
var new_mw;
var m_height;

function HistoryObj() 
{
	var stateVar = "nothin'";

	this.store = function(f_content) 
	{
		unFocus.History.addHistory(f_content);
	};
	
	this.historyListener = function(historyHash) 
	{

		stateVar = historyHash;
		var x = historyHash.split("!");
		document.title = "Valle Verde Arabians - " + x[1];
		get_content2(x[0]);
	};

	unFocus.History.addEventListener('historyChange', this.historyListener);
	this.historyListener(unFocus.History.getCurrent());
};

function get_content_cb(val)
{
	if(val.search("Invalid Template") != -1)
	{
		document.location='http://www.valleverdearabians.com/';
		//alert('INVALID TEMPLATE');
		return;
	}
		
	document.getElementById("main_content").innerHTML = val;
	if(val.search("<=-Gallery") != -1)
	{ 
		var gallery_info = val.search("<=-Gallery");
		//alert(gallery_info);
		var g_number = val.slice(gallery_info + 11,gallery_info + 13);
		var g_pics = val.slice(gallery_info + 14,gallery_info + 16);
		var use_funct = "gallery_funct('" + g_number + "'," + g_pics + ")";
		/*if (typeof use_funct == 'string' && eval('typeof ' + use_funct) == 'function')
		{
			use_funct = use_funct + "()";*/
			//alert(use_funct);
		setTimeout(use_funct,10);
		setTimeout("resize_center(24)",20);
	}
	else
		setTimeout("resize_center(24)",10);
	
}

function update_footer_pos()
{
	var new_size = document.getElementById("main_content").clientHeight;
	//alert(new_size);
	if(new_size < 930)
		new_size = 930;

	document.getElementById("wrapper").style.height = new_size + "px";
	document.getElementById("footer").style.top = new_size + "px";
}

function get_content(filename,ptitle) 
{
	if(filename == '')
		return;
	var hash = filename + "!" + ptitle;
	histApp.store(hash);
	x_get_content_aj(filename,get_content_cb);
}

function get_content2(filename) 
{
	if(filename == '')
		return;
	x_get_content_aj(filename,get_content_cb);
}

function cur_scroll()
{
	var body=document.body;
	var d=document.documentElement;
	if(body && body.scrollTop)
		return body.scrollTop;
	if (d && d.scrollTop)
		return d.scrollTop;
	if (window.pageYOffset)
		return window.pageYOffset;
	return 0;
}

function stop_scroll(e)
{
	if(window.event)
	{
		window.event.cancelBubble = true
		window.event.returnValue = false
		return;
	}
	
	if(e.preventDefault && e.stopPropagation) 
	{
	  e.preventDefault()
	  e.stopPropagation()
	}
}

function scroll(d)
{
	var i = window.innerHeight || document.documentElement.clientHeight;
	var h=document.body.scrollHeight;
	var ad = cur_scroll();
	if(d>ad)
		if(h-d>i)
			ad+=Math.ceil((d-ad)/2);
		else
			ad+=Math.ceil((d-ad-(h-d))/2);
	else
		ad = ad+(d-ad)/2;
		
	window.scrollTo(0,ad);
	if(ad==d || offsetTop==ad)
		clearInterval(sInterval);
	offsetTop=ad;
}

function jump_to(d)
{
	if(d == 'top')
	{
		clearInterval(sInterval);
		sInterval=setInterval('scroll('+0+')',10);
		return;
	}
	
	var ad = document.getElementsByTagName('a');	
	for (i=0;i<ad.length;i++)
	{
		if(ad[i].name == d)
		{
			var ty = ad[i];
			var gy = ty.offsetTop;
			if(ty.offsetParent)
			{
				do {
						//if(ty.offsetParent)
							//alert(ty.offsetParent.tagName);
						gy += ty.offsetTop;
					} while (ty = ty.offsetParent);
			}
			//alert(gy);	 				 
			clearInterval(sInterval);
			sInterval=setInterval('scroll('+gy+')',10);
		}
	
	}
}

function jump_to_2(d)
{
	if(d == 'top')
	{
		clearInterval(sInterval);
		sInterval=setInterval('scroll('+0+')',10);
		return;
	}
	
	var ad = document.getElementsByTagName('a');	
	for (i=0;i<ad.length;i++)
	{
		if(ad[i].name == d)
		{
			var ty = ad[i];
			var gy = ty.offsetTop;
			/*if(ty.offsetParent)
			{
				do {
						if(ty.offsetParent)
							alert(ty.offsetParent.tagName);
						gy += ty.offsetTop;
					} while (ty = ty.offsetParent);
			}*/
			//alert(gy);	 				 
			clearInterval(sInterval);
			sInterval=setInterval('scroll('+gy+')',10);
		}
	
	}
}

function set_window_params()
{
	var cheight = f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);

	var viewportwidth = 666666;
	var viewportheight = 666666;

	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

	if (typeof window.innerWidth != 'undefined')
	{
		viewportwidth = window.innerWidth,
		viewportheight = window.innerHeight
	}

	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

	else if (typeof document.documentElement != 'undefined'
	&& typeof document.documentElement.clientWidth !=
	'undefined' && document.documentElement.clientWidth != 0)
	{
		viewportwidth = document.documentElement.clientWidth,
		viewportheight = document.documentElement.clientHeight
	}

	// older versions of IE

	else
	{
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	//alert('Your viewport width is '+viewportwidth+'x'+);
	
	window_x = viewportwidth;
	window_y = viewportheight;
	
	cheight -= 110;

	if(viewportheight == 666666)
		viewportheight = cheight;
	else
		viewportheight -= 390;
}

function f_filterResults(n_win, n_docel, n_body) 
{
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function resize_frame(main_off)
{
	set_window_params();
	
	var new_lw = window_x * left_percent;
	new_mw = window_x * mid_percent;
	var new_rw = window_x * right_percent;
	//alert(new_lw+new_mw+new_rw);
	if((new_lw + new_mw + new_rw) > max_width + 5)
	{
		new_lw = max_width * left_percent;
		new_mw = max_width * mid_percent;
		new_rw = max_width * right_percent;
	}
	else if((new_lw + new_mw + new_rw) < min_width - 5)
	{
		new_lw = min_width * left_percent;
		new_mw = min_width * mid_percent;
		new_rw = min_width * right_percent;
	}
	var h_percent = new_lw / left_w;
	t_height = top_h * h_percent;
	$("#t_left").width(new_lw);
	$("#t_left").height(t_height);
	$("#t_mid").width(new_mw);
	$("#t_mid").height(t_height);
	$("#t_right").width(new_rw);
	$("#t_right").height(t_height);
	width_diff = (new_rw - new_lw)/2;
	var m_height = mid_h * h_percent;
	$("#m_left").width(new_lw);
	$("#m_left").height(m_height);
	$("#m_mid").width(new_mw);
	$("#menu_table").width(new_mw);
	//$("#menu_td").width(new_mw);
	//$("#menu_td").height(21);
	$("#m_mid").height(m_height);
	$("#m_right").width(new_rw);
	$("#m_right").height(m_height);
	$("#main_content").width(new_mw);
	$("#main_content").height(m_height - main_off);
	var b_height = bottom_h * h_percent;
	$("#b_left").width(new_lw);
	$("#b_left").height(b_height);
	$("#b_mid").width(new_mw);
	$("#b_mid").height(b_height);
	$("#b_right").width(new_rw);
	$("#b_right").height(b_height);
	//alert(new_lw+new_mw+new_rw);
}

function resize_center(main_off)
{
	set_window_params();
	
	var new_lw = window_x * left_percent;
	new_mw = window_x * mid_percent;
	var new_rw = window_x * right_percent;
	//alert(new_lw+new_mw+new_rw);
	if((new_lw + new_mw + new_rw) > max_width + 5)
	{
		new_lw = max_width * left_percent;
		new_mw = max_width * mid_percent;
		new_rw = max_width * right_percent;
	}
	else if((new_lw + new_mw + new_rw) < min_width - 5)
	{
		new_lw = min_width * left_percent;
		new_mw = min_width * mid_percent;
		new_rw = min_width * right_percent;
	}
	var h_percent = new_lw / left_w;
	m_height = mid_h * h_percent;
	//if($("#main_content").height() >= m_height + 20)
	{
		var the_diff = $("#main_content").height() - m_height;
		$("#main_content").height(m_height - the_diff - 50);
	}

}

function gallery_funct(gallery,pics) 
{
	setup_gallery('./images/gallery_' + gallery + '/','image',pics);
}

function HE_Window(url) 
{
	mmwin =window.open(url,"HEwin","toolbar=no,directories=no,menubar=no,resizable=yes,scrollbars=yes,left=0,top=0,width=660,height=585,screenX=10,screenY=10,");
}
