function redirect(newURI)
{
	gotopage=newURI.options[newURI.selectedIndex].value
	if (gotopage!="")
		window.location.href=gotopage
}


// open new window

function newWindow(newPage,newWidth,newHeight,newLeft,newTop)
{
	var thisWindowSize = "width=" + newWidth + ",height=" + newHeight + ",left=" + newLeft + ",top=" + newTop + ",scrollbars=yes";
	var thisWindow = window.open(newPage, 'newWin', thisWindowSize);
	thisWindow.focus();
}


function limitText(limitField, limitCount, limitNum) // limit the amount of text in a textbox form field
{
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}



function changeFeatured (li_num)
{
	var workingImage = document.getElementById(num);
	workingImage.style.display = 'none';
	num = li_num;
	var workingImage = document.getElementById(num);
	workingImage.style.display = 'block';
	return false;
}

function nextFeatured ()
{
	if (document.getElementById(num+1))
		changeFeatured (num+1);
	else
		changeFeatured (1);
	return false;
}

function prevFeatured ()
{
	if (document.getElementById(num-1))
		changeFeatured (num-1);
	else
	{
		var wrapperIDname = "sbImage";  // hardcoded in default - so we can get the number of photos in the gallery
		var nl = document.getElementById(wrapperIDname).getElementsByTagName('img');
		changeFeatured (nl.length-3); // -3 to account for icons in the navigation section
	}
	return false;
}


/* JQUERY STUFF */

$(document).ready(function() {

	/* handle tabs */
	$('.tab').click(function() {
		var parentID = $(this).parent().attr('id');
		var tabnum = $('#'+parentID+' .tab').index(this); // parentID necessary as selector, because there could be other tab sets on same page
		var boxes = $('#'+parentID).siblings('.newstab_list');
		boxes.hide();
		boxes.eq(tabnum).show();
		// switch which tab is in 'on' state
		$('#'+parentID+' .tab').removeClass('on').addClass('off');
		$(this).addClass('on');
		// in addition, for now, because these aren't sprites - must do hacky method of swapping images
		// ... get the base of each img src, and turn them all into '-g.gif' at the end (the off version)
		$('#'+parentID+' .tab').each(function() {
			var imgsrc = $(this).find('img').attr('src');
			var imgbase = imgsrc.substring(0,imgsrc.length-6);
			$(this).find('img').attr('src',imgbase+'-g.gif');
		});
		// ... do it again just for the one that's been picked, change to 'on' state
		var imgsrc = $(this).find('img').attr('src');
		var imgbase = imgsrc.substring(0,imgsrc.length-6);
		$(this).find('img').attr('src',imgbase+'-r.gif');
		return false;
	})
	
	// fix front page headline to move inside the image div
	if ($('#hli')) {
		$('#hli #imgtextWrap').append($('h1.topper'));
		$('#hli #imgtextWrap').append($('#opacityBG'));
	}



	// handle any pictorial modals
	// e.g. <a href="/news/pictorial2012.php?p=82" class="pictorialModalTrigger">Pictorial Test</a>
	$('.pictorialModalTrigger').click(function(e) {
		e.preventDefault();
		var url = $(this).attr('href');
		$('body').css('position','relative').append('<div class="pictorialUnderlay" />');
		// insert the css and js ...
		$('.pictorialUnderlay').append('<style>@import url("/css/pictorialModal.css");</style>');
		$('body').append('<div class="pictorialWrapper" />');
		var pict = $('.pictorialWrapper');
		pict.load(url+' #pictorial', function() {
			$.getScript("/php/jq/pictorial.js",function() { pictorial.setHandlers(); pictorial.show(pictorial.current); });
		});
		pict.css({ top: ($(window).height()/2)-(pict.outerHeight()/2), left: ($(window).width()/2)-(pict.outerWidth()/2) });
		pict.fadeIn();
	});
});

