/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


$(function() {
    $('#filtros_home .botao').click(function() {
        $this = $(this);
        if(!$(this).hasClass('selected')) {
            if(!$(this).parent().hasClass('clicked')) {
                $('#direita_home a[class!=salvar_filtro]').bind('click',function() { return false; }).css({cursor:'default'});
                $('#direita_home div').each(function() {
                    //alert($(this).parents('#filtros_home'));
                    if($(this).attr('id') != 'filtros_home') {
                        if($(this).parents('#filtros_home').attr('id') == undefined) {
                            //$(this).fadeTo('fast',0.15);
                            $(this).hide();
                        }
                    }
                });
                $checked = $('#filtros_home .clicked').find('input[type=checkbox]:checked');
                $text = $('#filtros_home .clicked').find('.filtered');
                if($checked.length > 0 || $text.length > 0) {
                    homeApplyFilter($('#filtros_home .clicked'));
                }
                $('#filtros_home .clicked').find('.conteudo_filtro').slideUp();
                $('#filtros_home .clicked').removeClass('clicked');

                setTimeout("$this.parent().find('.conteudo_filtro').slideDown();",130);
                $this.parent().addClass('clicked');
            }
            else {
                $this.parent().find('.conteudo_filtro').slideUp();
                $('#filtros_home .clicked').removeClass('clicked');
                $('#direita_home a[class!=salvar_filtro]').unbind('click').css({cursor:'pointer'});
                //$('#direita_home div').fadeTo('fast',1);
                $('#direita_home div').each(function() {
                    //alert($(this).parents('#filtros_home'));
                    if($(this).attr('id') != 'filtros_home') {
                        if($(this).parents('#filtros_home').attr('id') == undefined) {
                            //$(this).fadeTo('fast',0.15);
                            $(this).show();
                        }
                    }
                });
                $checked = $(this).parent().find('input[type=checkbox]:checked');
                $text = $(this).parent().find('.filtered');
                
                if($checked.length>0 || $text.length>0) {
                    homeApplyFilter($(this).parent());
                }
            }
        }
        return false
    });

    $('.remove_filtro_home').each(function() {
        $(this).qtip({
            content: 'Remover esse filtro',
            hide: { fixed: true },
            style: { name: 'cream',tip:'topRight',border: { radius: 5 } },
            position: {
                corner: {
                    target: 'bottomLeft',tooltip: 'topRight'
                }
            }
        });
    });

    $('.option_filtro').mouseover(function() {
        $(this).addClass('hover');
    }).mouseout(function() {
        $(this).removeClass('hover');
    }).click(function() {
        input = $(this).find('input')
        if(input.is(':checked')) {
            $(this).removeClass('checked');
            input.removeAttr('checked');
        }
        else {
            input.attr('checked','checked');
            $(this).addClass('checked');
        }
    });

    $('.filtro .text').keyup(function() {
        if($(this).val() != '') { $(this).addClass('filtered') }
        else { $(this).removeClass('filtered') }
    });


    $('input[prompt]').each(function() {
        if($(this).val() == '') {
            $(this).val($(this).attr('prompt'));
        }
    }).focus(function() {
        if($(this).val()==$(this).attr('prompt'))$(this).val('');
    }).blur(function() {
        if($(this).val() == '')
            $(this).val($(this).attr('prompt'));
    });

    $('textarea[prompt]').each(function() {
        $(this).html($(this).attr('prompt'));
    }).focus(function() {
        if($(this).html()==$(this).attr('prompt'))$(this).html('');
    }).blur(function() {
        if($(this).val() == '')
            $(this).html($(this).attr('prompt'));
    });

    $('.remove_filtro_home').live('click',function() {
        $parent = $(this).parent();
        $parent.removeClass('selected');
        $parent.find('.checked_home').hide();
        $parent.find('.remove_filtro_home').hide();
        $parent.find('.option_filtro.checked').removeClass('checked');
        $parent.find('input[type=checkbox]').removeAttr('checked').removeClass('checked');
        $parent.find('input[type=text]').val('');
    });

});

function homeApplyFilter($this) {
    $this.find('.remove_filtro_home').show();
    $this.find('.checked_home').show();
    //$this.find('.botao').addClass('selected');
}

function empty(Valor) {
    if(Valor == '' || Valor == null || Valor == undefined) {
        return true;
    }
    else
        return false;
}

function isset(VarName) {
    type = undefined;
    try {
        eval('type = typeof('+VarName+')');
        if(type == 'undefined') {
            return false;
        }
        else {
            return true;
        }
    }
    catch(e) {
        return false;
    }
}

function Debug(Var) {
    if(isset('window.console')) {
        window.console.debug(Var);
        return true;
    }
    return false;
}

