/************************************************
* Métodos Comuns (FO/BO)						*
*************************************************/

function popper(thisUrl,thisWidth,thisHeight,thisTop,thisLeft)
{
if (!thisLeft)
	var thisLeft=(screen.width-thisWidth)/2; // get center of screen minus popup width
if (!thisTop)
	var thisTop=(screen.height-thisHeight)/2; // get center of screen minus popup height

var optionString,mainWin;
optionString = ('width=' + thisWidth + ',height=' + thisHeight + ',top=' + thisTop + ',left=' + thisLeft + ',status=no,menubar=no,resizable=yes,scrollbars=no');
mainWin = window.open(thisUrl,'editorHtmlWindow',optionString);
mainWin.focus();
}

function popperScroll(thisUrl,thisWidth,thisHeight,thisTop,thisLeft,thisWindowName)
{

if (!thisLeft)
	var thisLeft=(screen.width-thisWidth)/2; // get center of screen minus popup width
if (!thisTop)
	var thisTop=(screen.height-thisHeight)/2; // get center of screen minus popup height
if(!thisWindowName)
{
	var thisWindowName = 'mainWindow';
}

var optionString,mainWin;
optionString = ('width=' + thisWidth + ',height=' + thisHeight + ',top=' + thisTop + ',left=' + thisLeft + ',status=no,menubar=no,resizable=yes,scrollbars=yes');
mainWin = window.open(thisUrl,thisWindowName,optionString);
mainWin.focus();
}

function dialogBox(thisUrl,thisWidth,thisHeight,thisTop,thisLeft)
{
var optionString,mainWin;
optionString = ('width=' + thisWidth + ',height=' + thisHeight + ',top=' + thisTop + ',left=' + thisLeft + ',status=no,menubar=no,resizable=no,scrollbars=yes');
mainWin = window.showModalDialog(thisUrl,'',optionString);

}

//Validação Input
function validatedFilename(string) {
    for (var i=0, output='', valid="_-0123456789abcdefghijklmnopqrstuvwxyz"; i<string.length; i++)
       if (valid.indexOf(string.charAt(i)) != -1)
          output += string.charAt(i)
		  
    return output;
}

cont = 0;

//Validação Input
function validatedTitle(string) {
    for (var i=0, output='', invalid="'\""; i<string.length; i++)
	{
		if (invalid.indexOf(string.charAt(i)) == -1){
			output += string.charAt(i)
		}
		else if (string.charAt(i) == '\''){
			cont++;
			if(cont%2 == 0){
				output += '´';
			}
			else{
				output += '`';
			}
		}
		else if (string.charAt(i) == '"'){
			cont++;
			if(cont%2 == 0){
				output += '»';
			}
			else{
				output += '«';
			}
		}
	}
    return output;
}

//Validação Input
function validatedPageTitle(string) {
    for (var i=0, output='', invalid="'\"&"; i<string.length; i++)
       if (invalid.indexOf(string.charAt(i)) == -1){
          output += string.charAt(i)
	   }
	   else if (string.charAt(i) == '&'){
			output += 'e';
	   }
		  
    return output;
}

function getPageSize(){
    var xScroll, yScroll;
   
    if (window.innerHeight && window.scrollMaxY) {   
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
   
    var windowWidth, windowHeight;
    if (self.innerHeight) {    // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }   
   
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){   
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }

    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
    return arrayPageSize;
}

function layerForm(width,height,e)
{

	var arr1 = width.split("px");
	var arr2 = height.split("px");
	
	formAbs = e;

	width = arr1[0];
	height = arr2[0];
	
	tamanhos = getPageSize();

	document.getElementById('layer_form').style.display='block';
	document.getElementById('layer_form1').style.display='block';
	document.getElementById('layer_form2').style.display='block';
	
	if (document.getElementById('layer_form').addEventListener || document.getElementById('layer_form1').addEventListener || document.getElementById('layer_form2').addEventListener){
		document.getElementById('layer_form').addEventListener('click', esconder, false); 
		document.getElementById('layer_form1').addEventListener('click', esconder, false); 
		document.getElementById('layer_form2').addEventListener('click', esconder, false); 
		
		largura_layer = tamanhos[0] - 210;
		altura_layer = tamanhos[1] - 150;
		
		altura_layer1 = tamanhos[1] - 20;
	
	} else if (document.getElementById('layer_form').attachEvent || document.getElementById('layer_form1').attachEvent || document.getElementById('layer_form2').attachEvent){
		document.getElementById('layer_form').attachEvent('onclick', esconder);
		document.getElementById('layer_form1').attachEvent('onclick', esconder);
		document.getElementById('layer_form2').attachEvent('onclick', esconder);
		
		largura_layer = tamanhos[0] - 190;
		altura_layer = tamanhos[1] - 130;
		
		altura_layer1 = tamanhos[1];
	}
		
	xcenter = (tamanhos[0]/2) - (width);
	
	if (window.pageYOffset) {
		ycenter = window.pageYOffset + (tamanhos[3]/2) - (height) + 30;
	} else {
		ycenter = document.documentElement.scrollTop + document.body.scrollTop + (tamanhos[3]/2) - (height) + 30;
	}
	
	document.getElementById('layer_form').style.width = largura_layer+'px';
	document.getElementById('layer_form').style.height = altura_layer+'px';
	document.getElementById('layer_form1').style.width = largura_layer+'px';
	document.getElementById('layer_form1').style.height = '130px';
	document.getElementById('layer_form2').style.width = '190px';
	document.getElementById('layer_form2').style.height = altura_layer1+'px';
	
	e.style.display='block';
	e.style.top = ycenter+'px';
	e.style.left = xcenter+'px';
}

function esconder()
{
	document.getElementById('layer_form').style.display='none';
	document.getElementById('layer_form1').style.display='none';
	document.getElementById('layer_form2').style.display='none';
	formAbs.style.display='none';
}


// CSS Browser Selector   v0.2.5
// Documentation:         http://rafael.adm.br/css_browser_selector
// License:               http://creativecommons.org/licenses/by/2.5/
// Author:                Rafael Lima (http://rafael.adm.br)
// Contributors:          http://rafael.adm.br/css_browser_selector#contributors
var css_browser_selector = function() {
	var 
		ua=navigator.userAgent.toLowerCase(),
		is=function(t){ return ua.indexOf(t) != -1; },
		h=document.getElementsByTagName('html')[0],
		b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('gecko/')? 'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',
		os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';
	var c=b+os+' js';
	h.className += h.className?' '+c:c;
}();
