$(document).ready(function(){
	/*
	$("#loading").ajaxStart(function(){
	  $(this).show();
	});
	$("#loading").ajaxStop(function(){
	  $(this).hide();
	});	
	*/
	joomexLog("document is now ready...");
	positionJoomexMessage(true);
	
	joomex_loading_opcity = "0.3";
	
	//$(window).scroll(function(){positionJoomexMessage(false)});
	
	/*
	$("#joomexHistoryHandler").mouseover(function(){
												  $("#joomexHistory").show("slow");
								  });
	$("#joomexHistory").mouseout(function(){
									  $("#joomexHistory").hide("slow");
									  });
	*/
	/*
	$("#jlog").mouseover(function(){
								  $(this).css("height","500px");
								  $(this).css("background-color","#FFFFFF");
								  $(this).css("overflow","auto");
								  $(this).css("border","2px solid #666666");
								  }).mouseout(function(){
									  $(this).css("height","65px");
									  $(this).css("background-color","");
									  $(this).css("overflow","hidden");
									  $(this).css("border","");
									  });
	*/
	/*
    $("#jlog").bind("mouseenter",function(){
								  $("#jlog").css("height","500px");
								  $("#jlog").css("background-color","#FFFFFF");
								  $("#jlog").css("overflow","auto");
								  $("#jlog").css("border","2px solid #666666");
    }).bind("mouseleave",function(){
								  $("#jlog").css("height","65px");
								  $("#jlog").css("background-color","");
								  $("#jlog").css("overflow","hidden");
								  $("#jlog").css("border","");
    });
	*/
	//alert($("#ajaxComponentContainer").offsetLeft);
	//$('form').append("<input type=\"hidden\" value=\"1\" name=\"isAjax\"/>");
	
	$('a').click(sanitize);
	//$("#jLogCleaner").click(function(){$("#jlog").empty();});
	
	$('form').submit(sendForm);
	
	//$('a').history(this.sanitize);
	//$('a').click(function(){return false;});
	//$('a').history(function(){alert('a'); return false;});
	showCount = 0;
	pages = new Array();
	
	
	$.ajaxHistory.initialize();
	//initializeHistory();
	/*
debugeHandler
joomexDebugeArea
joomexDebugeShowArea
*/
/*
	$('#debugeHandler').click(function(){
		if(!$('#joomexDebugeArea').hasClass('minimized')){
			//$('#joomexDebugeArea').animate({height: 0, top: -400},"slow",function(){$(this).hide()}).addClass('minimized');
			//$('#joomexDebugeArea').fadeOut(2000,function(){$(this).hide().addClass('minimized')});
			$('#joomexDebugeArea').animate({width:390, height:390, top:25 , right:25, opacity: 0}, 500, "easeout", function(){$(this).addClass('minimized').hide()});
		}else{
			//$('#joomexDebugeArea').fadeIn(2000,function(){$(this).removeClass('minimized')});
			//$('#joomexDebugeArea').animate({width: "400px"}, { "duration": 1000, "easing": "easein" });
			$('#joomexDebugeArea').animate({width:400, height:400, top:20 , right:20, opacity: .9}, 500, "easeout", function(){$(this).removeClass('minimized')});
		}
		//alert($('#joomexDebugeArea').hasClass('minimized'));
		//alert('a');
	});
	
	$('#adminHandler').click(function(){
		if(!$('#joomexAdminArea').hasClass('minimized')){
			//$('#joomexDebugeArea').animate({height: 0, top: -400},"slow",function(){$(this).hide()}).addClass('minimized');
			//$('#joomexDebugeArea').fadeOut(2000,function(){$(this).hide().addClass('minimized')});
			$('#joomexAdminArea').animate({width:390, height:390, top:25 , right:25, opacity: 0}, 500, "easeout", function(){$(this).addClass('minimized').hide()});
		}else{
			//$('#joomexDebugeArea').fadeIn(2000,function(){$(this).removeClass('minimized')});
			//$('#joomexDebugeArea').animate({width: "400px"}, { "duration": 1000, "easing": "easein" });
			$('#joomexAdminArea').animate({width:400, height:400, top:20 , right:20, opacity: .9}, 500, "easeout", function(){$(this).removeClass('minimized')});
		}
		//alert($('#joomexDebugeArea').hasClass('minimized'));
		//alert('a');
	});
*/
	/*
	$('.settings').click(function(){
		$("#joomexAdminShowArea").load("http://meijin:81/joomex101/administrator/index.php?option=com_modules&client=0&task=edit&cid[]=30");
	});
	*/
	
	
	/*
leftPanel
leftPanelContent
leftPanelHandle
	*/
	/*
	$('#leftPanel').css('height', $(document).height());
	$('#leftPanelContent').css('height',500);
	
	$(window).scroll(function(){
		//$('#leftPanelContent').animate({top:$(document).scrollTop()},100,"easeout");
		$('#leftPanelContent').css('top',$(document).scrollTop());
		$('#leftPanel').animate({left: -380}, 500, "easeout", function(){$(this).addClass("leftPanelMinized");});
	});
	
	$('#leftPanelHandle').click(function(){
		if(!$('#leftPanel').hasClass("leftPanelMinized")){
			$('#leftPanel').animate({left: -380}, 500, "easeout", function(){$(this).addClass("leftPanelMinized");});
		}else{
			$('#leftPanel').animate({left: 0}, 500, "easeout", function(){$(this).removeClass("leftPanelMinized");});
		}
		
	});
	*/
	/*
	$('#leftPanelHandle').mouseover(function(){
		if($('#leftPanel').hasClass("leftPanelMinized")){
			$('#leftPanel').animate({left: 0}, 500, "easeout", function(){$(this).removeClass("leftPanelMinized");});
		}
	});
	*/
	/*
	$('#leftPanel').mouseout(function(){
		if(!$('#leftPanel').hasClass("leftPanelMinized")){
			$('#leftPanel').animate({left: -380}, 500, "easeout", function(){$(this).addClass("leftPanelMinized");});
		}
	});
	$('#leftPanel').mouseover(function(){
		if($('#leftPanel').hasClass("leftPanelMinized")){
			$('#leftPanel').animate({left: 0}, 500, "easeout", function(){$(this).removeClass("leftPanelMinized");});
		}
	});
	*/
	
	joomexLog("Joomex is now ready...");
	
});



/*
function loadModuleAdmin(mid){
	//window.open("http://meijin:81/joomex101/administrator/index.php?option=com_modules&client=0&task=edit&cid[]="+mid);
	$("#joomexAdminShowArea").load("http://meijin:81/joomex101/administrator/index.php?option=com_modules&client=0&task=edit&cid[]="+mid, {isAjax:1}, function(){});
	//$("#joomexAdminShowArea").load("http://meijin:81/joomex101/administrator/index.php?option=com_modules&client=0&task=edit&cid[]="+mid);
}
*/

function sendForm(e){
	/*
	 * removed for now
	 */
	/*
	var options = {
		data: {isAjax:1},
		success: function(data){placePage(data);},
		target: $("#ajaxComponentContainer")
	};
	$(this).ajaxSubmit(options);
	return false;
	*/
	return true;
}

function joomexLog(message){
	/*
	$("#jlog").prepend("<hr />");
	//$("#jlog").prepend("<br />___________________________________________________________________<br />");
	
	
	
	//$("<span>"+message+"</span>")
	var el = $("<span>"+message+"</span>");
	$("#jlog").prepend(el);
	
	
	
	//el.fadeIn("fast");
	//$("#jlog").prepend(message);
	*/
}


function positionJoomexMessage(force){
	if(($("#loading").css("display") != "none") || (force == true)){
		$("#loading").css("top",$(document).scrollTop() + 20);
		$("#loading").css("left",(($(document).width()/2) - ($("#loading").width()/2)));
	}
}


/*
var href = "http://meijin:81/JJ/index.php?option=com_content&view=section&id=4&Itemid=37";
var hash = hex_md4(href);
alert(hash);
*/
function sanitize(e,force){
	if( (this.href.indexOf(baseurl) != -1) && (this.onclick==null) ){
		joomexLog("Link clicked, Loading:<br />"+this.href);
		
		//var hash = Base64.encode(this.href);
		var hash = joomex_encodeURL(this.href);
		
		
		
		leftNeedLoad = true;
		rightNeedLoad = true;
		breadCrumpNeedLoad = true;
		user1NeedLoad = true;
		user2NeedLoad = true;
		pageNeedLoad = true;
		btnLeftNeedLoad = true;
		btnCenterNeedLoad = true;
		btnRightNeedLoad = true;
		
		
		
		for(var i in pages){
			if(pages[i].url == this.href){
				//alert("in history!");
				joomexLog("Placing Component from history...");
				$("#ajaxComponentContainer").html(pages[i].html);
				$("#ajaxComponentContainer a").click(sanitize);
				$("#ajaxComponentContainer form").submit(sendForm);
				
				if(pages[i].Left != null){
					joomexLog("Placing Left Modules from history...");
					$("#left_inner").html(pages[i].Left);
					$("#left_inner a").click(sanitize);
					$("#left_inner form").submit(sendForm);
					leftNeedLoad = false;
				}else{
					leftNeedLoad = true;
				}
				if(pages[i].Right != null){
					joomexLog("Placing Right Modules from history...");
					$("#center_bot_pos_main_body").css("width","80%");
					$("#right_inner").show();
					$("#right_inner").html(pages[i].Right);
					$("#right_inner a").click(sanitize);
					$("#right_inner form").submit(sendForm);
					rightNeedLoad = false;
				}else{
					rightNeedLoad = true;
				}
				if(pages[i].BreadCrump != null){
					joomexLog(" from history Page BreadCrump from history...");
					$("#pathway_text").html(pages[i].BreadCrump);
					$("#pathway_text a").click(sanitize);
					$("#pathway_text form").submit(sendForm);
					breadCrumpNeedLoad = false;
				}else{
					breadCrumpNeedLoad = true;
				}
				if(pages[i].User1 != null){
					joomexLog("Placing User1 Modules from history...");
					$("#user12").show();
					$("#user1column").show();
					$("#user1_inner").html(pages[i].User1);
					$("#user1_inner a").click(sanitize);
					$("#user1_inner form").submit(sendForm);
					user1NeedLoad = false;
				}else{
					user1NeedLoad = true;
				}
				
				if(pages[i].btnLeft != null){
					joomexLog("Placing btnLeft Modules from history...");
					$("#main_body_foot_modules").show();
					$("#main_body_foot_modules_left").show();
					$("#main_body_foot_modules_left").html(pages[i].btnLeft);
					$("#main_body_foot_modules_left a").click(sanitize);
					$("#main_body_foot_modules_left form").submit(sendForm);
					btnLeftNeedLoad = false;
				}else{
					btnLeftNeedLoad = true;
				}
				
				if(pages[i].btnCenter != null){
					joomexLog("Placing btnCenter Modules from history...");
					$("#main_body_foot_modules").show();
					$("#main_body_foot_modules_center").show();
					$("#main_body_foot_modules_center").html(pages[i].btnCenter);
					$("#main_body_foot_modules_center a").click(sanitize);
					$("#main_body_foot_modules_center form").submit(sendForm);
					btnCenterNeedLoad = false;
				}else{
					btnCenterNeedLoad = true;
				}
				
				if(pages[i].btnRight != null){
					joomexLog("Placing btnRight Modules from history...");
					$("#main_body_foot_modules").show();
					$("#main_body_foot_modules_right").show();
					$("#main_body_foot_modules_right").html(pages[i].btnRight);
					$("#main_body_foot_modules_right a").click(sanitize);
					$("#main_body_foot_modules_right form").submit(sendForm);
					btnRightNeedLoad = false;
				}else{
					btnRightNeedLoad = true;
				}
				
				if(pages[i].User2 != null){
					joomexLog("Placing User2 Modules from history...");
					$("#user12").show();
					$("#user2column").show();
					$("#user2_inner").html(pages[i].User2);
					$("#user2_inner a").click(sanitize);
					$("#user2_inner form").submit(sendForm);
					user2NeedLoad = false;
				}else{
					user2NeedLoad = true;
				}
				
				if(!(leftNeedLoad || rightNeedLoad || breadCrumpNeedLoad || user1NeedLoad || user2NeedLoad)){
					return false;
				}else{
					pageNeedLoad = false;
				}
			}else{
				leftNeedLoad = true;
				rightNeedLoad = true;
				breadCrumpNeedLoad = true;
				user1NeedLoad = true;
				user2NeedLoad = true;
				pageNeedLoad = true;
				
				btnLeftNeedLoad = true;
				btnCenterNeedLoad = true;
				btnRightNeedLoad = true;
				
				$("#user12").hide();
				$("#user2column").hide();
				$("#user1column").hide();
				
				$("#main_body_foot_modules").hide();
				$("#main_body_foot_modules_left").hide();
				$("#main_body_foot_modules_center").hide();
				$("#main_body_foot_modules_right").hide();
			}
		}
		
		
		if (e.clientX || force) {
			//alert('Client Activity');
			joomexLog("Adding to history, URL:<br />"+this.href+"<br />Hash is:<br />"+hash);
			$.ajaxHistory.update(hash);
		}else{
			//alert('no Client Activity ');
		}
		
		
		
		showCount ++;
		positionJoomexMessage(true);
		$("#loading").show();
		
		
		
		//$("#ajaxComponentContainer").load(this.href, {isAjax:1}, function(){placePage();}); 
		
		joomexLog("Sending JSON Request...");
		
		
		
		//alert(e.clientX);
		$.getJSON(this.href, {isJSON:"1",isAjax:"0",loadURL:this.href},
		  function(data){
			//alert("Data Loaded: " + data);
			//alert("loadURL is: " + data.loadURL);
			var logStrig = "{<br />";
			for(var i in data){
				logStrig += i+": \""+data[i]+"\",<br />";
			}
			logStrig += "}<br />";
			joomexLog("JSON Returned for page:<br />"+logStrig);
			if(pageNeedLoad){
				joomexLog("Now Loading Main Page (Component)...");
				$("#ajaxComponentContainer").css("opacity",joomex_loading_opcity);
				$("#ajaxComponentContainer").load(data.loadURL, {isAjax:1}, function(){placePage(data);});
				
				
				
				
				
				
				/*
        $.ajax({
          type: 'POST',
          url: data.loadURL +'/1',
          _data: '',
          success: function (_data) {
            // Parse response
            //_data = Drupal.parseJson(_data);
            // Display errors
            if (!_data.status) {
              // Change label back
              //flipLabel(a);
              return;
            }
          },
          error: function (xmlhttp) {
            //alert('An HTTP error '+ xmlhttp.status +' occured.\n'+ data.loadURL);
			joomexLog('An HTTP error '+ xmlhttp.status +' occured.\n'+ data.loadURL);
			showCount --;
            // Change label back
            //flipLabel(a);
          }
        });
				
				*/
				
				
				
				
				
			}
			if(breadCrumpNeedLoad){
				showCount ++;
				joomexLog("Now Loading Pathway Module (BreadCrumb)...");
				$("#pathway_text").css("opacity",joomex_loading_opcity);
				$("#pathway_text").load(data.loadURL, {isAjax:1,MB:1}, function(){placeModule("BreadCrump",data);});
			}
			
			if((data.MLC != 0) && leftNeedLoad){
				showCount ++;
				joomexLog("Now Loading Left Modules...");
				$("#left_inner").css("opacity",joomex_loading_opcity);
				$("#left_inner").load(data.loadURL, {isAjax:1,ML:1}, function(){placeModule("Left",data);});
			}
			if((data.MRC != 0) && rightNeedLoad){
				showCount ++;
				//$("#center_bot_pos_main_body").css("width","80%");
				$("#rightColomn").show();
				//$("#right_innerGreyline").show();
				joomexLog("Now Loading Right Modules...");
				$("#right_inner").css("opacity",joomex_loading_opcity);
				$("#right_inner").load(data.loadURL, {isAjax:1,MR:1}, function(){placeModule("Right",data)});
			}else if((data.MRC == 0)){
				$("#rightColomn").hide();
				//$("#right_innerGreyline").hide();
				//$("#center_bot_pos_main_body").css("width","100%");
			}
			
			if(data.MU1C != 0){
				showCount ++;
				$("#user1column").show();
				//$("#usercolumnGreyline").show();
				joomexLog("Now Loading User1 Modules...");
				$("#user1_inner").css("opacity",joomex_loading_opcity);
				$("#user1_inner").load(data.loadURL, {isAjax:1,MU1:1}, function(){placeModule("User1");});
			}else if((data.MU1C == 0)){
				$("#user1column").hide();
				//$("#usercolumnGreyline").hide();
			}
			
			
			
			if(data.MBLC != 0){
				showCount ++;
				$("#main_body_foot_modules_left").show();
				//$("#usercolumnGreyline").show();
				joomexLog("Now Loading btnLeft Modules...");
				$("#main_body_foot_modules_left").css("opacity",joomex_loading_opcity);
				$("#main_body_foot_modules_left").load(data.loadURL, {isAjax:1,MBL:1}, function(){placeModule("btnLeft");});
			}else if((data.btnLeft == 0)){
				$("#main_body_foot_modules_left").hide();
				//$("#usercolumnGreyline").hide();
			}		
			
			if(data.MBCC != 0){
				showCount ++;
				$("#main_body_foot_modules_center").show();
				//$("#usercolumnGreyline").show();
				joomexLog("Now Loading btnCenter Modules...");
				$("#main_body_foot_modules_center").css("opacity",joomex_loading_opcity);
				$("#main_body_foot_modules_center").load(data.loadURL, {isAjax:1,MBC:1}, function(){placeModule("btnCenter");});
			}else if((data.btnCenter == 0)){
				$("#main_body_foot_modules_center").hide();
				//$("#usercolumnGreyline").hide();
			}
			
			if(data.MBRC != 0){
				showCount ++;
				$("#main_body_foot_modules_right").show();
				//$("#usercolumnGreyline").show();
				joomexLog("Now Loading btnRight Modules...");
				$("#main_body_foot_modules_right").css("opacity",joomex_loading_opcity);
				$("#main_body_foot_modules_right").load(data.loadURL, {isAjax:1,MBR:1}, function(){placeModule("btnRight");});
			}else if((data.btnRight == 0)){
				$("#main_body_foot_modules_right").hide();
				//$("#usercolumnGreyline").hide();
			}
			
			if(data.MU2C != 0){
				showCount ++;
				$("#user2column").show();
				//$("#usercolumnGreyline").show();
				joomexLog("Now Loading User2 Modules...");
				$("#user2_inner").css("opacity",joomex_loading_opcity);
				$("#user2_inner").load(data.loadURL, {isAjax:1,MU2:1}, function(){placeModule("User2");});
			}else if((data.MU2C == 0)){
				$("#user2column").hide();
				//$("#usercolumnGreyline").hide();
			}
			
			if((data.MU1C == 0) && (data.MU2C == 0)){
				$("#user12").hide();
			}else{
				$("#user12").show();
			}
			
			if((data.MBLC == 0) && (data.MBCC == 0) && (data.MBRC == 0)){
				$("#main_body_foot_modules").hide();
			}else{
				$("#main_body_foot_modules").show();
			}
			
			
			/*
			if(data.MU3C != 0){
				showCount ++;
				$("#left_inner").load(data.loadURL, {isAjax:1,MU3:1}, function(){placeModule("Left");});
			}
			if(data.MU4C != 0){
				showCount ++;
				$("#left_inner").load(data.loadURL, {isAjax:1,MU4:1}, function(){placeModule("Left");});
			}
			*/
		  });
		
		return false;
	}else if(this.onclick!=null){
		return false;
	}
	
}

/*
var _currentHash = location.hash;

function updateHistoryHash(hash){
	_currentHash = hash;
}

var _observeHistory = function() {
	if (location.hash) {
		if (_currentHash != location.hash) {
			_currentHash = location.hash;
			$('a[@href$="' + location.hash.substring(17) + '"]').trigger('click');
			//alert(_currentHash);
			//alert($('a[@href$="' + _currentHash.substring(1) + '"]').href);
			//alert($('a[@href$="' + _currentHash + '"]'));
			//alert(_currentHash);
			//alert($('a[href="' + _currentHash.substring(1) + '"]').click());
			//alert(_currentHash.substring(1));
			//$('a[href="' + _currentHash + '"]').click();
		}
	} else if (_currentHash) {
		_currentHash = '';
		$(document).trigger(RESET_EVENT);
	}
};
var RESET_EVENT = 'historyReset';
var _intervalId = null;
var _defaultReset = function() {
	//$('.remote-output').empty();
};
$(document).bind(RESET_EVENT, _defaultReset);
initializeHistory = function(callback) {
	// custom callback to reset app state (no hash in url)
	if (typeof callback == 'function') {
		$(document).unbind(RESET_EVENT, _defaultReset).bind(RESET_EVENT, callback);
	}
	// look for hash in current URL (not Safari)
	if (location.hash && typeof _addHistory == 'undefined') {
		//alert(location.hash.substring(30));
		//$('a[@href$="/JJ/index.php?option=com_newsfeeds&view=categories&Itemid=49"]').trigger('click');
		//alert(location.hash.substring(17));
		$('a[@href$="' + location.hash.substring(17) + '"]').trigger('click');
	}
	// start observer
	if (_observeHistory && _intervalId == null) {
		_intervalId = setInterval(_observeHistory, 200); // Safari needs at least 200 ms
	}
};
*/
function saveDataToHistory(data,modulePosition){
	
	if((data != null) && (data != undefined)){
		if(!modulePosition){
			pages.push({data:data,url:data.loadURL,html:$("#ajaxComponentContainer").html(),BreadCrump:'',Left:'',Right:'',User1:'',User2:'',btnLeft:'',btnCenter:'',btnRight:''});
			//$("#joomexHistory").prepend("<br />");
			//$("#joomexHistory").prepend("<a href='"+data.loadURL+"'>"+data.loadURL+"</a>");
		}else{
			for(var i in pages){
				if(pages[i].url == data.loadURL){
					
					switch (modulePosition){
					
						case 'BreadCrump':
						pages[i].BreadCrump = $("#pathway_text").html();
						break;
						
						case 'Left':
						pages[i].Left = $("#left_inner").html();
						break;
						
						case 'Right':
						pages[i].Right = $("#right_inner").html();
						break;
						
						case 'User1':
						pages[i].User1 = $("#user1_inner").html();
						break;
						
						case 'User2':
						pages[i].btnLeft = $("#user2_inner").html();
						break;
						
						case 'btnLeft':
						pages[i].User2 = $("#main_body_foot_modules_left").html();
						break;
						
						case 'btnCenter':
						pages[i].btnCenter = $("#main_body_foot_modules_center").html();
						break;
						
						case 'btnRight':
						pages[i].btnRight = $("#main_body_foot_modules_right").html();
						break;
						
					}
					
				}
			}
		}
		if(pages.length > 1){
			pages.shift();
		}
	}
}

function placePage(data){
	saveDataToHistory(data);
	showCount --;
	if(showCount == 0){
		$("#loading").hide();
	}
	joomexLog("Placing: Main Page(Component) on the page.");
	$("#ajaxComponentContainer a").click(sanitize);
	$("#ajaxComponentContainer form").append("<input type=\"hidden\" value=\"1\" name=\"isAjax\"/>");
	$("#ajaxComponentContainer button").attr("onclick","");
	$("#ajaxComponentContainer select#limit").attr("onchange","");
	$("#ajaxComponentContainer form").submit(sendForm);
	$("#ajaxComponentContainer").css("opacity","1");
}

function placeModule(modulePosition,data){
	joomexLog("Saving History...");
	saveDataToHistory(data,modulePosition);
	joomexLog("Placing: "+modulePosition+" Module(s) on the page.");
	if(modulePosition == "Left"){
		showCount --;
		if(showCount == 0){
			$("#loading").hide();
		}
		$("#left_inner a").click(sanitize);
		$("#left_inner form").submit(sendForm);
		$("#left_inner").css("opacity","1");
	}
	
	if(modulePosition == "BreadCrump"){
		showCount --;
		if(showCount == 0){
			$("#loading").hide();
		}
		$("#pathway_text a").click(sanitize);
		$("#pathway_text form").submit(sendForm);
		$("#pathway_text").css("opacity","1");
	}
	
	if(modulePosition == "Right"){
		showCount --;
		if(showCount == 0){
			$("#loading").hide();
		}
		$("#right_inner a").click(sanitize);
		$("#right_inner form").submit(sendForm);
		$("#right_inner").css("opacity","1");
	}
	
	if(modulePosition == "User1"){
		showCount --;
		if(showCount == 0){
			$("#loading").hide();
		}
		$("#user1_inner a").click(sanitize);
		$("#user1_inner form").submit(sendForm);
		$("#user1_inner").css("opacity","1");
	}
	
	if(modulePosition == "User2"){
		showCount --;
		if(showCount == 0){
			$("#loading").hide();
		}
		$("#user2_inner a").click(sanitize);
		$("#user2_inner form").submit(sendForm);
		$("#user2_inner").css("opacity","1");
	}
	
	if(modulePosition == "btnLeft"){
		showCount --;
		if(showCount == 0){
			$("#loading").hide();
		}
		$("#main_body_foot_modules_left a").click(sanitize);
		$("#main_body_foot_modules_left form").submit(sendForm);
		$("#main_body_foot_modules_left").css("opacity","1");
	}
	
	if(modulePosition == "btnCenter"){
		showCount --;
		if(showCount == 0){
			$("#loading").hide();
		}
		$("#main_body_foot_modules_center a").click(sanitize);
		$("#main_body_foot_modules_center form").submit(sendForm);
		$("#main_body_foot_modules_center").css("opacity","1");
	}
	
	if(modulePosition == "btnRight"){
		showCount --;
		if(showCount == 0){
			$("#loading").hide();
		}
		$("#main_body_foot_modules_right a").click(sanitize);
		$("#main_body_foot_modules_right form").submit(sendForm);
		$("#main_body_foot_modules_right").css("opacity","1");
	}
	/*
	if(modulePosition == "BreadCrump"){
		showCount --;
		if(showCount == 0){
			$("#loading").hide();
		}
		$("#pathway_text a").click(sanitize);
	}
	
	if(modulePosition == "BreadCrump"){
		showCount --;
		if(showCount == 0){
			$("#loading").hide();
		}
		$("#pathway_text a").click(sanitize);
	}
	
	if(modulePosition == "BreadCrump"){
		showCount --;
		if(showCount == 0){
			$("#loading").hide();
		}
		$("#pathway_text a").click(sanitize);
	}
	
	if(modulePosition == "BreadCrump"){
		showCount --;
		if(showCount == 0){
			$("#loading").hide();
		}
		$("#pathway_text a").click(sanitize);
	}
	*/
	
	
	
}

function loadPageFromFlash(url){
	createAndFireLink(URLDecode(url));
}

function createAndFireLink(url){
	//alert(url);
	//alert(decodeURI(url));
	var newLink = $("<a href=\""+url+"\"></a>");
	$("#linksGarbage").append(newLink);
	newLink.click(sanitize);
	newLink.trigger("click",[true]);
}

function createAndFireLinkForHistoryLoads(url){
	//alert(url);
	//alert(decodeURI(url));
	var newLink = $("<a href=\""+url+"\"></a>");
	$("#linksGarbage").append(newLink);
	newLink.click(sanitize);
	newLink.trigger("click");
}

// ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site 
//     with a link back to http://www.albionresearch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// And thanks to everyone else who has provided comments and suggestions.
// ====================================================================
function URLEncode(_url)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	//var plaintext = document.URLForm.F1.value;
	var plaintext = _url;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	//document.URLForm.F2.value = encoded;
  	//document.URLForm.F2.select();
	return encoded;
};

function URLDecode(_url)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   //var encoded = document.URLForm.F2.value;
   var encoded = _url;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   //document.URLForm.F1.value = plaintext;
   //document.URLForm.F1.select();
   return plaintext;
};


function joomex_encodeURL(_url){
	//var encoded = Base64.encode(_url);
	_url = _url.replace("#","");
	_url = _url.replace(baseurl+"/index.php?","");
	_url = _url.replace("option=com_","mod:");
	_url = _url.replace("task=","act:");
	_url = _url.replace("act:view","act:v");
	_url = _url.replace("act:category","act:c");
	_url = _url.replace("act:section","act:s");
	_url = _url.replace("Itemid=","i:");
	_url = _url.replace("sectionid=","s:");
	_url = _url.replace("id=","m:");
	_url = _url.replace(/&/g,",");
	_url = _url.replace(/=/g,":");
	var encoded = _url;
	return encoded;
}

function joomex_decodeURL(_url){
	//var decoded = Base64.decode(_url);
	_url = _url.replace("#","");
	_url = _url.replace("i:","Itemid=");
	_url = _url.replace("s:","sectionid=");
	_url = _url.replace("m:","id=");
	_url = _url.replace("act:v","act:view");
	_url = _url.replace("act:c","act:category");
	_url = _url.replace("act:s","act:section");
	_url = _url.replace("act:","task=");
	_url = _url.replace("mod:","option=com_");
	_url = _url.replace(/,/g,"&");
	_url = _url.replace(/:/g,"=");
	var decoded = baseurl+"/index.php?"+_url;
	return decoded;
}
