/************************************************
* 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 disableButtons() {
  var num_elem, num_forms;

  num_forms = window.document.forms.length;
  for (var i=0; i <num_forms; i++) {
    num_elem = window.document.forms[i].elements.length;
    for (var j=0; j<num_elem; j++) {
    frm = window.document.forms[i];
      if (frm.elements[j].type == 'submit') {
         frm.elements[j].disabled='true';
      }
      if (frm.elements[j].type == 'button') {
         frm.elements[j].disabled='true';
      }
      if (frm.elements[j].type == 'reset') {
         frm.elements[j].disabled='true';
      }
    }
  }
}

function enableButtons() {
  var num_elem, num_forms;

  num_forms = window.document.forms.length;
  for (var i=0; i <num_forms; i++) {
    num_elem = window.document.forms[i].elements.length;
    for (var j=0; j<num_elem; j++) {
    frm = window.document.forms[i];
      if (frm.elements[j].type == 'submit') {
         frm.elements[j].disabled=false;
      }
      if (frm.elements[j].type == 'button') {
         frm.elements[j].disabled=false;
      }
      if (frm.elements[j].type == 'reset') {
         frm.elements[j].disabled=false;
      }
    }
  }
}

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;
}

var formAbs = '';

function layerForm(width,height,e)
{

	var arr1 = width.split("px");
	var arr2 = height.split("px");
	
	formAbs = e;

	width = arr1[0];
	height = arr2[0];

	document.getElementById('layer_form').style.display='block';
	
	if (document.getElementById('layer_form').addEventListener){
		document.getElementById('layer_form').addEventListener('click', esconder, false); 
	} else if (document.getElementById('layer_form').attachEvent){
		document.getElementById('layer_form').attachEvent('onclick', esconder);
	}
	
	tamanhos = getPageSize();
	xcenter = (tamanhos[0]/2) - (width / 2);
	
	if (window.pageYOffset) {
		ycenter = window.pageYOffset + (tamanhos[3]/2) - (height / 2);
	} else {
		ycenter = document.documentElement.scrollTop + document.body.scrollTop + (tamanhos[3]/2) - (height / 2);
	}

	document.getElementById('layer_form').style.width = tamanhos[0]+'px';
	document.getElementById('layer_form').style.height = tamanhos[1]+'px';
	
	e.style.display='block';
	e.style.top = ycenter+'px';
	e.style.left = xcenter+'px';
	
	s = document.getElementsByTagName('select')
	for(var i=0;i<s.length;i++){
		s[i].style.display = 'none';
	}

}

function esconder()
{
	document.getElementById('layer_form').style.display='none';
	formAbs.style.display='none';

	s = document.getElementsByTagName('select')
	for(var i=0;i<s.length;i++){
		s[i].style.display = 'block';
	}
}


function searchTags()
{
	var tag = document.getElementById('q').value;
	
	document.getElementById('q').value = searchPtChars(tag);
	
	document.getElementById('q').value;
}



function searchPtChars(string)
{
	var result = '';
	var altera = false;
	var charSubs = '';
	
	var search = new Array('ã','â','á','à','ä','Ã','Â','Á','À','Ä','é','è','ê','ë','É','È','Ê','Ë',
					'í','ì','î','ï','Í','Ì','Î','Ï','ó','ò','õ','ô','ö','Ó','Ò','Õ','Ô','Ö',
					'ú','ù','û','ü','Ú','Ù','Û','Ü','ç','Ç','ñ','Ñ','´','º',
					'…','«','»','€','“','”','–',
					'ý','ÿ','Ý','`','®','©');

	var replace = new Array('a','a','a','a','a','A','A','A','A','A','e','e','e','e','E','E','E','E',
					'i','i','i','i','I','I','I','I','o','o','o','o','o','O','O','O','O','O',
					'u','u','u','u','U','U','U','U','c','c','n','N','','',
					'','','','','','','-',
					'y','y','Y','','','');
					
	
	for (i=0; i<string.length; i++)
	{
		for (j=0; j<search.length; j++)
		{
			if (string.charAt(i) == search[j])
			{
				charSubs = replace[j];
				altera = true;
			}
		}
		
		if (altera)
		{
			result += charSubs;
		}
		else
		{
			result += string.charAt(i);
		}
		
		altera = false;
	}				

	return result;
}

// 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;
}();
