/// <reference path="jquery.js"/>
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 fnLoadPage(pageID){
    $.post(root+"/page.class.php",
          {
            action: "2",
            pageid: pageID
          },
		  function(ret) {
		  	var i = ret.indexOf('\x0a');
		  	var title = ret.substr(0,i);
			var content = ret.substr(i+1);
    		oEditor.SetHTML(content);
			document.title = title;
			window.location.hash = pageID;
			top.pageid = pageID;
       });
}
function doSave(){
	if (top.pageid=='') {
		alert('Please load a page to edit.');
		return false;
	}
    $.post(root+"/page.class.php",
          {
            action: "1",
            pageid: top.pageid,
            content: oEditor.GetHTML()
          }, function(ret) {
          	if (ret=='') {
				alert('The page is saved successfully!');
			}else{
				alert(ret);
			}
       });
    return false;
}
function FCKeditor_OnComplete( editorInstance ){
    editorInstance.LinkedField.form.onsubmit = doSave;
    init();
}
function init(){
	oEditor = FCKeditorAPI.GetInstance('editor') ;
	oEditor.LoadCMSPage = fnLoadPage;
	var pageID = 'home';
	if(window.location.hash!='') pageID = window.location.hash.substr(1);
	fnLoadPage(pageID);
}

var root = getSiteRoot();
top.pageid='';
var oEditor = null;

var curTab = null;
$(function(){
    $("#TabsDiv a").click(function(){
        var imgUrl ="";
        var newImgUrl = "";
        if (curTab==this) return;
        if (curTab) {
            imgUrl = $("img", curTab).attr("src");
            newImgUrl = imgUrl.replace(/.gif/, "0.gif");
            $("img", curTab).attr("src", newImgUrl);
        }
        curTab = this;
        imgUrl = $("img",this).attr("src");
        newImgUrl = imgUrl.replace(/0.gif/, ".gif");
        $("img", curTab).attr("src", newImgUrl);
    });
    $("#TabsDiv a").focus(function(){
        this.blur();
    });
    curTab = $("#TabsDiv a:first")[0];
});