var g_WIDTH=1024;
var g_HEIGHT=768;
var pageAssociation={};

var rrSettings = {
  tl: { radius: 10 },
  tr: { radius: 10 },
  bl: { radius: 10 },
  br: { radius: 10 },
  antiAlias: true
}

function centerContent() {
	var w=parseInt($(window).width());
	var h=parseInt($(window).height());
	if (w<g_WIDTH) {
		$("#content-main").css("margin-left",-(w/2)+"px");
	}
	else {
		$("#content-main").css("margin-left","-512px");
	}
	if (h<g_HEIGHT) {
		$("#content-main").css("top",-(h/2)+"px");
	}
	else {
		$("#content-main").css("top","-384px");
	}
	
}

function hideCurrent(obj) {
	obj.each(function(index) {
		var tobj=$(this);
		var objCurrent=$(this).attr("current");
		if (objCurrent=="true") {
			tobj.css("display","none");
		}
	});
	hideCurrentFlyout();
}
function hideCurrentFlyout() {
	var obj=$('div[id^="flyout-"]');
	if (!obj.length) return;
	obj.each(function(index) {
		var tobj=$(this);
		var objCurrent=$(this).attr("currentFlyout");
		if (objCurrent=="true") {
			tobj.css("display","none");	
		}
	});
}

function showFlyOut(objid,parentid) {
	hideCurrentFlyout();
	var menuItem=$("#menuitem-"+objid);
	var flyout=$("#flyout-"+objid);
	var parentmenu=$("#"+parentid);
	var y=menuItem.offset().top;
	var x=parentmenu.offset().left+parentmenu.width()+10;
	flyout.css("display","block");
	flyout.offset({top:y,left:x});
	flyout.attr("currentFlyout","true");
	
	//curvyCorners(rrSettings, flyout[0]);
}

function showDropDown(objid) {
	hideCurrentFlyout();
	var dropdowns=$('div[id^="menu-dropdown"]');
	hideCurrent(dropdowns);
	var menuItem=$("#menu-item-"+objid);	
	var dropDown=$("#menu-dropdown-"+objid);
	var y=menuItem.offset().top+18;
	var x=menuItem.offset().left;
	if (!dropDown) return;
	dropDown.attr("current","true");
	dropDown.css("display","inline-block");
	dropDown.offset({top:y,left:x});
	
}

function findPage(itmid) {	
	var myPage=document.location.toString().toLowerCase();
	if (itmid=='events' && myPage.indexOf("wp/?cat=3")>-1) return true;
	else if (itmid=='market_news' && myPage.indexOf("/wp/")>-1
			&& myPage.indexOf("?cat=3")<0) return true;
	
	if (itmid=='recipes' && myPage.indexOf("recipe_results")>-1) return true;
	if (itmid=='recipes' && myPage.indexOf("recipeviewer")>-1) return true;

	return false;
}

function initRollovers() {
	var menuItems=$('div[id^="menu-item"]');
	menuItems.css("cursor","hand");
	menuItems.each(function(index) {	
			var obj=$(this);
			var objid=$(this).attr("id");
			var objImg=$("img",this);
			var tag=objid.replace("menu-item-","menu-dropdown-");
			var ddObj=$("#"+tag);
			if (ddObj.length) {
				var ddHR=$("hr",ddObj);
				if (ddHR.length) {
					ddHR.width(ddObj.width()-5);
				}
			}
			var newImgSrc=objid.replace("menu-item-","");
			newImgSrc=newImgSrc.replace("-","_");
			var itmid=newImgSrc;
			var onpage=(document.location.toString().indexOf(itmid)>1);
			if (!onpage) onpage=findPage(itmid);
			newImgSrc=prfx+"images/global/menu/"+newImgSrc+"_over.png";
		
			if ($("#mainPage").length || onpage) {
				objImg.attr("src",newImgSrc);
			}
				
	});
	

	$("#primary").mouseover(function() {
		var dropdowns=$('div[id^="menu-dropdown"]');
		hideCurrent(dropdowns);
	});
	$("#content").mouseover(function() {
		var dropdowns=$('div[id^="menu-dropdown"]');
		hideCurrent(dropdowns);
	});
	$("#header-title-content").mouseover(function() {
		var dropdowns=$('div[id^="menu-dropdown"]');
		hideCurrent(dropdowns);
	});
	$("#tec-content").mouseover(function() {
		var dropdowns=$('div[id^="menu-dropdown"]');
		hideCurrent(dropdowns);
	});
	$("#big").mouseover(function() {
		var dropdowns=$('div[id^="menu-dropdown"]');
		hideCurrent(dropdowns);
	});
	$("#main-table").mouseover(function() {
		var dropdowns=$('div[id^="menu-dropdown"]');
		hideCurrent(dropdowns);
	});
	var infoCell=$('div[class="info-cell"]')[0];
	if (infoCell && (typeof infoCell != 'undefined'))
		$('div[class="info-cell"]').corner();
		//curvyCorners(rrSettings, $('div[class="info-cell"]')[0]);

	$( 'div[id^="menu-item"]' ).mouseover(function() {
		var obj=$(this);
		var objid=$(this).attr("id");
		var objImg=$("img",this);
		var newImgSrc=objid.replace("menu-item-","");
		showDropDown(newImgSrc);
		newImgSrc=newImgSrc.replace("-","_");
		newImgSrc=prfx+"images/global/menu/"+newImgSrc+"_over.png";
		objImg.attr("src",newImgSrc);
	});
	$( 'div[id^="menu-item"]' ).mouseout(function() {
		var obj=$(this);
		var objid=$(this).attr("id");
		var objImg=$("img",this);
		var newImgSrc=objid.replace("menu-item-","");
		newImgSrc=newImgSrc.replace("-","_");
		var itmid=newImgSrc;
		var onpage=(document.location.toString().indexOf(itmid)>1);
		if (!onpage) onpage=findPage(itmid);
		newImgSrc=prfx+"images/global/menu/"+newImgSrc+".png";
		if (!$("#mainPage").length &&
			!onpage) objImg.attr("src",newImgSrc);
	});	
	
}


/*
<a href="http://freshfarmmarkets.org/wp/?p=23#more-23" class="more-link">&nbsp;&nbsp;&nbsp;more <span class="meta-nav">»</span></a>
*/
function resizeAsNecessary() {
	var mhDivs=$('div[maxheight]');
	
	mhDivs.each(function(index) {
		var obj=$(this);
		var oh=obj.height();
		var mh=parseInt(obj.attr("maxheight"));
		var suffx="";
		if (mh<oh) {
			var txt=obj.html();
			txt=txt.replace(/^\s+|\s+$/g, '');
			var patt=new RegExp("\<a[ ]+href=\"(.*)?\"[ ]+class=\"more-link\".*?<\/span><\/a>","ig");
			var res=patt.exec(txt);
			if (res && res.length>1) {
				var morelnk=res[0];
				suffx=morelnk;
				txt=txt.replace(morelnk,"");				
			}
			else {
				if (obj.attr("postid")) {
				   var postid=obj.attr("postid");
				   suffx="&nbsp;&nbsp;<a href='http://freshfarmmarkets.org/wp/?p="+postid+"'>more &raquo;</a>";
				}
			}
			
			var k=txt.length-1;
			var tmpTxt=txt.toLowerCase();
			var lastTag="";
			while (tmpTxt.charAt(k)!='<' && k>0) {
				lastTag+=tmpTxt.charAt(k);
				k--;
			}
			lastTag+=tmpTxt.charAt(k);
			if (lastTag==">p/<") suffx+="</p>";
			
			while ((txt.charAt(k)=='<' || txt.charAt(k)==' ') && k>0) k--;
			txt=txt.substring(0,k);
			while (obj.height()>mh) {
				while (txt.charAt(k)!=' ' && k>0) k--;
				txt=txt.substring(0,k);
				obj.html(txt+suffx);
			}
		}
	});

}

$(document).ready(function(){
   	//centerContent();
   	initRollovers();
   	resizeAsNecessary();
});

$(window).resize(function() {
  // centerContent();
});
