/**
 * @name Home
 * @author L.F.Martins
 * @since 23/11/10 15:52
 * @version 1.3
 * @internal
 *  - Retirado animação automática dos lançamentos.
 *  - Retirado banners dinâmicos.
 *  - Retirada animação rotate icones redes sociais.
 *  - Colocada animação icones redes sociais estilo sexy-bookmarks.
 *  ================================================================
 *  - Fix IE - Coloquei background-position-x/background-position-y
 *      para animação dos icones redes sociais.
 *
 */

/* Número de produtos iniciais. */
var currentItem = 4;

/* Variáveis Globais. */
var divs = [];
var length;
var interval;
var SessionPathHome = './library/home/session.php';

/* JQuery - Carrega com a página. */
jQuery(function($){
    try{
        ShowInit(currentItem);
        //RandPromotions();
        
        $('div.pro').click(function () {
            //clearInterval(interval);
            Next();
        });
        $('div.ant').click(Preview);

        //interval = setInterval(Next, 2500);
        //interval_pro = setInterval(RandPromotions, 10000);

        $('.action_icon').mouseenter(function() {            
            var bg_pos = $(this).css('background-position');                        
            if (bg_pos == 'undefined' || bg_pos == null) {
                bg_pos = $(this).css('background-position-x');
                $(this).css('background-position-x',bg_pos);
                $(this).css('background-position-y','-50px');
            } // if
            else {
                bg_pos = bg_pos.split(' ')[0];
                $(this).css('background-position',bg_pos+' -57px');
            } // else
        });

        $('.action_icon').mouseleave(function() {
            var bg_pos = $(this).css('background-position');
            if (bg_pos == 'undefined' || bg_pos == null) {
                bg_pos = $(this).css('background-position-x');
                $(this).css('background-position-x',bg_pos);
                $(this).css('background-position-y','8px');
            } // if
            else {
                bg_pos = bg_pos.split(' ')[0];
                $(this).css('background-position',bg_pos+' 8px');
            } // else
        });

        $('#opacidade').click(function () {
            CloseNews();
        });

        $("*").keydown(function (e) {
            e.stopPropagation();
            if (e.keyCode == '27' && $('#opacidade').is(':visible')) {
                CloseNews();
            } // if
        }); // function
        
    }catch(e){
        alert(e);
    } // catch
}) // JQuery

/**
 * @ShowInit
 * @param <Int> n - Número de produtos iniciais.
 * @internal Mostra os primeiros produtos e grava na
 * variável divs os produtos.
 */
function ShowInit(n) {
    for(var i=0;i<n;i++) {
        $("div.lancamentos_box div#"+i).fadeIn();
    } // for
    length = $('div.lancamentos_prod').length;
    $('div.lancamentos_prod').each(function () {
        divs.push("<div id="+$(this).attr('id')+" class='lancamentos_prod' >"+$(this).html()+"</div>");
        if ($(this).attr('id') >= currentItem) {
            $(this).remove();
        } // if
    }); // each
} // function

/**
 * @name Next
 * @internal Muda para o próximo produto.
 */
function Next() {              
    var $box = $('div.lancamentos_box');        
    $box.find('> div:first').fadeOut(1, function () {
        $(this).remove();
        $(divs[currentItem]).appendTo($box);
        $("div.lancamentos_box div#"+currentItem).fadeIn(1);
        currentItem++;
        if (currentItem >= length) {
            currentItem = 0;
        } // if
    }); // fadeOut
} // function

/**
 * @name Preview
 * @internal Muda para o produto anterior.
 */
function Preview() {
    clearInterval(interval);
    var $box = $('div.lancamentos_box');
    $box.find('> div:last').fadeOut(1, function () {
        $(this).remove();
        var p = ((currentItem - 5) < 0) ?  (length + (currentItem - 5)) : (currentItem - 5);
        $(divs[p]).prependTo($box);
        $("div.lancamentos_box div#"+p).fadeIn(1);
        currentItem--;
        if (currentItem < 0) {
            currentItem = (length - 1);
        } // if
    }); // fadeOut
} // function

/**
 * @name RandPromotions
 * @internal Exibe nova promoção/benefício
 */
function RandPromotions() {
    ShowPromotion("servicos");
    ShowPromotion("projetos");
    ShowBeneficios("beneficios");
} // function

/**
 * @name ShowBeneficios
 * @internal Mostra a imagem e link de Benefícios.
 */
function ShowBeneficios(id) {
    $("#"+id).fadeOut(1000, function () {
        $(this).empty();
        $(this).fadeIn(1);
        var p = GetBeneficios();
        var l = GetURLPath();
        var u = "<a href='"+l+"suporte/"+p+"' ></a>";
        $(u).appendTo("#"+id);
        $("<img>").attr({
            id: id,
            src: l+"templates/blue/images/home/beneficios/"+p+".png",
            title: p
        }).appendTo("#"+id+" > a"); // appendTo
        if (!$.browser.msie) {
            $("img#"+id).css({
                display: "none"
            }); // css
        } // if
        $("img#"+id).load(function () {
            $(this).fadeIn(1000); 
        }) // load
    }) // function
} // function

/**
 * @name ShowPromotion
 * @internal Mostra a imagem e link de Promoções.
 */
function ShowPromotion(id) {   
    $("#"+id).fadeOut(1000, function () {        
        $(this).empty();
        $(this).fadeIn(1);
        var c = $(this).attr('class');
        var p = GetPromotion("&position="+c).split(',');
        var l = GetURLPath();
        var u = "<a href='"+l+"acoes/"+p[1]+"' ></a>";
        //$(u).appendTo("#"+id);
        $("#"+id).append(u);
        $("<img>").attr({
            id: id,
            src: l+"templates/blue/images/home/promocoes/"+p[1]+".png",
            title: p[0]
        }).appendTo("#"+id+" > a"); // appendTo
        if (!$.browser.msie) {
            $("img#"+id).css({
                display: "none"
            }); // css
        } // if
        $("img#"+id).load(function () {
            $(this).fadeIn(1000);
        }) // load
    }) // function
} // function

/**
 * @name GetPromotion
 * @param <String> - posição do banner.
 * @return <String>
 */
function GetPromotion(p) {    
    return $.ajax({
        type: "POST",
        url: SessionPathHome,
        data: "action=MakeBanner"+p,
        async: false
    }).responseText; // ajax
} // function

/**
 * @name GetBeneficios
 * @return <String>
 */
function GetBeneficios() {
    return $.ajax({
        type: "POST",
        url: SessionPathHome,
        data: "action=MakeBeneficiosBanner",
        async: false
    }).responseText;
} // function

/**
 * @name GetURLPath
 * @internal Recebe caminho completo da página. - Ajax
 */
function GetURLPath() {
    return $.ajax({
        type: "POST",
        url: SessionPathHome,
        data: "action=GetURLPath",
        async: false
    }).responseText;
} // function

/**
 * @name OpenNews
 */
function OpenNews(i) {
    var o = $("#opacidade");
    var m = $("#div_maior"+i);
    var src = m.attr('src');    
    if ((o.is(':visible'))) {
        $(".noticias_iframe_div").each(function () {
            $(this).fadeOut(1);
        });
    } // if
    else {
        o.fadeTo("fast",0.8);
    } // else
    m.empty();
    m.fadeIn();
    m.append("<div class='news_close' id='"+i+"' onclick='javascript:CloseNews()'></div>");
    m.append("<iframe src='"+src+"' height='500px' width='900'></iframe>");
} // function

/**
 * @name CloseNews
 */
function CloseNews() {
    if ($("#opacidade").is(':visible') && $('.noticias_iframe_div').is(':visible')) {
        $('.noticias_iframe_div').each(function () {
            $(this).fadeOut(50);
        });// each
        $('#opacidade').fadeOut(600);
    } // if
} // function
