function __displaySubMenuRegion() {
    var smbR = document.getElementById("subMenuBar-region");
    var hide = document.getElementById("__displayer-hide");
    var show = document.getElementById("__displayer-show");

    if(hide.style.display == "") {
        smbR.style.display = hide.style.display = "none";
        show.style.display = "";
    }
    else {
        show.style.display = 'none';
        smbR.style.display = hide.style.display = "";
    }
}

//TODO: Não pode deixar fixo, tem que ser no propertie.
function deleteAttendment(contextPath, idAttendment) {
	if(confirm('Deseja realmente excluir?')) {
		var url = contextPath + "/Attendment.do?command=delete&attendmentVO.id=" + idAttendment;

		SPAjax.fill(url, "__singlepoint-scrap-book");
	}
}

function showScraps(contextPath) {
	var url = contextPath + "/Attendment.do?command=showScraps";

	SPAjax.fill(url, "__singlepoint-scrap-book");
}

function openAttendmentWindow(contextPath, idAttendment) {
	var params = "command=view&isPopUp=true&attendmentVO.id=" + idAttendment;

	openModal(contextPath + "/Attendment.do?" + params, "modal = yes, dialog = yes, top = 100, left = 100, width = 800px, height = 500px, scrollbars = yes, status = no, dependent = yes");
}

var tooltip = document.createElement("DIV");

with(tooltip.style) {
	backgroundColor = "#FEFFC8";
	width = "275px";
	fontFamily = "Arial, Verdana";
	fontSize = "11px";
	id = "__evt_tooltip";
	display = "block";
	zIndex = "999";
	position = "absolute";
	borderWidth = "1px";
	borderColor = "#000000";
	borderStyle = "solid";
	textAlign = "justify";
	paddingTop = paddingBottom = paddingLeft = paddingRight = "5px";
}

function moveMenuLeft() {
	if(rightMenus.length > 0) {
        var menus = SP.$id("menuContent");
        var firstChild = menus.firstChild;        
        
        leftMenus.push(firstChild.innerHTML);
        menus.removeChild(firstChild);
        
        var newChildContent = rightMenus.shift();
        var newChild = document.createElement("div");
        newChild.innerHTML = newChildContent;
        
        menus.appendChild(newChild);
    }
}

function moveMenuRight() {
	if(leftMenus.length > 0) {
        var menus = SP.$id("menuContent");
        var lastChild = menus.lastChild;

        rightMenus.unshift(lastChild.innerHTML);
        menus.removeChild(lastChild);
        
        var newChildContent = leftMenus.pop();
        var newChild = document.createElement("div");
        newChild.innerHTML = newChildContent;
        
        menus.insertBefore(newChild,menus.firstChild);
    }
}

function toggleAppLoader(isToShow) {
	if(isToShow == null) {
		var visible = SP.$id("__appLoader").style.display != "none";
		
		toggleAppLoader(! visible);
	}
	else {
		SP.$id("__appLoader").style.display = isToShow ? "" : "none";
	}
}

var __processing = false;
function __doSave(command, preSubmit) {
	if(__processing) {
		alert("Processando...");
	}
	else {
		if(preSubmit == null || preSubmit()) {
			SP.executeCommand(command);
		}
	}
}

function __toggle(obj, show) {
	if(typeof(obj) == "string") {
		obj = SP.$id(obj);
	}
	
	obj.style.display = show ? "" : "none";
}

function __resizeWindowTo(width, height) {
	window.resizeTo(width, height);
	window.moveTo((window.screen.width - width) / 2, (window.screen.height - height) / 2);
}

function focusBtnFind(){
	SP.$id("__btnFind").focus();
}

/**
 * @author andre-matteo
 * Para utilizar botão "Fechar" com a função close window coloque o id do botão como "closeWindowButton"
 * assim ele nao irá desabilitar o mesmo.
 */
function disabledAllForm() {
	var i = 0;
	var imgs = SP.$tag("img");
	var links = SP.$tag("a");
	for(var index = 0; index < imgs.length; index++) {
	   var img = imgs[index];
	   img.onclick = function() {};
	}
	for(var index = 0; index < links.length; index++) {
	   var link = links[index];
	   if (link.id != "linkToFile") {
		   link.href = "#";
		   link.onclick = "";
	   }
	}			
	for(i = 0; i < document.forms[0].length; i++) {
		if (document.forms[0].elements[i].id != "closeWindowButton") {
			document.forms[0].elements[i].disabled = true;
		}
	}
	
}

function toMap(obj) {
	var map = new Map();
	
	if(obj) {
		for(var index = 0; index < obj.attributes.length; index++) {
			var attr = obj.attributes[index].nodeName;
			var val = obj.attributes[index].nodeValue;
			
			if(attr.indexOf("_") == 0) {
				map.put(attr.substring(1), val);
			}
		}
	}
	
	return(map);
}
/**
 * @author andre-matteo
 * Remove acentos.
 * Deve ser passado o valor e não objeto!
 * 
 */
function removeAccent(objResp) {  
	 var varString = new String(objResp);  
	 var stringAcentos = new String('àâêôûãõáéíóúçüÀÂÊÔÛÃÕÁÉÍÓÚÇÜ');  
	 var stringSemAcento = new String('aaeouaoaeioucuAAEOUAOAEIOUCU');  
	   
	 var i = new Number();  
	 var j = new Number();  
	 var cString = new String();  
	 var varRes = '';  
	   
	 for (i = 0; i < varString.length; i++) {  
		 cString = varString.substring(i, i + 1);  
		 for (j = 0; j < stringAcentos.length; j++) {  
			 if (stringAcentos.substring(j, j + 1) == cString){  
				 cString = stringSemAcento.substring(j, j + 1);  
			 }  
		 }  
		 varRes += cString;  
	 }
	 return varRes;
}  

/**
 * @author andre-matteo
 * pega parametro da URL
 * getUrlVars()["param1"];
 * 
 */
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function moveSelectOptions(from, to, isAll) {
	from = typeof(from) == "string" ? SP.$id(from) : from;
	to = typeof(to) == "string" ? SP.$id(to) : from;
	isAll = isAll || false;
	var toOpts = [];
	
	for(var index = 0; index < from.options.length; index++) {
		var opt = from.options[index];

		if(opt.selected || isAll) {
			toOpts.push(new Option(opt.text, opt.value));
			from.remove(index--);
		}
	}
	
	while(to.options.length > 0) {
		var opt = to.options[0];
		toOpts.push(new Option(opt.text, opt.value));
		to.remove(0);
	}
	
	toOpts.sort(function(a, b) {
		var a = a.text.toLowerCase();
		var b = b.text.toLowerCase();
		
		if(a == b) {
			return(0);
		}
		
		return(a < b ? -1 : 1);
	});
	
	for(var index = 0; index < toOpts.length; index++) {
		to.add(toOpts[index]);
	}
}

function __isEmailValid(email) {
	var regexp = new RegExp("^\\w([-.]|\\w)*\\w@(\\w([-.]?\\w)*\\.[A-Za-z]+|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})$");
	
	if(! email) {
		return(false);
	}

	return(email.match(regexp) ? true : false);
}

function __contains(o, elem) {
	for(var param in o) {
		if(param == elem) {
			return(true);
		}
	}

	return(false);
}

function __dateFormat(pattern) {
	var res = pattern;
	
	res = res.replace("dd", this.getDate().lpad(2));
	res = res.replace("MM", (this.getMonth() + 1).lpad(2));
	res = res.replace("yyyy", this.getYear());
	res = res.replace("HH", this.getHours().lpad(2));
	res = res.replace("mm", this.getMinutes().lpad(2));
	res = res.replace("ss", this.getSeconds().lpad(2));
	
	return(res);
}

$(document).ready(function() {
    $("form:first").submit(function() {
        $("input[type=button],input[type=submit],button").attr("disabled","true");
		$(this).append("<img id=\"loading\" src=\"" + SP.context + "/images/ajax-loader.gif\" border=\"0\" />");
		$("#loading").addClass('loading');
		$("#loading").css("top", document.body.scrollTop + ($(window).height() / 2));
    });
});
