$(document).ready(function(){

    $('#container-1 > ul').tabs();
    
    $('#comment_loading').hide();
    $('#vote_loading').hide();
    $('#new_comment').hide();



	
    $('#comment_submit').submit(function(){
        var options = {
            beforeSubmit: before_comment, // pre-submit callback
            success: show_comment,
            error: function(req, errorMsg){
                alert("An error occurred: " + errorMsg);
            }
        };
        // inside event callbacks 'this' is the DOM element so we first
        // wrap it in a jQuery object and then invoke ajaxSubmit
        $(this).ajaxSubmit(options);
        
        // !!! Important !!!
        // always return false to prevent standard browser submit and page navigation
        return false;
    });
    
    var options1 = {
        beforeSubmit: before_vote,
        success: show_vote
    };
    
    // bind form using 'ajaxForm'
    $('#vote_submit').ajaxForm(options1);
    
    
    function before_vote(){
        $('#vote_submit').hide();
        $('#vote_loading').ajaxStart(function(){
            $(this).show();
        }).ajaxStop(function(){
            $(this).hide();
        });
    }
    
    function show_vote(responseText){
        $('#ocena').before('<span id="n">' + responseText + '</span>');
        $('#n').slideDown('slow');
    }
    
    // pre-submit callback
    function before_comment(formData, jqForm, options){
        var form = jqForm[0];
        if (form.comment.value.length < 10) {
            alert('Napisz wiecej...');
            return false;
    	}
        
        $('#comment_submit').hide();
        $('#comment_loading').ajaxStart(function(){
            $(this).show();
        }).ajaxStop(function(){
            $(this).hide();
        });
    }
    
    // post-submit callback
    function show_comment(responseText){
		$('#no_comments').hide();
        $('#new_comment').append(responseText).show('slow');
        $('#comment_submit').empty().append('<div class="error">Komentarz zostal dodany</div>').show();
    }
    
      
    
    //dodawanie/usuwanie z ulubionych
    $(".fav_place a").click(function(){
        if ($(this).attr('class') == 'add') {
            placeAddToFav();
        }
        if ($(this).attr('class') == 'del') {
            placeDelFromFav();
        }
        return false;
    });
    
    $(".search").focus(function(){
        jQuery(this).val('');
    });
    
    //dodawanie do ulubionych
    function placeAddToFav(){
        var place = $(".fav_place a").attr('title');
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: $(".fav_place a").attr('href'),
            data: 'type=ajax&place=' + place,
            timeout: 5000,
            beforeSend: function(){
            
            },
            error: function(msg){
                $(".add").show();
                alert(msg);
            },
            success: function(json){
                if (json.error == 'true') {
                    alert(json.text);
                }
                else {
                    $(".add").hide();
                    $(".del").show();
                }
            }
        });
    }
    
    //kasowanie z ulubionych
    function placeDelFromFav(){
        var place = $(".fav_place a").attr('title');
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: $(".fav_place a").attr('href'),
            data: 'type=ajax&place=' + place,
            timeout: 5000,
            beforeSend: function(){
            
            },
            error: function(msg){
                $(".del").show();
                alert(msg);
            },
            success: function(json){
                if (json.error == 'true') {
                    alert(json.text);
                }
                else {
                    $(".del").hide();
                    $(".add").show();
                }
            }
        });
    }
    
    //zaznacz/odznacz checkbox
    $('#checkboxSelectAll').click(function(){
        $('input[@type=checkbox]').attr({
            checked: "checked"
        });
    });
    
    $('#checkboxDeselectAll').click(function(){
        $('input[@type=checkbox]').removeAttr("checked");
    });
    
    
});
