var enableAjax = true;

var hideSite = false;
var flashBG = true;

//function createCookie(name,value,days) {
//	if (days) {
//		var date = new Date();
//		date.setTime(date.getTime()+(days*24*60*60*1000));
//		var expires = "; expires="+date.toGMTString();
//	}
//	else var expires = "";
//	document.cookie = name+"="+value+expires+"; path=/";
//}
//
//function readCookie(name) {
//	var nameEQ = name + "=";
//	var ca = document.cookie.split(';');
//	for(var i=0;i < ca.length;i++) {
//		var c = ca[i];
//		while (c.charAt(0)==' ') c = c.substring(1,c.length);
//		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
//	}
//	return null;
//}
//
//function eraseCookie(name) {
//	createCookie(name,"",-1);
//}
//
//
//function toggleBackground() {						
//	if (hideSite) {
//		$('#toggleVideoButton').show();
//		$('#toggleBGButton').text('FULLSCREEN');
//		$('#small_logo').fadeOut('fast');
//		$('#main_content').fadeIn('fast');
//		$('#title_bar').fadeIn('fast');
//		$('#logo').fadeIn('fast');
//		
//		if ($('#searchBarForm').attr('class') == "show") {
//			$('#searchBarForm').fadeIn('fast');
//		}
//		$('#menu').fadeIn('fast');
//		
//		hideSite = false;		
//	}else {
//		$('#toggleVideoButton').hide();
//		$('#main_content').hide();
//		$('#title_bar').hide();
//		$('#logo').hide();				
//		$('#menu').hide();
//		$('#small_logo').fadeIn('fast');
//		$('#searchBarForm').hide();				
//		$('#toggleBGButton').text('SHOW SITE');
//		
//		hideSite = true;		
//	}			
//}
//
//function toggleVideo() {
//	if (!flashBG) {
//		$('#toggleBGButton').show();
//		$('#flash_bg').fadeIn("fast");
//		$('#toggleVideoButton').text('STOP');
//		flashBG = true;
//		createCookie('flashBG',true,7);
//	}else {
//		$('#toggleBGButton').hide();
//		$('#flash_bg').hide();
//		$('#toggleVideoButton').text('PLAY');
//		$('#small_logo').hide();
//		flashBG = false;
//		createCookie('flashBG',false,7);
//	}					
//}

// When the document loads do everything inside here ...   

function displayMessage(message, duration) {
	if (duration == null) {duration = 1500;}
	$.blockUI({ message: "<h2>" + message + "</h2>" });     
	setTimeout(jQuery.unblockUI, 1500);
}

function clearMenuTabs(){
	$('a#menu_active').attr('id', '');
	$('.menu_tab').css({ backgroundImage:"none"});
	$('.menu_ltab').css({ backgroundImage:"none"});
	$('.menu_rtab').css({ backgroundImage:"none"});
}
function focusMenuTab(menu, cur_section) {	
	clearMenuTabs();
	$('#' + menu + '_tab').css({ backgroundImage:"url(images/framework/menu_tab_tile.gif)"});
	$('#' + menu + '_ltab').css({ backgroundImage:"url(images/framework/menu_tab_left.png)"});
	$('#' + menu + '_rtab').css({ backgroundImage:"url(images/framework/menu_tab_right.png)"});
	$('#'+menu+'_tab a').attr('id', 'menu_active');
}


/*
function unfocusMenuTab(menu, cur_section) {
	if (menu != cur_section) {
		$('#' + menu).css({ backgroundImage:"none"});
		$('#' + menu + '_ltab').css({ backgroundImage:"none"});
		$('#' + menu + '_rtab').css({ backgroundImage:"none"});
	}else {
		clearMenus();
		focusMenuTab(menu, cur_section);
	}
}
*/


//**********************
//initialize urchin stats for ajax calls
function initUrchin(parameters) {
	var thisView = parameters['thisView'];
	/*try {
	var pageTracker = _gat._getTracker("UA-822084-1");
	pageTracker._setDomainName("www.mandodiao.com");
	pageTracker._trackPageview(thisView);
	} catch(err) {}*/
	
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-822084-1']);
	if (parameters['thisView'] != null && parameters['thisView'] != "")
		_gaq.push(['_trackPageview'], parameters['thisView']);
	else
		_gaq.push(['_trackPageview']);
  
	(function() {
	  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();

}

function initUrchin_de(parameters) {	
	// Tyskland
	/*var thisView = parameters['thisView'];
	try {
	var pageTracker = _gat._getTracker("UA-6909223-2");
	pageTracker._setDomainName("www.mandodiao.com");
	pageTracker._trackPageview(thisView);
	} catch(err) {}
	*/
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-6909223-2']);
	if (parameters['thisView'] != null && parameters['thisView'] != "")
		_gaq.push(['_trackPageview'], parameters['thisView']);
	else
		_gaq.push(['_trackPageview']);
  
	(function() {
	  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
}

var current_query = "";
function ajaxLoad(query, el_id, show_loading, force_reload, rel_path)
{		
	if (rel_path == null || rel_path == "undefined") rel_path = "";

	if ($("#" + el_id + "_content").length <= 0 ) {			
		//alert("#" + el_id + "_content doesn't exist");
		//return false;	
	}
	
	//if (force_reload || query != current_query) {

		// query - ex. section=news
		// el_id - Element ID, ex: main_content
		// show_loading - true/false
		// load_method - Ex. content, titlebar
	
		if (el_id == undefined || el_id == "") {		
			el_id = "main";
		}
	
		if (show_loading == null) {
			show_loading = true;
		}			
		
		// Remove this manually
		if (el_id == "main") {
			// hide all id:s that ends with "_content"
			$("div[ id$=_content]").css("visibility", "hidden");
		}	
		
		if (el_id == "media_item") {
			$('#image_content img').css("visibility", "hidden");
		}

		if (query == "") query = "section=home";	
		

		if (query != "") {																	
			if (show_loading) 
			{				
				//window.open(rel_path +'_ajax.load_page.php?' + query,'_blank');
				
				if (!$.browser.safari) {
					rel_path = "";
				}				
				
				$.ajax({
					method: "get",url: rel_path + "_ajax.load_page.php",data: query,
					beforeSend: function(){$("#" + el_id + "_content").css("visibility", "hidden");$("#" + el_id + "_loading").show();}, //show loading just when link is clicked
					complete: function(){
						$("#" + el_id + "_loading").hide();
						$("#" + el_id + "_content").css("visibility", "visible");
						if (hideSite) {
							$("#" + el_id + "_content").hide();	
						}
					}, //stop showing loading when the process is complete					
					success: function(html){ //so, if data is retrieved, store it in html
						//$(".content").show("slow"); //animation
						$("#" + el_id + "_content").html(html);
					 },
					 error: function(){
						 	$("#" + el_id + "_content").html('Unexpected Error');
					 }
				}); //close $.ajax(			
			}
			else 
			{				
				$.ajax({
					method: "get",url: "_ajax.load_page.php",data: query,					
					success: function(html){ //so, if data is retrieved, store it in html
						//$(".content").show("slow"); //animation
						$("#" + el_id + "_content").html(html); //show the html inside .content div																												
					 }
				}); //close $.ajax(	
			}
	
		}else {			
			//$('#' + el_id + "_content").load('_ajax.load_page.php');	
		}	
		
		
		current_query = query;
	//}
	
	return false;
}	

function ajaxLoadModule(query, el_id, module_path)
{			
	// query - ex. section=news
	// el_id - Element ID, ex: mod_headlines

	// NOTE: The loading-id should be el_id + "_loading"
	
	if (query != "" && el_id != undefined && el_id != "") {

		$.ajax({			
			method: "get",url: "_ajax.load_module.php",data: query + "&module=" + module_path,
			beforeSend: function(){$("#" + el_id + "_content").css("visibility", "hidden");$("#" + el_id + "_loading").show();}, //show loading just when link is clicked
			complete: function(){ $("#" + el_id + "_loading").hide();$("#" + el_id + "_content").css("visibility", "visible");}, //stop showing loading when the process is complete					
			success: function(html){ //so, if data is retrieved, store it in html
				//$(".content").show("slow"); //animation								
				$("#" + el_id).html(html); //show the html inside .content div																												
			 }
		}); //close $.ajax(
		
	}
	
	
	return false;
}		

function ajaxUpdateQuery(query)
{

	if (query != "") {
	
		$.ajax({
			method: "get",url: "_ajax.load_section.php",data: query,
			//beforeSend: function(){$("#main_content").hide();$("#loading").show();}, //show loading just when link is clicked
			//complete: function(){ $("#loading").hide();$("#main_content").show();}, //stop showing loading when the process is complete
			success: function(html){ //so, if data is retrieved, store it in html
				//$(".content").show("slow"); //animation
				$("#main_content").html(html); //show the html inside .content div																												
			 }
		}); //close $.ajax(							

	}
}


function updatePreloadImages() 
{		
	$("img").load(function (){
		$(this).show();
		if ($(this).attr('class') == "preload" || $(this).attr('class') == "image_link preload") {
			var w = $(this).width();
			var h = $(this).height();
			var max_w = $(this).parent().width();
			var max_h = $(this).parent().height();
					
			if (w > max_w) {
				// Adjust width/height after $avatar_width 
				
				if ($(this).attr('rel') != "width") {
					if (w < h) {
						var ratio = max_w/w;
					}else {
						var ratio = max_h/h;
					}
				}else {			
					var ratio = max_w/w;
				}
				
				$(this).width(w*ratio);
				$(this).height(h*ratio);
				
			}
			//$(this).css("visibility","visible");		
			$(this).show();
		}
	});	
}

function convertOldLinks(section, page, item_id, category) {					
	//alert(section);
	var  query = "";
	//window.console.log(query);
	category = category + "";
	
	switch(section) {
		case '0':
			query = 'section=home';
			break;
		case '1':
			query = 'section=news';
			break;
		case '2':	// TV
			query = 'section=media';
			//query += "&page=video"
			break;
		case '4':
			query = 'section=games';
			break;
		case '5':
			query = 'section=media';
			query += "&page=gallery";				
			break;
		case '6':
			query = 'section=tour';
			break;
		case '7':
			query = 'section=discography';
			break;
		case '8':
			query = 'section=press';
			break;
		case 'citizens':
			query = 'section=citizens';
			if (page == "experiences") {
				if (category == "Stories" || category == "stories") query += "&page=articles&tag=stories"; 
				if (category == "Reviews" || category == "reviews") query += "&page=articles&tag=reviews"; 
			}else {
				if (page == "citizenofmonth") query += "&page=articles&tag=cotm"; 
				if (page == "poems") query += "&page=articles&tag=poems"; 
				if (page == "specialunits") query += "&page=articles&tag=units"; 
			}
			if (item_id != false) query += "&id=" + item_id;
			
			break;
	}

	query += "&top=1";

	return query;
}

function updateAjaxLinks() {
	
	if (enableAjax) {	
		
		 // IE6 png fix
		if ($.browser.msie && $.browser.version < 7) {
			/*if (sDivId != undefined && sDivId != '') {
				supersleight.limitTo(sDivId);
			}
			supersleight.run();
			*/
		}
		
		// Bold H1s for Windows
		if (BrowserDetect.OS == "Windows") {
			$('h1').css('font-weight','bold');
			$('h1 a').css('font-weight','bold');
			//$('#sub_menu a').css('font-weight','bold');
		}					
		
		// IMPORTANT ?query ==> #query&nav=nav_id
		$("a[href^=?]").each(
			function(){	
				var href = this.href;
				var query = href.substr(href.indexOf('?')+1, href.length);
				if (this.rel != "") {
					$(this).attr('href', '#' + query + "&nav=" + this.rel);
				}else {
					$(this).attr('href', '#' + query);
				}
			}
		)
		 
		// All external links in new window
		$("a[href^=http]").each(
			function(){	
				if ($(this).attr('class') != '_self') {
					if(this.href.indexOf(location.hostname) == -1) { 			
						$(this).attr('target', '_blank');
					}
				}
			}
		)
		
		
		// Sub menu behaviour
		$('#sub_menu a').click(function() { //start function when any link is clicked
			$("a#selected").removeAttr("id");
			$(this).attr("id", "selected");
		});
			
			
		tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
		imgLoader = new Image();// preload image
		imgLoader.src = tb_pathToImage;
		
		
		if (!$.browser.msie) {
			$("a[href*='main.php'][class='']").each(function() {
			
				var href = $(this).attr('href');
				var convert = false;
				if (href.indexOf('?') != -1) {			
					href = href.substring(href.indexOf('?'));
					convert = true;
				}else if (href.indexOf('#') != -1) {			
					href = href.substring(href.indexOf('#'));
					convert = true;
				}
				
				if (convert) {
					//window.console.log('original: ' +href);
					
					var section = parseQueryVal('section', href);
					var page = parseQueryVal('page', href);
					var item_id = parseQueryVal('item_id', href);
					var category = parseQueryVal('category', href);
		
					var new_hash = convertOldLinks(section, page, item_id, category);
									
					// Implement new link
					$(this).attr('class','ajax_nav');
					$(this).attr('href','?'+new_hash);
					$(this).attr('rel', 'main');
					
					//window.console.log('converted: ' +$(this).attr('href'));
				}
				
			})			
		}else {
			$("a[href*='main.php'][class='']").each(function() {
				$(this).attr('class','ajax_nav');
			});
		}
		
	}
		
}


function selectSubMenu(menu_attr) 
{
	$("#sub_menu a#selected").removeAttr("id");
	$("#sub_menu a[menu=" + menu_attr + "]").attr("id", "selected");
}

function selectListMenu(menu_attr) 
{
	//$(".list_item a#list_selected").parent().parent().css("background", "none");
	$(".list_item a#list_selected").removeAttr("id");	
	$(".list_item a[menu=" + menu_attr + "]").attr("id", "list_selected");
	//$(".list_item a[menu=" + menu_attr + "]").parent().parent().css("background-image", "url(images/framework/submenu_bg.gif)");
}

function parseQueryVal(var_name, url)
{
	// parse section variable
	if (url.indexOf(var_name + '=') != -1) {
		var start = url.indexOf(var_name + '=') + (var_name.length+1);		
		var end = url.indexOf('&', start);
		
		if (end == -1) {
			return url.substr(start);
		}else {		
			return url.substr(start, end - start);
		}
	}
	return false;
}

function stripHashVar(var_name, hash) {
	var varStart = hash.indexOf('&' + var_name + '=');

	if (varStart > 0) {		
		var varEnd = hash.indexOf('&', varStart + 1);
		
		if (varEnd > varStart)
			var varStr = hash.substr(varStart, (varEnd-varStart));
		else 
			var varStr = hash.substr(varStart);

		return hash.replace(varStr, "");
	}else {
		return hash;
	}
}

function cleanUpHash(hash) {
	// Remove nav= and update=1 in query so that section shows correctly

	var remove_string = "&update=1";
	hash = hash.replace(remove_string, "");
	
	hash = stripHashVar('del_post', hash);
	hash = stripHashVar('post', hash);
	
	if (section_initiating) {				
		hash = stripHashVar('nav', hash);
		hash = stripHashVar('rel_path', hash);
	}	
	return hash;
}

var section_initiating = true;
var section_change = false;
var section;

function page_load(hash, rel_path) {
	if (rel_path == null) rel_path = "";
	
	if ($.browser.safari && $.browser.version < 526) {
		// Safari 3 fix
		if (rel_path != "") hash = hash + "&rel_path=" + rel_path;
	}
	$.history.load(hash);
}

function page_load_direct(hash, rel_path) {		

	if (rel_path == null) rel_path = "";
	
	//if (hash != "") {
			
		//if ($.browser.msie && $.browser.version < 7) sUrl = unescape(sUrl);

		// Parse section	
		var new_section = parseQueryVal("section", hash);
		
		if (new_section != section) {
			section_change = true;
		}
					
		section = (new_section != "") ? new_section : "home";			
		
		focusMenuTab(section);
		
		// Parse destination ID to load ajax section		
		var nav_id = "main";	

		if (!section_initiating)
		{
			if (hash.indexOf('nav=') != -1) {			
				nav_id = parseQueryVal("nav", hash);
			}
			if ($.browser.safari) {
				var rel_path_var = parseQueryVal("rel_path", hash);
			}			
		}
		
		if (rel_path == "" && rel_path_var != "") rel_path = rel_path_var;

		// Remove nav= and update=1 in query so that section shows correctly
		//if (section_change || section_initiating) {		
		if (section_initiating) {		

			hash = cleanUpHash(hash);
			
			section_change = false;		
		}
			
		section_initiating = false;
		
		$('#tooltip').hide();
			
		if (hash.indexOf('?') != -1 ) {					
			// is query				
			var query_start = hash.indexOf('?')+1;
			var hash_length = hash.length;
			var query = hash.substr(query_start, hash_length - query_start);
			ajaxLoad(query, nav_id, true, false, rel_path);
		}else if (hash != "") {	
			ajaxLoad(hash, nav_id, true, false, rel_path);		
		}						
		return false;		
	//}
}

function update_userbar()
{	
	$.getJSON('data/json.user.data.php', function(data) {
		
		if (data.loggedIn) {			
			// Notifications
			
			if (data.notifications > 0) {
				if (data.notifications == 1) { $('#icon_profile').attr('title', data.notifications + ' ' + STRING_NEW_NOTIFICATION).show(); }
				else {	$('#icon_profile').attr('title', data.notifications + ' ' + STRING_NEW_NOTIFICATIONS).show(); }
				
				$('#icon_profile').attr('src', 'images/title_bar/icon_profile_new.gif');
				
				$('#num_notifications').text('(' + data.notifications + ')');
				$('a#profile_link').attr('href','?section=user&id=' + data.userID + '&page=notifications');
			} else {
				$('#icon_profile').attr('title', STRING_PROFILE).show();
				$('#icon_profile').attr('src', 'images/title_bar/icon_profile.gif');
				$('#num_notifications').text('');
				$('a#profile_link').attr('href','?section=user&id=' + data.userID);
			}
			
			// Guestbook entries
			if (data.gbNewEntries > 0) {
				if (data.gbNewEntries == 1) { $('#icon_gb').attr('title', data.gbNewEntries + ' ' + STRING_NEW_MESSAGE).show(); }
				else {	$('#icon_gb').attr('title', data.gbNewEntries + ' ' + STRING_NEW_MESSAGES).show(); }
				
				$('#icon_gb').attr('src', 'images/title_bar/icon_gb_new.gif');
			} else {
				$('#icon_gb').attr('title', STRING_WALL).show();
				$('#icon_gb').attr('src', 'images/title_bar/icon_gb.gif');
			}
			
			// Friend requests
			if (data.friendNewEntries > 0) {
				if (data.friendNewEntries == 1) { $('#icon_friends').attr('title', data.friendNewEntries + ' ' + STRING_NEW_REQUEST).show(); }
				else {	$('#icon_friends').attr('title', data.friendNewEntries + ' ' + STRING_NEW_REQUESTS).show(); }
				
				$('#icon_friends').attr('src', 'images/title_bar/icon_friends_new.gif');
				
				// update link
				$('a#friend_link').attr('href','?section=user&id=' + data.userID + '&page=requests');
				//$('a#friend_link').attr('onClick',"page_load('section=user&id=" + data.userID + "&page=requests&nav=main');return false;");
			} else {
				$('#icon_friends').attr('title', STRING_FRIENDS);
				$('#icon_friends').attr('src', 'images/title_bar/icon_friends.gif');
				
				// update link
				$('a#friend_link').attr('href','?section=user&id=' + data.userID + '&cat=friends');
				//$('a#friend_link').attr('onClick',"page_load('section=user&id=" + data.userID + "&cate=friends&nav=main');return false;");			
			}
			
			// PM entries
			if (data.pmNewEntries > 0) {
				if (data.pmNewEntries == 1) { $('#icon_pm').attr('title', data.pmNewEntries + ' ' + STRING_NEW_MESSAGE).show(); }
				else {	$('#icon_pm').attr('title', data.pmNewEntries + ' ' + STRING_NEW_MESSAGES).show(); }
				
				$('#inbox_menu').html('<b>' + STRING_INBOX + '&nbsp;(' +data.pmNewEntries+ ')</b>');
				
				$('#icon_pm').attr('src', 'images/title_bar/icon_pm_new.gif');
			} else {
				$('#icon_pm').attr('title', STRING_PM).show();
				$('#icon_pm').attr('src', 'images/title_bar/icon_pm.gif');
				$('#inbox_menu').text(STRING_INBOX);
			}
			
			// Update online and total users
			$('#status_users').html('<p style="font-size:11px;"><a class="ajax_nav" href="?section=citizens&amp;page=online" rel="main">' + data.usersOnline + ' ONLINE</a>&nbsp;&nbsp; | &nbsp;<a class="ajax_nav" style="font-weight:normal" href="?section=citizens&amp;page=search&amp;search_user=&amp;top=1">' + data.usersTotal + ' CITIZENS</a>');

		}else {
			// Not logged in
			if ($('#logoutForm').length > 0) {
				ajaxLoadModule('update=1','mod_userbar', 'base/mod_loginbar.php');
			}
		}	
	});		
	
	if ($("#mod_userbar").data("clearTimeout") != '') clearTimeout($("#mod_userbar").data("clearTimeout"));
    UserbarTimer = setTimeout(update_userbar, 150000);
    $("#mod_userbar").data("clearTimeout", UserbarTimer);
}


function updatePageTitle(sSection) {
	if (sSection != "") {
		document.title = 'MANDO DIAO | ' + sSection;
	}else {
		document.title = 'MANDO DIAO';		
	}
}


// Ajax Link Mouse Down
function ajaxLink_MouseDown(obj)  {
	var href = obj.attr('href') + "";
	if (href.indexOf('#') == -1) {
		href = unescape(href);
		var query = href.substr(href.indexOf('?')+1, href.length);
		if (obj.attr('rel') != "") obj.attr('href', '#' + query + "&nav=" + obj.attr('rel'));
		else obj.attr('href', '#' + query);
	}		
}


// Ajax Link Mouse up
function ajaxLink_MouseUp(obj)  {	
	//if ($(this).attr('rel') == 'sub') return false;				
	var hash = obj.attr('href') + "";
	if (hash.indexOf('?') != -1) {			
		hash = hash.substring(hash.indexOf('?')+1);		// IE Fix
	}else {
		hash = hash.replace(/^.*#/, '');
	}
	var new_section = parseQueryVal("section", hash);

	// During section change, hide current navigation div id
	if (new_section != section) {			
		//var cur_hash = self.document.location.hash;	
		//el_id = parseQueryVal("nav", cur_hash);			
		//$("#" + el_id + "_content").css("visibility", "hidden");
	}
	
	if (obj.attr('rel') != "") {
		if (parseQueryVal("nav", hash) != false) {
			hash = stripHashVar("nav", hash);
		}	
		hash = hash + '&nav=' + obj.attr('rel');
	}						
	// moves to a new section. 
	// sectionload is called at once. 
	
	$.history.load(hash);
	return false;
}

$(document).ready(function(){
						   
	// INIT section		
	
	if (enableAjax) {
		var hash = self.document.location.hash.substring(1);			
				
		var url = self.document.location + "";
		var main_file = "main.php";
		 
		//if (url.substring(url.length - main_file.length) == main_file) {
		if (url.indexOf('section=') == -1) {
			//page_load("section=home&nav=main");
		}				
		
		// Initialize history plugin.
		// The callback is called at once by present location.hash. 	
		
		
		//$.listen('mousedown', 'a.ajax_nav', ajaxLink_MouseDown)	;
		//$.listen('mouseup', 'a.ajax_nav', ajaxLink_MouseUp);	 
		
		$.listen('mousedown', 'a.ajax_nav', function(){
			$(this).data('click', true);
			if ($(this).parent().attr('class').indexOf('ajax_nav') == -1) ajaxLink_MouseDown($(this));											  
		})				
		$.listen('mouseup', 'a.ajax_nav', function(){
			if ($(this).data('click')) {
				$(this).data('click', false);
				if ($(this).parent().attr('class').indexOf('ajax_nav') == -1) ajaxLink_MouseUp($(this));		 			 
			}
		})
		
		if ($.browser.msie) {
			$.listen('mousedown', 'img', function(){
				
				$(this).data('click', true);
				
				var $parent = $(this).parent();
				if ($parent.attr('class').indexOf('ajax_nav') > -1) {
						ajaxLink_MouseDown($parent);
				}else {
					$parent = $parent.parent(); // Try next parent
					if ($parent.attr('class').indexOf('ajax_nav') > -1) 	ajaxLink_MouseDown($parent);
				}
			})				
			$.listen('mouseup', 'img', function(){
				if ($(this).data('click')) {
					$(this).data('click', false);
					var $parent = $(this).parent();
					if ($parent.attr('class').indexOf('ajax_nav') > -1) {
						ajaxLink_MouseUp($parent);
					}else {
						$parent = $parent.parent(); // Try next parent
						if ($parent.attr('class').indexOf('ajax_nav') > -1) 	ajaxLink_MouseUp($parent);
					}
				}
			})		
		}	 
		updateAjaxLinks();	// Ensure all links are #-ified
	}
	
	/*$.getJSON("data/json.user.data.php",
        function(data){
          	if (data.loggedIn) {
				// Logged in
				ajaxLoadModule('update=1','mod_userbar', 'base/mod_userbar.php');					
			}else {
				// Not logged in
				ajaxLoadModule('update=1','mod_userbar', 'base/mod_loginbar.php');
			}	
        });*/
		

	// -----------------------------------------
	//	ON MENU CLICK
	// -----------------------------------------
	
	$('a.menu').click(function() { //start function when any link is clicked
		
		//$(".content").slideUp("slow");
			
		var content_show = $(this).attr("load"); //retrieve title of link so we can compare with php file
		ajaxLoad(content_show);
		
	}); //close click(
	
	$('a.menu_quick').click(function() { //start function when any link is clicked
		
			//$(".content").slideUp("slow");
			
		var content_show = $(this).attr("load"); //retrieve title of link so we can compare with php file
		ajaxUpdateQuery(content_show);
		
	}); //close click(
	
	
	
	// Tooltip
    $('#tooltip').remove();
    $('body').append('<div id="tooltip" class="tooltip_box"></div>');
    $('#tooltip').hide();
	
	//$('#tooltip').show();
	
	$.listen('mouseover', '.tooltip', function(e) {										   	
		$(this).data('tooltip', this.title);
        $(this).attr('title', '');
	});
    $.listen('mousemove', '.tooltip', function(e) {											   
        if ($(this).data('tooltip') != '' && $(this).data('tooltip') != undefined) {
            var left = e.pageX + 10, top = e.pageY + 10;
            $('#tooltip').html('').append($(this).data('tooltip'));
            // Check position and adjust the tooltip
            if(left + 10 + $('#tooltip').width() > $(window).width()) left -= $('#tooltip').width() + 30;
            if(top + 10 + $('#tooltip').height() > $(window).height()) top -= $('#tooltip').height() + 30;
            
            $('#tooltip').css({ left: left + 'px', top: top + 'px' }).show();         
        }
    })
    $.listen('mouseout', '.tooltip', function() {
        $(this).attr('title', $(this).data('tooltip'));
        $(this).removeData('tooltip');
        $('#tooltip').hide();
    });
	
	
	//$('#page_title').css({ backgroundImage:"url(images/title_bar/title_" + section + ".gif)"});			
	
	if (BrowserDetect.browser == "Firefox" || BrowserDetect.browser == "Internet Explorer" || BrowserDetect.browser == "Explorer") {			
		$('#main_content').css('margin-top','-5px');
		$('#title_bar').css('margin-bottom','5px');					
	}									
				
	$('a.menu').nifty("small top transparent #ab2f36");
	
	$('body').height('100%');			
	
	if ($("#mod_userbar").data("clearTimeout") != '') clearTimeout($("#mod_userbar").data("clearTimeout"));
	UserbarTimer = setTimeout(update_userbar, 150000);
	$("#mod_userbar").data("clearTimeout", UserbarTimer);
		
	$.history.init(page_load_direct);	
	
 }); //close $(document).ready

