var submenuTimeout;
var menuTimeout
var quickAccessTimeout;
var month = new Date().getMonth();
var currentMenu;
var currentSubmenu;

var NATURA = {
    _init: function() {
        try {
            NATURA._pngfix();
            //NATURA._external_link();
            //NATURA._change_val();
            NATURA._natura_na_web();
            NATURA._subMenu();
            NATURA._quickAccess();
            NATURA._siteMap();
            NATURA._dateMask();
            //NATURA._userIdentify();
            NATURA._bgAlpha();
            NATURA._InsertCalenderCorrent();
            NATURA._calendarioIndex();
            NATURA._highlight_home_revista();
            NATURA._drop_list();
            NATURA._calendarioIndex();
            NATURA._hoverH3();
            NATURA._loader();
            NATURA._tabs();
            NATURA._tabsDescProdutos();
            NATURA._altProdutos();
            NATURA._productRelNatura();
            NATURA._passwordText();

            NATURA._highlight_more_content_();
            // NATURA._inputHover();
        } catch (e) {
            if (e) {
                alert(e.description);
            }
        };
    },

    //função para exibir o calendario no mês corrente.
    _InsertCalenderCorrent: function() {
        var calender = $('#cal01').html();
        $('corrente').html('');
        $('.mes .cal').removeClass('corrente');
        $('#cal' + month).addClass('corrente');
        $('#cal' + month).html(calender);
    },

    _calendarioIndex: function() {
        //mês corrente
        alert('#1');
        var atulMonth = month;

        var current = $('.mes .cal.corrente');
        $('.mes .cal').hide();
        current.show();
        current.prev('h3').hide();
        if (atulMonth != month) {
            alert('#2');
            var current = $('.mes .cal.corrente');
            $('.mes .cal').hide();
            current.show();
            current.prev('h3').hide();
            $('.mes h3 a').click(function() {

                var nextCal = $(this).parent().next('.cal');
                var ant = $(".corrente").html();

                $(".corrente").html('');

                $('.mes .cal').removeClass('corrente');
                $('.mes .cal').hide();
                $('.mes h3').show();
                $(this).parent().hide();

                nextCal.show();
                nextCal.addClass('corrente');
                $(".mes .cal").html(ant);
            });
        }
    },

    _pngfix: function() {
        if (!/msie [^6]\.0/i.test(navigator.userAgent) && /msie 6\.0/i.test(navigator.userAgent)) {
            DD_belatedPNG.fix('img, .pngfix, #nav a, #header ul, .rpl, .bege');
        };
    },
    _tabs: function() {
        $('.tabs').each(function() {
            var tab = $(this),
				nav = $(this).find('.tabs_nav'),
				nav_li = nav.find('li'),
				content = $(this).find('.tabs_content');

            nav_li.each(function(x) {
                $('a.linktab').live('click', function(e) {

                    if ($(this).attr('rel') == 'open-others') {
                        tab.find('.drop_list').show();
                        NATURA._drop_list();
                    }
                    else {
                        tab.find('.drop_list').hide();
                    };
                    nav.find('a.linktab').removeClass('selected');
                    $(this).addClass('selected');
                    content.removeClass('show').eq(x).addClass('show');
                    // return false;
                });
            });
        });
    },

    _tabsDescProdutos: function() {
        $('.products_tabs').each(function() {
            var tab = $(this),
				nav = $(this).find('.tabs_nav'),
				nav_li = nav.find('li'),
				content = $(this).find('.tabs_content');

            nav_li.each(function(x) {
                $(this).find('a').click(function() {
                    nav.find('a').removeClass('selected');
                    $(this).addClass('selected');
                    content.removeClass('show').eq(x).addClass('show');
                    return false;
                });
            });
        });
    },

    _natura_na_web: function() {
        var t = $('#natura_na_web .nav'),
			t_l = $('#natura_na_web .nav li').length,
			f = function(x) {
			    t.find('li:eq(' + x + ') a').live('click', function() {
			        t.find('li a').removeClass('selected');
			        $(this).addClass('selected');
			        //return false;
			    });
			};

        if (t_l > 0) {
            for (var x = 0; x < t_l; x++) {
                f(x);
            };
        };
    },

    _subMenu: function() {
        $('#main-nav li a').hover(function() {
            $('#main-nav li a:not(' + this + ')').removeClass('active');
            $(this).addClass('active');
            $('#main-nav-sub ul').css('display', 'none');
            last = $(this);
            var menu = $(this).attr('rel');
            $(menu).css('display', 'block').parent();
			
			currentMenu = $(this).attr('rel');
			currentSubmenu = menu;
        },
		    function() {
		        $(this).removeClass('active');
		        var menu = $(this).attr('rel');
		        $(menu).css('display', 'block').parent();
		        if ($(last).length) {
		            $(last).addClass('active');
		        }
		    });
        $("#nav").mouseout(function() {
            submenuTimeout = setTimeout(function() {
                if (typeof (currentMenu) != "undefined") {
                    $('#main-nav li a.active').removeClass("active");
                    $(currentMenu).addClass("active");
                }
                if (typeof (currentSubmenu) != "undefined") {
                    $('#main-nav-sub ul').css('display', 'none');
                    $(currentSubmenu).css('display', 'none');
                }
            }, 300);
        });
        $("#nav ol li, #main-nav-sub").mouseover(function() {
            clearTimeout(submenuTimeout);
        });
    },

    _quickAccess: function() {
        $("ul.nav_list li.list_item, ul.nav_list li.quick_access").live('mouseover', function() {
            clearTimeout(menuTimeout);
            $(".sub_nav_list").hide();
            $(this).find("a:eq(0)").addClass("active");

            var count = $(".sub_nav_list", this).length;

            if (count == 0)
                $(this).find("a:eq(0)").css("border-bottom", "none")

            try {
                $(this).width($(".sub_nav_list", this).width());
                $(".sub_nav_list", this).show();
            }
            catch (e) {

            }

        });

        $("ul.nav_list li.list_item, ul.nav_list li.quick_access").live('mouseout', function() {
            $(this).find("a:eq(0)").removeClass("active");

            $(this).css("width", "auto");

            if ($(this).attr("className").indexOf("quick_access") > -1) {
                $(this).css("width", "145px");
            }

            menuTimeout = setTimeout(function() {
                $(".sub_nav_list").hide();
            }, 100);
        });

        $("ul.nav_list li.list_item div a, ul.nav_list li.quick_access  div a").mouseover(function() {
            clearTimeout(menuTimeout);
        });
    }
    ,

    _siteMap: function() {
        $("a.bt_sitemap").toggle(function() {
            $("div.content_sitemap").hide()
				    .parent()
				    .find('a.bt_sitemap')
				    .addClass('mais');

        }, function() {
            $("div.content_sitemap").show()
				    .parent()
				    .find('a.bt_sitemap')
				    .removeClass('mais');
        });
    },

    //Função para atribuir uma mascara de data no formato 99/99/9999.
    _dateMask: function() {
        $(".dataMascara").mask("99/99/9999");
        $(".horaMascara").mask("99:99");
    }
    ,

    _userIdentify: function() {
        //$("#user_identify a.open_form").live('click', function() {
        showIdentification();

        return false;
        //});
    },
    /*
    *
    * função para ativar o hover dos boxes que tem imagem com alpha
    *
    */

    _bgAlpha: function() {
        $('.txt_placeholder').mouseover(function() { //TODO: código passível de erros quando utilizado com AJAX, trocar por .live no mouseover e mouseout e testar
            if ($(this).hasClass('negative')) {
                $(this).addClass('opaco')

            } else if ($(this).hasClass('bege')) {
                $(this).addClass('opaco')

            } else {
                $(this).addClass('opaco')
            } // função de callbeck
        }).mouseout(function() {
            if ($(this).hasClass('negative')) {
                $(this).removeClass('opaco')

            } else if ($(this).hasClass('bege')) {
                $(this).removeClass('opaco')

            } else {
                $(this).removeClass('opaco')
            }
        });
    },
    /*
    * destaque home revista
    */
    _highlight_home_revista: function() {
        var c = $('.highlight .content_highlight'),
			cc = $('.highlight .content_control');
        if (!c.length && !cc.length) return false;

        var qtd = c.length - 1,
			prev = $('.highlight .bt_prev'),
			next = $('.highlight .bt_next'),
			pause = $('.highlight .bt_pause'),
			cont = 0,
			delay = 8000;

        $(pause).toggle(
			function() {
			    setTimeout(function() {
			        cont++;
			        intervalo = setInterval(function() { action(cont); cont < qtd ? cont++ : cont = 0; }, delay);
			    }, 1);
			    return false;
			},
			function() {
			    stop();
			    return false;
			}
		);

        $(prev).live('click', function() {
            stop();
            cont == 0 ? cont = qtd : cont--;
            action(cont);
            return false;
        });

        $(next).live('click', function() {
            stop();
            action(cont);
            cont < qtd ? cont++ : cont = 0;
            return false;
        });

        function stop() {
            clearInterval(intervalo);
            return false;
        };

        function action(x) {
            c.stop();
            c.fadeTo(300, 0, function() {
                $(this).addClass('hide');
                c.eq(x).css('dysplay', 'none').removeClass('hide').fadeTo(300, 1);
            });
        };

        pause.click();
    },


    _highlight_more_content_: function() {
        $('.more_highlight_').each(function() {
            var c = $(this).find('.more_content_highlight_'),
				cc = $(this).find('.content_control_');
            if (!c.length && !cc.length || cc.length < 1) return false;

            var qtd = c.length - 1,
				prev = cc.find('.bt_prev'),
				next = cc.find('.bt_next'),
				cont = 0,
				delay = 8000;

            $(prev).live('click', function(event) {
                event.preventDefault();
                stop();
                cont == 0 ? cont = qtd : cont--;
                action(cont);
            });

            $(next).live('click', function(event) {
                event.preventDefault();
                stop();
                cont < qtd ? cont++ : cont = 0;
                action(cont);
            });

            function stop() {
                c.stop(true, true);
                return false;
            };

            function action(x) {
                c.fadeTo(300, 0, function() {
                    $(this).addClass('hide');
                    c.eq(x).css('opacity', 0).removeClass('hide').fadeTo(300, 1);
                });
            };

            setTimeout(function() {
                action(cont);
            }, 1);
        });
    },


    /*
    * dropdow list - dia,semana e mês
    */
    _drop_list: function() {

        $(".drop_list ol>li").live('mouseover', function() {
            $(".drop_sub", this).show()
        });

        $(".drop_list ol>li").live('mouseout', function() {
            $(".drop_sub", this).hide()
        });

    },
    /*
    * modal, expandir opções de compartilhamento
    */
    _expandir: function() {
        $('.expandir').live('click', function() {
            var item = $('.compartilhamento');
            var i_altura = item.height();
            if (i_altura == 28) {
                $(this).addClass('recolher');
                item.css('height', '200px')
            } else {
                $(this).removeClass('recolher');
                item.css('height', '28px')
            }
        })
    },

    _hoverH3: function() {

        $('h3 a').live('mouseover', function() {
            $(this).removeClass('gillSans');
            $(this).addClass('gillSansHover');
            Cufon.replace('.gillSansHover', { fontWeight: '300', color: '#333333' });
        });
        $('h3 a').live('mouseout', function() {
            $(this).removeClass('gillSansHover');
            $(this).addClass('gillSans');
            Cufon.replace('.gillSans', { fontWeight: '300', color: '#999999' });
        });

    },

    _inputHover: function() {

        $('.btInput').mouseover(function() {
            $(this).css('background-position', '-22px 0px')
        });
        $('.btInput').mouseout(function() {
            $(this).css('background-position', '0px 0px')
        });

    },

    //Menu do administrador

    _subMenuAdmin: function() {
        $("div#menu > ol > li").hover(function() {
            $("ul", this).slideDown("fast")
				.parent()
				.find('a')
				.addClass('active');
        }, function() {
            $("ul", this).slideUp("fast")
				.parent()
				.find('a')
				.removeClass('active');
        });
    },

    /*
    função para alert área do site index, 
    altera os calendários que vão ficar visíveis
    */
    _calendarioIndex: function() {
        //mês corrente
        var current = $('.mes .cal.corrente');


        $('.mes .cal').hide();
        current.show();
        current.prev('h3').hide();
        $('.mes h3 a').click(function() {

            var nextCal = $(this).parent().next('.cal');

            $('.mes .cal').removeClass('corrente');
            $('.mes .cal').hide();
            $('.mes h3').show();
            $(this).parent().hide();

            nextCal.show();
            nextCal.addClass('corrente');
            return false
        });
    },

    _loader: function() {
        var div = $('#loading_progress').css({ position: 'absolute' });
        $(document).mousemove(function(e) {
            div.css({ top: (e.pageY + 18), left: (e.pageX + 18) });
        });
    },

    _altProdutos: function() {

        $('.ulProcutos li a').click(function(e) {
            e.preventDefault();

            $('.box1').hide();
            $('.box2').hide();
            $('.box3').hide();
            $('.box4').hide();
            $('.box5').hide();
            $('.box6').hide();

            $('.ulProcutos li a').removeClass('active');

            if ($(this).hasClass('liBox1')) {
                $(this).addClass('active');
                $('.box1').show();
            };
            if ($(this).hasClass('liBox2')) {
                $(this).addClass('active');
                $('.box2').show();
            };
            if ($(this).hasClass('liBox3')) {
                $(this).addClass('active');
                $('.box3').show();
            }
            if ($(this).hasClass('liBox4')) {
                $(this).addClass('active');
                $('.box4').show();
            }
            if ($(this).hasClass('liBox5')) {
                $(this).addClass('active');
                $('.box5').show();
            }
            if ($(this).hasClass('liBox6')) {
                $(this).addClass('active');
                $('.box6').show();
            }
        });
    },
    _passwordText: function() {
        $('.password-clear').show();
        $('.password-password').hide();

        $('.password-clear').focus(function() {
            $('.password-clear').hide();
            $('.password-password').show();
            $('.password-password').focus();
        });

        $('.password-password').blur(function() {
            if ($('.password-password').val() == '') {
                $('.password-clear').show();
                $('.password-password').hide();
            }
        });
    },

    _productRelNatura: function() {
        $('.ulRelNatura input').each(function() {
            $(this).click(function(e) {
                if ($(this).parent().hasClass('checkColaborador')) {
                    $('.textConsultor').hide();
                    if ($(this).attr('checked')) {
                        $('.textColaborador').show();
                    }
                    else {
                        $('.textColaborador').hide();
                    }
                    $('span.checkConsultor input').removeAttr('checked');
                    $('span.checkPromotor input').removeAttr('checked');
                }
                if ($(this).parent().hasClass('checkConsultor')) {
                    $('.textColaborador').hide();
                    if ($(this).attr('checked')) {
                        $('.textConsultor').show();
                    }
                    else {
                        $('.textConsultor').hide();
                    }
                    $('span.checkColaborador input').removeAttr('checked');
                    $('span.checkPromotor input').removeAttr('checked');
                }
                if ($(this).parent().hasClass('checkPromotor')) {
                    $('.textConsultor').hide();
                    $('.textColaborador').hide();
                    $('span.checkConsultor input').removeAttr('checked');
                    $('span.checkColaborador input').removeAttr('checked');

                }
            })
        })
    }
}


function showIdentification() {
    $("#user_identify p").hide();
    $("#user_identify fieldset.open_login ").show();

    return false;
}

function hideIdentification() {
    $("#user_identify p").show();
    $("#user_identify fieldset.open_login ").hide();

    return false;
}

function abreIframe(url) {
    /*var location = String(document.location).replace("#", "");
    var iframeSrc = String(document.getElementById('iframeRedeSocial').src).replace("#", "");
    jQuery('.boxSiteExterno').css('zIndex', '80')
    if (iframeSrc == location) {
        var maskHeight = jQuery(document).height();
        var maskWidth = jQuery(window).width();
        jQuery('#iframeRedeSocial').css('height', maskHeight);
        jQuery('.webdoor_flash').css('display', 'none');
        jQuery('#RedeSocialBox').css('display', 'block')
        jQuery('#RedeSocialBox').css('float', 'right')
        jQuery('#RedeSocialBox').css('width', '0px')
        jQuery('#RedeSocialBox').animate({ width: maskWidth }, 2000, function() {
            // Animation complete.
        });
        document.getElementById('iframeRedeSocial').src = url;
    }
    else {
        if (iframeSrc == url) {
            jQuery('#RedeSocialBox').animate({ width: '1' }, 1000, function() {
                jQuery('#RedeSocialBox').css('display', 'none')
                jQuery('.boxSiteExterno').css('zIndex', '0')
            });
            document.getElementById('iframeRedeSocial').src = '';
            jQuery('.webdoor_flash').css('display', 'block')
        }
        else {
            document.getElementById('iframeRedeSocial').src = '';
            jQuery('#RedeSocialBox').animate({ width: '1' }, 1000, function() {
                // executa depois de fechar a janela														   
                jQuery('#RedeSocialBox').css('display', 'none')
                jQuery('.boxSiteExterno').css('zIndex', '80')
                var maskHeight = jQuery(document).height();
                var maskWidth = jQuery(window).width();
                jQuery('#iframeRedeSocial').css('height', maskHeight);
                jQuery('.webdoor_flash').css('display', 'none');
                jQuery('#RedeSocialBox').css('display', 'block')
                jQuery('#RedeSocialBox').css('float', 'right')
                jQuery('#RedeSocialBox').css('width', '0px')
                jQuery('#RedeSocialBox').animate({ width: maskWidth }, 2000, function() {
                    // Animation complete.
                });
                document.getElementById('iframeRedeSocial').src = url;
            });

        }

    }*/
}



$(document).ready(function() {
    NATURA._init();

    /*jQuery('.mn_facebook a').click(function() {
      abreIframe('http://www.facebook.com/#!/pages/Natura/382381445421?ref=ts')
    });


    jQuery('.mn_twitter a').click(function() {
        abreIframe('http://twitter.com/cuidedapele')
    });

    jQuery('.mn_flickr a').click(function() {
        abreIframe('http://www.flickr.com/photos/naturacosmetico/tags/cuidedapele/')        
    });

    jQuery('.mn_youtube a').click(function() {
        abreIframe('http://www.youtube.com/cuidedapele')
    });
    */
    
});

