$(function(){

    $(".gosearch").click(function(){
        $(this).parent().submit();
        return false;
    });
    $("#big_login_submit").click(function(){
        $("#big_login_form").submit();
        return false;
    });

    $("#reg_form_submit").click(function(){
        var is_agree = $("#is_term").attr("checked");
        if( is_agree == true ){
            $("#is_term").parent().css('color', '#000000');
            $("#is_term").parent().css('font-weight', 'normal');
            $("#reg_form").submit();
        }
        else{
            $("#is_term").parent().css('color', '#CC0000');
            $("#is_term").parent().css('font-weight', 'bold');
        }
        //$("#reg_form").submit();
        return false;
    });

    $("#update_pass_form_submit").click(function(){
        $("#pass_form").submit();
        return false;
    });

    $(".lansel").hide();

    /*$(".lan").mouseover(function(){
        $(".lansel").show();
        return false;
    });
    
    $(".lan").mouseout(function(){
        $(".lansel").hide();
        return false;
    });

    $(".lan").click(function(){
        var icon = $(this).children(".icon").val();
        var id = $(this).children(".id").val();
        $('input[name="language_search"]').val(id);
        $(".current_language_flag").attr('src', icon);
        $(".lansel").hide();
        return false;
    });*/


    $("#invitation").click(function(){
        
        return false;
    });


    $('#popupContact [type=text]').live('focus', function(){
        $(this).css('background-color', '#FAFAFA');
        $(this).css('border', '1px solid #000');
    });

    $('#popupContact [type=text]').live('focusout', function() {
        $(this).css('background-color', '#FFF');
        $(this).css('border', '1px solid #DDD');
    } );


    $('select').live('focus', function(){
        $(this).css('background-color', '#FAFAFA');
        $(this).css('border', '1px solid #000');
    });

    $('select').live('focusout', function() {
        $(this).css('background-color', '#FFF');
        $(this).css('border', '1px solid #DDD');
    } );

    $('.qs').focus(function(){
        if( $(this).val() == 'Quick search' ){
            $(this).val('');
            $(this).css('color', '#000000');
        }
    });

    $('.qs').blur(function(){
        if( $(this).val() == '' ){
            $(this).val('Quick search');
            $(this).css('color', '#999999');
        }
    });
    
    function in_array(what, where) {
        for(var i=0; i<where.length; i++)
            if(what == where[i]) 
                return true;
        return false;
    }

    var checked = [];
    var count_checked = 0
    
    function inCheckedArray(what){
        var where = checked;
        for(var i=0; i<where.length; i++)
            if( ( what['s'] == where[i]['s'] ) && ( what['d'] == where[i]['d'] ) ){
                alert( what['s'] == where[i]['s'] )
                return true;
            }
                
        return false;
    }
    
    $('.qs').keyup(function(){
        var qs_text = $(this).val();
        var dictionary = $('#current_dictionary').val();
        var i = checked.length;
        //alert(i);
        $("#dtable .active .check").each(function(){
            checked[i] = [];
            checked[i]['s'] = $(this).parent().parent().find(".source").text();
            checked[i]['d'] = $(this).parent().parent().find(".destination").text();            
            i++;
        });
        $.ajax({
            type: "POST",
            url: window.location.pathname,
            dataType: "json",
            data: "action=search&dictionary_id="+dictionary+"&quick_search="+qs_text,
            success: function(data) {
                $("#dtable tbody").html('');
                   
                $.each(data, function(index, value){
                    var new_checked = [];
                    new_checked['s'] = value.source;
                    new_checked['d'] = value.destination;
                    if(inCheckedArray(new_checked)){
                        var need_to_check = true;             
                    }else{
                        var need_to_check = false;
                    }
                    var append = '';
            
                    if(need_to_check){
                        append +='<tr class="active" valign="top">';
                    }else{
                        append +='<tr valign="top">';
                    }
                    append +='<input type="hidden" value="'+value.id+'" name="term_id">';
                    if( !is_shared ){
                        if(need_to_check){
                            append += '<td><span class="active check"></span></td>';
                        }else{
                            append += '<td><span class="check"></span></td>';
                        }
                    }
                    append +='<td width="263px" class="term source">'+value.source+'</td>'+
                        '<td width="257" class="trnsl term destination"> '+value.destination+
                        '</td>'+
                        '</tr>';
                    $("#dtable tbody").append(append);
                });
                $('.jspPane').css('top', 0);
            }
        });
        //alert( qs_text );
        //$('.qs_source').hide();
        //$('.qs_source[name^="'+qs_text+'"]').parent().css('background-color', '#CCCC00');
        //alert(qs_text);
        
        //$('.qs_source[name^="'+qs_text+'"]').parent().show();
        // $('.qs_source[name^="'+qs_text+'"]').parent().css('background-color', '#CCCC00');
        /*
       $('.qs_source').parent().hide();
       $('.qs_source').filter(function (index) {
           var name = $(this).attr('name');
           var rg = new RegExp(qs_text,'i');
           if (name.search(rg)>=0){
               $(this).parent().show();
           }
       });
       $('.qs_destination').filter(function (index) {
           var name = $(this).attr('name');
           var rg = new RegExp(qs_text,'i');
           if (name.search(rg)>=0){
               $(this).parent().show();
           }
           
       });
         */




    });

    $(".add_share_emails").focus(function(){
        if( $(".add_share_emails").val() == 'Enter email address'){
            $(".add_share_emails").val('');
            $(".add_share_emails").css('color', '#000000');
            $(".add_share_dictionary").show();
            $(".cancel_share_dictionary").show();
        }
    });

    $(".add_share_emails").blur(function(){
        if( $(".add_share_emails").val() == '' ){
            $(".add_share_emails").val('Enter email address');
            $(".add_share_emails").css('color', '#C1C1C1');
        }
    });

    $(".cancel_share_dictionary").click(function(){
        $(".add_share_dictionary").hide();
        $(".cancel_share_dictionary").hide();
        $(".add_share_emails").val('Enter email address');
        $(".add_share_emails").css('color', '#C1C1C1');
    });

    $(".add_share_dictionary").click(function(){
        var dictionary_id = $("#current_dictionary").val();
        var email = $(".add_share_emails").val();
        if( email != '' && email != 'Enter email address' ){
            $.ajax({
                type: "POST",
                url: site_url+'ajax/share',
                dataType: "json",
                data: "email="+email+"&dictionary_id="+dictionary_id,
                success: function(data){
                    $(".cancel_share_dictionary").click();
                    $(".permissions-list tbody").append('<tr class="acl-entry-item acl-permission"><td class="acl-entry-item-name">'+email+'</td></tr>');
                }
            });
        }
        else{
            $(".add_share_emails").focus();
            return false;
        }
    });

    $(".stop_sharing").click(function(){
        var dictionary_id = $("#current_dictionary").val();
        var message = "Are you sure you want to stop sharing?"
        if( confirm(message) ){
            $.ajax({
                type: "POST",
                url: site_url+'ajax/stop_share',
                dataType: "json",
                data: "dictionary_id="+dictionary_id,
                success: function(data){
                    $(".permissions-list tbody").html('');
                    disablePopup( $(".share_dictionary_popup") );
                }
            });
        }
    });

    $(".accept_alert").click(function(){
        $(".alert").hide();
        var dictionary_id = $("#current_dictionary").val();
        $.ajax({
            type: "POST",
            url: site_url+'ajax/confirm_share',
            dataType: "json",
            data: "dictionary_id="+dictionary_id
        });
    });

    $(".decline_alert").click(function(){
        $(".alert").hide();
        var dictionary_id = $("#current_dictionary").val();
        $.ajax({
            type: "POST",
            url: site_url+'ajax/decline_share',
            dataType: "json",
            data: "dictionary_id="+dictionary_id,
            success: function(){
                window.location.replace(site_url+"my_account");
            }
        });
    });
    $(".clear-dictionary").click(function(){
        var dictionary_id = $(this).parent().parent().attr('id');
        var message = "Are you sure you want to clear this dictionary?"
        if( confirm(message) ){
            $.ajax({
                type: "POST",
                url: site_url+'ajax/clear_dictionary',
                dataType: "json",
                data: "dictionary_id="+dictionary_id,
                success: function(data){
                    $(this).parent().parent().remove();
                }
            });
            if( $(this).parent().parent().hasClass('current') ){
                $("#dtable tbody").html('');
            }
        }
    });
    $(".delete-dictionary").click(function(){
        var list = $(this).parent().parent();
        var dictionary_id = list.attr('id');
        var message = "Are you sure you want to delete this dictionary?"
        var is_sharing = ($(this).hasClass("sharing-dictionary"))? true : false;
        if( confirm(message) ){
            $.ajax({
                async: false,
                type: "POST",
                url: site_url+'ajax/delete_dictionary',
                dataType: "json",
                data: "dictionary_id="+dictionary_id+"&is_sharing="+is_sharing,
                success: function(data){
                    list.hide();
                }
            });
            if( $(this).parent().parent().hasClass('current') ){
                window.location.replace(site_url);
            }
        }
        
    });
});
