$(function(){

    $('.rate').corner("8px");
    $('.ba').corner("8px");
    $('#wrapper').corner("12px");
    
    $('input.s, input.f').addClass("idleField");
    $('input.s, input.f').focus(function() {
        $(this).removeClass("idleField").addClass("focusField");
        if (this.value == this.defaultValue){ 
            this.value = '';
        }
        if(this.value != this.defaultValue){
            this.select();
        }
    });
    $('input.s, input.f').blur(function() {
        $(this).removeClass("focusField").addClass("idleField");
        if ($.trim(this.value) == ''){
            this.value = (this.defaultValue ? this.defaultValue : '');
        }
    });
    
    
    
    $("a.up").click(function(){
        //get the id
        the_id = $(this).attr('id');
        the_this = this;  
        
        //the main ajax request
        $.ajax({
            type: "POST",
            data: "action=vote_up&id="+$(this).attr("id"),
            url: "/balsuoti",
            success: function(msg)
            {
                if(msg == "login_error"){
                    jAlert('Norėdami balsuoti privalote prisijungti.', 'Pranešimas');
                }else if(msg == "vote_error"){
                    jAlert('Jūs jau balsavote už šią nuorodą.', 'Pranešimas');
                }else{
                    $(the_this).parent().html("<a href='' class='up'><img src='/i/arr_up_g.png' alt='' /></a><a href='' class='down'><img src='/i/arr_do.png' alt='' /></a>");
                    $("span#votes_count"+the_id).hide();
                    $("span#votes_count"+the_id).html(msg);
                    $("span#votes_count"+the_id).show();
                }
                
            }
        });
    });
    
    $("a.down").click(function(){
        //get the id
        the_id = $(this).attr('id');
        the_this = this;        
        
        //the main ajax request
        $.ajax({
            type: "POST",
            data: "action=vote_down&id="+$(this).attr("id"),
            url: "/balsuoti",
            success: function(msg)
            {
                
                if(msg == "login_error"){
                    jAlert('Norėdami balsuoti privalote prisijungti.', 'Pranešimas');
                }else if(msg == "vote_error"){
                    jAlert('Jūs jau balsavote už šią nuorodą.', 'Pranešimas');
                }else{
                    $(the_this).parent().html("<a href='' class='up'><img src='/i/arr_up.png' alt='' /></a><a href='' class='down'><img src='/i/arr_do_r.png' alt='' /></a>");
                    $("span#votes_count"+the_id).hide();
                    $("span#votes_count"+the_id).html(msg);
                    $("span#votes_count"+the_id).show();
                }
            }
        });
    });
    
    
    $("a.cup").click(function(){
        //get the id
        the_id = $(this).attr('id');
        the_this = this;  
        
        //the main ajax request
        $.ajax({
            type: "POST",
            data: "action=vote_up&id="+$(this).attr("id"),
            url: "/comment_balsuoti",
            success: function(msg)
            {
                if(msg == "login_error"){
                    jAlert('Norėdami balsuoti privalote prisijungti.', 'Pranešimas');
                }else if(msg == "vote_error"){
                    jAlert('Jūs jau balsavote už šį komentarą.', 'Pranešimas');
                }else{
                    $(the_this).parent().html("<a href='' class='up'><img src='/i/arr_up_g.png' alt='' /></a><a href='' class='down'><img src='/i/arr_do.png' alt='' /></a>");
                    $("span#c_votes_count"+the_id).hide();
                    $("span#c_votes_count"+the_id).html(msg);
                    $("span#c_votes_count"+the_id).show();
                }
                
            }
        });
    });
    
    $("a.cdown").click(function(){
        //get the id
        the_id = $(this).attr('id');
        the_this = this;        
        
        //the main ajax request
        $.ajax({
            type: "POST",
            data: "action=vote_down&id="+$(this).attr("id"),
            url: "/comment_balsuoti",
            success: function(msg)
            {
                
                if(msg == "login_error"){
                    jAlert('Norėdami balsuoti privalote prisijungti.', 'Pranešimas');
                }else if(msg == "vote_error"){
                    jAlert('Jūs jau balsavote už šį komentarą.', 'Pranešimas');
                }else{
                    $(the_this).parent().html("<a href='' class='up'><img src='/i/arr_up.png' alt='' /></a><a href='' class='down'><img src='/i/arr_do_r.png' alt='' /></a>");
                    $("span#c_votes_count"+the_id).hide();
                    $("span#c_votes_count"+the_id).html(msg);
                    $("span#c_votes_count"+the_id).show();
                }
            }
        });
    });
    
    $("a.favourite").click(function(){
        //get the id
        the_id = $(this).attr('id');
        the_this = this;        
        
        //the main ajax request
        $.ajax({
            type: "POST",
            data: "action=prideti&id="+$(this).attr("id"),
            url: "/megstamiausios_veiksmai",
            success: function(msg)
            {
                if(msg == "already_exists"){
                    jAlert('Ši nuoroda jau yra mėgstamiausių sąraše.', 'Pranešimas');
                }
                else if(msg == ""){
                    jAlert('Nuoroda pridėta į mėgstamiausių sąrašą.', 'Pranešimas');
                }
                
            }
        });
    });
    
});


$(function () {
    var tabContainers = $('div.tabs > div');
    tabContainers.hide().filter(':first').show();
    
    $('div.tabs ul.tabNavigation a').click(function () {
        tabContainers.hide();
        tabContainers.filter(this.hash).show();
        $('div.tabs ul.tabNavigation a').removeClass('selected');
        $(this).addClass('selected');
        return false;
    }).filter(':first').click();
});


function checkAll(field)
{
    if(field.length > 0){ 
        for (i = 0; i < field.length; i++)
            field[i].checked = true;
    }else{
        field.checked = true;
    }
}


function uncheckAll(field)
{
    if(field.length > 0){ 
        for (i = 0; i < field.length; i++)
            field[i].checked = false ;
    }else{
        field.checked = false;
    }
}


