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


$(document).ready(function(){
    //pokaĹź okienko logowania
    $("a.login_link").click(function(){
        hideAuthWindow();
        showAuthWindow("login_form");
        return false;
    });
    //pokaĹź okienko rejestracji
    $("a.register_link").click(function(){
        hideAuthWindow();
        showAuthWindow("register_form");
        return false;
    });
    //zamykanie okna auth
    $("a.close_auth_window").click(function(){
        hideAuthWindow();
        return false;
    });
    //dialog
    $("#dialog").dialog({resizable: false, closeText: 'zamknij', autoOpen: false});
    //masowe operacje na formularzach
    $("input[name='user_operation']").change(function(){
        change_users_for_remove();
    });
    //zaznacz wszystkie
    $("input[name='select_all']").change(function(){
        toggle_all();
    });
    //confirm delete users
    $("form.delete_users_form").submit(function(){
        if(confirm('Czy na pewno zmieni\u0107?') == true) {
            return true;
        }
        else {
            return false;
        }
    });
     //konfiguracja nowego toolbara
    CKEDITOR.editorConfig = function( config )
    {
        config.toolbar = 'MyToolbar';

        config.toolbar_MyToolbar =
        [
            ['NewPage','Preview'],
            ['Image', 'Table','HorizontalRule'],
            '/',
            ['Styles','Format'],
            ['Bold','Italic','Strike'],
            ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
            ['Link','Unlink','Anchor'],
            ['Maximize','-','About']
        ];
    };
    
    
    //ckeditor
    $("form.add_category_form div.content_input textarea").ckeditor();
    $("form.add_post_form div.content_input textarea, form.add_profile_form div.content_input textarea")
        .ckeditor(function(){}, {toolbar: [['Format', 'FontSize', 'Bold','Italic','Strike', 'NumberedList','BulletedList'], ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'], ['Link','Unlink'], ['Image', 'PasteText']]});
    //podpowiedzi link
    $("div.gmap_input").append('<a href="check_map" class="check_map small">Sprawd\u017a na mapie...</a>');
    //link pokazanie mapki
    $("a.check_map").click(function(){
    	$("div#check_map").remove();
    	$("div.gmap_input").prepend('<div id="check_map" class="span-16 last"></div>');
    	var centerAddress = "Polska, "+$("div.gmap_input input").val();
        if($("input:checked").length == 1) {
            centerAddress = $("div.gmap_input input").val();
        }
    	var geocoder = new GClientGeocoder();
    	if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("check_map"));
            geocoder.getLatLng(
            centerAddress,
            function(pt) {
                map.setCenter(pt, 12);
                var infoWindowHtml = "<p><b>"+centerAddress+"</b></p>";
                map.addOverlay(marker = new GMarker(pt));
                GEvent.addListener(marker, "click", function(){
                	marker.openInfoWindowHtml(infoWindowHtml);
                });
            });
            map.setUIToDefault();
          }

    	return false;
    });

    //register form accept rules
    $("form.register_form").submit(function(){
        if($("form.register_form div.accept_rules_input input:checked").val() == true) {
            return true;
        }
        else {
            alert('Aby si\u0119 zarejestrować, musisz zaakceptować regulamin.');
            return false;
        }
    });

    //news accordion
    $("div.news-accordion").accordion();

    //sortowanie

    $( "div#sorted" ).sortable({
       update: function(event, ui) {
           var positions = '';
            var ids = '';
            var i = 1;
            $("div#sorted div").each(function(){
                positions += i+'|';

                ids += $(this).children("p").children("b").children("span.id").text()+'|';

                i++;
            });
            $("div.positions_input input").val(positions);
            $("div.ids_input input").val(ids);
       }
    });
 
    $("input#keyword").autocomplete({
        source: function(request, response){
            $.ajax({
                url : baseURL+'ajax/searchable_keywords/',
                data : { like : request.term },
                method : 'GET',
                dataType : 'json',
                success : function(json) {
                    response(json, function(){
                        return json;
                    });
                }
            });
        },
        minLength: 2
    });
    $("input#address").autocomplete({
        source: function(request, response){
            $.ajax({
                url : baseURL+'ajax/searchable_regions/',
                data : { like : request.term },
                method : 'GET',
                dataType : 'json',
                success : function(json) {
                    response(json, function(){
                        return json;
                    });
                }
            });
        },
        minLength: 2
    });
    
    
    //komentarz
    $("#comment_content").textareaCount({'originalStyle': 'originalDisplayInfo', 'maxCharacterSize': 500});

    
    
});
