
	//-----------------------------------------------------//
	// GLOBAL
	//-----------------------------------------------------//
	
	$(function() {

		// NAV
		$("div#top ul#nav > li").hover(function() { $(this).find("ul").css("display","block"); $(this).find("a:first").addClass("active"); },function() { $(this).find("ul").css("display","none"); $(this).find("a:first").removeClass("active"); });
		$("div#top ul#nav li ul").each(function() { $(this).find("li:last").addClass("last"); $(this).append($("<li class=\"bottom\"></li>")); });

		// SUB NAV
		$("div#page ul#sub_nav > li:last").css("border-bottom","none");
		
		// BREADCRUMB
		$("div#page ul.breadcrumb > li:last").css("font-weight","bold");

		// DROPDOWNS
		$("div.dropdown a.arrow").click(function() {
			dropdown_close();
			var width = $(this).parent().width();
			$(this).parent().find("ul li a").css("width",(width+10)+"px");
			$(this).parent().find("ul:hidden").css("width",(width+18)+"px").slideDown(200);
			$("body").bind("click",dropdown_close);
			$(this).addClass("arrow_down");
			$(this).parent().css("z-index","10");
			return false;
		});
		$("div.dropdown ul.inline li a").click(function() {
			var title = $(this).attr("title");
			if(title.length>0) {
				var value = title;	
			} else {
				var value = $(this).text();	
			}
			$(this).parent().parent().parent().find("div.selected").html($(this).text());
			$(this).parent().parent().parent().find("input").val(value).change();
			dropdown_close();
			return false;
		});
		$("div.dropdown").each(function() { if($(this).find("ul").height() > 150) { $(this).find("ul").addClass("scroll"); } });
		
		// DISABLED BUTTONS
		$(".disabled").each(function() { $(this).css("opacity","0.3"); });
		
		// COMMENTS
		$("div#leave_a_comment form").attr("action","?post_comment=1");
		
		// BOX GRID
		$("ul.box_grid").each(function() {
			var items = $(this).find("li");
			var height = 0;
			for(var i = 0; i < items.length; i++) { 
				if(((i+1)%3)==1) { 
					height = $(items[i]).height();
					if($(items[i+1]).height()>height&&(i+1)<items.length) { height = $(items[i+1]).height(); }
					if($(items[i+2]).height()>height&&(i+2)<items.legnth) { height = $(items[i+2]).height(); }
				}
				$(items[i]).css("height",height);
				if(((i+1)%3)==0) { $(items[i]).css("margin-right",0); } 
			}
		});
		
		// QUOTE IMAGE
		$("div.quote_image").each(function() {
			var width = $(this).find("img").width();
			if(width>0) { $(this).css("width",width+"px"); }
			var height = $(this).find("img").height();
			if(height > 0) { $(this).css("height",height+"px"); }
			$(this).find("div.quote").css("width",(width-20)+"px");
		});
		
		// COUNTY ENTRIES
		$("div.entries div.county_entry:last").css("border-bottom","none");
		
		// CALENDAR
		$("div#calendar").hover(function() { $(this).css("z-index","100"); },function() { $(this).css("z-index","1"); });
		$("div#calendar ul.days li.event div").each(function() { $(this).append($("<div class=\"marker\"></div>")); });
		$("div#calendar ul.days li.event").hover(function() { $(this).find("div.popup").fadeIn(200); },function() { $(this).find("div.popup").fadeOut(100); });

		// ACCORDION
		$("ul.accordion li div.top a.expand").click(function() {
			var li = $(this).parent().parent();
			if($(li).find("div.body").is(":visible")) {
				$(li).find("div.body").slideUp(200);
				$(li).removeClass("open");
			} else {
				$(li).parent().find("li.open").each(function() {
					$(this).removeClass("open");
					$(this).find("div.body").slideUp(200);
				});
				$(li).find("div.body").slideDown(200);
				$(li).addClass("open");
			}
			return false;					 
		});
		$("ul.accordion li div.top").each(function() {
			var height = ($(this).height()/2)+6;
			$(this).find("a.expand").css("top",height+"px");
		});

	});

	//-----------------------------------------------------//
	// DROPDOWNS
	//-----------------------------------------------------//

	function dropdown_close() {
		$("div.dropdown a.arrow_down").removeClass("arrow_down");
		$("div.dropdown ul").css("display","none");
		$("div.dropdown").removeAttr("style");
		$("body").unbind("click",dropdown_close);
	}

	//-----------------------------------------------------//
	// HOMEPAGE
	//-----------------------------------------------------//
	
	function fix_homepage_height() {
		var left_height = $("div.top_stories").height()+$("div.featured_news").height()+$("div.upcoming_events").height();
		var right_height = $("div.login").height()+$("div.latest_media").height()+$("div.reader_poll").height();

		if(left_height > right_height) {

			var height = $("div.reader_poll div.module_frame").height();
			height += (left_height-right_height);
			$("div.reader_poll div.module_frame").css("height",height-10);
			
		} else if(right_height >= left_height) {
			
			var height = $("div.upcoming_events div.module_frame").height();
			height += (right_height-left_height);
			$("div.upcoming_events div.module_frame").css("height",height+10);
		}	
	}

	//-----------------------------------------------------//
	// ALERTS
	//-----------------------------------------------------//
	
	var alerts=null,alerts_total=0,alerts_current=1,alerts_return=false;
	
	function alerts_rotate() {
		alerts_current++;
		if(alerts_current > alerts_total) { alerts_return = 1; }
		var position = (alerts_current-1)*-18;
		$("div.alerts ul").animate({top:position+"px"},500,function() {
			if(alerts_return) { $(this).css("top",0); alerts_current = 1; alerts_return = false; }															 
		});
	}
	
	function alerts_prepare() {
		alerts_total = $("div.alerts ul li").length;
		alerts = setInterval(alerts_rotate,5000);
		$("div.alerts").hover(function() { clearInterval(alerts); alerts=null; },function() { alerts = setInterval(alerts_rotate,5000); });
		$("div.alerts ul").append($("div.alerts ul li:first").clone());
	}
	
	//-----------------------------------------------------//
	// TOP STORIES
	//-----------------------------------------------------//
	
	var top_stories=null,top_stories_total=0,top_stories_current=1,top_stories_return=false;

	function top_stories_update() {
		$("div.top_stories ul.nav li a.active").removeClass("active");
		var items = $("div.top_stories ul.nav li");
		if(top_stories_return) {
			$(items[top_stories_return-1]).find("a").addClass("active");
		} else {
			$(items[top_stories_current-1]).find("a").addClass("active");	
		}
	}
	
	function top_stories_rotate() {
		top_stories_current++;
		if(top_stories_current > top_stories_total) { top_stories_current = 1; }
		top_stories_show(top_stories_current);
	}
	
	function top_stories_show(story) {
		top_stories_current = story;
		top_stories_update();
		$("div.top_stories div.stories ul li:visible").fadeOut(800);
		var items = $("div.top_stories div.stories ul li");
		$(items[story-1]).fadeIn(1600);
	}
	
	function top_stories_prepare() {
		top_stories_total = $("div.top_stories div.stories ul li").length;
		top_stories = setInterval(top_stories_rotate,8000);
		$("div.top_stories").hover(function() { clearInterval(top_stories); top_stories=null; },function() { top_stories = setInterval(top_stories_rotate,8000); });
		$("div.top_stories ul.nav li a").click(function() { top_stories_show($(this).text()); return false; });
	}
	
	//-----------------------------------------------------//
	// LATEST MEDIA
	//-----------------------------------------------------//
	
	function latest_media_select(tab) {
		$("div.latest_media ul.nav li a.active").removeClass("active");
		$("div.latest_media ul.nav li."+tab+" a").addClass("active");
		$("div.latest_media div.home_slider_frame ul").css("display","none");
		$("div.latest_media div.home_slider_frame ul."+tab).css("display","block");
	}
	
	function latest_media_prepare() {
		$("div.latest_media ul.nav li").hover(function() { $(this).find("a").addClass("hover"); },function() { $(this).find("a").removeClass("hover"); });
		$("div.latest_media ul.nav li a").click(function() { latest_media_select($(this).text()); return false; });
		$("div.latest_media a.previous").bind("click",latest_media_previous);
		$("div.latest_media a.next").bind("click",latest_media_next);		
		$("div.latest_media div.home_slider_frame ul").each(function() {
			var first = $(this).find("li:first");
			var last = $(this).find("li:last");
			$(this).prepend($(last).clone());
			$(this).append($(first).clone());
			var total = $(this).find("li").length;
			var position = 184*total;
			$(this).css({width:position+"px",left:"-184px"});
		});
	}
	
	function latest_media_off() {
		$("div.latest_media a.previous").unbind("click",latest_media_previous).bind("click",empty);
		$("div.latest_media a.next").unbind("click",latest_media_next).bind("click",empty);
	}
	
	function latest_media_on() {
		$("div.latest_media a.previous").unbind("click",empty).bind("click",latest_media_previous);
		$("div.latest_media a.next").unbind("click",empty).bind("click",latest_media_next);		
	}
	
	function latest_media_previous() {
		latest_media_off();
		var ul = $(this).parent().find("div.home_slider_frame ul:visible");
		var total = $(ul).find("li").length;
		var position = $(ul).css("left");
		position = Math.floor(position.replace(/px/,""));
		position += 184;
		$(ul).stop().animate({left:position+"px"},500,function() {
			var position = $(this).css("left");
			position = Math.floor(position.replace(/px/,""));
			var total = $(this).find("li").length;
			if(position>=0) {
				total -= 2;
				var new_position = total*-184;
				$(this).css("left",new_position+"px");
			}
			latest_media_on();
		});
		return false;		
	}
	function latest_media_next() {
		latest_media_off();
		var ul = $(this).parent().find("div.home_slider_frame ul:visible");
		var total = $(ul).find("li").length;
		var position = $(ul).css("left");
		position = Math.floor(position.replace(/px/,""));
		position -= 184;
		$(ul).stop().animate({left:position+"px"},500,function() {
			var position = $(this).css("left");
			position = Math.floor(position.replace(/px/,""));
			var total = $(this).find("li").length;
			if(position<=((total-1)*-184)) {
				$(this).css("left","-184px");
			}
			latest_media_on();
		});
		return false;
	}
	
	//-----------------------------------------------------//
	// SLIDER
	//-----------------------------------------------------//
	
	function slider_prepare() {
		$("div.slider a.previous").bind("click",slider_previous);
		$("div.slider a.next").bind("click",slider_next);		
		$("div.slider div.slider_frame ul").each(function() {
			var first = $(this).find("li:first");
			var last = $(this).find("li:last");
			$(this).prepend($(last).clone());
			$(this).append($(first).clone());
			var total = $(this).find("li").length;
			var position = 544*total;
			$(this).css({width:position+"px",left:"-544px"});
		});
	}
	
	function slider_off() {
		$("div.slider a.previous").unbind("click",slider_previous).bind("click",empty);
		$("div.slider a.next").unbind("click",slider_next).bind("click",empty);
	}
	
	function slider_on() {
		$("div.slider a.previous").unbind("click",empty).bind("click",slider_previous);
		$("div.slider a.next").unbind("click",empty).bind("click",slider_next);		
	}
	
	function slider_previous() {
		slider_off();
		var ul = $(this).parent().find("div.slider_frame ul");
		var total = $(ul).find("li").length;
		var position = $(ul).css("left");
		position = Math.floor(position.replace(/px/,""));
		position += 544;
		$(ul).stop().animate({left:position+"px"},500,function() {
			var position = $(this).css("left");
			position = Math.floor(position.replace(/px/,""));
			var total = $(this).find("li").length;
			if(position>=0) {
				total -= 2;
				var new_position = total*-544;
				$(this).css("left",new_position+"px");
			}
			slider_on();
		});
		return false;		
	}
	function slider_next() {
		slider_off();
		var ul = $(this).parent().find("div.slider_frame ul");
		var total = $(ul).find("li").length;
		var position = $(ul).css("left");
		position = Math.floor(position.replace(/px/,""));
		position -= 544;
		$(ul).stop().animate({left:position+"px"},500,function() {
			var position = $(this).css("left");
			position = Math.floor(position.replace(/px/,""));
			var total = $(this).find("li").length;
			if(position<=((total-1)*-544)) {
				$(this).css("left","-544px");
			}
			slider_on();
		});
		return false;
	}
	
	//-----------------------------------------------------//
	// PULL QUOTE
	//-----------------------------------------------------//
	
	function add_pullquote(pull_quote_text) {
		var remaining = 1000;
		var paragraphs = $("#article_content p").length;
		if(paragraphs > 1) {
			for(var i = 1; i < paragraphs; i++) {
				var p_text = $("#article_content").find("p:eq("+i+")").text();
				remaining -= p_text.length;
				if(p_text.length>0) { remaining-= 50; }
				if(remaining <= 0) {
					var pull_quote = "<div class=\"pull_quote\">"+pull_quote_text+"</div>";
					$("#article_content").find("p:eq("+i+")").after(pull_quote);
					return true;
				}
			}
		}
		return false;
	}
	