/*
	Each instance of this class, represents a single binding to a menu,
	and handles page requests via ajax. 
*/
var cContentLoader = function(v_name, o_menu, o_content, v_autoinit){

	this.name 				= v_name;
	this.menu 				= $(o_menu);
	this.submenu			= null;
	this.navtrail			= null;
	this.menuitems			= o_menu.find('li');
	this.menulinks			= this.menuitems.find('a');
	this.content		 	= $(o_content);
	this.preloader			= $('<div/>')
		.addClass('content content-loader').html('Loading, please wait..');
	
	this.title				= '';	
	this.active_link		= '';	
	this.active_url			= THEFOOT.getPageFromURL(document.location.href);	
		if (this.active_url.length == 0){this.active_url = 'index.php';}
		
	/*
		Initialise the menu - bind it to this class
	*/
	this.initMenu = function(){

		var $thisroot = this;
		
		// Hide the content section of the loaded URL
		if (!gv_showContent && (THEFOOT.getPageFromURL(document.location.href).length > 0)){
			o_content.empty();
		}
		
		
		// Highlight the current url
		this.updateMenu();
		
		// Activate links
		$thisroot.menulinks.click(function(e){
			e.preventDefault();
		
			// Position and show the loading div
			$thisroot.content.html('').append($thisroot.preloader);
			
			// Record current URL
			$thisroot.active_link = $(this);
			$thisroot.active_url = $(this).attr('href');
		
			// Load the content via history plugin
			$.history.load($thisroot.active_url.replace(/^.*#/, '').replace('.php', ''));
		
			return false;
		});
	} // this.initmenu()
	
	/*
		Retrieve a remote page and display it in the content section
	*/
	this.loadUrl = function(v_url){
	
		// Record current URL
		this.active_link = this.menulinks.filter('.current').eq(0);
		this.active_url = v_url;
		
		$.ajax({
			url		: v_url,
			data	: {'co': 1},
			dataType: 'html',
			success	: function(v_content){
				this.loadContent(v_content);
			}.bind(this),
			error	: function(){
				THEFOOT.showError("There was a problem retrieving the page content.  Click OK to load the requested page manually.");
				window.location.href = v_url;
			}
		});
		return this;
	} // this.loadUrl()
	
	/*
		Load and display markup in the content section
	*/
	this.loadContent = function(v_markup){

		var $thisroot = this;
		
		// Inject the new page content
		var o_dom = $('<div/>').append(v_markup);
		var v_new_c = o_dom.find('div#content').html();
		
		// Hide old content and replace with new
		if (this.navtrail){this.navtrail.remove();this.navtrail = null;}
		if (this.submenu){this.submenu.remove();this.submenu = null;}
		this.content.hide().html(v_new_c);
		
		// Inject breadcrumb trail if any
		var o_trail = o_dom.find('div#breadcrumbtrail');
		if (o_trail.length > 0){
			this.navtrail = o_trail.eq(0);
			this.content.before(this.navtrail.get(0));
		}
		
		// Inject submenu if any
		var o_submenu = o_dom.find('div#submenutabs');
		if (o_submenu.length > 0){
			this.submenu = o_submenu.eq(0);
			this.content.before(this.submenu.get(0));
		}
		
		// Fade in the content
		this.preloader.fadeOut(400, function(){

			// Look for forms to customise
			$thisroot.content.find('form[name="contactform"]').each(function(){							
				THEFOOT.convertContactForm(this);							
			});
			
			// Look for image galleries to convert
			$thisroot.content.find('div.photogallery').each(function(){							
				THEFOOT.convertPhotoGallery(this);							
			});
			
			// Look for flash to load
			$thisroot.content.find('div.flashembed').each(function(){							
				THEFOOT.convertFlash(this);							
			});
			
			// Fade in the new content
			$thisroot.content.fadeIn(200, function(){
				//THEFOOT.fixLayout();
				
				// Look for accordions to convert
				$thisroot.content.find('div.accordion').each(function(){							
					THEFOOT.convertAccordion(this, true);							
				});
				
				$thisroot.updateLinks().updateMenu().updateNavTrail().updateSubMenu();
			});
			
		});
		
		// Highlight the current item
		this.updateSubMenu().updateNavTrail().updateMenu().updateLinks();
		
		// Hide tooltips
		$('div.qtip').hide();
		
		// Page title
		if (
			this.active_link 
			&& this.active_link.data('title')
			&& (this.active_link.data('title').length > 0)){
			this.title = this.active_link.data('title');
			document.title = this.title + " .:. " + THEFOOT.SITE_NAME;
		}
		
		return this;
	} // this.loadContent()
	
	/*
		Update all links in the content section;
			External links will be popups
			Internal links will be fetched via ajax
	*/
	this.updateLinks = function(){
	
		var $thisroot = this;
		
		// Add external and internal link handling
		if (this.content && this.content.is(':visible')){
		
			// External links (popup)
			this.content.find('a:urlExternal').attr('target', '_blank');
			
			// Internal links (ajax)
			this.content.find('a:urlInternal').each(function(){
		
				var o_thislink = $(this);
				var v_thisurl = o_thislink.attr('href');
				o_thislink.unbind('click').click(function(e){
					e.preventDefault();
				
					// Position and show the loading div
					$thisroot.content.html('').append($thisroot.preloader);
					
					// Record current URL
					$thisroot.active_link = $(this);
					$thisroot.active_url = $(this).attr('href');
				
					// Load the content via history plugin
					$.history.load($thisroot.active_url.replace(/^.*#/, '').replace('.php', ''));
				
					return false;
				});
				
			});
		}
		
		// Convert all content tooltips
		THEFOOT.convertAllTooltips(this.content);
		if (this.navtrail){THEFOOT.convertAllTooltips(this.navtrail);}
		if (this.submenu){THEFOOT.convertAllTooltips(this.submenu);}
		
		return this;
	} // this.updateLinks()
	
	/*
		Highlight the currently selected menu item
	*/
	this.updateMenu = function(){
	
		var $thisroot = this;
		
		// Update the current URL path in the main menu
		this.menulinks.each(function(){
			$(this).removeClass('current');
			$(this).children().removeClass('current');
			if ($(this).attr('href') == $thisroot.active_url.replace('../', '')){
				$(this).addClass('current').parent().addClass('current');
				$(this).closest('li[rel="root"]').addClass('current').children('a').eq(0).addClass('current');

			}
		});
	
		return this;
	} // this.updateMenu ()
	
	/*
		Convert navtrail links to pageloader links
	*/
	this.updateNavTrail = function(){
	
		var $thisroot = this;
		
		// Check if we have a navtrail
		if (this.navtrail){
		
			// External links (popup)
			this.navtrail.find('a:urlExternal').attr('target', '_blank');
			
			// Internal links (ajax)
			this.navtrail.find('a:urlInternal').each(function(){

				var o_thislink = $(this);
				var v_thisurl = o_thislink.attr('href');
				o_thislink.unbind('click').click(function(e){
					e.preventDefault();
				
					// Position and show the loading div
					$thisroot.content.html('').append($thisroot.preloader);
					
					// Record current URL
					$thisroot.active_link = $(this);
					$thisroot.active_url = $(this).attr('href');
				
					// Load the content via history plugin
					$.history.load($thisroot.active_url.replace(/^.*#/, '').replace('.php', ''));
				
					return false;
				});
				
			});
		
		}
	
		return this;
	} // this.updateNavTrail ()
	
	/*
		Convert submenu links to pageloader links
	*/
	this.updateSubMenu = function(){
	
		var $thisroot = this;
		
		// Check if we have a submenu
		if (this.submenu){
		
			// External links (popup)
			this.submenu.find('a:urlExternal').attr('target', '_blank');
			
			// Internal links (ajax)
			this.submenu.find('a:urlInternal').each(function(){

				var o_thislink = $(this);
				var v_thisurl = o_thislink.attr('href');
				o_thislink.unbind('click').click(function(e){
					e.preventDefault();
				
					if (!$(this).hasClass('current')){
					
						// Position and show the loading div
						$thisroot.content.html('').append($thisroot.preloader);
						
						// Record current URL
						$thisroot.active_link = $(this);
						$thisroot.active_url = $(this).attr('href');
					
						// Load the content via history plugin
						$.history.load($thisroot.active_url.replace(/^.*#/, '').replace('.php', ''));
					}
					return false;
				});
				
			});
		
			// Update the current URL path
			this.submenu.find('a:urlInternal').each(function(){
				$(this).removeClass('current').children().removeClass('current');
				if (THEFOOT.getPageFromURL($(this).attr('href')) == THEFOOT.getPageFromURL($thisroot.active_url)){
					$(this).addClass('current').parent().addClass('current');
				}
			});
		
		}
	
		return this;
	} // this.updateSubMenu ()
	
	/*
		Set the active URL
	*/
	this.setUrl = function(v_url){
		this.active_url = v_url;
		return this;
	} // this.setUrl()
	
	/*
		Callback function for the jQuery.history plugin
	*/
	this.handlerHistory = function(v_hash){	
		if (v_hash.length == 0){return this;}
		this.loadUrl(v_hash + '.php');
		this.updateMenu();
		return this;
	} // this.handlerHistory()
	
	// Constructor code
	if (v_autoinit){this.initMenu();}
	
	return this;	
} // cContentLoader()
