//array for using the back button.
var queue = new Array;

//function for adding highlights and hovers to tables inside of the container.
function tablehandler() {
	//handle table highlights and hovers
  $('.filetable tr:not(:first)').hover(	//hover function for rows in the table ignoring the first row
    function() { $(this).addClass('hover');}, //add hover
    function() { $(this).removeClass('hover');} //remove hover
  );	
  $('.filetable tr:odd').addClass('tabbgdark');	//odd rows tabbgdark
  $('.filetable tr:even').addClass('tabbglight'); //even rows tabbglight
}

function hideshowtoggle() {
	//change + to - or - to + and hide or show the menu
		if ($(this).find('.expandhide').html() == '+')
			$(this).find('.expandhide').html('-');
		else
			$(this).find('.expandhide').html('+');
		$(this).next('.filetable, .container_files').toggle();
}

//adds click event, even/odd table coloring and hover to table rows when clicking on links
function colortables(e) {
	//add navsystem function to a class .navlist click
	$('.navlist').click(navsystem);	//add click event function
	
	//add hover to classes
	$('.navlist, a div, .hideshow, .browsetoclick').hover(
			function() { $(this).addClass('hover');}, //on over event add hover
			function() { $(this).removeClass('hover');}  //on leave event remove hover      
	);
	
	//handle table highlights and hovers
	tablehandler();
		
	//handle clicking on a .hideshow div.
	$('.hideshow').click(hideshowtoggle);
	
	//if class browsetolink is clicked go to it in the navpanel and load that page.
	$('.browsetoclick').click(function() {
		browseto($(this).attr('path'));
	});
}

//function for handling the navigation system.
function navsystem(e) {
	var item = $(this);
	
	//Handle data loading stuff from the click event
	if ($(this).hasClass('container_nav')) { //handle if click came from container_nav
    $('#navpanel').find('div').each(function (){ //look for nav item with same path
      if (item.attr('path') == $(this).attr('path')) { //item path = nav path
        $(this).click();	//simulate a click on the nav panel
      }
		});
  }	else {	//click was in navpanel
	  $('.hover1').removeClass('hover1'); //remove hover1
	  item.addClass('hover1'); //add hover1 to current item
	  
	  if (item.attr('has_link')) { //item has a custom container page, load it.
	    $.get(item.attr('has_link'), {'path':item.attr('path'), 'home':$('#main').attr('home'), 'functions':$('#main').attr('functions')},function(data) {
		    $('#container').html(data); //load data into #container
        colortables(e);
	    });
	  }
	  else {	//use the default container.php page for data
	    $.get('./phpcode/container.php', {'path':item.attr('path'), 'showfolders':1, 'home':$('#main').attr('home'), 'functions':$('#main').attr('functions')},function(data) {
		    $('#container').html(data); //load data into #container
        colortables(e);
	    });
	  }
  }

	//handle the navpanel after clicking
	if (!$(this).next('navsub').hasClass('viewable')) { //doesn't contain .viewable already
		//hide and remove .viewable from any siblings
		$(this).siblings('.viewable').find('.viewable').hide().removeClass('viewable').end().hide().removeClass('viewable');
	} else {
		//check if any sub items contain .viewable then hide and remove them.
		if ($(this).next('navsub').hasClass('.viewable')) {
		  if ($(this).next('navsub').find('.viewable').size() > 0) {
			  $(this).next('navsub').find('.viewable').hide().removeClass('viewable');
			}
	  }
	}
	//add viewable to item and make it visible
	if ($(this).next().hasClass('.navsub'))
		$(this).next('.navsub').show().addClass('viewable');
	
	//add link to the queue.
	queue.push(item.attr('path'));
}

//function used for browsing to a specific link in the navpanel and triggering a click.
//a path is taken and it is case sensitive. ex: Support/How To/RSS
function browseto(path) {
	$('.viewable').removeClass('viewable').hide();
	
	//determine if the path provided in loc is in the nav panel paths.
	if ($('#navpanel').find("div[path='"+$('#navpanel').attr('path')+'/'+path+"']").length == 1) {
		var location = path.split('/');
		var item = $('#navpanel');
		for (x in location) {
			if (location.length -1 != x) 
				item = item.find(".navlist[innerHTML='"+location[x]+"']").next().show().addClass('viewable');
			else 
				item.find(".navlist[innerHTML='"+location[x]+"']").click();
		}
	}
}

function resetpage() {
			colortables(1);
			
			if ($('#navpanel').find("div[path='"+$('#navpanel').attr('path')+'/'+$('#navpanel').attr('loc')+"']").length != 1) {
				$.get($('#main').attr('files')+'/_'+$('#main').attr('files').slice(2)+'.php', {'path':$('#main').attr('files'), 'home':$('#main').attr('home'), 'functions':$('#main').attr('functions')},function(data) {
					$('#container').html(data); //load data into #container
					colortables(1);	//apply any formatting or events to the new data
				});   
			}  
			
			//hide anything that is already expanded or selected in nav panel
			$('.viewable').removeClass('viewable').hide();
			$('.hover1').removeClass('hover1');
			
			//if $_GET location passed then browse to it
			if ($('#navpanel').attr('loc').length > 0)
				browseto($('#navpanel').attr('loc'));
}

function goback() {
	if (queue.length >= 2) {	//make sure queue is 2 or more elements.
		queue.pop(); //pop last item off array
		var newpath = queue.pop().split('/'); //split string to array from / char and return
		newpath.splice(0, 2); //drop first 2 array elements . and websupport
		browseto(newpath.join('/'));	//browse to string of newpath elements joined with /
	} else
		alert('You can\'t go back any farther.'); //already at beginning
}

//    ==== Beginning of JQuery code ====

    $(function() {
			
			//initialize things
			resetpage();
			
			//reset to first load.  home link.
			$('#reset').click(resetpage);
			
			$('#goback').click(goback);
			
			//add hover when mouse goes over a link 
			$('a span, .navoptions, #reset').hover(
				function() { $(this).addClass('hover');}, //on over event add hover
				function() { $(this).removeClass('hover');}  //on leave event remove hover      
			);
				
			$('.goback').mousedown(function() { $(this).addClass('gobackpush');});
			$('.goback').mouseup(function() { $(this).removeClass('gobackpush');});
			$('.goback').mouseout(function() { $(this).removeClass('gobackpush');});
			$('.goback').hover(
				function() { $(this).addClass('gobackhover');}, //on over event add hover
				function() { $(this).removeClass('gobackhover');}  //on leave event remove hover      
			);				
		});

//    ==== End of JQuery code ====

