var active = true;

$(function()
{   

    $('#woj').live('change', function()
    {
        var $ob = $(this);
        
        if($ob.attr('value') != '')
        {
            
            
            $.ajax(
            {
                url : 'ajax/loadCities.php',
                dataType : 'json',
                type : 'POST',
                data : 
                {
                    'woj' : $ob.attr('value')
                },
                           
                success : function(data)
                {   
                    $('.city').html('');
                    for(index in data)  
                        $('.city').append('<option value="'+data[index].key+'">'+data[index].item+'</option>')
                    
                }
            });
        }
    });
     
    
    
    
    $('#token').live('click', function()
    {
        $(this).attr('src', 'images/token.jpg?t='+new Date().getTime());
        return false;
    });
    
    
    $('#ulubione li').live('click', function()
    {
        carousel.next();
        return false;
    });
    
    $('.delList').live('click', function()
    {
        var $ob = $(this);
        $.ajax(
        {
            url : $ob.attr('href'),
            dataType : 'json',
            type : 'GET',
                       
            success : function(data)
            {   
                if(data.status == 1)
                    $ob.closest('div.news_box').slideToggle();
                        
                alert(data.info);
            }
        });
           
        return false;    
    });
    
    $('.list').live('click', function()
    {
        var $ob = $(this);
        $.ajax(
        {
            url : 'ajax/addFavourites.php',
            dataType : 'json',
            type : 'POST',
            data : 
            {
                'gid' : gid,
                'type' : $ob.attr('rel')
            },
                       
            success : function(data)
            {   
                //if(data.status == 1)
                    //$ob.closest('span.com_box').slideToggle();
                        
                alert(data.info);
            }
        });
           
        return false; 
    });

    $('.delComment, .report').live('click', function()
    {
        var $ob = $(this);
        var $type = $ob.attr('class');
        
        if($type == 'report')
            $type = 'zgłosić';
        else if($type == 'delComment')
            $type = 'usunąć';
        
           
        if(confirm('Czy na pewno chcesz '+$type+' ten komentarz?'))
        {
            var url = $ob.attr('href');
            
            $.ajax({
                url : url,
                dataType : 'json',
                type : 'POST',
                        
                success : function(data)
                {   
                    if(data.status == 1)
                        $ob.closest('span.com_box').slideToggle();
                    
                    $ob.attr('href', '').text('Komentarz został zgłoszony');
                }
            });
        }   
        
        return false; 
    });
    
    if($('#facebook').length != 0)
        $('#facebook').html('<iframe style="border: 0px;width: 180px; height: 150px;" src="http://www.facebook.com/plugins/like.php?href='+window.location.href+'&amp;layout=standard&amp;show_faces=true&amp;width=150&amp;action=like&amp;font=tahoma&amp;colorscheme=light"></iframe>');
    
    $('ul#nav li.dir ul li a').hover(function()
    {
        $('ul#nav li.dir ul li big').text($(this).closest('ul.chars').prev().text());
        return false;
    });
    
    $('ul#nav li.dir ul, ul#nav li.dir ul li a, #footer').corner();
    
    $('div.box div.bookmark').live('click', function()
    {
        var $id = $(this).attr('id');
        var $ob = $(this).closest('.box').attr('id');
        $('#'+$ob+' ul').hide();
        $('#l'+$id).show();
        
                        
        return false;
    });
    
    
    $('#plasu_minus img').live('click', function()
    {
        var $ob = $(this);
        var $id = $ob.closest('.com_box').attr('id');
        var $ocena = '+';
        
        if($('#'+$id+' #plasu_minus img').index(this)+1 == 2)
            $ocena = '-';
        
        $.ajax({
            url : 'ajax/addCommentVote.php',
            dataType : 'json',
            type : 'POST',
            data : 
            {
                'vote' : $ocena,
                'id' : $id.split('id_')[1]
            },
                    
            success : function(data)
            {
                $ob.parent().text(data.info);
            }
        });
        return false;
    });
    
    $('#user p a:first').live('click',
    function()
    {
        $(this).parent().next().slideToggle();
        $(this).parent().slideToggle(function(){
            $('input[name=login]').focus();
        });

        return false;
    });
    
    $('.rank2 img').live('click', 
    function()
    {
        if(active)
        {
            var $ob = $(this);    
            var vote = $('.rank2 img').index(this)+1;
            
            $.ajax({
                url : 'ajax/addVote.php',
                dataType : 'json',
                type : 'POST',
                data : 
                {
                    'vote' : vote,
                    'gid' : gid
                },
                        
                success : function(data)
                {   
                    if(data.status == 1)
                      active = false;
                        
                    alert(data.info);
                }
            });  
        }
        return false;
    });
    
    $('.rank2 img').hover(
    function()
    {
        if(active)
        {
            $(this).attr('src', 'templates/new/images/star_1.jpg');   
            $(this).prevAll().attr('src', 'templates/new/images/star_1.jpg');
            $(this).nextAll().attr('src', 'templates/new/images/star_2.jpg');
        }
    });
    
    $('#lastAdded a').live('click',
    function()
    {
        $ob = $(this).parent().attr('class');
        if($('#'+$ob).length != 0)
        {
            $('.qmenu').hide();
            $('#'+$ob).show();
        }
        return false;
    });
    
    $('a#bottom').live('click',
    function()
    {
        $('#addComment').toggle();
        return false;
    });
    
    $('#showTags').live('click',
    function()
    {
        $ob = $(this).next();
        $(this).next().slideToggle();
        return false;
    });
});
