$(document).ready( function () {
							
	
	/* DISPLAY THE JS BUTTONS */
	$(".hide").css("visibility","visible");
							
	
							
	/* Print/mail/fonts feature */
	$("#mail_print #emailThisPage").click(function(){ //Email
												
		var SubjectLine=top.document.title;
		var url = top.document.location.href;
		
		if ($("#school_stories").length > 0){ //Special case if we are on School stories page
			
			var BodyText = "The sender of this link has forwarded the URL because of your interest in MindMatters:\n\n";
			
			BodyText = BodyText + url.substring(0, url.indexOf(".html") + 5) ;
			
			
			BodyText = BodyText + "?state=" + $("#school_stories #school_stories_map img").attr("id") + "&story=" + $("#school_stories .school_stories_story").attr("id");
			
		} else {
			var BodyText='The sender of this link has forwarded the URL because of your interest in MindMatters:\n\n' + url;
			
		}
		
		location.href = "mailto:?SUBJECT="+escape(SubjectLine)+"&BODY="+escape(BodyText);	
		return false;
	});
	
	$("#mail_print #printThisPage").click(function(){ //Print
		window.print();							
	});
	
	/* Left hand side menu hover */
	$("#left_frame dt").hover(
		function(){
			if ( $("a",this).size()  ) {
				$(this).addClass("hover");		
				$("a",this).addClass("hover");		
			}
		},
		function(){
			if ( $("a",this).size()  ) {
				$(this).removeClass("hover");		
				$("a",this).removeClass("hover");		
			}									
		}
	); 
	$("#left_frame dt").click(
		function(){

		 	if ( $("a",this).size()  && $("a",this).attr("href").search("http://") == -1) {
				document.location.href = $("a",this).attr("href");		
			}
		}
	);
	
	/* Search auto fill in */
	$(".frmSiteSearch input[name = query]").keyup(
		function(){
			if (!$(this).attr("value")) {
				$(".frmSiteSearch input[name = query]").attr("value","");				
			} else {
				$(".frmSiteSearch input[name = query]").attr("value",$(this).attr("value"));						
			}
		}
	);
	
	/* Workshops registration */
	$("#submit_button_TC").css("display","none");
	$("#ddlTcState").change(
		function(){
			if ($(this).attr("value") != -1) {
				$("#frmTcSelect").submit();
			}
		}
	);
	
	
	
	/* SCHOOL STORIES FEATURE */
	if ($("#school_stories").length > 0){
		initStorySelection();
	}
	
	/* Bind the hover on the home page map/state map */
	if ($("#map_large").length > 0){
		$("#map_large area").hover(function (){
				$("#australia_map").attr("src","/images/map_australia" + $(this).attr("name") + ".gif");
			},
			function(){		
				$("#australia_map").attr("src","/images/map_australia.gif");
				
		});
		/* Bind the hover on the state's list */
		if ($("#states_list").length > 0){
			$("#states_list a").hover(function (){
				$("#australia_map").attr("src","/images/map_australia" + $(this).html().toLowerCase() + ".gif");
			},
			function(){		
				$("#australia_map").attr("src","/images/map_australia.gif");
				
			});
		}
	}
	
	/* GOOGLE Stats for files downloads */
	$("a").bind("click",function(){
								
		/* Add required files extension */
		var filesTypeArray = new Array ("pdf","doc","mov","wmv","txt","ppt","swf","gif","jpg");

		/* Get the extension of the link */								
		extension = $(this).attr("href").substring( $(this).attr("href").lastIndexOf(".") + 1 , $(this).attr("href").length );

		if( filesTypeArray.join().indexOf(extension.toLowerCase()) >= 0 ) {	
			try {
			var pageTracker = _gat._getTracker("UA-7566215-1");
			pageTracker._trackPageview($(this).attr("href"));
			} catch(err) {}
		}
						
	 });
	
	/* over on large map */

	if ($("#map_large_CC_links").length > 0){
		$("#map_large_CC_links area").hover(function (){
				$("#large_CC_links").attr("src","/images/large_map/map_" + $(this).attr("name") + "_over.gif");
			},
			function(){		
				$("#large_CC_links").attr("src","/images/large_map/map_all_states.gif");
				
		});
	}
	
});


	/* Function to uninit the click on the map */
	var unInitStorySelection = function () {
		
		/* unbind click on map */
		$("#school_stories #school_stories_map area").unbind("click");
		$("#school_stories #school_stories_map area").click(function(){return false;});
		
		/* unbind click on state's list */
		$("#school_stories #states_list a").unbind("click");
		$("#school_stories #states_list a").click(function(){return false;});
		
		/* unbind click on results list */
		$("#school_stories #school_stories_results li a").unbind("click");
		$("#school_stories #school_stories_results li a").click(function(){return false;});
	}

	/* Function to init the click on the map */
	var initStorySelection = function () {
		
		
		/* Bind the click on the map */
		$("#school_stories #school_stories_map area").bind("click",function(){
			
			currentState = $("#school_stories #school_stories_map img").attr("id");
			newState = $(this).attr("name");
																			
			if (currentState != newState) {
	
				/* Uninit clicks */
				unInitStorySelection();
				
				$("#school_stories #school_stories_map area").removeClass("selected");				
				$(this).addClass("selected");
				$("#school_stories #states_list a").removeClass("selected");				
				$("#school_stories .school_stories_story").attr("id","");
				$("#school_stories #school_stories_map img").attr("id",newState);
				$("#school_stories #school_stories_map img").attr("src","/images/map_australia" + newState + ".gif");
				
				$("#school_stories #states_list a").each(function(){
					if ($(this).html().toLowerCase() === newState ) {
						$(this).addClass("selected"); 
					}
																  
				});
				
				/* Get results for the selected state */
				var newContent = $.ajax({
						url: $(this).attr("href") + "&ajax=school_stories_results",
						async: false,
						cache: false
					}).responseText;
				
				newContent = newContent.substring( newContent.indexOf("<datas>") + 7 , newContent.indexOf("</datas>") ) ;
				
				$("#school_stories .school_stories_story").fadeOut(500);
				$("#school_stories #school_stories_results #results_frame").fadeOut(500).hide(function(){
																									   
					$("#school_stories #school_stories_results #results_frame").html(newContent); 
					unInitStorySelection();
					$("#school_stories #school_stories_results").fadeIn(500); 
					$("#school_stories #school_stories_results #results_frame").fadeIn(500).show(function(){initStorySelection();});  
				});
				
			}
			return false;
		});
		
		/* Bind the hover on the map */
		$("#school_stories #school_stories_map area").hover(function (){
				$("#school_stories #school_stories_map img").attr("src","/images/map_australia" + $(this).attr("name") + ".gif");
			},
			function(){		
				$("#school_stories #school_stories_map img").attr("src","/images/map_australia" + $("#school_stories #school_stories_map img").attr("id") + ".gif");
				
		});
		
		/* Bind the click on the state's list */
		$("#school_stories #states_list a").bind("click",function(){
			
			currentState = $("#school_stories #school_stories_map img").attr("id");
			newState = $(this).html().toLowerCase();
																			
			if (currentState != newState) {
	
				/* Uninit clicks */
				unInitStorySelection();
				
				
				$("#school_stories .school_stories_story").attr("id","");
				$("#school_stories #school_stories_map img").attr("id",newState);
				$("#school_stories #states_list a").removeClass("selected"); 
				$(this).addClass("selected"); 
				
				/* Get results for the selected state */
				var newContent = $.ajax({
						url: $(this).attr("href") + "&ajax=school_stories_results",
						async: false,
						cache: false
					}).responseText;
				
				newContent = newContent.substring( newContent.indexOf("<datas>") + 7 , newContent.indexOf("</datas>") ) ;
				
				$("#school_stories .school_stories_story").fadeOut(500);
				$("#school_stories #school_stories_results #results_frame").fadeOut(500).hide(function(){
					$("#school_stories #school_stories_results #results_frame").html(newContent); 
					unInitStorySelection();
					$("#school_stories #school_stories_results").fadeIn(500); 
					$("#school_stories #school_stories_results #results_frame").fadeIn(500).show(function(){initStorySelection();});  
				});
			}
			
			return false;
		});
		
		/* Bind the hover on the state's list */
		$("#school_stories #states_list a").hover(function (){
			$("#school_stories #school_stories_map img").attr("src","/images/map_australia" + $(this).html().toLowerCase() + ".gif");
		},
		function(){		
			$("#school_stories #school_stories_map img").attr("src","/images/map_australia" + $("#school_stories #school_stories_map img").attr("id") + ".gif");
			
		});
		
		/* Bind the click on the results list */
		$("#school_stories #school_stories_results li a").bind("click",function(){
																								
			currentStory = $("#school_stories .school_stories_story").attr("id");
			newStory = $(this).attr("id");
																			
			if (currentStory != newStory) {
	
				/* Uninit clicks */
				unInitStorySelection();
				
				/* Record in GOOGLE Stats */
				newHref = window.location.href.substring(window.location.href.indexOf(".edu.au") + 7, window.location.href.indexOf(".html") + 5) ;
				newHref = newHref + "?state=" + $("#school_stories #school_stories_map img").attr("id") + "&story=" + newStory;
				try {
				var pageTracker = _gat._getTracker("UA-7566215-1");
				pageTracker._trackPageview();
				} catch(err) {}
				
				$("#school_stories .school_stories_story").attr("id",newStory);
				$("#school_stories #school_stories_results li a").removeClass("selected"); 
				$(this).addClass("selected"); 
				
				/* Get results for the selected state */
				var newContent = $.ajax({
						url: $(this).attr("href") + "&ajax=school_stories_story",
						async: false,
						cache: false
					}).responseText;
				
				newContent = newContent.substring( newContent.indexOf("<datas>") + 7 , newContent.indexOf("</datas>") ) ;
				
				$("#school_stories .school_stories_story").fadeOut(1000).hide(function(){
					$("#school_stories .school_stories_story").html(newContent); 
					unInitStorySelection();
																					
					$("#school_stories .school_stories_story").fadeIn(1000).show(function(){initStorySelection();});  
				});
				return false;
			}
		});
	}
