﻿var submenuTimeout;
var quickAccessTimeout;
var month = new Date().getMonth();

function log(s) {
    try {
        if (window.console) {
            window.console.log(s);
        }
    }
    catch (e) { }
}

var functionArray = [];

function traceFunctionWithSelectorCall(pSelector, pFunctionName) {
    $(pSelector).each(function() {
        traceFunction(pFunctionName);
    });
}

function traceFunction(pFunctionName) {
    var index = $.inArray(pFunctionName, functionArray);
    if (index == -1) {
        functionArray.push(pFunctionName);

        log(pFunctionName);
    }
}

var NATURA = {
    _init: function() {
        try {
            NATURA._util();
            NATURA._tabs();
            NATURA._subMenu();
            NATURA._quickAccess();
            NATURA._siteMap();
            NATURA._userIdentify();
            NATURA._bgAlpha();
            NATURA._drop_list();
            NATURA._hoverH3();
            NATURA._inputHover();
            NATURA._loader();
        } catch (e) {
            if (e) {
                alert(
				'Arquivo   : ' + e.fileName + ' \n ' +
				'Linha     : ' + e.lineNumber + ' \n ' +
				'Nome      : ' + e.name + ' \n ' +
				'Descrição : ' + e.message + ' \n '
			    );
            }
        };
    },

    /*
    * inicio funções globais
    */

    _util: function() {
        /* IE png fix */
        if (!/msie [^6]\.0/i.test(navigator.userAgent) && /msie 6\.0/i.test(navigator.userAgent)) {
            DD_belatedPNG.fix('.pngfix, #nav a, #header ul, .rpl, .bege, .bg_default');
        };

        /* target blank */
        $("a[rel='external-link']").attr('target', '_blank');

        /* color checkbox */
        $('.filter_category').find('input:checked').next().css('color', '#3E1A5F');

    },

    _tabs: function() {

        //traceFunctionWithSelectorCall('.tabs', '_tabs');

        $('.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.linktab').live('click', function() {
                    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');
                    nav.find('a.linktab').parent().removeClass('border_bottom_none').removeClass('nodash');
                    $(this).addClass('selected');
                    $(this).parent().addClass('border_bottom_none');

                    if (!$(this).parent().is(".tabs .tabs_nav li:first-child"))
                        $(this).parent().prev().addClass("nodash");

                    content.removeClass('show').eq(x).addClass('show');
                    // return false;
                });
            });
        });
    },

    _subMenu: function() {

        //traceFunctionWithSelectorCall('#main-nav li a', '_subMenu');

        $('#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();
        },
		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', 'block');
                }
            }, 100);
        });
        $("#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(quickAccessTimeout);

            $("ul.nav_list li").each(function() {
                hideMenu(this);
            });

            $(this).find("a:eq(0)").addClass("active");
            if ($(".sub_nav_list", this).length > 0) {
                $(this).width($(".sub_nav_list", this).width());

                if ($(this).attr("className").indexOf("quick_access") > -1) {
                    $(this).css("width", "136px");
                }

                $(".sub_nav_list", this).show();
            }
        });

        $("ul.nav_list li.list_item, ul.nav_list li.quick_access").live('mouseout', function() {
            currentItem = this;

            if ($(this).attr("className").indexOf("quick_access") > -1) {
                $(this).css("width", "136px");
            }

            quickAccessTimeout = setTimeout(function() {
                hideMenu(currentItem);
            }, 100);
        });

    },

    _siteMap: function() {

        //traceFunctionWithSelectorCall('a.bt_sitemap', '_siteMap');

        $("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');


            var b = $('body')[0];
            $(b).scrollTo('100%', 200, { queue: true })
        });
    },

    _userIdentify: function() {
        $("#user_identify a.open_form").live('click', function() {
            showIdentification();
            return false;
        });
    },

    _bgAlpha: function() {

        //traceFunctionWithSelectorCall('.txt_placeholder', '_bgAlpha');

        $('.txt_placeholder').mouseover(function() {
            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')
            }
        });
    },

    _drop_list: function() {

        //traceFunctionWithSelectorCall('.drop_list ol>li', '_drop_list');

        $(".drop_list ol>li").live('mouseover', function() {
            $(".drop_sub", this).show()
        });

        $(".drop_list ol>li").live('mouseout', function() {
            $(".drop_sub", this).hide()
        });

    },

    _expandir: function() {

        //traceFunctionWithSelectorCall('.expandir', '_expandir');    
        $('.expandir').live('click', function() {
            var item = $('.compartilhamento');
            var i_altura = item.height();
            console.info(i_altura);
            if (i_altura == 22) {
                $(this).addClass('recolher');
                item.css('height', '117px')

            } else {
                $(this).removeClass('recolher');
                item.css('height', '22px')
            }
        })
    },

    _hoverH3: function() {

        //traceFunctionWithSelectorCall('h3 a', '_hoverH3');

        $('h3 a').live('mouseover', function() {
            $(this).removeClass('gillSans');
            $(this).addClass('gillSansHover');
            Cufon.replace('.gillSansHover', { fontWeight: '300', color: '#3E1A5F' });
        });
        $('h3 a').live('mouseout', function() {
            $(this).removeClass('gillSansHover');
            $(this).addClass('gillSans');
            Cufon.replace('.gillSans', { fontWeight: '300', color: '#1a1a1a' });
        });

    },

    _inputHover: function() {

        //traceFunctionWithSelectorCall('.btInput', '_inputHover');

        $('.btInput').mouseover(function() {
            $(this).css('background-position', '-22px 0px')
        });
        $('.btInput').mouseout(function() {
            $(this).css('background-position', '0px 0px')
        });

    },

    _loader: function() {
        var div = $('#loading_progress').css({ position: 'absolute' });
        $(document).mousemove(function(e) {
            div.css({ top: (e.pageY + 18), left: (e.pageX + 18) });
        });
    }
};

function showIdentification() {
    traceFunction('showIdentification');
    $("#user_identify").addClass("open");
    $("#user_identify p").hide();
    $("#user_identify fieldset.open_login ").show();

    return false;
}

function hideIdentification() {
    $("#user_identify").removeClass("open");
    $("#user_identify p").show();
    $("#user_identify fieldset.open_login ").hide();

    return false;
}

function hideMenu(item) {
    $(item).find("a:eq(0)").removeClass("active");
    if ($(item).hasClass("quick_access"))
        $(item).width("136px");
    else {
        $(item).width("auto");
    }
    $(".sub_nav_list li").height("20px");
    $(".sub_nav_list", item).hide();
}

function isPostBack() {
    if (document.referrer != null && document.referrer == window.location.href) {
        return true;
    }

    return false;
}

var paginationButtonEventOrigin = false;

function triggerPagination(pageIndex) {
    // Used only in browser navigation (back and forward) and initial load
    if (paginationButtonEventOrigin == false) {
        if ($(".pagging a:contains('" + pageIndex + "')").length > 0) {
            eval($(".pagging a:contains('" + pageIndex + "')").attr("href").replace("javascript:", ""));
        }
//        else {
//            var mainFormSelector = '#aspnetForm';
//        
//            var input = $('<input>').attr('type', 'hidden').attr('name', 'PageIndex').val(pageIndex);
//            $(mainFormSelector).append($(input));
//            $(mainFormSelector).submit();
//        }
    }

    paginationButtonEventOrigin = false;
}

$(window).bind('hashchange', function(e) {
    var pageIndex = e.getState("PageIndex") || 1;

    triggerPagination(pageIndex);
});

$(document).ready(function() {
    NATURA._init();

    if (window.location.hash.indexOf('PageIndex') > -1) {
        var pageIndex = window.location.hash.split('=')[1];

        triggerPagination(pageIndex);
    }

    $('.pagging a').live('click', function() {
        $.bbq.pushState({ PageIndex: $(this).text() });
        paginationButtonEventOrigin = true;
    });
});

