jQuery.noConflict();
jQuery(document).ready(function(){

	// Script variables.
	var nav_container = "#sub_navs_holder";
	var itemtomove = "#pointer";
	var movingitemtexttype = "h6"
	var offset = 10;				
		// Offset is the space between the top of the link and the 
		// top of the list item it is in. (in pixels)
	
	var content_container = jQuery("#content");
	var loading_screen = jQuery('#loading');
	
	
	// Don't touch these variable.
	var hidden = false;
	var pointer = jQuery(itemtomove);
	
	
	// This section is used when the page is initially loaded
	// to decide where to position the pointer item.
			jQuery('#pointer').show();
			listitems = jQuery(nav_container+" a");
			
			if(chosen_nav && chosen_nav!='undefined'){
				start_int = chosen_nav;
			}else{
				//start_int = 0;
				start_int = 0;
			}
						
			
			// Choose the pointer text.
			start_text = jQuery(listitems[start_int]).text();
			jQuery(itemtomove+' '+movingitemtexttype).text(start_text);
			
			// Find the starting pointer position
			// and move it there.
			var start_position = jQuery(listitems[start_int]).position();
			var parent = jQuery(nav_container).position();	
			start_move = start_position.top - parent.top - offset;
			pointer.css({marginTop:start_move});
			


	// This function is fired whenever a sub-navigation item
	// is clicked.
	jQuery("a.sub_nav").click(function(event){
		event.preventDefault();
	
		// Figure out where to move the moving item, and move it.
		var item_position = jQuery(this).position();
		var move_to = item_position.top-parent.top-offset;
		
		if(hidden==true){
			pointer.show(450, function(){
				pointer.animate({marginTop:move_to});
			});
			
			hidden=false;
		}else{
			pointer.animate({marginTop:move_to});
		}
		
		// Switch the text of the moving item.
		var text = jQuery(this).text();
		jQuery(itemtomove+" "+movingitemtexttype).text(text);

		link = jQuery(this).attr('href')+'/ajax';

		// Fade out content, replace it, and fade it back in.	
		content_container.fadeTo('fast', 0, function(){
			
			
			// Ajax specifications
			jQuery.ajax({
				url: link,
				
				// Show the loading screen if Ajax takes a while.
				beforeSend: function(){
					loading_screen.show();
				},
				
				// Fill content area with error message.
				error: function(){
					content_container.text("Sorry, but there was an error.");
				},
				
				// Fill content area with new content.
				success: function(data){
					content_container.html(data);
				},
				
				// Hide loading screen and show new content, whatever it is.
				complete: function(){
					//Cufon.refresh();
					loading_screen.hide();
					
					if(window.add_move_handels){
						add_move_handels();
					}
					
					if(window.start_editor_buttons){
						start_editor_buttons();
					}

					content_container.fadeTo('fast', 1);
				} 
			});
		});
	});
});
