$(document).ready( function() {
	//Handle external links
	$("a[@rel$=\"external\"]").click(function(){this.target = "_blank";});

	//Check to see if running IE6
	if( $.browser.msie && $.browser.version=="6.0" )
	{
		//Catch onmouseover and outmouseout events on each item on the navigation
		$("#container #header .left ul.nav li").mouseover(function(){ $(this).addClass("selected"); });
		$("#container #header .left ul.nav li").mouseout(function(){ $(this).removeClass("selected"); });
	}

	/*cmx form */
	if(jQuery.browser.mozilla) {
		$("form.cmxform").hide().find("label:not(.nocmx)").each(function(){
			var $labelContent = $(this).html();
			var $labelWidth = $(this).css("width");
			$(this).empty();
			$(this).append('<span style="display: block; width: '+$labelWidth+';">');
			$(this).prepend("</span>");
			$(this).css("display", "-moz-inline-box");
			$(this).find("span").html($labelContent);
			$("form.cmxform").show();
		});
	};

	//Hide the menu sub items and show the first one
	$("ul#newsmenu ul").hide();

	//Check to see if we have any elements with the class expanded (programatically set)
	if ( $("ul#newsmenu li.expanded").length == 0 )
	{
		//Show the first level as we have not been told to show any particular one
		$("ul#newsmenu ul:first").slideDown()	;
		$("ul#newsmenu li:first").addClass("expanded");
	}
	else
	{
		//Show the one with the class set to expanded (programatically)
		$("ul#newsmenu li.expanded ul").slideDown()	;
	}

	//Handle the news navigation expanding
	$("ul#newsmenu li a").click(function () {
		//Check to see if this item is already visibl
		if ( $(this).next("ul").is(":hidden") )
		{
			//Hide all articles first
			$("ul#newsmenu li ul").slideUp("slow");

			$("ul#newsmenu li").removeClass("expanded");

			//Add a class to the item that is being clicked on
			$(this).parent("li").addClass("expanded");

			//Show the immediate child list (news articles)
			$(this).next("ul").slideDown("slow");
		}
		else
		{
			//Remove a class from the item that is being clicked on
			$(this).parent("li").removeClass("expanded");

			//Hide the immediate child list (news articles)
			$(this).next("ul").slideUp();
		}
	});

	//Check to see if we are on the homepage
	if ( $("body.home").length > 0 )
	{
		//Embed the flash movies
		swfobject.embedSWF("swf/intro.swf", "intro", "635", "309", "7.0.0");
		swfobject.embedSWF("swf/quotes.swf", "quotes", "355", "221", "7.0.0");

		//Check to see if we are working locally
		if ( location.href.indexOf("web2") 	> 0 )
			var flashvars = { isHome : 1, areaURL : "http://web2/streetsmart/area.php?iAreaID=" };
		else
			var flashvars = { isHome : 1, areaURL : "http://www.streetsmart.org.uk/area.php?iAreaID=" };

		//Set up our flash parameters
		var params = {
			bgcolor : "#ffffff",
			quality : "high",
			allowFullScreen : false,
			loop : true
		};

		//Embed the flash movie
		swfobject.embedSWF("swf/map.swf", "map", "433", "332", "7.0.0", "", flashvars, params);
	}

	//Check to see if we are on the streetsmart locations page
	if ( $("body.area-home").length > 0 )
	{
		//Check to see if we are working locally
		if ( location.href.indexOf("web2") 	> 0 )
			var flashvars = { areaURL : "http://web2/streetsmart/area.php?iAreaID=" };
		else
			var flashvars = {	 areaURL : "http://www.streetsmart.org.uk/area.php?iAreaID=" };

		//Set up our flash parameters
		var params = {
			bgcolor : "#ffffff",
			quality : "high",
			allowFullScreen : false,
			loop : true
		};

		//Embed the flash movie
		swfobject.embedSWF("swf/map.swf", "map", "433", "332", "7.0.0", "", flashvars, params);
	}

	//Focus on the first input field automatically
	$("form ol li input:first").focus();

	//execute when the area select box is changed on the sign-up page
	$("body.sign-up select#Area").change(function(){checkLocation();});

	//run check location on page load too incase we have an invalid form
	checkLocation();
});

function checkLocation ()
{
	//check to see if our selected location is 'Other'
	if ( $("body.sign-up select#Area").val() == "Other") {
		//show the 'other' box so the user can enter a custom location
		$("body.sign-up li#other").css("visibility","visible");
		$("body.sign-up li#other").css("display","block");

	}
	else {
		//hide the 'other' box as we have selected a normal location
		$("body.sign-up li#other").css("visibility","hidden");
		$("body.sign-up li#other").css("display","none");
	}
}



