var reloadTop = false;
var anotherUrl = null;

function getObject(idArg) {
    return(document.getElementById(idArg));
}
function $(id){
    return document.getElementById(id);
}

function hideElement(element) {
    $(element).style.display = 'none';
}

function selectMenu(menuId) {
    var subMenu = getObject(menuId);
    if (subMenu != null && subMenu != 'undefined') {
        subMenu.style.textDecoration = 'underline';
        var mainMenuIdArr = menuId.split('_');
        var mainMenuId = mainMenuIdArr[0]+'_'+mainMenuIdArr[1];
        var mainMenu = getObject(mainMenuId);
        if (mainMenu != null && mainMenu != 'undefined') {
            /*mainMenu.style.textDecoration = 'underline';*/
            var mouseOver = mainMenu.onmouseover;
            window.setTimeout(mouseOver);
        }
    }
}



//// MODAL - INICIO

function showModal(urlArg, widthArg, heightArg, titleArg, scrollBarArg, showCloseArg, topAjustArg, reloadPage, otherUrl) {
    // this.scroll(1,-1000);
    var dmp = getObject('divModalPanel');
    var divModalBorder = getObject('divModalBorder');

    var dm = getObject('divBackground');

    // default is true
    if (scrollBarArg == null || scrollBarArg == undefined) {
        scrollBarArg = true;
    }
    if (scrollBarArg) {
        getObject('divModalPanelFrame').setAttribute('scrolling','auto');
        getObject('divModalPanelFrame').scrolling='auto';
		
    } else {
        getObject('divModalPanelFrame').setAttribute('scrolling','no');
        getObject('divModalPanelFrame').scrolling='no';
    }

    if (titleArg == null || titleArg == '') {
        titleArg = '&nbsp;';
    }
    getObject('divModalTitle').innerHTML = titleArg;

    getObject('divModalPanelFrame').src = urlArg;
    getObject('divModalPanelFrame').width = widthArg;
    getObject('divModalPanelFrame').height = heightArg;
    divModalBorder.width = widthArg;
    divModalBorder.height = heightArg;
	
    if (showCloseArg == null && showCloseArg == undefined) {
        showCloseArg = true;
    }

    if (showCloseArg) {
        getObject('closeModalEvent').style.display = '';
    } else {
        getObject('closeModalEvent').style.display = 'none';
    }
	
    if (topAjustArg == null) {
        topAjustArg = 0;
    }
	
	
    dm.style.display='';
    dmp.style.display='';

    var altTela = Math.floor(document.body.offsetHeight);
    var largTela =  Math.floor(document.body.offsetWidth);
    var largDiv = Math.floor(divModalBorder.offsetWidth);
    var altDiv = Math.floor(divModalBorder.offsetHeight);

    dm.style.width=largTela+'px';
    dm.style.height=altTela+'px';

    dmp.style.left=((largTela/2)-(largDiv/2))+'px';
    dmp.style.top=(topAjustArg + 30)+'px';
		
    // nova parte
    dmp.style.display = dm.style.display = 'block';
	
    swapNativeFunctions(1);
	
    if (reloadPage){
        reloadTop = reloadPage;
        anotherUrl = otherUrl;
    //alert(otherUrl);
    }
}


function closeModal(timeArg) {
    if (timeArg != undefined && timeArg != null && timeArg > 0) {
        window.setTimeout(function() {
            closeModal();
        }, timeArg*1000);
    } else {
        getObject('divModalPanel').style.display='none';
        getObject('divBackground').style.display='none';
        getObject('divModalTitle').innerHTML = '';
        getObject('divModalPanelFrame').src = '';
    }
    //nova parte
    swapNativeFunctions(0);
	
    if(reloadTop){
        if(anotherUrl == null){
            top.location.href = top.location.href;
        }else{
            top.location.href = anotherUrl;
        }
		
    }
}
//// MODAL - FINAL



function fakeScroll(){
    document.body.scrollTop = 0;
//return false;
}
var hideType='replace';

function swapNativeFunctions(flow){
    // 1 - to really swap functions
    // 0 - to bring back the old way
    // 2 - alternate flow, when modal is already open
    if(flow == 1){
        // special < IE7 -only processing for windowed elements, like select
        if (window.XMLHttpRequest == null){
            var type = hideType;
			
            if (type == 'iframe')
                $('modalIframe').style.display = 'block';
            if (type == 'replace')
                ReplaceSelectsWithSpans();
        }
	
        // comentado p/ nao redimensionar de comeco
        // call once to center everything
        OnWindowResize();
		
        if (window.attachEvent){
            window.attachEvent('onresize', OnWindowResize);
        }else if (window.addEventListener){
            window.addEventListener('resize', OnWindowResize, false);
        }else{
            window.onresize = OnWindowResize;
        }
		
        // we won't bother with using javascript in CSS to take care
        //   keeping the window centered
        window.onscroll = OnWindowResize;
		
	
    }else if(flow == 0){
        if (document.all)	{
            var type = hideType;
		
            if (type == 'iframe')
                $('modalIframe').style.display = 'none';
            if (type == 'replace')
                RemoveSelectSpans();
        }
		
        if (window.detachEvent){
            window.detachEvent('onresize', OnWindowResize);
        }else if (window.removeEventListener){
            window.removeEventListener('resize', OnWindowResize, false);
        }else{
            window.onresize = null;
        }
        window.onscroll = null;
    }else if(flow == 2){
	
        if (window.attachEvent){
            top.window.attachEvent('onresize', OnWindowResize);
        }else if (window.addEventListener){
            top.window.addEventListener('resize', OnWindowResize, false);
        }else{
            top.window.onresize = OnWindowResize;
        }
		
        // we won't bother with using javascript in CSS to take care
        //   keeping the window centered
        top.window.onscroll = OnWindowResize;
        OnWindowResize();
		
    }else{
        alert('flow is unknown or not specified.');
    }
	
}

function OnWindowResize(){
    // we only need to move the dialog based on scroll position if
    //   we're using a browser that doesn't support position: fixed, like < IE 7
    var left = window.XMLHttpRequest == null ? document.documentElement.scrollLeft : 0;
    var top = window.XMLHttpRequest == null ? document.documentElement.scrollTop : 0;
    var div;
    var divBg;
	
    if($('divModalPanel')){
        div = $('divModalPanel');
        divBg = $('divBackground');
        div.style.left = Math.max((left + (GetWindowWidth() - div.offsetWidth) / 2), 0) + 'px';
    }else{
        div = parent.document.getElementById('divModalPanel');
        divBg = parent.document.getElementById('divBackground');
    }

    var divTop = Math.max(parent.document.body.scrollTop,
        parent.document.documentElement.scrollTop);
	
    div.style.top = divTop + 80 + 'px';
    divBg.style.top = divTop +'px';

    divBg.style.width = "100%";
    divBg.style.height = "100%";



//        alert('Entrou: \n    ' +
//          'div.top = '+div.style.top + ', ' +
//          'divBg.top = '+divBg.style.top + ', ' +
//          '\nparent.doc.body.scrollTop = ' + document.body.scrollTop+ ', ' +
//          '\nparent.doc.documentElement.scrollTop = ' + document.documentElement.scrollTop+ ', '
//        )
}

/* These functions deal with IE's retardedness in not allowing divs to 
 * cover select elements by replacing the select elements with spans. */

function RemoveSelectSpans(){
    var selects = document.getElementsByTagName('select');
	
    for (var i = 0; i < selects.length; i++){
        var select = selects[i];
		
        if (select.clientWidth == 0 || select.clientHeight == 0 ||
            select.nextSibling == null || select.nextSibling.className != 'selectReplacement'){
            continue;
        }
			
        select.parentNode.removeChild(select.nextSibling);
        select.style.display = select.cachedDisplay;
    }
}

function ReplaceSelectsWithSpans(){
    var selects = document.getElementsByTagName('select');
	
    for (var i = 0; i < selects.length; i++){
        var select = selects[i];
		
        if (select.clientWidth == 0 || select.clientHeight == 0 ||
            select.nextSibling == null || select.nextSibling.className == 'selectReplacement'){
            continue;
        }
			
        var span = document.createElement('span');
		
        // this would be "- 3", but for that appears to shift the block that contains the span
        //   one pixel down; instead we tolerate the span being 1px shorter than the select
        span.style.height = (select.clientHeight - 4) + 'px';
        span.style.width = (select.clientWidth - 6) + 'px';
        span.style.display = 'inline-block';
        span.style.border = '1px solid rgb(200, 210, 230)';
        span.style.padding = '1px 0 0 4px';
        span.style.fontFamily = 'Arial';
        span.style.fontSize = 'smaller';
        span.style.position = 'relative';
        span.style.top = '1px';
        span.className = 'selectReplacement';
		
        span.innerHTML = select.options[select.selectedIndex].innerHTML +
        '<img src="custom_drop.gif" alt="drop down" style="position: absolute; right: 1px; top: 1px;" />';
		
        select.cachedDisplay = select.style.display;
        select.style.display = 'none';
        select.parentNode.insertBefore(span, select.nextSibling);
    }
}

function GetWindowWidth(){
    var width =
    document.documentElement && document.documentElement.clientWidth ||
    document.body && document.body.clientWidth ||
    document.body && document.body.parentNode && document.body.parentNode.clientWidth ||
    0;
    return width;
}

function GetWindowHeight(){
    var height =
    document.documentElement && document.documentElement.clientHeight ||
    document.body && document.body.clientHeight ||
    document.body && document.body.parentNode && document.body.parentNode.clientHeight ||
    0;
    return height;
}

function openPopup(urlArg, widthArg, heightArg) {
    window.open(urlArg, "profileDetail", "width="+widthArg+",height="+heightArg+",scrollbars=1");
}