	
	var carosel_timer;
	var menu_click;
	var first_load = true;
	
	$(document).ready(function(){
		
		$.historyInit(show_page);
		
		ajaxlinks();
		menulinks();
		quicksearch();
		
		//setTimeout(menufind,1000);

		$("#menu ul").each(function(){
			$(this).find("ul").hide();
			$(this).find("li:last").css("border-bottom","none");
		});
		
		carosel_timer = setTimeout(feature_item,100);
		googleMaps();
		
		$('#sidebar_body div.block:last').addClass('lastblock');
		
		
		
		
	});
	
	function feature_item()
	{
		var last = $(".carosel_last").html();
		if (last) var uri = BASE_URL+"?/view/feature/"+last;
		else var uri = BASE_URL+"?/view/feature";
		
		$.get(uri,function(result){
			if (result)
			{
				$("#mycarousel li").fadeOut('fast',function(){
					$(this).html(result).slideDown('fast');
					ajaxlinks('#mycarousel');
				});
			}
		});
		//carosel_timer = setTimeout(feature_item,10000);
	}

	function show_page(link)
	{
		
		if (!link) return;
		
		clearTimeout(carosel_timer);
		
		if (link.indexOf("/ajax") < 0) {
			link = link + "/ajax";
		}
		
		if (link.indexOf("http") < 0) {
			link = BASE_URL + link; //ie6? huh?
		}
		
		//alert(link);
		
		$.scrollTo(0,400,function(){
			
			
			// moves to a new page. 
			// pageload is called at once. 
			
			
			$("#leftfade").hide();
			$('#content').addClass('loading');
				
				$.get(link,function(result){	
					$("#left").html(result);
					
					//alert(result);
					if (link.indexOf("view/index") > 0) {
						$('#left').addClass('homebg');
					} else {
						$('#left').removeClass('homebg');
					}
					
					$("#leftfade").show();
				
						ajaxlinks('#left');
						menulinks();
						googleMaps();
						//carosel_timer = setTimeout(feature_item,10000);
						first_load = false;
						
						$('#sidebar_body div.block:last').addClass('lastblock');
						
						$('#content').removeClass('loading');
						
					if (!menu_click) setTimeout(menufind,300);
					menu_click = false;
				});
			
		});
	}
	
	function googleMaps()
	{
		$(".google").hide();
	}

	function ajaxlinks(container)
	{
		if (!container) container = "#left, #topheader, #right, #footer";
		
		//unbind all internal links, not # links, rebind with ajax loading.

		$(container+" a[href~=citsolutions.edu.au/?], a[href~=citsolutions.cit.act.edu.au/?], a[href^="+BASE_URL+"?], "+container+" a[href^=?], "+container+" a[href^=/?]").not("[href~=#],.no_js").unbind().click(function(){
			var link = $(this).attr('href');
			link = link.replace(/^.*\?/, '?');
			
			$('li.open').removeClass('open');
			$('li.kids').removeClass('kids');
			
			menu_click = false;
			$.historyLoad(link);
			//show_page(link,true);
			return false;
		});
		
		$(container+" a[href~=#]").unbind().click(function(){
			var oldhash = window.location.hash;
			var uri = $(this).attr('href');
			if (uri) {
			uri = uri.split('#');
			uri = uri.reverse();
					
				if (uri[0])
				{
					var n_uri = $('a[name='+uri[0]+']');
					if (n_uri.length)
					{
						$.scrollTo(n_uri,400);
						return false;
					}
				}
			
			}
			return true;
		});
		
	}
	
	function menulinks()
	{
		$("#menu a[href^="+BASE_URL+"?], #menu a[href~=citsolutions.edu.au], #menu a[href~=citsolutions.cit.act.edu.au/?], #menu a[href^=?], #menu a[href^=/?]").not("[href~=#]").unbind().click(function(){
			menu_click = true;
			
			var ignore = [];
			var p_ul = $(this).parents("ul").each(function(){
				var id = $(this).attr("id");
				ignore.push(id);
			});
			
			$("#menu ul").each(function(){
				var id = $(this).attr("id");
				var skip = false;
				for (i in ignore) if (ignore[i] == id) skip = true;
				if (!skip) $(this).slideUp('slow');
			});
			
			
			$(this).parents('li').find("ul:first").slideDown('fast');
			
			$('li.open').removeClass('open');
			$('li.kids').removeClass('kids');
			
			$(this).parents('li[id^=sect]').addClass('open');
			var ee = this;
			$(this).parents('li').find("ul:first").each(function(){
				$(ee).parents('li[id^=sect]').removeClass('open').addClass('kids');
			});
			
			var link = $(this).attr('href');
			link = link.replace(/^.*\?/, '?');
			
			$.historyLoad(link);
			
			//show_page(link,false);
			return false;
		});
	}
	
	function quicksearch()
	{
		$("#quicksearch").submit(function(){
			var val = $("#quicksearch_val").val();
			var link = $(this).attr("action")+"/ajax";
			
			if (!val) return false;
			
			
			$("#leftfade").fadeOut('fast',function(){
				
				$.post(link,{search: val},function(result){
					
					$("#left").html(result);
					
					if (link.indexOf("view/index") > 0) {
						$('#left').addClass('homebg');
					} else {
						$('#left').removeClass('homebg');
					}
					
					$("#leftfade").fadeIn('fast');
					
						ajaxlinks('#left');
						menulinks();
					
				});
			});
			
			return false;
		});
	}

	function menufind()
	{
		var id = $("#menu_val").html();
		var pid = $("#page_val").html();
		
		
		if (pid) {
			if ($("#id_0 ul:visible li[rel="+pid+"]").length) {
				//find something in the open meniu first
				
				$("#id_0 ul:visible li[rel="+pid+"]:first").parents('ul:hidden').slideDown('slow');
				$("#id_0 ul:visible li[rel="+pid+"]:first > ul").slideDown('slow');
				$("#id_0 ul:visible li[rel="+pid+"]:first").parents('li').addClass('kids');
				$("#id_0 ul:visible li[rel="+pid+"]:first").addClass('open');
				return;
			}
		}
		
		
		if (!id) return;
		
		var ul_id = "#id_"+id;
		var li_id = "#sect_"+id;
		
		$("#menu ul:visible").not(":first").slideUp('slow');
		
		$(ul_id+","+li_id).parents("ul").each(function(){
			$(this).slideDown('slow');
		});
		
		$(ul_id).slideDown('slow');
		
		try {
			if ($(ul_id).length > 0) {
				$(ul_id).parents('li[id^=sect]').addClass('open');
			} else {
				$(li_id).addClass('open');
			}
		} catch(e) {
			//nothing
		}
		
		var ee = ul_id;
		$(ul_id).parents('li').find("ul:first").each(function(){
			$(ee).parents('li[id^=sect]').removeClass('open').addClass('kids');
		});
	}
	
	function showGooglemap()
	{
		//alert("called");
		$(".google").dialog("destroy");
		$(".google").css("width",640).css("height",410).show();
		
		setTimeout(function(){
			$(".google").dialog({ 
			    modal: true, 
			    overlay: { 
			        opacity: 0.5, 
			        background: "black" 
			    },
			    resizable: false,
			    width: 640,
			    height: 410,
			    title: "Google Map"
			});
		},1000);
		
		setTimeout(function(){
			onGoogleLoad();
			$(".google").find(".ui-dialog-content").css("padding","0");
		},1500);

		
		
		return false;
	}
	
	function showExtras()
	{
		$('#rotating-promo').show();
		$('#footer').css('padding-top','0');
	}
	
	function hideExtras()
	{
		$('#rotating-promo').hide();
		$('#footer').css('padding-top','80px');
	}
	
	
	
	
	var animatedtab = true;
	
	// Rotating promos
	var rotating_promo_current = 1;
	var rotating_promo_total = 0;
	var rotating_promo_timer = null;
	
	function rotating_promo_init() {
		rotating_promo_total = $('.rotating-promo-info').length;
		
		$('#rotating-promo').mouseover(function() {clearTimeout(rotating_promo_timer);});
		$('#rotating-promo').mouseleave(function() {rotating_promo_timer = setTimeout('rotating_promo_switch()',3000);});
	
		$('#rotating-promo-menu ul > li').click(function(e) {
			e.preventDefault();
			
			var obj = $(this);
			var id = obj.attr('id').split('-')[4];
			
			rotating_promo_switch(id);
		});
		
		rotating_promo_switch(1);
		if (rotating_promo_total > 1) {
			rotating_promo_timer = setTimeout('rotating_promo_switch()',6000);
		}
	}
	
	function rotating_promo_switch(id) {
		if (id == undefined) {
			if ((++rotating_promo_current) > rotating_promo_total) {
				rotating_promo_current = 1;
			}
			
			rotating_promo_timer = setTimeout('rotating_promo_switch()', 6000);
		}
		else {
			if (rotating_promo_timer != null) {
				clearTimeout(rotating_promo_timer);
				rotating_promo_timer = null;
			}
			
			rotating_promo_current = id;
		}
	
		id = rotating_promo_current;
		
		var img = $('#rotating-promo-info-image-' + id);
		
		$('#rotating-promo-current-title').html(img.attr('alt'));
		$('#rotating-promo-current-description').html($('#rotating-promo-info-description-' + id).html());
		$('#rotating-promo-link').attr('href', $('#rotating-promo-info-link-' + id).attr('href'));
		
		$('#rotating-promo-menu ul > li').removeClass('selected');
		$('#rotating-promo-menu-item-' + id).addClass('selected');
		
		$('#rotating-promo-image').attr('src', img.attr('src')).attr('alt', img.attr('alt')).hide().fadeIn("slow");
	}
	
	
	//TODO: remove this. needed for front page
	function frontpromo() {
		$('#frontpage-promo-images ul > li').css("display","none");
		frontpromo_current = 0;
		frontpage_promo_switch(1);
	}
	
	var frontpage_rotateTimeout;
	
	function frontpage_promo_switch(num) {
		if (num == undefined) {
			if ((++frontpromo_current) > frontpromos_number) { frontpromo_current = 1; }
		} else {
			frontpromo_current = num;
		}
		num = frontpromo_current;
		$('#frontpage-promo-caption-title').html(frontpromos_data[num-1]['title']);
		$('#frontpage-promo-caption-desc').html(frontpromos_data[num-1]['desc']);
		$('#frontpage-promo-images ul > li').css("display","none");
		$('#frontpage-promo-img-'+num).fadeIn("slow");
		$('#frontpage-promo-menu ul > li').removeClass();
		$('#frontpage-promo-button-'+num).addClass('selected');
		document.getElementById('frontpage-promo-link').href = frontpromos_data[num-1]['url'];
		if (frontpage_rotateTimeout != undefined) clearTimeout(frontpage_rotateTimeout);
		frontpage_rotateTimeout = setTimeout('frontpage_promo_switch()',3000);
	}


