// JavaScript Document
$(function()
{
	$("#footer").html("<div>Windhorse Zen Community 580 Panther Branch Rd, Alexander, NC, 28701. 828-645-8001</div>");

	$("#banner").html('<table id="tblHeader" cellpadding="0" cellspacing="0" border="0" style="width:900px;" align="center"><tr><td id="tdHeader"><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td><a href="../../../../default.asp"><img src="../../../../images/logoMain.jpg" width="192" height="66" border="0" /></a></td><td align="center"><img src="../../../../images/imgSiteTitle.jpg" width="391" height="37" border="0" /></td><td width="192" valign="bottom" align="right"><a href="../../../../default.asp" style="color:#FFFFFF;">Home </a></td></tr></table>');
	
	$(".container").prepend("<table id='tblPageTitle' border='0' cellpadding='0' cellspacing='0' width='100%'><tr><td class='headertext' style='padding-top:5px'>Windhorse Latest News </td><td class='headertext' style='font-size:11pt;color:#585858;font-style:italic;text-align:right;padding-top:5px'>Community Blog</td></tr></table><hr class='hr' />");
	
	var bannerSource;
	try
	{
		bannerSource = bannersource ? bannersource : "bannerNews2.jpg";
	}
	catch(e)
	{
		bannerSource = "bannerNews2.jpg";
	}
	
	$("#pagebanner").attr("src", "../../../../images/banners/"+bannerSource);
	
	$("#s").val("search news...").focus(function()
	{
			this.select();
	});
	
	$("div.com>div>div:last").remove();
	
	//assign the #comments id to the div with class "com", fix bug in template
	//comments link not working
	var comdiv = $("div.com");
	if(comdiv.length == 0) 
		comdiv = $("div.leavereply");
		
	comdiv.attr("id","comments");
	
	
	//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
			);
		}
	});
	
});

	

///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);
	
});

//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;
}