// @+@+@+@+@+@+@+@+@+@+@+@+@+@+@+@+@+@+@+@+@+@+@+@+@+@+@+@+@ //
// ARQUIVO DE FUNCÍES GENERICAS EM JS
// BY: OBJECT@
// AT: 18/11/2010
// AUTHOR: ACELMAR

var cBox = 10;
function addCorners( obj ){
	var cor = obj.id;
	var alt = $(obj).attr("alt");
	var wd = $(obj).width() - (cBox*2);
	var hd = $(obj).height() + (cBox*2);
	var htmlText = $(obj).html();
	//alert(htmlText);
	$(obj).html("");
	
	var objContenter = document.createElement('div');
	for(var ld=1; ld <= 4; ld++){
		var objDiv = document.createElement('div');
		objContenter.appendChild(objDiv);
		objDiv.className = "box"+cor+"C boxC boxC"+ld;
		var objDivHV = document.createElement('div');
		objContenter.appendChild(objDivHV);
		if(ld == 1 || ld == 3){
			objDivHV.className = "box"+cor+"H boxH boxF"+ld;
		} else {
			objDivHV.className = "box"+cor+"V boxV boxF"+ld;
		}
	}
	var objDivContent = document.createElement('div');
	objDivContent.className = "box"+cor+" boxContent";
	$(objDivContent).append(htmlText);
	objContenter.appendChild(objDivContent);
	objContenter.className = "boxContenter";
	if(alt != undefined){
		if(alt.indexOf("|") >= 0){
			var altX = alt.split("|");
			wd = altX[0];
			hd = altX[1];
			if(wd == "auto"){
				$(objContenter).width("auto");
			} else {
				$(objContenter).width(wd - (cBox*2));
			}
			if(hd == "auto"){
				$(objContenter).height("auto");
			} else {
				$(objContenter).height(hd - (cBox*2));
			}
		}
	}
	//$(objContenter).width(wd - (cBox*2));
	//$(objContenter).height(hd - (cBox*2));
	//$("#rec").append(wd + " x " + hd + " = " + alt + "<br>");
	obj.appendChild(objContenter);
	$(obj).width(wd);
	$(obj).height(hd);
}

function addButton(obj){
	var label = $(obj).html();
	$(obj).html("");
	var cor = $(obj).attr("id");
	var wbutton = $(obj).width() - 5;
	var abreDiv = document.createElement('div');
	obj.appendChild(abreDiv);
	abreDiv.className = "button"+cor+" buttonAbre";
	var fechaDiv = document.createElement('div');
	obj.appendChild(fechaDiv);
	fechaDiv.className = "button"+cor+" buttonFecha";
	if(wbutton <= 0){ wbutton = "98%"; }
	$(fechaDiv).width(wbutton);
	var labelDiv = document.createElement('div');
	fechaDiv.appendChild(labelDiv);
	labelDiv.className = "buttonLabel";
	$(labelDiv).html(label);
	$(obj).hover(
		function(){ $(this).find(".button"+cor).addClass("button"+cor+"Over"); },
		function(){ $(this).find(".button"+cor).removeClass("button"+cor+"Over"); }
	);
}

function objImageOver(obj){
	var images = $(obj).find("img");
	$(obj).hover(
		function(){
			$.each(images, function(index, val) {
					var src = $(val).attr("src");
					if(src.indexOf("_over.") >= 0){
					} else {
						src = src.replace(".","_over.");
						$(val).attr("src", src);
					}
				}
			);
		},
		function(){
			$.each(images, function(index, val) {
					var src = $(val).attr("src");
					if(src.indexOf("_over.") >= 0){
						src = src.replace("_over.",".");
						$(val).attr("src", src);
					}
				}
			);
		}
	);
	
}

function Get_Cookie( check_name ) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false;
	var i = '';
	for ( i = 0; i < a_all_cookies.length; i++ ){
		a_temp_cookie = a_all_cookies[i].split( '=' );
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		if ( cookie_name == check_name ){
			b_cookie_found = true;
			if ( a_temp_cookie.length > 1 ){
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ){ return null; }
}

function Set_Cookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ){ expires = expires * 1000 * 60 * 60 * 24; }
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}





function sendOnline(){
	urlGO(rootSite+"system/_ajax_.php?acaoAjax=checarOnline","onlineDiv","POST","SEMLOADING");
}

function processPermalink(elVal){
	var charn = new Array(225,224,227,229,226,233,234,237,243,244,245,250,252,231,193,192,195,194,201,202,205,211,212,213,218,220,199,732,241,232);
	var chars = new Array("a","a","a","a","a","e","e","i","o","o","o","u","u","c","a","a","a","a","e","e","i","o","o","o","u","u","c","_","n","e");
	
	var valSplit = elVal.toLowerCase().split('');
	var valNChar = new Array();
	var newch = "";
	var t = valSplit.length;
	for(t in valSplit){
		var tC = valSplit[t].charCodeAt();
		newch = valSplit[t];
		if(valSplit[t] == " "){ newch = "-"; }
		if(valSplit[t] == "/"){ newch = "_slash_"; }
		for(n in charn){
			if(charn[n] == tC){
				newch = chars[n];
			}
		}
		valNChar.push(newch);
	}
	val = valNChar.join('');
	val = val.replace(/[^a-zA-Z0-9_-]/g, function(c) { return ''; });
	val = val.replace("_slash_","/");
	return val;
}
function completePermalink(form, fieldFrom, fieldTo){
	var elFrom = eval("document."+form+"."+fieldFrom);
	var elTo = eval("document."+form+"."+fieldTo);
	if(elTo.value.length == 0){
		elTo.value = processPermalink(elFrom.value);
	}
}

function clearPermalink(form, fieldVal){
	var elVal = eval("document."+form+"."+fieldVal);
	if(elVal.value.length > 0){
		elVal.value = processPermalink(elVal.value);
	}
}



function lettersonly(e){
	var unicode=e.charCode? e.charCode : e.keyCode
	if (unicode!=8){
		if((unicode > 64 && unicode < 91) || (unicode > 96 && unicode < 123) || (unicode == 95) || (unicode == 45)){ return true; } else { return false; }
	}
}

function elementFocusBlur(el, inival){
	if(eval(el).value == inival){
		eval(el).value = "";
	}
	el.style.backgroundColor = "#FFFFFF";
	$(el).bind("blur", function(){
		if(el.value == ""){ el.value = inival; }
	});
}

function getSelected(form, field){
	var elForm = eval("document."+form+"."+field);
	return elForm.options[elForm.options.selectedIndex].value;
}

function camposForm(form, keys){
	var nfail = 0;
	$.each(keys, function(){
		var vk = this;
		var campoV = vk[0];
		var valorV = vk[1];
		var alertV = vk[2];
		if(campoV != undefined){
			if(eval("document."+form+"."+campoV)){
				var elForm = eval("document."+form+"."+campoV);
				var corFail = "#fffbeb";
				var cor = "#FFFFFF";
				var setCor = 0;
				if(vk[1] == "select"){ if(parseInt(elForm.options[elForm.options.selectedIndex].value) == 0){ setCor = 1; }
				} else if(vk[1] == "number"){ if(elForm.value == 0){ setCor = 1; }
				} else if(valorV == "check"){ if(elForm.checked === false){ setCor = 1; }
				} else if(valorV.toString().match("^!")){ if(elForm.value == valorV.substr(1)){ setCor = 1; }
				} else { if(elForm.value.length < parseInt(vk[1])){ setCor = 1; }
				}
				if(setCor == 1){ cor = corFail; nfail++; }
				elForm.style.backgroundColor = cor;
			}
		}
	});
	if(nfail > 0){ return false; } else { return true; }
}

function verificaForm(form, keys, minimo){
	var recadoForm  = "";
	var numValores = 0;
	$.each(keys, function(){
		var vk = this;
		var campoV = vk[0];
		var valorV = vk[1];
		var alertV = vk[2];
		if(campoV != undefined){
			if(eval("document."+form+"."+campoV)){
				var elForm = eval("document."+form+"."+campoV);
				var corFail = "#FFEEE9";
				var cor = "#FFFFFF";
				var setCor = 0;
				if(valorV == "select"){ if(parseInt(elForm.options[elForm.options.selectedIndex].value) == 0){ setCor = 1; }
				} else if(valorV == "number"){ if(elForm.value == 0){ setCor = 1; }
				} else if(valorV == "check"){ if(elForm.checked === false){ setCor = 1; }
				} else if(valorV.toString().match("^!")){ if(elForm.value == valorV.substr(1)){ setCor = 1; }
				} else { if(elForm.value.length < parseInt(valorV)){ setCor = 1; }
				}
				if(minimo > 0){
					if(setCor == 0) numValores++;
				} else {
					if(setCor == 1){ cor = corFail; recadoForm += "\n" + alertV; }
					elForm.style.backgroundColor = cor;
				}
			}
		}
	});
	if(minimo > 0){
		if(numValores < minimo){
			alert("Favor preencher no minimo " + minimo + " informa" + (minimo>1?"oes":"ao"));
			return false;
		} else {
			return true;
		}
	} else {
		if(recadoForm.length > 0){
			alert("Preencha corretamente o campos:\n" + recadoForm);
			return false;
		} else {
			return true;
		}
	}
}




function flash(ID, arquivo, w, h) {
	document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" id="' + ID + '" width="' + w + '" height="' + h + '">');
	document.write('<param name="movie" value="' + arquivo + '">');
	document.write('<param name="quality" value="high">');
	document.write('<param name="wmode" value="transparent">');
	document.write('<embed src="' + arquivo + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + w + '" height="' + h + '" WMODE="transparent" NAME=' + ID + ' swLiveConnect=true></embed>');
	document.write('</object>');
}

function flashFLV(ID, arquivo, w, h) {
	document.write("<object width='"+w+"' height='"+h+"' id='"+ID+"'>");
	document.write("<param name='allowFullScreen' value='true'>");
	document.write("<param name='allowScriptAccess' value='always'>");
	document.write("<param name='movie' value='"+arquivo+"'>");
	document.write("<embed src='"+arquivo+"' width='"+w+"' height='"+h+"' allowFullScreen='true' type='application/x-shockwave-flash' allowScriptAccess='always'>");
	document.write("</object>");
}



function makeWindow(vURL,nameW,w,h){
	if(w == 0 && h == 0){
		var vScreenWidth = window.screen.availWidth;
		var vScreenHeight = window.screen.availHeight;
		var lf = 0;
		var tp = 0;
		var vFeatures = "fullscreen=yes, toolbar=no, status=no, menubar=no, scrollbars=yes, resizable=yes, directories=no, location=no, left="+lf+", top="+tp+", width="+vScreenWidth+", height="+ vScreenHeight;
	} else {
		var vScreenWidth = w;
		var vScreenHeight = h;
		var lf = (window.screen.availWidth - w)/2;
		var tp = (window.screen.availHeight - h)/2;
		var vFeatures = "toolbar=no, status=yes, menubar=no, scrollbars=yes, resizable=yes, directories=no, location=no, left="+lf+", top="+tp+", width="+vScreenWidth+", height="+ vScreenHeight;
	}
	window.open(vURL,nameW,vFeatures);
}



function Browser() {
	var ua, s, i;
	this.isIE    = false;
	this.isNS    = false;
	this.version = null;
	ua = navigator.userAgent;
	s = "MSIE";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isIE = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}
	s = "Netscape6/";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}
	// Treat any other "Gecko" browser as NS 6.1.
	s = "Gecko";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = 6.1;
		return;
	}
}
var browser = new Browser();
// Global object to hold drag information.
var dragObj = new Object();
dragObj.zIndex = 1800;
function dragStart(event, id) {
	var el;
	var x, y;
	// If an element id was given, find it. Otherwise use the element being
	// clicked on.
	if (id){
		dragObj.elNode = document.getElementById(id);
	} else {
		if (browser.isIE) dragObj.elNode = window.event.srcElement;
		if (browser.isNS) dragObj.elNode = event.target;
		// If this is a text node, use its parent element.
		if (dragObj.elNode.nodeType == 3) dragObj.elNode = dragObj.elNode.parentNode;
	}
	// Get cursor position with respect to the page.
	if (browser.isIE) {
		x = window.event.clientX + document.documentElement.scrollLeft
			+ document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop
			+ document.body.scrollTop;
	}
	if (browser.isNS) {
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}
	// Save starting positions of cursor and element.
	dragObj.cursorStartX = x;
	dragObj.cursorStartY = y;
	dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
	dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);
	if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
	if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;
	// Update element's z-index.
	dragObj.elNode.style.zIndex = ++dragObj.zIndex;
	// Capture mousemove and mouseup events on the page.
	if (browser.isIE) {
		document.attachEvent("onmousemove", dragGo);
		document.attachEvent("onmouseup",   dragStop);
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (browser.isNS) {
		document.addEventListener("mousemove", dragGo,   true);
		document.addEventListener("mouseup",   dragStop, true);
		event.preventDefault();
	}
}
function dragGo(event) {
	var x, y;
	// Get cursor position with respect to the page.
	if (browser.isIE) {
		x = window.event.clientX + document.documentElement.scrollLeft
			+ document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop
			+ document.body.scrollTop;
	}
	if (browser.isNS) {
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}
	// Move drag element by the same amount the cursor has moved.
	dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
	dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";
	
	if (browser.isIE) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (browser.isNS) event.preventDefault();
}

function dragStop(event) {
	// Stop capturing mousemove and mouseup events.
	if (browser.isIE) {
		document.detachEvent("onmousemove", dragGo);
		document.detachEvent("onmouseup",   dragStop);
	}
	if (browser.isNS) {
		document.removeEventListener("mousemove", dragGo,   true);
		document.removeEventListener("mouseup",   dragStop, true);
	}
}
