/*
UNCG SAS Website - 2009, Thomas Hopkins
*/

function getPage(pid)
{
	if (pid == "pictures") //Is it the picture gallery page?
		return $.get(make_ajax_url("gallery_images/galleries.txt"), function(data) { doGallery(data); });
	//Otherwise, the following line executes and we grab data 'normally.'
  $.get(make_ajax_url(pid+".txt"), function(data){ $("#containerText").html(data); }); 
}

function doGallery(galleryData) 
{
	 	/* This function loops through the gallery information file to extract and compile relevant information.
		Each step in the process is explained below in subsequent comments*/
		var par = new Array(); //Set up an array in which to tokenize the data
		var gallery_base_name = ""; //The directory name and thumbnail file name
		var gallery_desc = ""; //The description to appear below the gallery
		var file_list = ""; //The list of files provided in the information file
		
		var output = ""; //For all the output
		var nameexp = new RegExp("galleryname="); //Test for the beginning of the first line... These are fairly self-explanatory
		var descexp = new RegExp("description=");
		var fileexp = new RegExp("files=");
		var endexp = new RegExp("-endgallery-"); //Each gallery's information section *must* end with -endgallery-
		par = galleryData.split("\n"); //Split up the file
		for (i = 0; i < par.length; i++) //Let's loop through the lines one by one
		{
			if ((nameexp.test(par[i])) == true) //Is it the name of the gallery, i.e. does it contain galleryname= ?
			{
				gallery_base_name = par[i].split("=")[1]; //All characters after the "=" on that line form the description
				
			} else if (descexp.test(par[i]) == true) //Is it the description?
			{
				gallery_desc = par[i].split("=")[1];
				
			}  else if (fileexp.test(par[i]) == true)
			{
				var files = new Array(); //A temporary array to store the files
				files = (par[i].split("=")[1]).split(";"); //First split the line by an equal sign to get the list of files. Split it again by a semicolon to get each filename.
				
				for (n = 0; n < files.length; n++) //Loop through the files, now in separate array elements
				{
					//Here, we'll build a string to output on the gallery page--all this needs to contain is a series of <a> tags with the appropriate information
					file_list = file_list+"<a href=\""+make_ajax_url("gallery_images/"+gallery_base_name+"/"+files[n])+"\" rel=\""+gallery_base_name+"\"></a>"; 
				}							
			} else if(endexp.test(par[i]) == true) //Have we reached the end?
			{
				//Add the thumbnail image in the form <gallery name>_thumb.png, append the file list, and make sure the thickbox init function gets the data it needs. We call make_ajax_url here to ensure it uses the proper URL. Thickbox tends to behave strangely if we do not do this.
				output = output+"<div class='galleryWrapper'><img src='gallery_images/"+gallery_base_name+"_thumb.png' onclick=\"tb_show('"+gallery_desc+"', '"+make_ajax_url("gallery_images/"+gallery_base_name+"/"+files[0])+"', '"+gallery_base_name+"')\" /><div>"+gallery_desc+"\n"+file_list+"</div></div>"; 
				//Empty the variables in case the parent for loop needs to execute again, i.e. there is another gallery waiting to be processed.
				gallery_base_name = "";
				gallery_desc = "";
				file_list = "";
			} else if (par[i] == "\n") //This might not be necessary, but I wanted to be sure it ignored any superfluous newlines
			{
				continue;
			}
						
		}
		//window.alert(output);
		$("#containerText").html(output); //Finally, add the composite output to the proper section.
		return true;	
	
}

function make_ajax_url(path) //This is mainly for testing--it should make filesystem paths work with ajax--firefox, for example, disallows remotely accessing a local filesystem via javascript, so the URLs cannot be absolute
{
	if (window.location.protocol == "file:")
		return path;
	else
		return window.location.protocol+'//'+window.location.hostname+window.location.pathname+path;
}

$(document).ready(function() {
	(window.location.hash == "#" || window.location.hash == "") ? getPage('home') : getPage((window.location.hash).substring(1)); //Load a page based on the 'hashtag' in the URL
	$("#calendarmini").click(function() {
		//this.title = "test!";
		tb_show("Our Calendar...", "http://www.google.com/calendar/feeds/ak3m4d4mjno9rhpejsk14e1cng%40group.calendar.google.com/public/basic");
	 });
	
	$(".menulink").click(function() { //Process Menu links
		$('#containerText').fadeOut('fast', function(){getPage((window.location.hash).substring(1));});
		$('#containerText').fadeIn(1000);							  
	});
	
//This exists to fix a strange bug appearing in IE7 and below. If the html and body overflow are set to auto, the navigation menu tends to disappear on pages where the entire page scrolls.
 if($.support.cssFloat == true)  //As of this point in time, jQuery returns false for IE6/7
 {
    $("html").css({"overflow" : "auto"});
	$("body").css({"overflow" : "auto"});
 }

});
