/* JavaScript Document */

/**
 * @author Jonas A. Scheguschevski (jonas@scheguschevski.com.br)
 * @package web-site
 */

var f = get_size('#fundo');
var r = get_size('#rodape');
var pl = get_size('#popup_layer');
var resized = false;
var cache = false;

$(document).ready(function(){
    var objImageBg = new Image();
    objImageBg.src = $('#fundo img').attr('src');

    objImageBg.onload = function(){
        $('#loading').hide();
        cache = true;

        objImageBg.onload=function(){};
    };

    resize_bg();
    popup_position();
    
    $(window).resize(function(){
        resized = true;
        resize_bg();
        popup_position();
    });

    $('#topo ul li a').click(function(){
        var url = $(this).attr('href');

        open_popup(url);
        return false;
    });

    $('#pl_close').click(function(){
        close_popup();
    });

    $(document).bind('keydown', 'Esc', function(){
        if($('#background_photos').css('display')=='block')
            close_photo();
        else
            close_popup();
        
        return false;
    });

    $('#popup_content').ajaxError(function(resposta){
        popup_error();
    });

    $('#background_photos').click(function(){
        close_photo();
    });

    if(objImageBg.complete){
        cache = true;
        $('#loading').hide();
    }
});

function resize_bg(){
    var p = get_size('#principal');

    if(p.height == 0)
        p = get_size(document);

    var top_r = p.height - 50;

    var width = 0;
    var height = 0;

    var top = 0;
    var left = 0;

    width = p.width;
    height = (p.width * f.height) / f.width;

    top = (height > p.height)?(p.height - height) / 2:0;
    left = (width > p.width)?(p.width - width) / 2:0;

    if(p.height > height){
        width = (p.height * f.width) / f.height;
        height = p.height;

        top = (height > p.height)?(p.height - height) / 2:0;
        left = (width > p.width)?(p.width - width) / 2:0;
    }

    width = Math.round(width);
    height = Math.round(height);
    top = Math.round(top);
    left = Math.round(left);

    $('#fundo img').css({'width':width+'px', 'height':height+'px'});
    $('#fundo').css({'top':top+'px', 'left':left+'px'}).show();
    
    if($.browser.msie && $.browser.version < 8)
        $('#rodape').css({'top':top_r+'px'});

    var p_f = get_size('#popup_photos');

    top = (p.height - p_f.height) / 2;
    left = (p.width - p_f.width) / 2;

    top = Math.round(top);
    left = Math.round(left);

    $('#popup_photos').css({'top':top+'px', 'left':left+'px'});
}

function popup_position(){
    var p = get_size('#principal');

    if(p.height == 0)
        p = get_size(document);

    var top = (p.height - pl.height) / 2;
    var left = (p.width - pl.width) / 2;

    top = Math.round(top);
    left = Math.round(left);

    $('#popup_layer').css({'top':top+'px', 'left':left+'px'});
}

function open_popup(url){
    $('#popup_loading').show();
    $('#popup_content').html('');

    $('#background_layer').show();
    $('#popup_layer').show();

    $('#popup_content').load(url, {view:true}, function(resposta){
        $('#popup_loading').hide();
        $('#popup_content').show();
    });
}

function popup_error(){
    $('#popup_loading').hide();
    $('#popup_error').show();
}

function close_popup(){
    $('#background_layer').hide();
    $('#popup_layer').hide();
    $('#popup_error').hide();
    $('#popup_content').hide();
}

function get_size(element, tl){
    var width = $(element).width();
    var height = $(element).height();

    if(tl == undefined){
        return {width:width, height:height};
    }else{
        if(tl=='width')
            return width;
        else
            return height;
    }
}

function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);

    if(typeof(mail) == "string"){
        if(er.test(mail)){
            return true;
        }
    }else
    if(typeof(mail) == "object"){
        if(er.test(mail.value)){
            return true;
        }
    }else{
        return false;
    }

    return false;
}

function open_photo(url){
    $('#background_photos').show();
    
    $('#popup_photos').html('');
    $('#popup_photos').css('background', '#FFF url(./images/website/ico-loading.gif) no-repeat center').show();

    $.getJSON(url, function(data){
        var objImagePreloader = new Image();
        objImagePreloader.src = data.foto;

        objImagePreloader.onload = function() {
            show_photo(objImagePreloader.width, objImagePreloader.height, data.foto, data.titulo_uri, data.legenda, data.cod_anterior, data.cod_proxima);
            objImagePreloader.onload=function(){};
        };
    });
}

function show_photo(width, height, url, galeria, legenda, anterior, proxima){
    var lnk_h = height;
    height += 60;

    var Photo = $('#popup_photos');
    var p = get_size('#principal');

    if(p.height == 0)
        p = get_size(document);

    var top = (p.height - height) / 2;
    var left = (p.width - width) / 2;

    top = Math.round(top);
    left = Math.round(left);

    if(url != undefined && url != ''){
        var html = '';
        html += '<div id="show_photo">';
        html += '   <a href="fotos/ver/'+galeria+'/'+anterior+'" title="Foto anterior" id="photo_prev"><img src="./images/website/anterior.png" alt="" /></a>';
        html += '   <a href="fotos/ver/'+galeria+'/'+proxima+'" title="Próxima foto" id="photo_next"><img src="./images/website/proxima.png" alt="" /></a>';
        html += '   <div id="photo_legenda">'+legenda+'</div>';
        html += '   <a href="javascript:void(0);" title="Fechar" id="photo_fechar"><img src="./images/website/fechar.png" alt="" /></a>';
        html += '</div>';

        Photo.html(html);
        Photo.css('background', '#FFF url('+url+') no-repeat center 10px');

        $('#photo_prev').mouseover(function(){ $('#photo_prev img').show(); }).mouseout(function(){ $('#photo_prev img').hide(); }).css('height', lnk_h+'px');
        $('#photo_next').mouseover(function(){ $('#photo_next img').show(); }).mouseout(function(){ $('#photo_next img').hide(); }).css('height', lnk_h+'px');

        $('#photo_prev').click(function(){
            var p_url = $(this).attr('href');
            open_photo(p_url);

            return false;
        });

        $('#photo_next').click(function(){
            var n_url = $(this).attr('href');
            open_photo(n_url);

            return false;
        });

        $('#photo_legenda').css('width', Math.round(width - 85)+'px');
        $('#photo_fechar').click(function(){ close_photo(); });
    }else
        Photo.css('background', '#FFF url(../../images/website/ico-loading.gif) no-repeat center');

    Photo.css({'top':top+'px', 'left':left+'px', 'width':width+'px', 'height':height+'px'});

    $('#popup_photos img').fadeIn();
    
    $('#photo_prev img').hide();
    $('#photo_next img').hide();
}

function close_photo(){
    $('#background_photos, #popup_photos').fadeOut();
}

function go_top(){
    $(document).scrollTop(0);
    window.setTimeout("go_top();", 500);
}
