var linkID='home';
var navID='home';
var render=null;
var loading = false;
var loadingTimer = null;
function showIcon(id){
    var item;
    if (typeof(id)=='object') {
       item = id;
    }else{
       item = $('#'+id);
    }
    item.children("a").children("img").fadeIn().prev().fadeTo("normal",0);
}
function hideIcon(id){
    var item;
    if (typeof(id)=='object') {
       item = id;
    }else{
       item = $('#'+id);
    }
    if(item) item.children("a").children("img").fadeOut().prev().fadeTo("normal",1);
}
function loadPage(url){
	var n = url.indexOf("/#");
	if ( n>0){
		url = url.substr(n+1);
	}
	if ((url.substr(0,7)=="http://") ){
		return;
	}
    if (url=='') return;
    linkID = url.substr(1);
    var nav=linkID;
    if (linkID.indexOf('/')>=0) {
        nav = linkID.substr(0, linkID.indexOf('/'))
    }
    if (nav!=navID) {
        hideIcon(navID);
        navID = nav;
    }
    showIcon(navID);
    loading = true;
    loadingTimer = window.setTimeout('$("#loadingPanel").show()',1000);
    $.post(root+"/cmsaction.php",
        {action: "2", linkid: linkID},
        function(ret) {
            render(ret);
            loading = false;
            clearTimeout(loadingTimer);
            $("#loadingPanel").hide();
            $("#mask").fadeOut();
   });
   var t = $("a[@href="+url+"]").attr('title');
   if (t!=undefined) {
        document.title = t + " - American Systems Registrar";
   }else{
        document.title = 'American Systems Registrar';
   }
}
function showPage(ret){
    $('#content').html(ret);
	$("a", $("#content")).click(function(){
		loadPage($(this).attr("href"));
	});
}
function getSiteRoot(){
    var ret = document.location.href.substring(0,document.location.href.lastIndexOf('/'));
    if (ret.indexOf('#')!=-1) return ret.substring(0,ret.lastIndexOf('/'))
    else return ret;
}
function loadHomePage(){
    if (window.location.hash==''){
       loadPage('#home');
    }else{
        loadPage(window.location.hash);
    }
}
render = showPage;
root = getSiteRoot();

$(document).ready(function(){
    $(".navItem:last").children("a").css("border-right","0px");
    $("#container").prepend("<div id='mask'></div><div id='loadingPanel'>Loading ... </div>");
    $("#mask").css("top", $("#content").offset().top + "px");

    $(".navItem").hover(function(event){
            showIcon($(this));
        $("#mask").show().height($("#content").height()).fadeTo('fast',0.8);
        if($(this).find("ul").size()==1){
            $(this).children("ul").show();
            return false;
		}
        },function(){
            if (!loading) $("#mask").hide();
            $(this).children("ul").fadeOut();
            if (navID!=$(this).attr('id')) hideIcon($(this));
     });
    $(".navItem a").focus(function(event){event.target.blur()});
    $(".navItem").click(function(event){
        $("#mask").show().height($("#content").height()).fadeTo('fast',0.8);
        if($(this).find("ul").size()==1){
            $(this).children("ul").show();
            return false;
        }else{
            loadPage($(this).children("a").attr("href"));
        }
    });
    $(".navItem").each(function(){
        $(this).children("a").children("img").css("left",$(this)[0].clientWidth/2 - 26+"px");
        $(this).children("ul").css("left", $(this)[0].clientWidth/2 - 26+"px").each(function(i,o){
            $(this).prepend("<li style='border-left: 1px solid white; height:12px;'></li>");
        });
    });
    $(".submenu a").click(function(event){
       event.stopPropagation();
       loadPage($(this).attr('href'));
       $(this).parents("ul.submenu").hide();
    });
    if (typeof(FCKeditor_OnComplete)!='function') {
        loadHomePage();
    }
	$("a", $("#footer")).click(function(){
		loadPage($(this).attr("href"));
	});
	$("a", $("#siteInfo")).click(function(){
		loadPage($(this).attr("href"));
	});

});
