//Added 10/7/09 - jQuery page load items, new jQuery menu items//

///NEW MENU ITEMS - STANDALONE FUNCTIONS//
//global timer for menu
var timer;
//position flynavs
function positionNavs()
{
	$("div[id*=navOpt]").each(function()
	{
		var ind = this.id.substr(this.id.length-1, 1); //navOptX corresponds to flymenuX
		var div = $(this);
		var pos = div.offset();
		var top = pos.top + div.height()-11;
		var left = pos.left + div.width()-15;
		$("div#flymenu"+ind).css({"top" : top, "left" : left});
	});
}

//hide all menus
function hideAllMenus()
{
	$("div[id*=flymenu]").each(function()
	{
		if($(this).is(':visible')) 
		$(this).hide("fast");
	});
}

//jQuery page load
$(function()
{
	//NEW MENU ITEMS - SET STYLES AND EVENTS ON LOAD//
	//assign click handler to menu links - so it can be triggered later.
	$("a.menulink[target],a.flyoutlink[target]").click(function(e)
	{
		var params = $(this).attr("params") ? $(this).attr("params") : ""; 
		window.open($(this)[0].href, "_new", params);
		return false;
	});
	//links without a target property
	$("a.menulink:not([target]),a.flyoutlink:not([target])").click(function(e)
	{
		document.location.href = $(this)[0].href;
		return false;
	});
	//Assign jquery click handler to the links in the nav. new window or not is based on target prop. 
	//all the navOptions get a click handler
	$("div.navOption,div.flyout").click(function(){$(this).children().click();});  
	
	//for each flymenu, the last child div has no border on bottom
	$("div[id*=flymenu]").each(function()
	{
		$(this).find("div:last").css("border-bottom", "none");
	});
	//assign over / out for flyout menu classes
	$("div.flyout").mouseover(function()
	{
		$(this).addClass("flyouthover");
	});
	$("div.flyout").mouseout(function()
	{
		$(this).removeClass("flyouthover");
	});
	//menuoptions with flyouts - handlers - mouse over and out
	$("div[id*=navOpt]").mouseover(function()
	{
		//clear timeout, if exists
		clearTimeout(timer);
		//hide menu, if it doesn't correspond to this option
		var ind = this.id.substr(this.id.length-1, 1);
		$("div[id*=flymenu]").each(function()
		{
			if(this.id.indexOf(String(ind)) == -1) 
				$(this).hide("fast");
		});
		//show corresponding menu
		var menu = $("div#flymenu"+ind);
		if(!menu.is(':visible')) 
			menu.show("fast");
	});
	
	//set a timeout for the mouse out of the flymenu or nav options divs
	$("div[id*=flymenu], div[id*=navOpt]").mouseout(function()
	{
		timer = window.setTimeout(hideAllMenus, 500);
	});
	//for flymenu, it's children, and the menuoption associated, assign over behaviour
	$("div[id*=flymenu], div[id*=flymenu]>* ").mouseover(function()
	{
		clearTimeout(timer);
	});
	
	//position the navs and set to position again on resize
	positionNavs();
	$(window).resize(positionNavs);
	
	//////////BUBBLE POPUP SCRIPTS////////////
	//popup bubble scripts
	var beingShown = false;
	var shown = false;
	var time = 250;
	var hideDelay = $.browser.msie ? 1000 : 250;
	var hideDelayTimer = null;

	//set correct style property for non-IE browsers for bubble
	if(!$.browser.msie)
		$("#bubble").css({"opacity" : "0", "display" : "block"}); 
	else
		$("#bubble").css("width", "500px"); //ie doesn't support max width inside the blog
		
	
	//set behavior for the text bubble link
	$(".trigger, #bubble").mouseover(function()
	{
		if (hideDelayTimer) clearTimeout(hideDelayTimer);
		if (beingShown || shown || (this.id == "bubble" && beingShown == false && shown == false)) 
			return;// don't trigger the animation when bubble is already shown or has been rolled over in invisible state (non-ie)
			
		biengShown = true;
		
		function showBubble(aTag)
		{
			//get bubble width & height and set vars
			var paddinghei = -15;
			var paddingwid = 5;
			var bubble = $("#bubble");
			var widthDistance = (bubble.width() + paddingwid) / 2;
			var heightDistance = bubble.height() + paddinghei;
			
			//position item
			//var aTag = $(this);
			var pos = aTag.offset();
			var top = Math.floor(pos.top - heightDistance);
			top = top > 0 ? top : 0;
			var left = Math.floor(pos.left + (aTag.width()/2) - widthDistance);
			left = left > 0 ? left : 0;
			bubble.css({"top" : top, "left" : left });
			if($.browser.msie)
			{
				$("#bubble").css("display", "block");
				beingShown = false;
				shown = true;
			}
			else
				bubble.animate({opacity: 1},time, 'swing', function() {beingShown = false; shown = true;});
		}
		
		//ajax content
		var atag = $(this); //referring to the anchor tag
		var divID = this.href.substr(this.href.indexOf("#")); 
		var loadArg = this.href + " div" + divID;
		$("#popup-contents").load(loadArg , function(){showBubble(atag)});
		
				
	}).mouseout(function()
	{
		if($.browser.msie)
		{
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout
			(
				function () 
				{
					$("#bubble").css("display", "none");
					shown = false;
				}, hideDelay
			);
			//$("#bubble").css("display", "none");
			//shown = false;
		}
		else
		{
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout
			(
				function () 
				{
					$("#bubble").animate({opacity: 0},time, 'swing', function() {shown = false;$("#bubble").css("top","-200px");})
				}, hideDelay
			);
		}
	});
	
});


//redirect for clearwaterzen.org - replaced with asp code in i-header - probably no longer needed.
var address = document.location.href;
if(address.indexOf("clearwaterzen") > -1) document.location.replace("http://www.clearwaterzen.org/clearwater/");

//set / read cookies
function setCookie(cookieName,cookieValue,nDays,path) 
{
	path = path ? path : "/"
	var today = new Date();
	var expire = new Date();
	nDays = nDays ? nDays : 1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue)
					 + ";expires="+expire.toGMTString()+";path="+path;
}
function readCookie(cookieName) 
{
 	var theCookie=""+document.cookie;
	 var ind=theCookie.indexOf(cookieName);
 	if (ind==-1 || cookieName=="") return ""; 
	 var ind1=theCookie.indexOf(';',ind);
	 if (ind1==-1) ind1=theCookie.length; 
	 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}
//opens a new window
function goEx(URL,width,height)
{
	var winProps="toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,height=" +
    	((height==null || height=='') ? 630 : height) + ",width=" + ((width==null || width=='') ? 800 : width);
  	window.open(URL,"Win",winProps);
	return false;
}
//backwards compat - 2 functions
function goTo(URL, targ, hei, wid){goEx(URL, wid, hei);}
function GoPage(URL,target,height,width){goEx(URL,width, height);}

//get a random number between 0 and x
function getRand(len)
{
	return Math.floor(Math.random()*len)
}

function doPrintView(id)
{
	alert("Print View coming soon!");
	//TODO
}	

function PageQuery(q) 
{
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; }	
}
function queryString(key){
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}