//On load page, init the timer which check if the there are anchor changes each 300 ms
$().ready(function(){
	setInterval("checkAnchor()", 300);
});
var currentAnchor = null;
if (typeof $.cookie('session_id') == 'udefined') { // no cookie, set it to blank
  $.cookie('session_id', '', {expires: 10});
}
//Function which chek if there are anchor changes, if there are, sends the ajax petition
function checkAnchor(){
	//Check if it has changes
	if(currentAnchor != document.location.hash){
		currentAnchor = document.location.hash;
		//if there is no anchor, we load the default page
		if(!currentAnchor)
			query = "page_name=_top";
		else
		{
			//Creates the  string callback. This converts the url URL/#main&id=2 in URL/?section=main&id=2
			var splits = currentAnchor.substring(1).split('&');
			//Get the section
			var section = splits[0];
			delete splits[0];
			//Create the params string
			var params = splits.join('&');
			var query = "session_id=" + $.cookie('session_id') + "&page_name=" + section + params;
		}
		//Send the request
		$.post("loadContent.php",query, function(data){
			if ( $.browser.msie ) {
				xml = new ActiveXObject("Microsoft.XMLDOM");
				xml.async = false;
				xml.loadXML(data);
			} else {
				xml = data;
			}
			//$('#edit_bar').hide('fast');
			$(xml).find("content").each(function() {
                        	newcontent = $(this).text();
                        	contentID = $(this).attr("id");
				$("#" + contentID).html(newcontent);
			});
			$(xml).find("session_id").each(function() {
                                $.cookie('session_id', $(this).text(), {expires: 10});
			});
		});
	}
}

//START Get all FORM values
function formGET(formID) {
	var getArgs = '';

	var Args = document.getElementById(formID);
	for (var i=0; i<Args.length; i++) {
		getArgs = getArgs + '&' + Args.elements[i].name + '=' + $.URLEncode(Args.elements[i].value);
	}

	return getArgs;
}
//END Get all FORM values

function sendLogin(action){
        if (action == undefined) {
		$('#loginProgress').show('fast');
		query = formGET('user_login');
		sendhome = "no";
        } else {
		query = '&action=' + action
		sendhome = "yes";
	}
        query = query + '&session_id=' + $.cookie('session_id')
	//Send the request
	$.post("loginUser.php",query, function(xml){
		$('#loginProgress').hide('fast');
		$("#loginPopUp").hide('slow');
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
		if (sendhome == "yes") {
			window.location = "index.php#_top";
		}
	});


}

function checkLoginEnter (e){ //e is event object passed from function invocation
	var characterCode; // literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		//document.forms[0].submit() //submit the form
		sendLogin();
		return false;
	}
	else{
	return true;
	}

}


function sendEditor(category){
        hash = document.location.hash;
        current_page_name = hash.substring(1).split('&');
        if (category == undefined) {
		category = 'unknown';
	}
        query = '&session_id=' + $.cookie('session_id') + '&category=' + category + '&current_page_name=' + current_page_name[0];
	//Send the request
	$.post("editpage.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});


}

function editPage(){
        hash = document.location.hash;
        page_name = hash.substring(1).split('&');
        query = '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
	//Send the request
	$.post("editpage.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});


}

// Resource handling

function manageResources(){
        hash = document.location.hash;
        page_name = hash.substring(1).split('&');
        query = '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
	//Send the request
	$.post("addResources.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});


}

function resourceFileInfo(fileUrl){
	document.getElementById( 'x_resource_url' ).value = fileUrl ;
}

function browseServer(page_id){
	var finder = new CKFinder();
	finder.BasePath = '/ckfinder/' ;
	finder.Id = page_id ;
	finder.SelectFunction = resourceFileInfo ;
	//finder.Create() ;
	finder.Popup() ;
}

function addNewResource(page_id){
	query = formGET('new_resource');
        hash = document.location.hash;
        page_name = hash.substring(1).split('&');
        query = query + '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
	//Send the request
	$.post("addResources.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});

}

function removeResourceAlert(resource_id, resource_name){
	var removeConfirm = confirm("You are about to remove the resource '" + resource_name + "'?");
	if (removeConfirm == true){
        	hash = document.location.hash;
        	page_name = hash.substring(1).split('&');
        	query = '&resource_id=' + resource_id + '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
		//Send the request
		$.post("addResources.php",query, function(xml){
			$(xml).find("content").each(function() {
                       		newcontent = $(this).text();
                       		contentID = $(this).attr("id");
				$("#" + contentID).html(newcontent);
			});
			$(xml).find("session_id").each(function() {
				$.cookie('session_id', $(this).text(), {expires: 10});
			});
		});
        }
}

function deletePage(){
       	hash = document.location.hash;
       	page_name = hash.substring(1).split('&');
	var deleteConfirm = confirm("You are about to remove the page named '" + page_name[0] + "'?");
	if (deleteConfirm == true){
        	query = '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
		//Send the request
		$.post("deletePage.php",query, function(xml){
			$(xml).find("content").each(function() {
                       		newcontent = $(this).text();
                       		contentID = $(this).attr("id");
				$("#" + contentID).html(newcontent);
			});
			$(xml).find("session_id").each(function() {
				$.cookie('session_id', $(this).text(), {expires: 10});
			});
		});
        }
}

// Manage Objects

function manageObjects(what){
        hash = document.location.hash;
        page_name = hash.substring(1).split('&');
        if (what == 'news') {
        	query = '&session_id=' + $.cookie('session_id') + '&news=1&page_name=' + page_name[0];
	} else {
        	query = '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
	}
	//Send the request
	$.post("manageObjects.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});


}

function editPageObject(object_id){
        hash = document.location.hash;
        page_name = hash.substring(1).split('&');
        query = '&session_id=' + $.cookie('session_id') + '&object_id=' + object_id + '&page_name=' + page_name[0];
	//Send the request
	$.post("editObjects.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});


}

function updatePageObject(new_object_content){
        hash = document.location.hash;
	query = formGET('page_object');
        page_name = hash.substring(1).split('&');
        //var new_object_content = CKEDITOR.instances.object_content.getData();
        query = query + '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0] + '&new_object_content=' + $.URLEncode(new_object_content);
	//Send the request
	$.post("manageObjects.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	//newcontent = $(this).text();
                       	//contentID = $(this).attr("id");
			//$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});


}

function removeObjectAlert(object_id, object_name){
	var removeConfirm = confirm("You are about to remove the object '" + object_name + "'?");
	if (removeConfirm == true){
        	hash = document.location.hash;
        	page_name = hash.substring(1).split('&');
        	query = '&object_id=' + object_id + '&remove_object=1&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
		//Send the request
		$.post("manageObjects.php",query, function(xml){
			$(xml).find("content").each(function() {
                       		newcontent = $(this).text();
                       		contentID = $(this).attr("id");
				$("#" + contentID).html(newcontent);
			});
			$(xml).find("session_id").each(function() {
				$.cookie('session_id', $(this).text(), {expires: 10});
			});
		});
        }
}

function addNewObject(){
	query = formGET('new_object');
        hash = document.location.hash;
        page_name = hash.substring(1).split('&');
        query = query + '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
	//Send the request
	$.post("manageObjects.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});

}

function filterObjects(){
	query = $('#filter_objects').serialize();
        hash = document.location.hash;
        page_name = hash.substring(1).split('&');
        query = query + '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
	//Send the request
	$.post("manageObjects.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});

}

function displayObject(object_id){
	query = '&object_id=' + object_id;
        $.post("displayObject.php",query,function(data){
                        if ( $.browser.msie ) {
                                xml = new ActiveXObject("Microsoft.XMLDOM");
                                xml.async = false;
                                xml.loadXML(data);
                        } else {
                                xml = data;
                        }
                $(xml).find("content").each(function() {
                        newcontent = $(this).text();
                        contentID = $(this).attr("id");
                        $("#" + contentID).html(newcontent);
                        $('#object').makeFloat({x:255, y:125, speed:'fast'});
		        $('#object').show('fast');
                });
	});
}

function displayNews(news_id){
	query = '&news_id=' + news_id;
        $.post("displayNews.php",query,function(data){
                        if ( $.browser.msie ) {
                                xml = new ActiveXObject("Microsoft.XMLDOM");
                                xml.async = false;
                                xml.loadXML(data);
                        } else {
                                xml = data;
                        }
                $(xml).find("content").each(function() {
                        newcontent = $(this).text();
                        contentID = $(this).attr("id");
                        $("#" + contentID).html(newcontent);
                        $('#object').makeFloat({x:255, y:125, speed:'fast'});
		        $('#object').show('fast');
                });
	});
}

function foobar(foojack){
        query = '&foojack=' + $.URLEncode(foojack);
	//Send the request
	$.get("foojack.php",query, function(xml){
		$(xml).find("content").each(function() {
		});
	});


}

// anchor stuff
function manageAnchors(what){
        hash = document.location.hash;
        page_name = hash.substring(1).split('&');
        if (what == 'news') {
        	query = '&session_id=' + $.cookie('session_id') + '&news=1&page_name=' + page_name[0];
	} else {
        	query = '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
	}
	//Send the request
	$.post("manageAnchors.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});


}

function removeAnchorAlert(anchor_id, anchor_description){
	var removeConfirm = confirm("You are about to remove the anchor '" + anchor_description + "'?");
	if (removeConfirm == true){
        	hash = document.location.hash;
        	page_name = hash.substring(1).split('&');
        	query = '&anchor_id=' + anchor_id + '&remove_anchor=1&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
		//Send the request
		$.post("manageAnchors.php",query, function(xml){
			$(xml).find("content").each(function() {
                       		newcontent = $(this).text();
                       		contentID = $(this).attr("id");
				$("#" + contentID).html(newcontent);
			});
			$(xml).find("session_id").each(function() {
				$.cookie('session_id', $(this).text(), {expires: 10});
			});
		});
        }
}

function addNewAnchor(){
	query = formGET('new_anchor');
        hash = document.location.hash;
        page_name = hash.substring(1).split('&');
        query = query + '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
	//Send the request
	$.post("manageAnchors.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});

}

function editAnchor(anchor_id){
        hash = document.location.hash;
        page_name = hash.substring(1).split('&');
        query = '&session_id=' + $.cookie('session_id') + '&edit_anchor=1&anchor_id=' + anchor_id + '&page_name=' + page_name[0];
	//Send the request
	$.post("manageAnchors.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});


}

function updateAnchor(){
        hash = document.location.hash;
	query = formGET('ea');
        page_name = hash.substring(1).split('&');
        query = query + '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
	//Send the request
	$.post("manageAnchors.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});


}


// user stuff
function manageUsers(){
        hash = document.location.hash;
        page_name = hash.substring(1).split('&');
        query = '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
	//Send the request
	$.post("manageUsers.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});


}

function removeUserAlert(uid, username){
	var removeConfirm = confirm("You are about to remove the user '" + username + "'?");
	if (removeConfirm == true){
        	hash = document.location.hash;
        	page_name = hash.substring(1).split('&');
        	query = '&uid=' + uid + '&remove_user=1&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
		//Send the request
		$.post("manageUsers.php",query, function(xml){
			$(xml).find("content").each(function() {
                       		newcontent = $(this).text();
                       		contentID = $(this).attr("id");
				$("#" + contentID).html(newcontent);
			});
			$(xml).find("session_id").each(function() {
				$.cookie('session_id', $(this).text(), {expires: 10});
			});
		});
        }
}

function addUser(){
	query = formGET('new_user');
        hash = document.location.hash;
        page_name = hash.substring(1).split('&');
        query = query + '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
	//Send the request
	$.post("manageUsers.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});

}

function editUser(uid){
        hash = document.location.hash;
        page_name = hash.substring(1).split('&');
        query = '&session_id=' + $.cookie('session_id') + '&edit_user=1&uid=' + uid + '&page_name=' + page_name[0];
	//Send the request
	$.post("manageUsers.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});


}

function updateUser(){
        hash = document.location.hash;
	query = formGET('edit_user');
        page_name = hash.substring(1).split('&');
        query = query + '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
	//Send the request
	$.post("manageUsers.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});


}


// permission stuff
function managePermissions(){
        hash = document.location.hash;
        page_name = hash.substring(1).split('&');
        query = '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
	//Send the request
	$.post("managePermissions.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});


}

function removePermissionAlert(id, username){
	var removeConfirm = confirm("You are about to remove permissions on current page for user '" + username + "'?");
	if (removeConfirm == true){
        	hash = document.location.hash;
        	page_name = hash.substring(1).split('&');
        	query = '&id=' + id + '&remove_pid=1&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
		//Send the request
		$.post("managePermissions.php",query, function(xml){
			$(xml).find("content").each(function() {
                       		newcontent = $(this).text();
                       		contentID = $(this).attr("id");
				$("#" + contentID).html(newcontent);
			});
			$(xml).find("session_id").each(function() {
				$.cookie('session_id', $(this).text(), {expires: 10});
			});
		});
        }
}

function addPermission(){
	query = formGET('new_pid');
        hash = document.location.hash;
        page_name = hash.substring(1).split('&');
        query = query + '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
	//Send the request
	$.post("managePermissions.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});

}

function editPermission(id){
        hash = document.location.hash;
        page_name = hash.substring(1).split('&');
        query = '&session_id=' + $.cookie('session_id') + '&edit_pid=1&id=' + id + '&page_name=' + page_name[0];
	//Send the request
	$.post("managePermissions.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});


}

function updatePermission(){
        hash = document.location.hash;
	query = formGET('edit_pid');
        page_name = hash.substring(1).split('&');
        query = query + '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
	//Send the request
	$.post("managePermissions.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});


}

// Opermission stuff
function manageOPermissions(object_id){
        hash = document.location.hash;
        page_name = hash.substring(1).split('&');
        query = '&session_id=' + $.cookie('session_id') + '&object_id=' + object_id + '&page_name=' + page_name[0];
	//Send the request
	$.post("manageOPermissions.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
                $('#oPerm').makeFloat({x:200, y:125, speed:'fast'});
		$('#oPerm').show('fast');
	});


}

function removeOPermissionAlert(id, username){
	var removeConfirm = confirm("You are about to remove permissions on current page for user '" + username + "'?");
	if (removeConfirm == true){
        	hash = document.location.hash;
        	page_name = hash.substring(1).split('&');
        	query = '&id=' + id + '&remove_pid=1&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
		//Send the request
		$.post("manageOPermissions.php",query, function(xml){
			$(xml).find("content").each(function() {
                       		newcontent = $(this).text();
                       		contentID = $(this).attr("id");
				$("#" + contentID).html(newcontent);
			});
			$(xml).find("session_id").each(function() {
				$.cookie('session_id', $(this).text(), {expires: 10});
			});
		});
        }
}

function addOPermission(){
	query = formGET('new_pid');
        hash = document.location.hash;
        page_name = hash.substring(1).split('&');
        query = query + '&session_id=' + $.cookie('session_id') + '&page_name=' + page_name[0];
	//Send the request
	$.post("manageOPermissions.php",query, function(xml){
		$(xml).find("content").each(function() {
                       	newcontent = $(this).text();
                       	contentID = $(this).attr("id");
			$("#" + contentID).html(newcontent);
		});
		$(xml).find("session_id").each(function() {
			$.cookie('session_id', $(this).text(), {expires: 10});
		});
	});

}


