$(document).ready(function() {
//grab total dimension of the page by its image widths.
	function getActive(n){
		// Grab url and strip everything after php and split by "/"
		var myurl = document.location;
		myurl = myurl.toString();
		myurl = myurl.toLowerCase();
		charlength = myurl.length;
		var incount = myurl.indexOf('.php');
		incount = incount + 5;
		myurl = myurl.substring(incount, charlength);
		var segments = new Array();
		if(incount > 4){ // amount of spaces after for .php
			segments = myurl.split('/');	
		}
		return segments;
	}
	
	function activeLinks(n, p, x){ //n is the array. p is the location, x is the iteration.
		if(x === undefined){
			x = 0;
		}
			q = p.attr('href');
			check = q.match(n[x]);
			loc = "";
			loc = document.location;
			loc = loc.toString();
			loc = loc.toLowerCase();
			charlength = loc.length;
			ncount = loc.indexOf('.php');
			ncount = ncount -6;
			loc = loc.substring(ncount, charlength);
			
			if(check != null && q != ''){
				p.parent('li').removeClass('active1');
				x++;
				
				if(loc == q){
					p.parent('li').addClass('active5');
				
				}else{
				//see if last entry matches url				
				p.parent('li').addClass('active1');
				}
				//activeLinks(n, p, x);
			}else{
				return false;
		}
	}
	
	$("body").css({overflow: "hidden"});
	$("#content").prepend("<div id='skirt'></div>");
	$("#skirt").css({
		backgroundColor: "#ffffff",
		position: "absolute",
		left: "0",
		top: "0",
		width: $(window).width(),
		height: $(window).height(),
		backgroundImage: "url('/images/ajax-loader.gif')",
		backgroundRepeat: "no-repeat",
		backgroundPosition: "center"	
	});
	
	$(window).resize(function(){
		init();
	});
	$(window).load(function(){
		killskirt();
		init();
	});
	function killskirt(){
		$("#skirt").fadeOut(400);
	}
	function init(){
		$("#content:not(#static) img").removeAttr("width");
		$("body").css({overflow: "auto"});
		$totalwidth = 0;
	//	$("#content, #content:not(#static) img").css({
		//	height: (Math.round($(window).height()) - 100)
		//  height: "600px";
	//	});
		$("#content img").each(function(){
			$totalwidth += Math.round(($(this).width()) + 15);
		});	
		$totalwidth += 300;
		if($totalwidth <= 800){
			$("body").css({width: "100%"});
		}else{
		
		$("body").css({
			width: $totalwidth + "px"
		});
	}
	}
	
	
	$sublinks = $("#menu li ul"); // all sub ul elements.
	$links = $("#menu li a"); // all links in mainmenu
	$sublinks.hide(); // all hidden
	$("#menu li:has(.active1) ul").hide();
	
	$segments = getActive();
			if($segments == "" || $segments == null){
				$links.eq(0).parent('li').addClass('active1');		
				}else{
			$links.each(function(){
					$item = $(this);
					$addclass = activeLinks($segments, $item);
				});
			}	
	$("#menu li:first").hide();
	$("#menu li ul").each(function(){
		if($(this).children("li").attr("class") == "active1" || $(this).parent("li").attr("class") == "active1" || $(this).parent("li").attr("class") == "active5"){
			$(this).show();
		}else{
			$(this).hide();
			//$(this).css({height: "0px"});		
		}
	});
	
/*	
	$("#menu li:has('ul'):not(li.active1, li.active5)").each(function(){
		// check to be sure that there is a child ul under the list item before firing
		$(this).hoverIntent(function(){
			
			
			if($(this).children("ul").css("display") == "none"){
			
			//$(this).children("ul").animate({height: '100px'}, 500);				
			$(this).children("ul").slideDown(600);
			}			
		}, 
		function(){
			if($(this).children("ul").children("li.active").length == 0){
				//$(this).children("ul").animate({height: '0px'}, 500);				
				
				$(this).children("ul").slideUp(600);
			}	
		});
	});
*/	
	var config = {
		sensitivity: 3,
		interval: 300,
		over: menuSlideDown,
		timeout: 100,
		out: menuSlideUp
	};
	$("#menu li:has('ul'):not(li.active1, li.active5)").each(function(){
		$(this).hoverIntent(config);
	});
	
	
	function menuSlideUp(){
		$(this).children("ul").slideUp(600);
	}
	function menuSlideDown(){
		$(this).children("ul").slideDown(600);
	}
	
	
	
	function cleanImage(n){
		n = n.replace(/ /g,'');
		n = n.toLowerCase();
		return n;
	}


	function imageReplace(n, p){ //n=image p=imageheight
		$img = cleanImage(n.text());
		$last = $img.lastIndexOf("/");		
		// setup rollover.  CSS must be set to overview hidden. 
		n.text("").html("<img src='/images/btn-"+ $img +".gif' />");		
		n.children("img").css({marginTop:"0px"});
		n.css({display: "block", overflow: "hidden", height: ""+ p + "px"});
		n.hover(function(){
				n.children("img").css({marginTop:"-"+ p + "px"});
			},function(){
				n.children("img").css({marginTop:"0px"});
		});

	}
	
		$("#community a").each(function(){imageReplace($(this), 29);});
	
});
