
function resize(){
	var winWidth = $(window).width();
	
	$("#elBGim img") 
		.width(winWidth) 
		.height(winWidth * .67); 
	
	if($("#elBGim img").height() <= $(window).height()){	
		$("#elBGim img")
			.height($(window).height())
			.width($(window).height() * 1.5);
	}
	if ($.browser.msie && $.browser.version <= 7) {
		$("#right-column").width(winWidth - 332);
	} else {
		$("#right-column").width(winWidth - 320);
	}
}

function setBG() {
	wid = $(window).width();
	hei = $(window).width() *.67;
	
	if (hei <= $(window).height()) {
		hei = $(window).height();
		wid = $(window).height() * 1.5;
	}
	if ($.browser.msie && $.browser.version <= 7) {
		wid -= 7;
	}
	page = window.location.pathname.toLowerCase().replace('/','').replace('.html','');
	if(page=="" || page =="index") { page="home"; }
	imgSrc = '/images/' + page + '/background-' + page + '-bw.jpg';
	imStr = '<DIV ID="elBGim" class="noPrint"'
		+ ' STYLE="position:absolute;left:0;top:0;z-index:1">'
		+ '<IMG NAME="imBG" class="noPrint" BORDER=0 SRC="' + imgSrc + '"'
		+ ' WIDTH="' + wid + '" HEIGHT="' + hei + '" /></DIV>';
	$("body").prepend(imStr);
}


var newsid = 1;
var maxnewsid = 3;
	
$(window).resize(function(){ resize(); });
$(document).ready(function(){
		
	$("body").css("overflow-x","hidden");
	
	setBG();
	resize();
	
	// IE6 Left Col Opacity
	if($.browser.msie) {
    	if($.browser.version < 7.0) {
			$('#left-column').css("opacity", 0.8);
			DD_belatedPNG.fix('#left-column a#branding, #right-column');
		}
	}
	
	
	// Preload Black and White Images
	$.preloadImages("/images/home/background-home-bw.jpg", "/images/precinct/background-precinct-bw.jpg", "/images/retailers/background-retailers-bw.jpg", "/images/news/background-news-bw.jpg", "/images/contact/background-contact-bw.jpg");


	// Slide Up/Down Content Toggle
	$("#subnav a").click(function() {
 		var linkId = $(this).parent().attr("class");
	
		if($(this).hasClass("active")) {
			// already open do nothing
		} else {
			
			$("#subnav a").removeClass("active");
			$(this).addClass("active");
			$(".content-toggle").slideUp("normal");
			$("#" + linkId).slideDown("normal");
			
		}
	});
	
	
	// News Controls
	currentNewsItem();
	
	$("a.previous").click(function() {
		if( newsid != 1) {
			$("#news"+newsid).slideUp("normal").removeClass("first");
			$("#news"+(newsid-1)).slideDown("normal").addClass("first");
			newsid--;
			currentNewsItem();
		}
	});
	
	$("a.next").click(function() {
		if( newsid != maxnewsid) {
			$("#news"+newsid).slideUp("normal").removeClass("first");
			$("#news"+(newsid+1)).slideDown("normal").addClass("first");
			newsid++;
			currentNewsItem();
		} 
	});
	
	
	// Image Colour Fade Switch
	setTimeout( function () {
		$("#elBGim img").ImageSwitch(
			{Type:"FadeIn",
			NewImage: $("#elBGim img").attr("src").replace("-bw.", "-color."),
			EffectOriginal: false,
			StartLeft:0,
			StartTop:0,
			EndLeft:0,
			EndTop:0,
			Speed:2000
			});
	},6000);


});

function currentNewsItem() {
	
	if($("#news1").hasClass("first")) {
		$("a.previous").css("opacity", 0.3);
	}  else {
		$("a.previous").css("opacity", 1);
	}
	
	if($("#news"+maxnewsid).hasClass("first")) {
		$("a.next").css("opacity", 0.3);
	} else {
		$("a.next").css("opacity", 1);
	}
	
}

jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", arguments[i]);
	}
}