/*
Copyright 2009 Milan Raj
*/

var lastLink="";
$.ajaxSetup ({
		cache: true
	});

/*
Assumptions:
Files listed in the bar do not link to anything outside the root of the directory
ie. a valid link is "meetings.html" <-- links to file in same directory
ie. an invalid link is "../hi.html" <-- links to file in parent directory
ie. an invlaid link is "candy/thebest/mandm.html" <-- links to file in subdirectory

FIles listed in the link cannot have extra paramters attached and must end in .html
ie. valid link is "iamgood.html" <-- no parameters ends in .html
ie. invalid link is "iambad.php" <-- does not end in .html
ie. invalid link is "iamverybad.php?works=fails" <-- has extra parameters and does not end in .html
ie. invalid link is "iamveryverybad.php?dir=home/iwill/fail.html" <-- dear god, i do not know what will happen

This function works by first taking the link given of the hover and finding the directory leadding up to it
Then the function load the element of class scroller from the remote page into the local page
Then the links loaded into the page are edited to point to the right directory
the whole count system is to control how many times a link is edited.. essentially an IE Hack
*/
function replaceWithXML(link)
{
	var directory = link;

	//handle case where points to either just directory or to index.html
	if(link.indexOf("index.html")!=-1)
		directory = link.substring(0,link.indexOf("index.html"));
	$("td.scroller").fadeTo("fast",0);
		//to fix:
		//the get function only executes the callback only if textStatus = success
		//so if the get fails, the text never comes back
		$.get(link, function(responseText, textStatus)
		{
				//insert the broken links into the document, jquery will not change the 
				//variables inside of responseText
				$("#scroller").html($("#scroller", responseText).html())
				
				//fix the links because of stupid ie
				//$("td.scroller div", responseText).children("a").each(function()
				$("td.scroller div").children("a").each(function()
				{
						var toReplace = $(this).attr("href");
						//remove any subdirectories if made into full path (happens in IE)
						if(toReplace.lastIndexOf("/")!=-1)
							toReplace = toReplace.substring(toReplace.lastIndexOf("/")+1);
						//toReplace is now the name of the file being pointed to
						//directory is the full path to navigate to the directory	
						toReplace = directory + toReplace;
						$(this).attr("href", toReplace);
				});
				$("td.scroller").stop(true,true).fadeTo("fast",1);
		});
}

$(document).ready(function()
{

	//make table box clickable
	$("td.nav").click(function(){
		document.location = $(this).children("a").attr("href");
	});
	
	//navbar loader thing
	$("td.nav").hoverIntent({
		sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)    
		interval: 100, // number = milliseconds for onMouseOver polling interval    
		over: function()
		{
			var link = $(this).children("a").attr("href");
			if(link!=lastLink)
			{
				replaceWithXML(link);
				lastLink=link;
			}

		}, // function = onMouseOver callback (REQUIRED)    
		timeout: 500, // number = milliseconds delay before onMouseOut    
		out: function()
		{
		//empty function
		} // function = onMouseOut callback (REQUIRED)    
	});
	
	//ie hover fix
	$('td.nav').hover(function() {
		$(this).addClass('td-nav-ie-hover');
		$(this).children("a").removeClass("nav");
		$(this).children("a").addClass("a-nav-ie-hover");
	}, function() {
		$(this).removeClass("td-nav-ie-hover");
		$(this).children("a").addClass("nav");
		$(this).children("a").removeClass("a-nav-ie-hover");
	});
	
		//gallery fade effect
	$('p.gallery').children().hover(function() {
		$(this).siblings().stop().fadeTo(500,0.5);
	}, function() {
		$(this).siblings().stop().fadeTo(500,1);
	});
	
	
	//valid css and xhtml button effect

	$("td.bottombar").children("p").children("a").children("img").each(function(index,obj) {
		//save the original image url
		$(obj).parent().attr("rel", $(obj).attr("src"));
		//make the hover element
		$(obj).hover(function(){
			var imgLink= $(this).attr("src").substring(0, $(this).attr("src").indexOf("-gray"))+ ".gif";
			$(this).attr("src", imgLink);
		},function(){
			$(this).attr( "src", $(this).parent().attr("rel") );
		});
	});
});
