// JavaScript Document

$(document).ready(function() {

    /*novo*/
    //sistema de tabs simples
    if ($("#cont1").length > 0) {
        $('#cont1 > ul').tabs();
    }

    //sistema de tabs simples
    if ($("#cont2").length > 0) {
        $('#cont2 > ul').tabs();
    }

    //sistema de tabs simples
    if ($("#container-1").length > 0) {
        $('#container-1 > ul').tabs();
    }

    //fix colunas iguais para ie6
    manageColHeigths1();
    //activar o png fix para pngs transparentes em ie6
    $(document).pngFix();
    $('body').pngFix();

    //fix as linhas verticais dos li previos dos links
    $(".navigationBar a:not(.activeMenuLink)").hover(function() {
        $(this).parent().prev().toggleClass("removeLiLine");
    }, function() {
        $(this).parent().prev().toggleClass("removeLiLine");
    });

    //remove linha vertical do previo li do link activo do menu principal
    if ($(".activeMenuLink").length > 0) {
        $(".activeMenuLink").parent().prev().addClass("removeLiLine");
    }

    if ($(".navigationBar").length > 0) {
        $(".navigationBar a:last").parent().addClass("removeLiLine");
    }

    if ($(".blockNavContexto").length > 0) {
        $(".blockNavContexto li:last").addClass("removeLiLine");
    }

    //detectar divs para alteracao font size
    if ($(".blockContentArtigo").length > 0) {
        changeFontSize(".blockContentArtigo,.blockUltimasEntradas");
    } else if (($(".blockUltimasEntradas").length > 0)) {
        changeFontSize(".blockUltimasEntradas");
    } else if (($(".blockTemplateArtigo1").length > 0)) {
        changeFontSize(".blockTemplateArtigo1");
    }

    //remover background li no ultimo a
    if ($(".containerArtigoOpcoes").length > 0) {
        $(".containerArtigoOpcoes a:last").parent().addClass("removeLiLine");
    }

    //adicionar border bottom na ultima div do bloco
    if ($(".leftBlockColItem .blockColItem1 .leftBlockColItem1").length > 0) {
        var sibs = $(".leftBlockColItem").children(":last-child");
        sibs.addClass("lastVerticalColItem");
    }

    //remover padding na ultima div do bloco
    if ($(".blockRowListaEdicoes .blockColEdicao").length > 0) {
        var sibs = $(".blockRowListaEdicoes").children(":last-child");
        sibs.prev().addClass("lastHorizontalColItem");
    }

    //adicionar border bottom na ultima div do bloco
    if ($(".blockRowPesquisaArquivo").length > 0) {
        var sibs = $(".blockRowPesquisaArquivo").children(":last-child");
        sibs.prev().addClass("noBorderBottom");
    }

    //tornar blocos iguais no footer destaque gps
    if ($(".blockFooterDestaque").length > 0) {
        manageColHeigthFooterDestaque();
    }

    //remover li background
    if ($(".blockPaginacaoMedia").length > 0) {
        $(".blockPaginacaoMedia li:last").addClass("removeLiLine");
    }

    //remover li background
    if ($(".blockResultadosPaginas").length > 0) {
        $(".blockResultadosPaginas ul li:last").addClass("removeLiLine");
    }

    //remover border dos produtos multiplos de 4 em gps gourmet lista
    if ($(".prodListaContent").length > 0) {
        initListagemProd();
    }

    //altura extra igual em multimedia
    /*if($(".blockNavMultimedia").length > 0 ){
    blockNavMultimediaHeigth = $('.blockNavMultimedia').height();
    $('.block .blockMultimediaExtraHeigth').css("height",blockNavMultimediaHeigth);
    }*/

    //adicionar tooltip para links header gps que nao estejam activos
    /*if($(".headerGps").length > 0 ){
    $(".headerGps .rightBlock .navigationBar ul li a[@title]").click(function() { return false; });
    $(".headerGps .rightBlock .navigationBar ul li a[@title]").tooltip({showURL: false});
    }*/

    /****************************************************************
    *****************************************************************
    *********** BEGIN BIG MULTIMEDIA (Código Alterado ZEONE)*********
    *****************************************************************
    *****************************************************************/
    //IF MODULE MULTIMEDIA BIG EXISTS
    if ($('.blockMainMultimedia').length) {
        //initModuleMultimedia();
    }
    if ($('#MultimediaDetailBig').length) {
        $("#MasterPageContentDiv").css('background-color', '#1A1A1A');
        $(".newsList").find("a").css('color', '#6E6E6E');
        $(".newsGroupText").find("a").css('color', '#6E6E6E');
    }
    /****************************************************************
    *****************************************************************
    *********** END BIG MULTIMEDIA (Código Alterado ZEONE)*********
    *****************************************************************
    *****************************************************************/

    //IF MODULE MULTIMEDIA TABS EXISTS
    if ($('.blockMultimediaTabs').length) {
        initModuleMultimediaTabs();
    }

    //IF MODULE FOTO DIA EXISTS
    if ($('.blockDayPhoto').length) {
        initModuleDayPhoto();
    }

    //IF MODULE SABADO GPS EXISTS
    if ($('.blockSabadoGps').length) {
        initModuleSabadoGps();
    }

    //IF MODULE SABADO GPS EXISTS
    if ($('.blockInputPesquisaNew').length) {

        $('.blockInputPesquisaNew input').bind('blur', function() {
            myBlur(this);
        });

        $('.blockInputPesquisaNew input').bind('focus', function() {
            myFocus(this);
        });
    }

    //IF MODULE MULTIMEDIA TAB PAGE EXISTS
    if ($('.bigTabMultimediaPage').length) {
        //$('#tabsMultimediaPage > ul').tabs();
        manageColHeigths1();
    }

    //IF MODULE PROMO TAB PAGE EXISTS
    if ($('.bigTabPromoPage').length) {
        initPromoPageTabs();
    }

    //IF MODULE DROP MENU
    if ($('.blockDropMenu').length) {
        initBlockDropMenu();
    }


});

function initBlockDropMenu() {
    var elDropMenu = $('.blockDropMenu');
    var elDropMenuHeader = elDropMenu.find('h2.moduleHeader');

    elDropMenuHeader.click(function() {
        // refresh netscope
        refreshNetscope(GetSectionNetScope(), GetSubSectionNetScope());
        
        var elMenuHeader = $(this);
        var elDropMenuContent = elMenuHeader.parent().parent().find('.blockDropMenuContent');
        var elScrollPane = elDropMenuContent.find('.scroll-pane');
        var elAccordion = elDropMenuContent.find('.blockDropMenuAccordion');
        if (elDropMenuContent.css('display') == 'none') {
            elMenuHeader.addClass('open');
            elDropMenuContent.slideDown('slow', function() {

                if (elScrollPane.length) {
                    elScrollPane.jScrollPane({ showArrows: true, scrollbarWidth: 15 });
                }
                if (elAccordion.length) {
                    elAccordion.accordion({
                        collapsible: true,
                        autoHeight: false
                    });
                }

            });
        } else {
            if (elScrollPane.length) {
                //elScrollPane.hide();
            }
            elDropMenuContent.slideUp('slow', function() {
                elMenuHeader.removeClass('open');
            });
        }

        return false;
    });
}


//INIT TABS ON PAGE PROMO

function initPromoPageTabs() {
    $('#tabsMultimediaPage > ul').tabs({
        show: function(e, ui) {
            var elCarousel = $('.blockCarrouselContainer', ui.panel);
            if (elCarousel.length) {
                $('#carrousel1', ui.panel).jcarousel();

                $('#carrousel1 a', ui.panel).click(function() {
                    var linkIndex = $(this).parent('li').prevAll().length;
                    $('.promoBlockContent', ui.panel).hide();
                    $('.promoBlockContent:eq(' + linkIndex + ')', ui.panel).show();
                    return false;
                });


            }
            changeFontSize('.promoBlockContent');
        }
    });
    changeFontSize('.promoBlockContent');

    $('.mainBlock').css("height", "auto");
}

//INIT LISTAGEM PROD 

function initListagemProd() {
    var prodListadoRow = $('.prodListaContent tr');

    $('.prodListaContent tr').each(function() {
        var tableRow = $(this);
        tableRow.find('td:eq(3)').addClass('noBorder');
    });
}


//############# funcao para equal columnss ie 6 ####################

function manageColHeigths1() {
    var blockLeftHeigth, blockRigthHeigth, largestBlock;
    if (detectBrowserIE6_v1()) {
        $('.mainBlock').css("height", "auto");
        blockLeftHeigth = $('.mainBlock .leftBlock').height();
        blockRigthHeigth = $('.mainBlock .rightBlock').height();
        largestBlock = Math.max(blockLeftHeigth, blockRigthHeigth);

        $('.mainBlock').css("height", largestBlock);
        $('.mainBlock .leftBlock').css("height", "100%");
        $('.mainBlock .rightBlock').css("height", "100%");

    }
}

//############# funcao para equal columnss blockFooterDestaque ####################

function manageColHeigthFooterDestaque() {
    var footerDestaqueHeigth, boxTopElementHeigth;
    footerDestaqueHeigth = $('.blockLeftFooterDestaque .boxTopElement').height();
    boxTopElementHeigth = (footerDestaqueHeigth / 2) - 15;
    $('.blockRigthFooterDestaque .boxTopElement:first').css("height", boxTopElementHeigth);
}

//############# funcao para aumentar diminui fontsize ####################
function changeFontSize(blockElement) {
    //aumenta diminui font size
    $('.blockLinksControlText a').click(function() {
        var $speech = $(blockElement);
        var currentSize = $speech.css('font-size');
        var num = parseFloat(currentSize, 10);
        var unit = currentSize.slice(-2);
        if (this.id == 'switcher-large') {
            num *= 1.4;
        } else if (this.id == 'switcher-small') {
            num /= 1.4;
        }
        if (num < 11) {
            $speech.css('font-size', 11 + unit);
        } else if (num > 10 && num < 24) {
            $speech.css('font-size', num + unit);
        }
        //alert(num);
        manageColHeigths1();
        return false;
    });
}

//############# funcao para detectar ie 6 v1 ####################

function detectBrowserIE6_v1() {
    jQuery.browser.msie6 = jQuery.browser.msie && parseInt(jQuery.browser.version) == 6 && !window["XMLHttpRequest"];
    return jQuery.browser.msie6;
}

//############# funcao para detectar ie 6 v2 ####################

function detectBrowserIE6_v2() {
    if (window.XMLHttpRequest) {
        // IE 7, mozilla, safari, opera 9
        var typeBrowser = false;
    } else {
        // IE6, older browsers
        var typeBrowser = true;
    }
    return typeBrowser;
}

//############# funcao para detectar ie 6 v3 ####################

function detectBrowserIE6_v3() {
    if (document.all && !window.opera && !window.XMLHttpRequest && $.browser.msie) {
        var isIE6 = true;
    } else {
        var isIE6 = false;
    }
    return isIE6;
}










//MODULE SABADO GPS

function initModuleSabadoGps() {
    $('#listProg li:even').addClass('even');

    $('.blockProgContainer').each(function() {
        $(this).find('.blockProgContent:last').addClass('noBorder')
    });

    $('#listProg > li a').mouseover(function() {
        $(this).parents('#listProg').children('li').removeClass('selected');
        $(this).parent().addClass('selected');

        var linkIndex = $(this).parent().prevAll().length;

        $('.blockProgContainer').hide();
        $('.blockProgContainer:eq(' + linkIndex + ')').show();

    });

    $('#listProg > li a').click(function(e) {
        //e.preventDefault();								 
    });
}

//END MODULE SABADO GPS
//MODULE FOTO DIA

function initModuleDayPhoto() {

    $('.blockDayPhoto .blockMediaCarrousel:eq(0)').show();

    $('#listDayPhoto').jcarousel();

    $('#listDayPhoto > li a').click(function() {
        var mediaArray = []

        var countLink = Number($(this).parent().prevAll().length);

        var carrouselContainer = $(this).parents('.blockCarrouselContainer');

        carrouselContainer.find('.blockMediaCarrousel').each(function() {
            mediaArray.push($(this));
        });
        carrouselContainer.find('.blockMediaCarrousel').remove();
        for (var i = mediaArray.length; i >= 0; i--) {
            carrouselContainer.prepend(mediaArray[i]);
        }
        carrouselContainer.find('.blockMediaCarrousel').hide();
        carrouselContainer.find('.blockMediaCarrousel:eq(' + countLink + ')').show();
        // reload thickbox events
        bindThickBoxEvents();
        mediaArray = new Array();
        // refresh netscope
        refreshNetscope();
        return false;
    });

}
// ********** Remove thickbox events ******************/
function removeThickBoxEvents() {
    $('.thickbox').each(function(i) {
        $(this).unbind('click');
    });
}
// ********** Reload thickbox events ******************/
function bindThickBoxEvents() {
    // need to remove and attach again tickbox to do not duplicate them
    removeThickBoxEvents();
    tb_init('a.thickbox, area.thickbox, input.thickbox');
}



//END MODULE FOTO DIA
//MODULE TABS MULTIMEDIA 

function initModuleMultimediaTabs() {
    $('#tabsMulti > ul').tabs();

    $('#carrousel1').jcarousel();

    initMediaCarrousel();

    $('.blockCarrouselContainer').each(function() {
        $(this).find('.blockMediaCarrousel:first').show();
    });

    //$('#carrousel1').carousel('.simplePrevious', '.simpleNext');

    $('#tabsMulti .ui-tabs-nav li:eq(0) a').click(function() {
        window.setTimeout(function() {
            $('#carrousel1').jcarousel();
        }, 500);
        initMediaCarrousel();
    });

    $('#tabsMulti .ui-tabs-nav li:eq(1) a').click(function() {
        window.setTimeout(function() {
            $('#carrousel2').jcarousel();
        }, 500);
        initMediaCarrousel();
    });
    $('#tabsMulti .ui-tabs-nav li:eq(2) a').click(function() {
        window.setTimeout(function() {
            $('#carrousel3').jcarousel();
        }, 500);
        initMediaCarrousel();
    });
    $('#tabsMulti .ui-tabs-nav li:eq(3) a').click(function() {
        window.setTimeout(function() {
            $('#carrousel4').jcarousel();
        }, 500);
        initMediaCarrousel();
    });
    // get event on click from media galery
    $('.blockCarrouselFrame ul > li a').click(function() {
        refreshNetscope('Galeria_Media');
    });



}

function initMediaCarrousel() {

    var count = 0;
    var mediaList = $('.blockCarrouselFrame ul li');
    var mediaArray = []

    $('.blockCarrouselFrame ul > li a').click(function() {
        var countLink = Number($(this).parent().prevAll().length);

        var carrouselContainer = $(this).parents('.blockCarrouselContainer');

        carrouselContainer.find('.blockMediaCarrousel').each(function() {
            mediaArray.push($(this));
        });
        carrouselContainer.find('.blockMediaCarrousel').remove();
        for (var i = mediaArray.length; i >= 0; i--) {
            carrouselContainer.prepend(mediaArray[i]);
        }
        carrouselContainer.find('.blockMediaCarrousel').hide();
        carrouselContainer.find('.blockMediaCarrousel:eq(' + countLink + ')').show();
        mediaArray = new Array();
        return false;
    });

}

//END MODULE TABS MULTIMEDIA 

//MODULE MULTIMEDIA BIG

//function initModuleMultimedia(){
//	
//	var mediaObjectArray = [];
//	
//	$('.blockMediaList:last').addClass('noBg');
//	$('.blockMediaList:first').addClass('selected');
//	
//	//blockMediaList CLICK EVENT HANDLER
//	
//	$('.blockMediaList').click(function(){
//		$(this).parent().children('.blockMediaList').removeClass('selected');
//		$(this).prev('.blockMediaList').addClass('noBg');
//		$(this).siblings().removeClass('selected').end().addClass('selected');
//		
//		var listIndex = $(this).prevAll().length;
//		
//		$('.blockMainMultimediaContent').hide();
//		$('.blockMainMultimediaContent:eq('+ listIndex +')').show();
//		
//		//DUE TO IE BUG, WE NEED TO REMOVE THE OBJECT AND APPEND IT AGAIN
//		$('.blockVideoContainer').each(function(){
//			mediaObjectArray.push($(this));									
//		});
//		$('.blockVideoContainer').remove();
//		$('.blockMainMultimediaContent').each(function(index){
//			$(this).find('h2').after(mediaObjectArray[index]);										   
//		});
//		mediaObjectArray = new Array();
//			
//	});
//	
//	//blockMediaList HOVER EVENT HANDLER
//	
//	$('.blockMediaList').hover(function(){
//		$(this).prev('.blockMediaList').addClass('noBg');
//		$(this).addClass('hoverSelected');
//	},function(){
//		$(this).parent().children('.blockMediaList:not(:last)').removeClass('noBg');
//		$(this).parent().children('.selected)').prev().addClass('noBg');
//		$(this).removeClass('hoverSelected');
//	});
//}


//END MODULE MULTIMEDIA BIG

//CLEAR DEFAULT VALUE INPUT SEARCH
function myFocus(element) {
    if (element.value == element.defaultValue) {
        element.value = '';
    };
}
function myBlur(element) {
    if (element.value == '') {
        element.value = element.defaultValue;
    };
}





