// preload images function
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

// preload images
$.preloadImages(
	"http://www.targetzero.info/images/templates/btn_go_over.gif",
	"http://www.targetzero.info/images/templates/nav_droparrow.gif",
	"http://www.targetzero.info/images/templates/nav_rule.gif",
	"http://www.targetzero.info/images/templates/nav_top.gif",
	"http://www.targetzero.info/images/templates/nav_bottom.png",
	"http://www.targetzero.info/images/templates/icon_error.gif",
	"http://www.targetzero.info/images/templates/icon_ok.gif",
	"http://www.targetzero.info/images/templates/loading.gif",
	"http://www.targetzero.info/images/templates/btn_maximise.gif"
);

var call_welcome = function() {
	$('#welcome_text').fadeIn(10);
}

function buildinghilite(buildingtype) {
	var flashmovie = document.getElementById('tz_town');
	flashmovie.hiliteBuilding(buildingtype);
}

function buildingdeselect(buildingtype) {
	var flashmovie = document.getElementById('tz_town');
	flashmovie.deselectBuilding(buildingtype);
}

function buttonshide() {
	var flashmovie = document.getElementById('tz_town');
	flashmovie.hideButtons();
}

function buttonsrestore() {
	var flashmovie = document.getElementById('tz_town');
	flashmovie.showButtons();
}

var call_popup = function(uri) {
	$('#welcome_text').hide();
	$.fn.colorbox({href:"http://www.targetzero.info/" + uri + "/ajax #content", opacity:0.8, onOpen: function(){ buttonshide(); }, onComplete: function(){ $('#cboxTitle').html($('#content').attr('title')); $('form',this).validate(); }, onClosed: function(){ buttonsrestore(); }});
}

var initialisebuttons = function() {
	if($('body').attr('id') == 'home') {
		buttonsrestore();
	}
}

$(function() {
	
	$('#tz_town').css({'height': $(window).height() + $(window).scrollTop()});
	
	$(window).resize(function () {
		$('#tz_town').css({'height': $(window).height() + $(window).scrollTop()});
	});
	
	$(window).scroll(function () {
		if (($('#tz_town').height() - 180) < $('#content').height() + 124) {
			$('#tz_town').css({'height': $(window).height() + $(window).scrollTop()});
		}
	});
	
	$('#home #welcome_text').hide(0.01).offset({left: $(window).width() / 2 - 498,top: ($(window).height() + $(window).scrollTop()) - 239}).oneTime(4000, function() {
		$(this).fadeIn(10, function() {
			$(this).oneTime(15000, function() {					
				$(this).hide();  
			});	   
		});
	});
	$('#home #welcome_text p').append(' Click on the highlighted buildings to reveal the key information contained within.');
	$('#home #welcome_text h2').append('<a rel="nofolow"><img src="images/templates/btn_close.png" alt="Close" class="png"></a>');
	
	$('body:not("#games_arcade") #reports_nav > li').hoverIntent(
		function(){
			rep_id = $(this).attr('id');
			rep_req = rep_id.replace('nav_','');
			buildinghilite(rep_req);
		},
		function(){
			buildingdeselect(rep_req);
		}
	);
	
	$('body:not("#games_arcade") #reports_nav > li > a').each(
		function(){
			report_id = $(this).parent('li').attr('id');
			report_req = report_id.replace('nav_','');
			$(this).colorbox({href:"http://www.targetzero.info/guidance_reports/view/" + report_req + "/ajax #content", opacity:0.8, onOpen: function(){ buttonshide(); }, onLoad: function() { $('#welcome_text').fadeOut('fast'); }, onComplete: function(){ $('#cboxTitle').html($('#content').attr('title')); }, onClosed: function(){ buttonsrestore(); }});
		}
	);
	
	$('#games_arcade #reports_nav > li > a').each(
		function(){
			report_id = $(this).parent('li').attr('id');
			report_req = report_id.replace('nav_','');
			$(this).colorbox({href:"http://www.targetzero.info/guidance_reports/view/" + report_req + "/ajax #content", opacity:0.8, onComplete: function(){ $('#cboxTitle').html($('#content').attr('title')); }});
		}
	);
	
	$('body:not("#games_arcade") #nav > li').hoverIntent(
		function(){
			$(this).find('ul').css({'display':'block'});
			nav_id = $(this).find('a').attr('id');
			nav_req = nav_id.replace('nav_','');
			buildinghilite(nav_req);
		},
		function(){
			$(this).parent().find('ul').css({'display':'none'});
			buildingdeselect(nav_req);
		}
	);
	
	$('#games_arcade #nav > li').hoverIntent(
		function(){
			$(this).find('ul').css({'display':'block'});
		},
		function(){
			$(this).parent().find('ul').css({'display':'none'});
		}
	);
	
	$('#nav > li > ul > li > a').click(
		function(){
			var target = $(this).attr('href');
			top.location = target;
		}
	);
	
	$('body:not("#games_arcade") h2 > .min_btn').click(
		function(){
			$('body').toggleClass('minimised');
			buttonsrestore();
		}
	).css({'cursor':'pointer'});
	
	$('#games_arcade" h2 > .min_btn').click(
		function(){
			top.location = "../";
		}
	).css({'cursor':'pointer'});
	
	$('#welcome_text > h2 > a').click(
		function(){
			$('#welcome_text').hide();
		}
	).css({'cursor':'pointer'});
	
	$('#exhib_popup').click(
		function(){
			call_popup("exhibition_centre");
			return false;
		}
	);
	
	$('#school_popup').click(
		function(){
			call_popup("guidance_reports/view/school");
			return false;
		}
	);
	
	$('#warehouse_popup').click(
		function(){
			call_popup("guidance_reports/view/warehouse");
			return false;
		}
	);
	
	$('#office_popup').click(
		function(){
			call_popup("guidance_reports/view/office");
			return false;
		}
	);
	
	$('#mixed_use_popup').click(
		function(){
			call_popup("guidance_reports/view/mixed_use");
			return false;
		}
	);
	
	$('#supermarket_popup').click(
		function(){
			call_popup("guidance_reports/view/supermarket");
			return false;
		}
	);
	
});