var panelState = 0; //Current layout state - 0 = Default; 1 = Minimised; 2 = Media Style;
var projectImages; //The list of project images currently available;
var currentImage = 0; //The zero based index of the current image;
var maxThumbScroll = 0;
var selectionObjectToScroll = "thumbs"; //Options are either thumbs or list
var selectionObjectCurrentPosition = 0;
var scrollInTransition = false;
var doImageRotation = true;
var rotationDirection = "next"; //Options are either "next" or "previous"
	
var activeMediaArticle = 0;

var availableYears = new Array();
var currentYearSelection = 0;
var imageSwitchInterval = 10; //Amount of time in seconds before the next image rotates.
	
var sMenuState = 1;
var sMenu = "";

var debugMode = false;

//jQuery Image Preloader
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function getRandomNumber(max)
{
	
	return randomnumber=Math.floor(Math.random()*(max+1));
	
}

function getItemCount( sInputString )
{
	return sInputString.split("|||||").length;
}

function getItem ( nIndex, sInputString )
{
	return sInputString.split("|||||")[nIndex];
}

function getItemVariable ( nIndex, sVarName, sInputString )
{
	myItem = getItem(nIndex, sInputString);
	index = myItem.indexOf("[[" + sVarName + ":");
	if ( index == -1 )
		return "";
	
	return myItem.substring(index).split("]]")[0].replace("[[" + sVarName + ":", "");
}

//Resets the position of the background layer
//and the primary interface.
function resetPositions()
{
	var footerHeight = 938;
	var footerWidth = 939;
	var minHeight = 595;
	var documentHeight = $(document).height();
	var documentWidth = $(document).width() - 1;
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();
	var interfaceHeight = $("#interface").height();
	var interfaceWidth = $("#interface").width();

	var footerPositionTop;
	var footerPositionLeft;

	if ( windowHeight < footerHeight )
	{
			if ( windowHeight >= minHeight )
			{
				$("#background").css("height", windowHeight + "px");
			} else {
					$("#background").css("height", minHeight + "px");
			}
			
			footerPositionTop = 0;
	} else {
			footerPositionTop = documentHeight - footerHeight;
	}			
	
	footerPositionLeft = windowWidth - footerWidth;	
	
	$("#background").css("top", footerPositionTop + "px");
	$("#background").css("left", footerPositionLeft + "px");
	
	$("#interface").css("left", (windowWidth - interfaceWidth)/2 + "px");
	$("#interface").css("top", (windowHeight - interfaceHeight)/2 + "px");
	
	if (( (windowHeight - interfaceHeight)/2) < 0 )
		$("#interface").css("top", "0px");
	
	if (( (windowWidth - interfaceWidth)/2) < 0 )
		$("#interface").css("left", "0px");
	
	if ( $("#modal").css("display") != "none" )
		setModalPosition();
}



function setHookUps()
{
    $("#lnkTogglePanel").click(function() {
        doShowHidePanel();
        return false;
    });
	
	$(".navigation .media a").click( function () {
	doShowMedia();
	return false;
	});
	
	$("#header .imgnav .left a").click( function () {
	showPrevImage();
	return false;
	});
	
	$("#selection .imgnav .right a").click( function () {
	showNextImage();
	return false;
	});
	
	$("#scrollUp").click( function () {
	scrollUp();
	return false;
	});
	
	$("#scrollDown").click( function () {
	scrollDown();
	return false;
	});
	
	$(".selectionDropDownLink a").click ( function () {
	toggleViewDropDown();
	return false;
	});
	
	$(".prevYear").click ( function () {
	previousYear();
	return false;
	});
	
	$(".nextYear").click ( function () {
	nextYear();
	return false;
	});

	//$(".navigation .developments a").attr("href", "javascript:void(0)");

	$(".navigation .developments a").click(function() {

	//evt.preventDefault();
	    
	    selectionObjectToScroll = "thumbs";

	    if (panelState == 2)
	        initProjectImages(projectImages);

	    if (panelState == 0)
	        doMaximisePanel(false);

	    if (panelState != 0)
	        doMaximisePanel(true);

	    panelState = 0;
	    hidePanelToggle();
	    //alert("HELLO");
	    return false;
	});
	
	$(".navigation .home a").click( function () {
		
		selectionObjectToScroll = "thumbs";

		if ( panelState == 2 )
				initProjectImages(projectImages);
			
		if ( panelState == 0 )
			doMaximisePanel(false);
		
		if ( panelState != 0 )
			doMaximisePanel(true);
		
		panelState = 0;
		hidePanelToggle();
	});
	
	$(".navigation .about-cityheart a").click( function () {
	getPageContent("about-cityheart.aspx");
	return false;
	});
	
	$(".navigation .get-in-touch a").click( function () {
	getPageContent("get-in-touch.aspx");
	return false;
	});
	
	$("#siteMap").click( function () {
	getPageContent("site-map.aspx");
	return false;
	});

	$("#partnerLink a").click(function() {
	getPageContent("cityheart-partners.aspx");
	return false;
	});
			
	jQuery(function($) {
		$("#selection .content .selectionThumbs").bind('mousewheel', function(event, delta) {
					if ( delta > 0 )
					{
						scrollUp();
					} else {
						scrollDown();
					}
				return false;
			});
	});
			
	jQuery(function($) {
		$("#selection .content .selectionList").bind('mousewheel', function(event, delta) {
					if ( delta > 0 )
					{
						scrollUp();
					} else {
						scrollDown();
					}
				return false;
			});
	});
	
	$("h1 a").click( function () {

		selectionObjectToScroll = "thumbs";

		if ( panelState == 2 )
				initProjectImages(projectImages);
			
		if ( panelState == 0 )
			doMaximisePanel(false);
		
		if ( panelState != 0 )
			doMaximisePanel(true);
		
		panelState = 0;
		hidePanelToggle();
		return false;
		
	});
	
	$(".listview").click( function () {
		//initList();
		$(".selectionOptions .listview a").addClass("active");
		$(".selectionOptions .thumbview a").removeClass("active");
		$(".selectionDropDownLink a span").text("List view");
		toggleViewDropDown();
		//getMediaSummary("list", false);
		getMediaSummaryByYear(availableYears[currentYearSelection], "list");
		return false;
	});
	
	$(".thumbview").click( function () {
		//initThumbs();
		$(".selectionOptions .listview a").removeClass("active");
		$(".selectionOptions .thumbview a").addClass("active");
		$(".selectionDropDownLink a span").text("Thumbnail view");
		toggleViewDropDown();
		//getMediaSummary("thumbs", false);
		getMediaSummaryByYear(availableYears[currentYearSelection], "thumbs");
		return false;
	});
	
	setTimeout("rotateImage()", imageSwitchInterval * 1000);
}

function doHomeClick()
{
	selectionObjectToScroll = "thumbs";

	if ( panelState == 2 )
			initProjectImages(projectImages);
		
	if ( panelState == 0 )
		doMaximisePanel(false);
	
	if ( panelState != 0 )
		doMaximisePanel(true);
	
	panelState = 0;
	hidePanelToggle();
}

function toggleViewDropDown()
{
	if ( $(".selectionDropDown").height() == 26 )
	{
		//Open the drop down
		$(".selectionDropDown").animate({
		height: "73px"}, 500, "", function () { 
			$(".selectionOptions").css("display", "inline");
		} );
	} else {
		$(".selectionOptions").css("display", "none");
		$(".selectionDropDown").animate({
		height: "26px"}, 500, "", function () {
		 
		}	);
	}
}

function rotateImage()
{
	if ( panelState == 0 || panelState == 1 )
	{
		if ( rotationDirection == "next" )
		{
			if ( hasNextImage() )
			{
				showNextImage();
			} else {
				showPrevImage();
				rotationDirection = "prev";
			}
		} else {
			if ( hasPrevImage() )
			{
				showPrevImage();
			} else {
				showNextImage();
				rotationDirection = "next";
			}
		}
	}
	
	setTimeout("rotateImage()", imageSwitchInterval * 1000);
}

function hideLeftNav()
{
		$("#header .imgnav .left a").css("display", "none");
		$("#header .imgnav .left").animate({
		width: "0px"}, 500 );
}

function hideRightNav()
{
		$("#selection .imgnav .right a").css("display", "none");
		$("#selection .imgnav .right").animate({
		width: "0px"}, 500 );
}

function hideScrollUp()
{
		$("#scrollUp").css("display", "");
		$("#scrollUp").animate({
		height: "0px"}, 500 );
}

function showScrollUp()
{
		$("#scrollUp").css("display", "block");
		$("#scrollUp").animate({
		height: "19px"}, 500 );
}

function hideScrollDown()
{
		$("#scrollDown").css("display", "");
		$("#scrollDown").animate({
		height: "0px"}, 500 );
}

function showScrollDown()
{
		$("#scrollDown").css("display", "block");
		$("#scrollDown").animate({
		height: "19px"}, 500 );
}

function showLeftNav()
{
		$("#header .imgnav .left a").css("display", "block");
		$("#header .imgnav .left").animate({
		width: "27px"}, 500 );
}

function showRightNav()
{
		$("#selection .imgnav .right a").css("display", "block");
		$("#selection .imgnav .right").animate({
		width: "27px"}, 500 );
}

function hidePanelToggle() {
    //alert("RUnnin Hide Panel Toggle");
	if ( panelState == 0 || panelState == 1 )
	{
		$("#header #hidePanel .hideButton a").css("display", "block");
		$("#header #hidePanel .hideButton").animate({
		height: "31px"}, 500 );
	} else {

		$("#header #hidePanel .hideButton a").css("display", "none");
		$("#header #hidePanel .hideButton").animate({
		height: "0px"}, 500 );	
	}
}

//


//Displays the media configuration
function doShowMedia()
{
		panelState = 2;
		
		$("h2.project").text("News and Media");
		
		$("#header .spaceTop").animate({
		height: "150px"}, 500 );
		
		$("#header .spaceBottom").animate({
		height: "150px"}, 500 );
		
		$(".yearSelection").animate({
		height: "19px"}, 500 );
		
		$("#header .project").animate({
		height: "57px"}, 500 );
		
		$("#header .headerbg").animate({
		height: "96px"}, 500 );
		
		$("#information .top").animate({
		height: "0px"}, 500 );
		
		$("#information .imageNumberPanel .numberPanel").animate({
		height: "0px"}, 500 );
		
		//#interface .center #main #information .content .scrollableContent
		
		$("#information .imageNumberPanel .numberPanel").css("padding-top", "0px");
		$("#information .imageNumberPanel .numberPanel span").css("display", "none");
		
		$("#information .cornerRow").css("background-image", "url(images/layout/information_top.png)");
		$("#information .cornerRow").css("background-repeat", "no-repeat");
		
		$("#information .content").css("background-image", "url(images/layout/information_bg.png)");
		
		$("#main").css("background-image", "url(images/layout/media_bg.jpg)");
		
		hideLeftNav();
		hideRightNav();
		hidePanelToggle();
		showScrollUp();
		showScrollDown();

		$("#information .content").animate({
		    height: "435px"
		}, 500, "", function() {

		    $("#information .content .scrollableContent").jScrollPaneRemove();
		    $("#information .content .scrollableContent").html("");

		    $("#information .content .date").css("display", "block");
		    $("#information .content h2").css("display", "block");

		    $(".selectionDropDown").css("display", "inline");
		    $(".selectionDropDownLink").css("display", "inline");

		    $(".selectionOptions .listview a").addClass("active");
		    $(".selectionOptions .thumbview a").removeClass("active");
		    $(".selectionDropDownLink a span").text("List view");

		    getMediaSummary("list", true);
		    getCurrentYear();

		    getMediaSummaryByYear(availableYears[currentYearSelection], "list");
		});
		
		$("#information .top").animate({
		height: "0px"}, 500 );
		
		$("#information .spaceTop").animate({
		height: "0px"}, 500 );
		
		$("#selection .content").animate({
		height: "144px"}, 500 );
		
		$("#selection .spaceTop").animate({
		height: "96px"}, 500 );
		
		$("#selection .spaceBottom").animate({
		height: "96px"}, 500 );
		
		$("#header .projectLabel").css("visibility", "hidden");
		$("#header .project").css("margin-top", "0px");
}

//Forces the panel into it's most minimised state
function doMinimisePanel()
{

		hidePanelToggle();
		
		$("#header .projectLabel").css("visibility", "hidden");
		$("#header .project").css("margin-top", "0px");
		$("#header .project").css("height", "165px");
		
		$("#header .spaceTop").animate({
		height: "150px"}, 500 );
		
		$("#header .spaceBottom").animate({
		height: "150px"}, 500 );
		
		$("#header .project").animate({
		height: "57px"}, 500 );
		
		$("#header .headerbg").animate({
		height: "96px"}, 500 );




		if ($.browser.msie && parseInt($.browser.version) == 6) {
		    $("#information .content").animate({
		        height: "64px"
		    }, 500);
		} else {
		    $("#information .content").animate({
		    height: "78px"}, 500 );
		}

		
		$("#information .spaceTop").animate({
		height: "349px"}, 500 );
		
		$("#information .top").animate({
		height: "10px"}, 500 );
		
		$("#information .imageNumberPanel .numberPanel").animate({
		height: "32px"}, 500 );
				
		$("#information .imageNumberPanel .numberPanel").css("padding-top", "7px");
		$("#information .imageNumberPanel .numberPanel span").css("display", "");
		
		if ( hasPrevImage() )
			showLeftNav();
	
		if ( hasNextImage() )
			showRightNav();
		
		hideScrollUp();
		hideScrollDown();
		
		$("#information .content .jScrollPaneContainer").css("display", "none");














		if ($.browser.msie && parseInt($.browser.version) == 6) {
		    $("#selection .content").animate({
		        height: "36px"
		    }, 500);
		} else {
		    $("#selection .content").animate({
		        height: "36px"
		    }, 500);
		}
		
		if ( selectionObjectToScroll == "thumbs" )
		{
			$("#selection .content .selectionThumbs").removeAttr("style");
			$("#selection .content .selectionThumbs").attr("style", "display:none");			
		} else {
			$("#selection .content .selectionList").removeAttr("style");
			$("#selection .content .selectionList").attr("style", "display:none");	
		}

		//$("#selection .content .selectionThumbs").removeAttr("style");
		//$("#selection .content .selectionList").css("display", "none");
		
		$("#selection .spaceTop").animate({
		height: "150px"}, 500 );
		
		$("#selection .spaceBottom").animate({
		height: "150px"}, 500 );
		
		$("#lnkTogglePanel").attr("class","togglePanelUp");
		
		$("#information .cornerRow").css("background-image", "url(images/layout/bg_light.png)");
		$("#information .cornerRow").css("background-repeat", "repeat");
		
		$("#information .content").css("background-image", "url(images/layout/bg_light.png)");
	
}

//Shows the panel in its default state
function doMaximisePanel( doGetSummary )
{
		hidePanelToggle();
		
		$(".selectionOptions").css("display", "none");
		$(".selectionDropDownLink").css("display", "none");
		$(".selectionDropDown").css("height", "26px");
		
		$(".selectionDropDown").css("display", "none");
			
		$("#header .spaceTop").animate({
		height: "96px"}, 500 );
		
		$(".yearSelection").animate({
		height: "0px"}, 500, "", function () {
			$(".yearSelection").css("display", "none");
		} );

		if ($.browser.msie && parseInt($.browser.version) == 6) {
		    $("#header .spaceBottom").animate({
		        height: "104px"
		    }, 500);
		} else {
		    $("#header .spaceBottom").animate({
		    height: "96px"}, 500 );
		}

		if ($.browser.msie && parseInt($.browser.version) == 6) {
		    $("#header .project").animate({
		        height: "145px"
		    }, 500);
		} else {
		    $("#header .project").animate({
		    height: "153px"}, 500 );
		}

		
		
		$("#header .project").css("margin-top", "12px");

		if ($.browser.msie && parseInt($.browser.version) == 6) {
		    $("#header .headerbg").animate({
		        height: "196px"
		    }, 500);
		} else {
		    $("#header .headerbg").animate({
		    height: "204px"}, 500 );
		}

       var myUnit = 184;

		if ($.browser.msie && parseInt($.browser.version) == 6)
		    myUnit = 176;
		
		
        $("#information .content").animate({
		            height: myUnit + "px"}, 500, "", function() { 
		
			$("#information .content .jScrollPaneContainer").css("display", "");
			
			if ( selectionObjectToScroll == "thumbs")
			{
				//alert("thumbs mode detected");		
			
				$("#information .content .date").css("display", "none");
				$("#information .content h2").css("display", "none");
				
				$("#selection .content .selectionList").removeAttr("style");		
				$("#selection .content .selectionList").attr("style", "display:none");
				
				$("#selection .content .selectionThumbs").removeAttr("style");		
				$("#selection .content .selectionThumbs").attr("style", "display:inline");

				$("#selection .content .selectionThumbs").animate({
				top: selectionObjectCurrentPosition + "px"}, 500 );
				
				initThumbs();
				
			} else {
				//alert("List mode detected");			
			
				$("#information .content .date").css("display", "none");
				$("#information .content h2").css("display", "none");
				
				$("#selection .content .selectionList").removeAttr("style");		
				$("#selection .content .selectionList").attr("style", "display:inline");
				
				$("#selection .content .selectionThumbs").removeAttr("style");		
				$("#selection .content .selectionThumbs").attr("style", "display:none");

				$("#selection .content .selectionList").animate({
				top: selectionObjectCurrentPosition + "px"}, 500 );
				
				initList();
			}
			
		} );
		
		if ( doGetSummary )
			getDevelopmentsSummary();
		

		if ($.browser.msie && parseInt($.browser.version) == 6) {
		    $("#information .spaceTop").animate({
		    height: "249px"}, 500 );
		} else {
		    $("#information .spaceTop").animate({
		    height: "241px"}, 500 );		
		}

		
		$("#information .top").animate({
		height: "10px"}, 500 );
		
		$("#information .imageNumberPanel .numberPanel").animate({
		height: "32px"}, 500 );
		
		$("#information .imageNumberPanel .numberPanel").css("padding-top", "7px");
		$("#information .imageNumberPanel .numberPanel span").css("display", "");
		
		if ($.browser.msie && parseInt($.browser.version) == 6) {
		    $("#selection .content").animate({
		    height: "134px"}, 500, "", function () { 
    		
			    $("#information .content .scrollableContent").jScrollPaneRemove();
			    $("#information .content .scrollableContent").css("height", "150px");
			    $("#information .content .scrollableContent").jScrollPane({ scrollbarWidth: 10, maintainPosition: false });
    		
		    } );
		} else {
		    $("#selection .content").animate({
		    height: "144px"}, 500, "", function () { 
    		
			    $("#information .content .scrollableContent").jScrollPaneRemove();
			    $("#information .content .scrollableContent").css("height", "150px");
			    $("#information .content .scrollableContent").jScrollPane({ scrollbarWidth: 10, maintainPosition: false });
    		
		    } );		
		}

		
		$("#selection .spaceTop").animate({
		height: "96px"}, 500 );
		
		$("#selection .spaceBottom").animate({
		height: "96px"}, 500 );
		
		if ( hasPrevImage() )
			showLeftNav();
		
		if ( hasNextImage() )
			showRightNav();
		
		
		showScrollUp();
		showScrollDown();
		
		$("#lnkTogglePanel").attr("class","togglePanel");
		
		$("#header .projectLabel").css("visibility", "");
		
		$("#information .cornerRow").css("background-image", "url(images/layout/information_top_merged.png)");
		$("#information .cornerRow").css("background-repeat", "no-repeat");
		
		$("#information .content").css("background-image", "url(images/layout/information_bg.png)");
	
}

//Functionality for minimising and maximising the "Hide Project" panel
function doShowHidePanel()
{
	if ( panelState == 0 )
	{
		
		panelState = 1;
		doMinimisePanel();
		
	} else {
		
		panelState = 0;
		doMaximisePanel(false);
			
	}
}

//Initialises the project images and shows the 1st one
function initProjectImages( imageList )
{
	//alert(imageList);
	projectImages = imageList;
	currentImage = 0;
	hideLeftNav();
	
	if ( !hasNextImage() )
	{
		hideRightNav();
	} else {
		showRightNav();
	}
	displayImage();
	
	updateNumberPanel();
}

function hasNextImage()
{
	if ( currentImage >= (imageCount()-1) )
	{
		return false;
	} else {
		return true;
	}
}

function hasPrevImage()
{
	if ( currentImage > 0 )
	{
		return true;
	} else {
		return false;
	}
}

function displayImage()
{
	$("#main").css("background-image", "url(" + projectImages.split(";")[currentImage] + ")");
}

function showNextImage()
{
	if ( hasNextImage() )
	{
		currentImage++;
		displayImage();
		if ( !hasNextImage() )
			hideRightNav();
		
		if ( hasPrevImage() )
			showLeftNav();
		
		updateNumberPanel();
	}
}

function showPrevImage()
{
	if ( hasPrevImage() )
	{
		currentImage--;
		displayImage();

		if ( !hasPrevImage() )
			hideLeftNav();
		
		if ( hasNextImage() )
			showRightNav();
		
		updateNumberPanel();
	}
}

function scrollDown()
{
	if ( scrollInTransition )
		return;
	
	if ( selectionObjectToScroll == "thumbs" )
	{
		if ( selectionObjectCurrentPosition < maxThumbScroll )
		{
			scrollInTransition = true;
			
			selectionObjectCurrentPosition += 1;
			$("#selection .content .selectionThumbs").animate({
			top: (parseInt($("#selection .content .selectionThumbs").css("top")) - 78) + "px"}, 500, "", function () { scrollInTransition = false; } );
		}
	} else {
	
		if ( selectionObjectCurrentPosition >= ( maxThumbScroll + 14 ) )
		{
			scrollInTransition = true;
			
			selectionObjectCurrentPosition -= 14;
			$("#selection .content .selectionList").animate({
			top: selectionObjectCurrentPosition + "px"}, 500, "", function () { scrollInTransition = false; } );
		}
	}
	
	
	
}

function scrollUp()
{
	if ( scrollInTransition )
		return;
	
	if ( selectionObjectToScroll == "thumbs" )
	{

		if ( selectionObjectCurrentPosition > 0 )
		{
			
			scrollInTransition = true;
			
			selectionObjectCurrentPosition -= 1;
			$("#selection .content .selectionThumbs").animate({
			top: (parseInt($("#selection .content .selectionThumbs").css("top")) + 78) + "px"}, 500, "", function () { scrollInTransition = false; } );
		}
		
	} else {
	
		if ( selectionObjectCurrentPosition < 0 )
		{
			
			scrollInTransition = true;
			
			selectionObjectCurrentPosition += 14;
			$("#selection .content .selectionList").animate({
			top: selectionObjectCurrentPosition + "px"}, 500, "", function () { scrollInTransition = false; } );
		}
		
	}
}

function imageCount()
{
	return projectImages.split(";").length;
}

function initThumbs()
{
	$("#selection .content .selectionList").removeAttr("style"); 
	$("#selection .content .selectionList").css("display", "none");
	
	$("#selection .content .selectionThumbs").removeAttr("style"); 
	$("#selection .content .selectionThumbs").css("display", "inline");
	maxThumbScroll = Math.ceil($("#selection .content .selectionThumbs div").length/3) - 2;//($("#selection .content").height() + 10) - $("#selection .content .selectionThumbs").height();
	if ( maxThumbScroll < 0 )
		maxThumbScroll = 0;
	//alert("maxThumbScroll = " + maxThumbScroll);
	selectionObjectToScroll = "thumbs";
	selectionObjectCurrentPosition = 0;
	$("#selection .content .selectionThumbs").animate({
			top: selectionObjectCurrentPosition + "px"}, 500 );
}

function initList()
{
	$("#selection .content .selectionThumbs").removeAttr("style"); 
	$("#selection .content .selectionThumbs").css("display", "none");
	
	$("#selection .content .selectionList").removeAttr("style"); 
	$("#selection .content .selectionList").css("display", "inline");
	maxThumbScroll = $("#selection .content").height() - $("#selection .content .selectionList").height();
	selectionObjectToScroll = "list";
	selectionObjectCurrentPosition = 0;
	$("#selection .content .selectionList").animate({
			top: selectionObjectCurrentPosition + "px"}, 500 );
}

function highlightDevelopmentLink( devID )
{
	//alert(index);
	$("#selection .selectionThumbs a").removeClass("active");
	
	$("#selection .selectionThumbs a").each(function () { 
		if ( $(this).attr("devID") == devID )
			$(this).addClass("active");
	});
	
}

function highlightMediaLink( mediaID )
{
	
	//alert("Attempting to highlight " + mediaID);
	if ( selectionObjectToScroll == "thumbs" )
	{
		$("#selection .selectionThumbs a").removeClass("active");
		
		$("#selection .selectionThumbs a").each(function () { 
			if ( $(this).attr("mediaID") == mediaID )
				$(this).addClass("active");
		});
		
	} else {
			
		$("#selection .selectionList a").removeClass("active");
		
		$("#selection .selectionList a").each(function () { 
			if ( $(this).attr("mediaID") == mediaID )
				$(this).addClass("active");
		});
		
	}

}

function updateNumberPanel()
{
	
	var nIMGCount = imageCount() + "";
	var nCurrentImage = (currentImage + 1) + "";
	
	if ( nIMGCount.length == 1 )
		nIMGCount = "0" + nIMGCount;
	
	if ( nCurrentImage.length == 1 )
		nCurrentImage = "0" + nCurrentImage;
	
	$(".numberPanel .currentImage").text(nCurrentImage);
	$(".numberPanel .imageCount").text(nIMGCount);
}

//----------------------------------------------------------
// Get the content for a page
//----------------------------------------------------------
function getPageContent( sPageURL )
{

    $.ajax({
        type: "POST",
        url: "WebServices/Data.asmx/getPageContent",
        data: "{'pageURL':'" + sPageURL + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            //alert(msg.d);
            if (sPageURL == "get-in-touch.aspx") {
                //set site map height to 163 and content height to 163;
                $("#map_canvas").css("height", "213px");
                $("#myModalContent").css("height", "113px");
            } else {
                //set site map height to 0px and content height t0 326px;
                $("#map_canvas").css("height", "0");
                $("#myModalContent").css("height", "326px");
                //$("#myModalContent").css("display", "none");
            }
            populatePageContent(msg.d);
            if ( sPageURL == "get-in-touch.aspx")
                initializeMap();
        }
    });
}

//----------------------------------------------------------
// Apply the page content and show the modal
//----------------------------------------------------------
function populatePageContent( sContent )
{
	
	$("#modal .modalContent .content h1").text(getItemVariable(0, "Heading", sContent));
	$("#modal .modalContent .content .scrollableContent").html(getItemVariable(0, "Content", sContent));
	$("#modal").jqmShow();
	$('#modal .scrollableContent').jScrollPane({ scrollbarWidth: 10, maintainPosition: false });
	setModalPosition();
	
}


function setModalPosition()
{
	//$("#modal")
		
	var footerHeight = 938;
	var footerWidth = 939;
	var minHeight = 595;
	var documentHeight = $(document).height();
	var documentWidth = $(document).width() - 1;
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();
	var interfaceHeight = $("#modal").height();
	var interfaceWidth = $("#modal").width();

	var footerPositionTop;
	var footerPositionLeft;

	$("#modal").css("left", (windowWidth - interfaceWidth)/2 + "px");
	$("#modal").css("top", (windowHeight - interfaceHeight)/2 + "px");
	
	if (( (windowHeight - interfaceHeight)/2) < 0 )
		$("#modal").css("top", "0px");
	
	if (( (windowWidth - interfaceWidth)/2) < 0 )
		$("#modal").css("left", "0px");

}

//----------------------------------------------------------
// Get the development summary data
//----------------------------------------------------------
function getDevelopmentsSummary()
{
	//var sReturnedData = "";
	
  $.ajax({
    type: "POST",
    url: "WebServices/Data.asmx/getDevelopmentsSummary",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
      //alert(msg.d);
			populateDevelopmentsSummary(msg.d);
    }
  });
}

//----------------------------------------------------------
// Apply the Development Summary Data (Write it on the page)
//----------------------------------------------------------
function populateDevelopmentsSummary( sContent )
{
	var nItemCount = getItemCount(sContent);
	var sItem = "";
	var devID = 0;
	var activeItem = 0;
	
	activeItem = getRandomNumber(nItemCount-1);
	
	$("#selection .selectionThumbs div").remove();
	
	for ( i=0; i<nItemCount; i++ )
	{
		$("#thumbTemplate").clone(true).attr("id", "devThumb" + i ).appendTo("#selection .selectionThumbs");
		
		devID = getItemVariable(i, "DevelopmentID", sContent);
		$("#devThumb" + i + " a").attr("devID", devID);

		$("#devThumb" + i + " a").click(function() {

		    //alert($(this).attr("devID"));
		    getDevelopment($(this).attr("devID"));

		});
		
		$("#devThumb" + i + " img").attr("src", getItemVariable(i, "DevelopmentImage", sContent));
		$("#devThumb" + i + " span").text(getItemVariable(i, "DevelopmentName", sContent));
		$("#devThumb" + i).removeAttr("style");			
		
		if ( i == activeItem )
		{
			getDevelopment(getItemVariable(i, "DevelopmentID", sContent));
		}
	}
	
	initThumbs();
	
}

//----------------------------------------------------------
// Get the development summary data
//----------------------------------------------------------
function getCurrentYear()
{
	//var sReturnedData = "";

    $.ajax({
        type: "POST",
        url: "WebServices/Data.asmx/getCurrentYear",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            //
            //alert("Getting available years");
            $.ajax({
                type: "POST",
                url: "WebServices/Data.asmx/getMediaAvailableYears",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(rmsg) {
                    //alert("Got em!");
                    //alert(rmsg.d);
                    var nItemCount = getItemCount(rmsg.d);
                    var myList = "";

                    for (i = 0; i < nItemCount; i++) {
                        myList += getItemVariable(i, "Year", rmsg.d);
                        if (i != (nItemCount - 1))
                            myList += ",";
                    }

                    availableYears = myList.split(",");
                    initYears();
                    $(".yearSelection .Year").text(availableYears[currentYearSelection]);
                    //initList();
                }
            });

        }
    });
}

function initYears()
{
	currentYearSelection = availableYears.length - 1;
	$(".nextYear").css("background-image", "url(images/spacer.gif)");
	
	if ( !prevYearAvailable() )
	{
		$(".prevYear").css("background-image", "url(images/spacer.gif)");
	} else {
		$(".prevYear").css("background-image", "url(images/buttons/previous-year.gif)");
	}
}

function nextYearAvailable()
{
	if ( currentYearSelection == availableYears.length - 1 )
	{
		return false;
	} else {
			return true;
	}
}

function prevYearAvailable()
{
	//alert("Current year selection = " + currentYearSelection);
	if ( currentYearSelection > 0 )
	{
		return true;
	} else {
		return false;
	}	
}

function nextYear()
{
	if ( nextYearAvailable() )
	{
		currentYearSelection++;
		$(".yearSelection .Year").text(availableYears[currentYearSelection]);
		getMediaSummaryByYear(availableYears[currentYearSelection], selectionObjectToScroll);
		if ( !nextYearAvailable() )
			$(".nextYear").css("background-image", "url(images/spacer.gif)");
		
		if ( nextYearAvailable() )
			$(".nextYear").css("background-image", "url(images/buttons/next-year.gif)");
		
		if ( prevYearAvailable() )
			$(".prevYear").css("background-image", "url(images/buttons/previous-year.gif)");
	}
}

function previousYear()
{
	if ( prevYearAvailable() )
	{
		currentYearSelection--;
		$(".yearSelection .Year").text(availableYears[currentYearSelection]);
		getMediaSummaryByYear(availableYears[currentYearSelection], selectionObjectToScroll);
		
		if ( !prevYearAvailable() )
			$(".prevYear").css("background-image", "url(images/spacer.gif)");
		
		if ( prevYearAvailable() )
			$(".prevYear").css("background-image", "url(images/buttons/previous-year.gif)");
		
		if ( nextYearAvailable() )
			$(".nextYear").css("background-image", "url(images/buttons/next-year.gif)");
	}
}


//----------------------------------------------------------
// Get the development data
//----------------------------------------------------------
function getDevelopment( devID )
{
    //alert(devID);
    $.ajax({
        type: "POST",
        url: "WebServices/Data.asmx/getDevelopment",
        data: "{'developmentID':'" + devID + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            //alert("About to load");
            if (debugMode)
                alert(msg.d);
                
            populateDevelopment(msg.d);
            highlightDevelopmentLink(devID);
        }
    });
	
}

function populateDevelopment( sContent )
{
	$("h2.project").text(getItemVariable(0, "DevelopmentName", sContent));
	$("#information .scrollableContent").html(getItemVariable(0, "DevelopmentContent", sContent));
	$('#information .scrollableContent p:first-child').css("margin-top", "0px");
	$('#information .content .scrollableContent').jScrollPane({ scrollbarWidth: 10, maintainPosition: false })
	initProjectImages(getItemVariable(0, "DevelopmentImages", sContent));
}

//----------------------------------------------------------
// Get the news data
//----------------------------------------------------------
function getMedia( mediaID )
{
	
	activeMediaArticle = mediaID;
	
  $.ajax({
    type: "POST",
    url: "WebServices/Data.asmx/getMediaArticle",
    data: "{'articleID':'" + mediaID + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
			populateMedia(msg.d);
			highlightMediaLink(mediaID);
    }
  });
	
}

function populateMedia( sContent )
{
	$("#information .content .date").text(getItemVariable(0, "NewsDate", sContent));
	$("#information .content h2").text(getItemVariable(0, "NewsTitle", sContent));
	$("#information .scrollableContent").html(getItemVariable(0, "NewsContent", sContent));
	$('#information .scrollableContent p:first-child').css("margin-top", "0px");
	
	var contentAreaHeight = 385 - $("#information .content h2").height();
	
	$("#information .content .scrollableContent").css("height", contentAreaHeight + "px");// Need to remove 75px and display the crap above it (title n date)
	$("#information .content .scrollableContent").jScrollPane({ scrollbarWidth: 10, maintainPosition: false });

}


function getMediaSummaryByYear ( year, type )
{
	
  $.ajax({
    type: "POST",
    url: "WebServices/Data.asmx/getMediaSummaryYear",
    data: "{'nYear':'" + year + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
			populateMediaSummary(msg.d, type, true);
    }
  });
	
}

//----------------------------------------------------------
// Get the development summary data
//----------------------------------------------------------
function getMediaSummary( type, getRandomEntry )
{
	//var sReturnedData = "";
	
  $.ajax({
    type: "POST",
    url: "WebServices/Data.asmx/getMediaSummary",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
			populateMediaSummary(msg.d, type, getRandomEntry);
    }
  });
}

//----------------------------------------------------------
// Apply the Media Summary Data (Write it on the page)
//----------------------------------------------------------
function populateMediaSummary( sContent, type, getRandomEntry )
{
	var nItemCount = getItemCount(sContent);
	var sItem = "";
	var devID = 0;
	var activeItem = activeMediaArticle;
	
	if ( getRandomEntry )
	{
		activeItem = 0;//getRandomNumber(nItemCount-1);
		//alert("Getting a random article");
	}
	
	//Clear anything in there at the moment.
	$("#selection .selectionThumbs div").remove();
	$("#selection .selectionList div").remove();	
	
	if ( type == "thumbs" )
	{
		for ( i=0; i<nItemCount; i++ )
		{
			$("#thumbTemplate").clone(true).attr("id", "devThumb" + i ).appendTo("#selection .selectionThumbs");
			
			mediaID = getItemVariable(i, "NewsID", sContent);
			$("#devThumb" + i + " a").attr("mediaID", mediaID);

			$("#devThumb" + i + " a").click( function () {
				
				getMedia($(this).attr("mediaID"));
				
			});

			if (getItemVariable(i, "NewsImage", sContent) != "") {
			    $("#devThumb" + i + " img").attr("src", getItemVariable(i, "NewsImage", sContent));
			    //alert(getItemVariable(i, "NewsImage", sContent));
			} else {
			    $("#devThumb" + i + " img").attr("src", "images/temp_thumb2.jpg");
			}
			
			$("#devThumb" + i + " span").text(getItemVariable(i, "NewsTitle", sContent));
			$("#devThumb" + i).removeAttr("style");			
			
			if ( i == 0 && getRandomEntry )
			{
				getMedia(mediaID);
			} else if ( i == 0 && !getRandomEntry ) {
				setTimeout("highlightMediaLink(" + activeMediaArticle + ")", 1000);
			}
		}
		
		initThumbs();
		
	} else {
		for ( i=0; i<nItemCount; i++ )
		{
			$("#listItemTemplate").clone(true).attr("id", "listItem" + i ).appendTo("#selection .content .selectionList");
			
			mediaID = getItemVariable(i, "NewsID", sContent);
			$("#listItem" + i).css("display", "inline");
			$("#listItem" + i + " a").attr("mediaID", mediaID);
			$("#listItem" + i + " a").click( function () {
				
				getMedia($(this).attr("mediaID"));
				
			});
			
			$("#listItem" + i + " .title").text(getItemVariable(i, "NewsTitle", sContent));
			$("#listItem" + i + " .date").text(getItemVariable(i, "NewsDate", sContent));
			
			if ( i == 0 && getRandomEntry )
			{
				getMedia(mediaID);
			} else if ( i == 0 && !getRandomEntry ) {
				setTimeout("highlightMediaLink(" + activeMediaArticle + ")", 1000);
			}
		}
		
		initList();
		
	}
	
	
	
}

function displayIE6Message() {
$("#modal .modalContent .content h1").text("Please update your browser");
$("#modal .modalContent .content .scrollableContent").html("<p>Internet Explorer 6 is over 9 years old.  You would have a better internet experience if you downloaded a more recent version. </p><p><a title=\"Get the latest version of Internet Explorer\" href=\"http://www.microsoft.com/ie/\" target=\"_blank\">Click here to download the latest version of Internet Explorer, it's free.</a></p>");
	    $("#modal").jqmShow();
	    $('#modal .scrollableContent').jScrollPane({ scrollbarWidth: 10, maintainPosition: false });
	    setModalPosition();
}

$(document).ready(
	function() {

	    setHookUps();

	    //Initialise Developments
	    $("#interface").css("position", "absolute");
	    $("#interface").css("margin-top", "0");

	    $("#interface .center #main #selection .content .selectionThumbs").css("display", "none");
	    $("#interface .center #main #selection .content").css("overflow", "hidden");
	    $("#interface .center #main #selection .content").css("overflow-x", "hidden");
	    $("#interface .center #main #selection .content").css("overflow-y", "hidden");

	    $("#interface .center #main #information .content .scrollableContent").css("overflow", "hidden");
	    $("#interface .center #main #information .content .scrollableContent").css("overflow-x", "hidden");
	    $("#interface .center #main #information .content .scrollableContent").css("overflow-y", "hidden");

	    $("#interface .center #main #header #hidePanel .hideButton").css("background-image", "url(images/layout/project_panel_bg.png)");
	    $("#interface .center #main #header #hidePanel .hideButton a").css("display", "block");
	    $("#partnerLink").css("display", "none");


	    //

	    if ($.browser.msie && parseInt($.browser.version) == 6)
	        setTimeout("displayIE6Message()", 2500);
	    //getPageContent("internet-explorer.aspx");
	    //myvar = '[[PageID:7]][[Heading:Please update your browser]][[Content:<p>This site is best viewed in the most recent browsers. You are currently using Internet Explorer 6 which is not supported by this web site.</p><p><a title="Get the latest version of Internet Explorer" href="http:\/\/www.microsoft.com/ie/" target="_blank">Click here to get the latest version of Internet Explorer.</a></p>]]'
	    //populatePageContent(myvar);




	    //}


	    //Because of the scrollbars being on the screen for a touch longer 
	    //than we'd like, we need to run this bad boy twice or we'll get 
	    //a gap where the scrollers were
	    resetPositions();
	    getDevelopmentsSummary();

	    setTimeout("resetPositions()", 2500);

	    $(window).resize(function() {
	        resetPositions();
	        setTimeout("resetPositions()", 100);
	    });

	    $(".modal .modalContent .scrollableContent p:first-child").css("margin-top", "0px");
	    $("#modal").jqm();

	}
)
	

	
	
	
	
	
	
	
	
	
	
	
	
	
function showSubMenu2(menulist) {
  setMenuState(menulist, 1);
  var sMenuState = getMenuState(menulist);

  if (document.getElementById(menulist).style.display == "")
    return;
  if (sMenuState == 1) {
      $('#' + menulist).slideDown(200);
  }

}

function setMenuState(menulist, state) {
  if (sMenu.indexOf(menulist) == -1) {
    sMenu += menulist + "," + state + ":";
  } else {
    var nCurrentMenuState = getMenuState(menulist);
    var sCurrentMenu = menulist + "," + nCurrentMenuState + ":";
    var sNewMenu = menulist + "," + state + ":";
    sMenu = sMenu.replace(sCurrentMenu, sNewMenu);
  }

  return -1;
}

function getMenuState(menulist) {
  if (sMenu.indexOf(menulist) == -1) {
    setMenuState(menulist, 0);
    return 0;
  } else {
    var sMenuArray = new Array();
    sMenuArray = sMenu.split(":");

    for (i = 0; i < sMenuArray.length; i++) {
      if (sMenuArray[i].indexOf(menulist) != -1) {
        var sMenuItemArray = new Array();
        sMenuItemArray = sMenuArray[i].split(",");
        return sMenuItemArray[1];
      }
    }
  }

  return -1;
}

function doHideSubMenu2(menulist) {

  if (getMenuState(menulist) == 0 && document.getElementById(menulist) && document.getElementById(menulist).style.display != "none") {
    $('#' + menulist).slideUp(200);
	}
}

function hideSubMenu2(menulist) {
  var nMenuState = getMenuState(menulist);
  nMenuState = 0;
  setMenuState(menulist, nMenuState);

  setTimeout("doHideSubMenu2('" + menulist + "')", 0);
  
}

function incrementWatcher2(menulist) {
  var nMenuState = getMenuState(menulist);
  nMenuState += 1;
  setMenuState(menulist, nMenuState);
}
