/**javascript file to create the functions for yabiru.js for the menu of 
 * yabiru login and other things
 */
/**
 * url to load what to load where number i dont know always 0
 */

function loadItemMenu(urlToLoad, divIdDestination, number) {
	data = new Array();
	$.ajax({
		url : urlToLoad,
		data : data,
		cache : false,
		type : "POST",
		async : false,
		success : function(htmlAnswer) {
			// clean the div with the options
			// $("#yabiruUpperMenu").empty();
			// clean the option here
			$("#yabiruOption").empty();

			// append the new option here
			$("#yabiruOption").append(htmlAnswer);

		}
	});

}

function mc_logout() {
	doYabiruLogout();
}

function  doYabiruLogout() {
	var isUserLoggedIn = isUserLoggedInYabiru();
	var dataForLogout = "type=LOGOUT_USER";
	var logoutAnswer = "";
	if (isUserLoggedIn) {
		$.ajax({
			url : "controller/ajaxYabiru.php",
			data : dataForLogout,
			cache : false,
			async : true,
			type : "GET",
			success : function(logoutAnswer) {
				showUiForNonYabiruUser();
			}
		});
	}
}

/**
 * 
 */
function doYabiruLogin() {
	var user = $("#user").val();
	var password = $("#password").val();
	var checkUser = (user.length > 0);
	var checkPassword = (password.length > 0);
	var userState = 0;
	var dataForLogin = "";
	var loginAnswerSuccesful = false;

	if (checkUser && checkPassword) {
		// valid user and password
		var isLoggedUser = isUserLoggedInYabiru();
		var loginAnswerString = "0";
		var loginSuccessful = false;
		// isLoggedUser=true;
		if (!isLoggedUser) {
			dataForLogin = "type=LOGIN_USER&user=" + user + "&password="
					+ password;
			$.ajax({
				url : "controller/ajaxYabiru.php",
				data : dataForLogin,
				cache : false,
				type : "GET",
				async : false,
				success : function(answer) {
					if (answer == "1") {
						loginAnswerSuccesful = true;
					}
					if (loginAnswerSuccesful) {
						showUiForYabiruUser();
						initYabiruMenu();
					}
					if(!loginAnswerSuccesful){
						//invalid user or password
						alert("Login failed ");
					}
				}
			});
			;
		} else {
			// user logged here
			showUiForYabiruUser();
		}
	}else{
		alert("Need to user user and password");
	}

}
/**
 * check if user is logged in yabiru
 * 
 * @returns {Boolean}
 */
function isUserLoggedInYabiru() {
	var boolAnswer = false;
	var stringAnswer;
	data = "type=CHECK_LOGIN";
	$.ajax({
		url : "controller/ajaxYabiru.php",
		data : data,
		async : false,
		cache : false,
		type : "GET",
		success : function(answer) {
			stringAnswer = answer;
			if (stringAnswer == "1") {
				boolAnswer = true;
				return boolAnswer;
			}
		}
	});
	return boolAnswer;
}

function fetchUrlContent(url,type){
	var answer = "";
	$.ajax({
		url : url,
		async : false,
		cache : false,
		type : type,
		success : function(urlContent) {
			answer = urlContent;
		}
	});
	return answer;
}


function fetchUrlContent(url) {
	var answer = "";
	$.ajax({
		url : url,
		async : false,
		cache : false,
		type : "POST",
		success : function(urlContent) {
			answer = urlContent;
		}
	});
	return answer;
}

/**
 * hide yabiru login box and show the menues for the users logged
 * 
 */
function showUiForYabiruUser() {

	$("#nota").empty();

	// remove yabiruUpperMenu
	// $("#yabiruUpperMenu").remove();
	// remove yabiru menu
	// $("#yabiruMenu").remove();
	// remove the option of yabiru option
	// $("#yabiruOption").remove();

	// show user information
	/*var userInformation = fetchUrlContent("cajas/caja_loginInfo.php");
	$("#nota").append("<div id=\"yabiruUpperMenu\"></div>");
	$("#yabiruUpperMenu").append(userInformation);*/

	// show the menu for the user
	var autogestionMenu = fetchUrlContent("cav/mc_autogestion.php");
	$("#nota").append("<div id=\"yabiruMenu\"></div>");
	$("#yabiruMenu").append(autogestionMenu);

	// add a div for the options
	$("#nota").append("<div id=\"yabiruOption\"></div>");

	// hide the yabiruLogin form here
	$("#cta_yabiru").hide();
}


/**
 * show yabiru login form show yabiru
 */
function showUiForNonYabiruUser() {
	// remove yabiruUpperMenu
	$("#yabiruUpperMenu").remove();
	// remove yabiru menu
	$("#yabiruMenu").remove();
	// remove the option of yabiru option
	$("#yabiruOption").remove();
	// show user information

	$("#cta_yabiru").show();

}
function showYabiruOptionFromSection(userLoggedIn, sectionId) {
	adaptSectionUiToInternet();
	switch (sectionId) {
	case 0:
		if (userLoggedIn) {		
			showUiForYabiruUser();
			// show datos personales here
			loadItemMenu('cav/mc_datospersonales.php', 'yabiruOption', 0);
		} else {
			showUiForNonYabiruUser();
		}
		break;
	case 1:
		if (userLoggedIn) {
			showUiForYabiruUser();
		} else {
			showUiForNonYabiruUser();
		}
		break;
	}

}

function showYabiruOptionFromInternet(userLoggedIn, sectionId) {
	switch (sectionId) {
	case 0:
		if (userLoggedIn) {
			showUiForYabiruUser();
			// show datos personales here
			loadItemMenu('cav/mc_datospersonales.php', 'yabiruOption', 0);
		} else {
			showUiForNonYabiruUser();
		}
		break;
	case 1:
		if (userLoggedIn) {
			showUiForYabiruUser();
		} else {
			showUiForNonYabiruUser();
		}
		break;
	}

}

function showYabiruOption(seccionId) {

	var beforeState = $("#state").val();
	// remove the div with state
	$(".toRemove").remove("#state");
	// add the new state
	$("#contenedor").append(createHiddenDiv('INTERNET'));

	var userLoggedIn = isUserLoggedInYabiru();

	if (beforeState == "INTERNET") {
		showYabiruOptionFromInternet(userLoggedIn, seccionId);
	} else if (beforeState == "SECTION") {
		showYabiruOptionFromSection(userLoggedIn, seccionId);
	}
}

function showPersonalDataFromMenu() {
	// remove yabiruUpperMenu
	$("#yabiruUpperMenu").remove();
	// remove yabiru menu
	$("#yabiruMenu").remove();
	// remove the option of yabiru option
	// $("#yabiruOption").remove();

	$("#yabiruOption").clear();

	var userInformation = fetchUrlContent("cav/mc_datospersonales");
	$("#yabiruOption").append(userInformation);

	if (!isUserLoggedInYabiru) {
		$("#cta_yabiru").show();
	}
}
function showAutogestionFromMenu() {
	// remove yabiruUpperMenu
	$("#yabiruUpperMenu").remove();
	// remove yabiru menu
	$("#yabiruMenu").remove();
	// remove the option of yabiru option
	$("#yabiruOption").remove();

	// show user information
	var userInformation = fetchUrlContent("cajas/caja_loginInfo.php");
	$("#nota").append("<div id=\"yabiruUpperMenu\"></div>");
	$("#yabiruUpperMenu").append(userInformation);

	// show the menu for the user
	var autogestionMenu = fetchUrlContent("cav/mc_autogestion.php");
	$("#nota").append("<div id=\"yabiruMenu\"></div>");
	$("#yabiruMenu").append(autogestionMenu);

	// add a div for the options
	$("#nota").append("<div id=\"yabiruOption\"></div>");

}

function adaptSectionUiToInternet(){
	var script = document.createElement( 'script' );
	script.type = 'text/javascript';
	script.src = 'include/bannerPrincipal.js';
	$("head").append(script);
	
	$("#no_banner_principal").css("visibility","");
	$("body").css("background-image","url(images/bg_yabiru.png)");
	$(".banner_secundario").hide();
	$("#slide_noticias").hide();
	$("#nota_desplegada").width("605px");
	$("#forms").show();
}
/*functions for the menu 
 * 
 * 
 */
/**from botonera return the array of nodes here
 * 
 */
function getYabiruMenu(){
	var selectionMenu="#botonera > li:eq(1) > ul:eq(0) > li:eq(1) > ul:eq(0) ";
	var jQuerySelection=$(selectionMenu);
	var answer=jQuerySelection.toArray();
	return answer;
}

function appendItem(menuNode,url,functionToExecute,textContent,id){	
	if($('#botonera li ul #'+id).length != 0){
		return false;
	}
	else{	
		var textPattern = '';
		
		var add = true;
		$('li a').each(function(index) {			
			//console.log( $(this).text().substring(0,9) + " ==  "  + "Autogesti" );
			if($(this).text().substring(0,9) === "Autogesti")
			{
				add = false;
			}
			
		  });
		
		/*if(id){
		
			textPattern='<li><a id="'+id+'">'+textContent+'</a></li>';
		}
		else{
			
			textPattern='<li><a>'+textContent+'</a></li>';
			
		}*/
		//textPattern='<li><a id="'+id+'">'+textContent+'</a></li>';
		if (add){
			$(menuNode).append('<li><a href="'+url+'" onclick="'+functionToExecute+'">'+textContent+'</a></li>');
		}
		
		/*
		var node=$(textPattern).find("a").attr("href",url);
		node=$(textPattern).find("a").attr("onClick",functionToExecute);
		$(menuNode).append.(node)*/
	}
	
}

function detectMenu(){
	var internetMenu=getYabiruMenu();
	$(internetMenu).append(getJqueryNodeOfMenuItem("urlNew","textContent"));
	
}

function initYabiruMenu(){

	//remove the option from menu
	$(".toRemove").remove("li");
	var isLogged=isUserLoggedInYabiru();
	
	if(isLogged){
		/*
		var functionStringForAutoGestion='doAjax("cav/mc_autogestion2.php","11","10")';
		var functionStringForDatosPersonales='doAjax("cav/mc_datospersonales.php","11","10")';
		*/
		var functionStringForAutoGestion='loadCavContent(50,10)';
		var functionStringForDatosPersonales='loadCavContent(51,10)';
		
		var domMenu=getYabiruMenu();
		appendItem(domMenu,"javascript:void(0)",functionStringForAutoGestion,"Autogesti&oacute;n","autogestion");
		//appendItem(domMenu,"javascript:void(0)",functionStringForAutoGestion,"Prueba");
		//appendItem(domMenu,"javascript:void(0)",functionStringForAutoGestion,"Datos personales");
		$("#cta_yabiru").hide();
	}
	
	
}



