/*
    Document   : main
    Created on : 20/10/2010, 09:31:00
    Author     : CarlosGarcia
    Description: All alerts Admin, it uses a thickbox lib
    Purpose of the javascript follows.
*/

var isTrace = false;
var isCloseable = true;
$(document).ready(function() {

    $('#bt_close_message').click(function(){
        tb_removeAlert()
        });
    $('#container_result_message').hide();            
    initBlurAction();

});//Close Document Ready

/*
 * Alert for all common result messages
 * It has just 2 lines of text
 *
 */
function showAlertDim(data){
    showPopUp("toast", "Alteração realizada com sucesso!",  'message_simple', 400, 30, false);
//    $('#resultAlertMessage').empty().append(data);
//    $('#container_result_message').show("fast");
//    $('.bg_result_message').show();
//    $('#bt_close_message').click(function(){
//        tb_removeAlert()
//    });
//    
//    tb_removeLogin();
//
//    //If overlay clicked it closes the dim message
//    isCloseable = true;
//    tb_showAlert();
}

/*
 * Alert for all common result messages
 * It has just 2 lines of text
 *
 */
function showAlertDimPreloader(data){
    showPopUp("preloader", "Salvando...",  'preloader_simple', 190, 30, false);
}

/*
 * Shows options message
 * Default message
 * 
 */
function showAlertDimCancel(){
    showPopUp("message_options", "message_options",  'message_options', 400, 120, false);
}

/*
 * Alert login for site login
 * This method below uses a special controller
 * Components to load a specific template component
 * 
 * @param string
 * @param string
 *  
 */
function showPopUp(type, componente, layout, sizeHeight, sizeWidth, isAction){       
   
    if(isAction)var next_action_supply = type + "-" + componente + "-" + layout;
    
    setSizePopUp(sizeHeight, sizeWidth);
    
    if(type == "toast" || type == "preloader"){
        $(".bt_close_login").hide();
    }
    
    if(type == "modulo"){
        $("#loaderCadastrarComponents").hide();
    }    
        
    $.post("/site/component/action", {           
        type: type,
        component: componente,
        layout: layout,
        next_action : next_action_supply
        
    },function(data){
        
        $("#loaderCadastrarComponents").hide();
        $("#loaderCadastrarComponents").empty().append(data); 
        $("#loaderCadastrarComponents").fadeIn("fast");
        
        if(type == "toast"){            
            setTimeout("tb_removeLogin()", 1300);
        }
        
        if(type == "message_options"){
            $('#bt_alertdimsite_cancel').click(function(){tb_removeLogin(); cancelActionAlertDim();});
            $('#bt_alertdimsite_yes').click(function(){tb_removeMessage();completeActionAlertDim()});
        }
        
        if(type == "pages"){
            $("#container_login").hide();
            $(".text_result_preloader").hide();
        }
    });       
        
    //If overlay clicked it closes the dim message
    isCloseable = false;
    tb_showAlert();
}

function reloadPopUp(type, componente, layout, next_action){
    
    $.post("/site/component/action", {           
        type: type,
        component: componente,
        layout: layout,
        next_action : next_action
    },

    function(data){
        $("#loaderCadastrarComponents").hide();
        $("#loaderCadastrarComponents").empty().append(data); 
        $("#loaderCadastrarComponents").fadeIn("slow");
    }); 
}

/*
 * It jsut removes the message no t the dim!
 * 
 */
function tb_removeMessage(){
    $('#container_result_message_cancel_2').hide("fast"); 
    $('.bg_result_popup').fadeOut("fast");
    $('#container_result_popup').fadeOut("slow");
    
}
    
/*
 *  This method uses a fancybox resources to show the
 *  component
 *    
 */   
function tb_showAlert() {//function called when the user clicks on a thickbox link

    try { 
        if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
            $("body","html").css({
                height: "100%", 
                width: "100%"
            });
            $("html").css("overflow","hidden");
            if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
                $("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
                if(isCloseable){
                    $("#TB_overlay").click(tb_removeAlert);
                }
            }
        }else{//all others
            if(document.getElementById("TB_overlay") === null){
                $("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
                if(isCloseable){
                    $("#TB_overlay").click(tb_removeAlert);
                }
            }
        }

        if(tb_detectMacXFF()){
            $("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
        }else{
            $("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
        }




        var baseURL;
        if(url.indexOf("?")!==-1){ //ff there is a query string involved
            baseURL = url.substr(0, url.indexOf("?"));
        }else{
            baseURL = url;
        }



        if(!params['modal']){
            document.onkeyup = function(e){
                if (e == null) { // ie
                    keycode = event.keyCode;
                } else { // mozilla
                    keycode = e.which;
                }
                if(keycode == 27){ // close
                    tb_remove();
                }
            };
        }

    } catch(e) {
    //nothing here
    }
}

function tb_removeAlert() {

    $("#TB_imageOff").unbind("click");
    $("#TB_closeWindowButton").unbind("click");
    $("#TB_window").fadeOut("fast",function(){
        $('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();
    });
    $("#TB_load").remove();

    if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
        $("body","html").css({
            height: "auto", 
            width: "auto"
        });
        $("html").css("overflow","");
    }

    
    $('#container_result_message').fadeOut("fast");

    document.onkeydown = "";
    document.onkeyup = "";


    return false;
}


function tb_removeLogin() {

    $("#TB_imageOff").unbind("click");
    $("#TB_closeWindowButton").unbind("click");
    $("#TB_window").fadeOut("fast",function(){
        $('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();
    });
    $("#TB_load").remove();

    if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
        $("body","html").css({
            height: "auto", 
            width: "auto"
        });
        $("html").css("overflow","");
    }

    $('.bg_result_popup').fadeOut("fast");
    $('#container_result_popup').fadeOut("slow");
    removePreloaderAnimation();

    document.onkeydown = "";
    document.onkeyup = "";

    return false;
}

function setSizePopUp(sizeWidthPopUp, sizeHeightPopUp){
    
    var middleWSize = (sizeWidthPopUp - 40) + "px"; 
    var middleHSize = (sizeHeightPopUp) + "px";
    var sizeMarginLeftPopUp = ((sizeWidthPopUp / 2)*-1) + "px";
    var sizeMarginTopPopUp  = (((sizeHeightPopUp / 2) + 40)*-1) + "px";
    
    $(".bg_result_popup").css("width", sizeWidthPopUp+"px");
    $(".bg_result_popup").css("height", sizeHeightPopUp);
    
    $(".bg_result_popup").css("margin-left", sizeMarginLeftPopUp);
    $(".bg_result_popup").css("margin-top", sizeMarginTopPopUp);
    
    
    $(".bg_result_popup_top").css("width", sizeWidthPopUp+"px");
    $(".bg_result_popup_footer").css("width", sizeWidthPopUp+"px");
    $(".bg_result_popup_middle").css("width", sizeWidthPopUp+"px");    
    
    $(".bg_result_popup_top_middle").css("width", middleWSize);
    $(".bg_result_popup_footer_middle").css("width", middleWSize);
    $(".bg_result_popup_middle_middle").css("width", middleWSize);
    $(".bg_result_popup_middle_middle").css("height", middleHSize);
    $(".bg_result_popup_middle_corner_left").css("height", middleHSize);
    $(".bg_result_popup_middle_corner_right").css("height", middleHSize);
       
    $('.bg_result_popup').fadeIn("fast");
    $('#container_popup').fadeIn("fast");            
    
    $('.bt_close_message').show("fast");
    $('#bt_close_login').click(function(){
        tb_removeLogin();
    });
    
    
}

function initBlurAction(){
        
    $('#email_pf').focus(function(){        
        if($('#email_pf').val() == "E-mail"){
            $('#email_pf').val('');
        }        
    });

    $('#email_pf').blur(function(){
        if($('#email_pf').val() == ''){
            $('#email_pf').val('E-mail');
        }
    });

    $('#loginCadastradoCPF').focus(function(){
        if($('#loginCadastradoCPF').val() == "CPF"){
            $('#loginCadastradoCPF').val('');
        }
    });

    $('#loginCadastradoCPF').blur(function(){
        if($('#loginCadastradoCPF').val() == ""){
            $('#loginCadastradoCPF').val('CPF');
        }
    });

    $('#email_pj').focus(function(){
        if($('#email_pj').val() == "E-mail"){
            $('#email_pj').val('');
        }
    });

    $('#email_pj').blur(function(){
        if($('#email_pj').val() == ''){
            $('#email_pj').val('E-mail');
        }
    });

    $('#loginCadastradoCNPJ').focus(function(){
        if($('#loginCadastradoCNPJ').val() == "CNPJ"){
            $('#loginCadastradoCNPJ').val('');
        }
    });

    $('#loginCadastradoCNPJ').blur(function(){
        if($('#loginCadastradoCNPJ').val() == ''){
            $('#loginCadastradoCNPJ').val('CNPJ');
        }
    });

}

