// JavaScript

var districtddhover=false;
var districtnavhover=false;
var articlegalcount=0;
var rightmost=3;
var curScroll=0;

$.fn.clearForm = function() {
	return this.each(function() {
	var type = this.type, tag = this.tagName.toLowerCase();
	if (tag == 'form')
		return $(':input',this).clearForm();
	if (type == 'text' || type == 'password' || tag == 'textarea')
		this.value = '';
	else if (type == 'checkbox' || type == 'radio') {
		this.checked = false;
	} else if (tag == 'select')
		this.selectedIndex = -1;
	});
};

$(document).ready(function() {
	
	$(".navbutton").hover( function() { $(this).toggleClass("navbuttonhover"); }, function() { $(this).toggleClass("navbuttonhover"); });
	$("#districtnavbutton").unbind(); // will handle these events separately
	$("#districtdropdown").css('opacity',0);
	
	$("#districtnavbutton").hover( 
		function() { 
			districtnavhover=true; 
			if ($("#districtdropdown").css('display')=='none') $(this).addClass("navbuttonhover");
			$("#districtdropdown").css('display','block').animate( { opacity:1 }, 100 ); },
		function() { districtnavhover=false; setTimeout("hideDistrictDD()", 250);  }
	);
	
	$("#districtdropdown").hover(
		function() { districtddhover=true; },
		function() { districtddhover=false;	setTimeout("hideDistrictDD()", 250); }
	);
	
	$(".dropdownitem").hover( function() { $(this).toggleClass("dropdownitemhover"); },	function() { $(this).toggleClass("dropdownitemhover"); } );
	$(".dropdownitem").click( function() { window.location='topic.php?topic='+$(this).text();} );
	
	
	$("#articlegalleryleft").find('img').click( function() { 
		rightmost--;
		curScroll=$("#articlegalleryslot").attr('scrollLeft');
		curScroll=curScroll-160;
		if (curScroll<0) curScroll=0;
		$("#articlegalleryslot").animate({scrollLeft:curScroll},300);
		checkScrollArrows();
	});

	$("#articlegalleryright").find('img').click( function() { 
		rightmost++;
		curScroll = $("#articlegalleryslot").attr('scrollLeft');
		curScroll = curScroll+160;
		$("#articlegalleryslot").animate({scrollLeft:curScroll},300);
		checkScrollArrows();
	});
	
	$(".galleryarrow").hover(function() { $(this).css('background-color','#FAAFBA'); }, 
		function() { $(this).css('background-color','#FFF'); });
		
});

function checkScrollArrows() {
	if (curScroll<=0)
		$("#articlegalleryleft").find('img').css('display','none');
	else
		$("#articlegalleryleft").find('img').css('display','block');
		
	if (rightmost>=articlegalcount)
		$("#articlegalleryright").find('img').css('display','none');
	else
		$("#articlegalleryright").find('img').css('display','block');
}

function hideDistrictDD() {
	if (!districtddhover && !districtnavhover) {
		$("#districtnavbutton").removeClass("navbuttonhover");
		$("#districtdropdown").animate( { opacity:0 }, 100 ).css('display','none');
	}
}

function articleGallery() {
	$(".hiddenphoto").each(function() {
		articlegalcount++;
		$("#articlegallerywrapper").append("<a class='lightbox' href='"+
			$(this).attr('src')+"'><img class='articlegalphoto' src='"+$(this).attr('src')+"' /> ");
	});
	$("#articlegalleryslot").attr('scrollLeft',0);
	if (articlegalcount==0) {
		$("#articlegalleryslot").css('display','none');
	} else 
		checkScrollArrows();
}
