$(function () {
	/*************************************************/
	/* ADD IN SOME ARIA ATTRIBUTES FOR ACCESSIBILITY */
	/*************************************************/
	$("#content-area, #sub-nav,#tactical-boxes").attr("aria-live", "polite");
	/************************************************/
	/* END THE ARIA ACCESSIBILITY STUFF             */
	/************************************************/


	/** AJAX behaviour injection **/
	$("#sub-nav a:not(a[href*=.pdf])").live ("click", function ()
	{
		update ($(this).attr('href'));
		$("#sub-nav a").removeClass("active");
		$(this).addClass("active");
		location.href="#" + $(this).attr("href");
		try {
			$(this).fixIElinks();
			tmp = $("#header embed")[0]
			tmp.TGotoLabel("/clintons_controller", $(this).attr("href"));
		} catch (E) { /*alert($(this).attr("href"));*/}
		dealWithTactBoxDiv();
		return false;
	});
	$("#tactical-boxes a:not(#box-gallery,#box-case-study)").live ("click", function ()
	{
		update ($(this).attr('href'));
		location.href="#" + $(this).attr("href");
		try {
			$(this).fixIElinks();
			tmp = $("#header embed")[0]
			tmp.TGotoLabel("/clintons_controller", $(this).attr("href"));
		} catch (E) { /*alert($(this).attr("href"));*/}
		dealWithTactBoxDiv();
		return false;
	});
	$("#box-gallery").live ("click", function ()
	{
		show_gallery();
		return false;
	});
	$("#box-case-study").live ("click", function ()
	{
		show_case_study();
		return false;
	});
	$("div.scrollable img:not(#right-arrow,#left-arrow)").live ("click", function ()
	{
		$("#large-picture").html('<img src="' + $(this).attr("src").substring(0,$(this).attr("src").length-5) + '.jpg" />');
		return false;
	});
	$("#right-arrow,#left-arrow").live ("click", function ()
	{
		return false;
	});
	$(".smd_cal_navprev,.smd_cal_navnext").live ("click", function ()
	{
		$.ajax ({
			url:'/activities/calendar'+$(this).attr("href")+"&AJAX=1",
			success:function(html) {
				$("#content-area").html(html);
			}
		})
		return false;
	});
	$("#case-study-left a").live ("click", function ()
	{
		$.ajax (
		{
			type: 'GET',
			url: $(this).attr("href"),
			data: 'AJAX=1&BODY=1',
			cache:true,
			success: function (html) {$("#case-study-content").html(html); return false;}
		});
		return false;
	});
	if (location.hash)
	{
		/* find the url to load and get the data*/
		loadURL = location.hash.substring(1,location.hash.length);
		update(loadURL);
		setTimeout("initialiseFlash(loadURL)",4000)
	}
});

function initialiseFlash (loadURL)
{
		try {
			tmp = $("#header embed")[0];
			tmp.TGotoLabel("/clintons_controller", loadURL);
		} catch (E) {}
}





/* function for flash to interact with the site */
function update (page)
{
	/* load in the content */
	$.ajax({
		type:'GET',
		url:page,
		data: 'AJAX=1',
		cache:true,
		success:function(html) {$("#content-area").html(html)}
	})
	/* load in the subnav */
	$.ajax({
		type:'GET',
		url:'/_assets/subnav.php',
		data:'uri='+page,
		cache:true,
		success:function(html) {
			$("#sub-nav").html(html)
			highlightSubnav(page);
		}
	})
	/* load in the tactboxes */
	$.ajax({
		type:'GET',
		url:'/_assets/tactbox.php',
		data:'uri='+page,
		cache:true,
		success:function(html) {
			$("#tactical-boxes").html(html);
		}
	})
	location.hash = "#" + page;
	dealWithTactBoxDiv();
	return false;
}

function highlightSubnav (page)
{
	$("#sub-nav a").removeClass("active");
	$("#sub-nav a[href^="+page+"]").addClass("active");
}

/** MODAL WINDOWS **/
$(function () {
		$('#modal').jqm({
			ajax: '/gallery/?AJAX=1',
			ajaxText: '<h3>Loading...</h3>',
			target: $("#modal #modal_content")[0],
			modal: true,
			trigger: '#display_gallery'
		});
		$('#modal2').jqm({
			// ADD LAUREN's STUFF HERE
			ajax: '/case-studies/?AJAX=1',
			ajaxText: '<h3>Loading...</h3>',
			target: $("#modal2 #modal2_content")[0],
			modal: true,
			trigger: '#display_case_study'
		});
		$("#box-gallery").click (function (event) {show_gallery();return false;});
		$("#box-case-study").click (function (event) {show_case_study();return false;});
});

function dealWithTactBoxDiv ()
{
	parts = location.href.split("#")
	if (parts[1])
	{
	    if (parts[1].indexOf ('activities/calendar')==-1)
	    {
	        $("#tactical-boxes").css("display", "block");
	        $("#content-area").removeClass("full-content-width");
	    }
	    else
	    {
	        $("#tactical-boxes").css("display", "none");
	        $("#content-area").addClass("full-content-width");
	    }
	}
	else
	{
	    if (parts[0].indexOf ('activities/calendar')==-1)
	    {
	        $("#tactical-boxes").css("display", "block");
	        $("#content-area").removeClass("full-content-width");
	    }
	    else
	    {
	        $("#tactical-boxes").css("display", "none");
	        $("#content-area").addClass("full-content-width");
	    }
	}
}

function show_gallery ()
{
	$("#display_gallery").trigger("click");
}
function show_case_study ()
{
	$("#display_case_study").trigger("click");
}

jQuery.fn.fixIElinks = function ()
{
	if ($.browser.msie)
	{
		return this.each(function()
		{
			$(this).attr("href", $(this).attr("href").substring(location.host.length-1));
		});
	}
	return this.each (function () {});
};


$(function () {
	var so = new SWFObject("/_assets/swf/common/container.swf", "/_assets/swf/common/container.swf", "934", "414", "8","#ffffff");
	so.addParam("menu", "false");
	so.addParam("wmode", "transparent");
	so.write("header");
});


function flashHeaderSwitch ()
{
	var LA_start = location.protocol.length + 2 + location.host.length + 1;
	var $header = $("#header embed");
	if ($header && $($header).attr("src"))
	{
		if (location.hash)
		{
			if (location.hash.substring(2,12) === 'short-stay')
			{
				if ($($header).attr("src").indexOf ('short-stay')==-1)
				{
					$($header).attr("src", "/_assets/swf/common/short-stay/container.swf");
				}
			}
			else
			{
				// must be student accommodation
				if ($($header).attr("src").indexOf ('short-stay')!=-1)
				{
					$($header).attr("src", "/_assets/swf/common/container.swf");
				}
			}
		}
		else
		{
			if (location.href.substring(LA_start, LA_start + 10) === 'short-stay')
			{
				if ($($header).attr("src").indexOf ('short-stay')==-1)
				{
					$($header).attr("src", "/_assets/swf/common/short-stay/container.swf");
				}
			}
			else
			{
				if ($($header).attr("src").indexOf ('short-stay')!=-1)
				{
					$($header).attr("src", "/_assets/swf/common/container.swf");
				}
			}
		}
	}
	setTimeout(flashHeaderSwitch, 500);
}
flashHeaderSwitch();