
//BASE URL's
var normalBaseURL = '/webapp/site/go?where=' ;
var adminBaseURL = '/webapp/admin/' ;

//Current ajax loaded page title
var title = "";

//This function is to be called whenever the user
//Chooses a new page and an AJAX request is made
function onAjaxPageRequest(){
	//Hides the current content from the user
    $("#title").css("display", "none");
    $("#content").css("display", "none");
    
    //Shows the spinning wheel
    $("#wheel").css("display", "block");
}

//This function is called whenever an AJAX request 
//is complete
function onAjaxPageRequestComplete(){	
	//Updates the new page title
    $("#title-content").html(title);

	//Hides the spinning wheel from the user
    $("#wheel").css("display", "none");
    
	//Shows the content to the user
    $("#title").css("display", "block");
    $("#content").css("display", "block");
}

//Request a new page via AJAX and puts the result on the "content" div
//page = name of the page to be loaded
//title = page title to be loaded
function superRequestPage(page, new_title, base_url, onCompleteHandler, onRequestHandler, params){
	//alert(page  + " " + new_title +  " " + base_url + " " + onCompleteHandler + " " + onRequestHandler);
	//If no page is passed, the default one is introduction
	if (page == null) page = 'introduction';

	//Calculates the url for the page
	var url = ROOT + base_url + page;
	//alert(url);

	//If no new title is passed, then uses de default title
	if (new_title == null) new_title = 'Fluir';

	//Saves the new title
	title = new_title;

	//If no on complete handler is passed, the default is onAjaxPageRequestComplete
	if (onCompleteHandler == null) onCompleteHandler = onAjaxPageRequestComplete;

	//If no on request handler is passed, the default handler is onAjaxPageRequest
	if (onRequestHandler == null) onRequestHandler = onAjaxPageRequest;

	//Calls the on start handler
    onRequestHandler();
    
    //Makes the ajax request and updates the "content" div
    $("#content").load(url, params, onCompleteHandler);    
}

//Normal request page
function requestPage(page, new_title, onCompleteHandler, onRequestHandler, base_url){
	if (base_url == null) base_url = normalBaseURL;
	
	superRequestPage(page, new_title, base_url , onCompleteHandler, onRequestHandler);
}
//Requests a project to be shown
function requestProject(id){
	var params = {project_id: id};

	superRequestPage('project', 'Projecto', '/webapp/site/' , onRequestProjectPageComplete, null, params);
}

//Project request complete handler
function onRequestProjectPageComplete(){
	onAjaxPageRequestComplete();

	//Initialize ImageBox	
	$.ImageBox.init(
		{
	        loaderSRC: ROOT + '/images/wheel.gif',
	        closeHTML: '<img style="border: 0px;" src="' + ROOT + '/images/imagebox/fechar.gif"/>'
	    }
	);                        	
}

//Administration request page
function adminRequestPage(page, new_title, onCompleteHandler, onRequestHandler, params){
	//If no onCompleteHandler is passed, calls the default one for the administration
	if (onCompleteHandler == null) onCompleteHandler = onAdminPageRequestComplete;

	superRequestPage(page, new_title, adminBaseURL, onCompleteHandler, onRequestHandler, params);
}

//Default administration page request complete event
function onAdminPageRequestComplete(){
	//Calls the default on page request complete
	onAjaxPageRequestComplete();
	
	
	//Puts an event handler on the remove button, in case it exists
	if ($("#removeSubmit") != null){
		$("#removeSubmit").click(function(){
			return confirm("Deseja mesmo remover os itens selecionados?" );
		});
	}

	//Options for the AJAX form
	var options = {
		target: "#content",
		success: onAdminPageRequestComplete
	};


	//makes the form, an AJAX form
	$('#form').ajaxForm(options);	
}

function onProjectRequestPageComplete(){
	//Calls the default on page request complete
	onAjaxPageRequestComplete();
}

function newImage(){
	$("#new_image").slideDown(500);
	$("#fotoButton").attr("disabled", "disabled");
}

function cancelNewFoto(){
	$("#new_image").slideUp(500);
	$("#fotoButton").removeAttr("disabled");	
}



//Handler for services page request
var visible = false;
function onServicesPageRequestComplete(){
	onAjaxPageRequestComplete();

	$("#use").click(function(){
		//$("#service-company-description").toggle();
		
		if (visible){
			$("#service-company-description").fadeOut("slow");	
		}
		else{
			$("#service-company-description").fadeIn("slow");	
		}
		visible = !visible;
	});	
}

//Handler for the contact page request complete
function onContactPageRequestComplete(){

	//Calls the default page request complete
	onAjaxPageRequestComplete();
	
	//Options for the AJAX form
	var options = {
		target: "#content",
		beforeSubmit: onAjaxPageRequest,
		success: onAjaxPageRequestComplete
	};

	//Makes the contact form an AJAX form
	$('#contactForm').ajaxForm(options);		
}



//Handler for the contact administration page
function onAdminContactsRequestComplete(){
	onAdminPageRequestComplete();

}



















//Makes an AJAX request
function ajaxRequest(page, params, newTitle){
	title = newTitle;
    startAjaxRequestEffects();
    $("#content").load(page, params, onAjaxRequestCompleteEffects);    
}











function ajaxPost(page, params){
    startAjaxRequestEffects();
    $("#content").post(page, params, onAjaxRequestCompleteEffects);    
}


function showProject(project){
	var projectLink = ROOT + '/webapp/site/project?project_id=';

    startAjaxRequestEffects();
    $("#content").load(projectLink + project, onAjaxRequestCompleteEffects);
}




function edit(id, url, newTitle){	
	if (url == null) url = "/webapp/admin/showContact";
	var rurl = ROOT + url;	
	
	title = newTitle;
	startAjaxRequestEffects();
	$("#content").load(rurl, {"id": id}, startEditForm);   	
	
}

function startEditForm(){
	onAjaxRequestCompleteEffects();

	var options = {
		target: "#content",
		beforeSubmit: startAjaxRequestEffects,
		success: onAjaxRequestCompleteEffects
	};
	
	$('#saveForm').ajaxForm(options);
	
	
	$("#title-content").html(title);
}
