$(document).ready(function(){
    
    var $firstInput= $('#search input[type=text]').eq(2);
    var $secondInput=$('#search input[type=text]').eq(3);
    var $thirdInput= $('#search input[type=text]').eq(0);
    var $fourthInput=$('#search input[type=text]').eq(1);
    
    var $firstLabel= $('#search label').eq(2);
    var $secondLabel=$('#search label').eq(3);
    var $thirdLabel= $('#search label').eq(0);
    var $fourthLabel=$('#search label').eq(1);
    //console.log($firstInput.position().left);
    $firstLabel.css('left',$firstInput.position().left+2);
    $secondLabel.css('left',$secondInput.position().left+2);
    $thirdLabel.css('left',$thirdInput.position().left+2);
    $fourthLabel.css('left',$fourthInput.position().left+2);
    
    $firstInput.focus(function(){
        if(!$(this).val().length) $firstLabel.hide();
    });
    $firstInput.blur(function(){
        if(!$(this).val().length) $firstLabel.show();
    });
    
    $secondInput.focus(function(){
        if(!$(this).val().length) $secondLabel.hide();
    });
    $secondInput.blur(function(){
        if(!$(this).val().length) $secondLabel.show();
    });
    
    $thirdInput.focus(function(){
        if(!$(this).val().length) $thirdLabel.hide();
    });
    $thirdInput.blur(function(){
        if(!$(this).val().length) $thirdLabel.show();
    });
    
    $fourthInput.focus(function(){
        if(!$(this).val().length) $fourthLabel.hide();
    });
    $fourthInput.blur(function(){
        if(!$(this).val().length) $fourthLabel.show();
    });
    $firstLabel.click(function(){ 
        $(this).hide(); 
        $firstInput.focus();
    });
    $secondLabel.click(function(){ 
        $(this).hide(); 
        $secondInput.focus();
    });
    $thirdLabel.click(function(){ 
        $(this).hide(); 
        $thirdInput.focus();
    });
    $fourthLabel.click(function(){ 
        $(this).hide(); 
        $fourthInput.focus();
    });
    var $loader = $('<span class="news-loader"></span>');
    $loader.html(ajaxLoader.setContent('trwa wyszukiwanie...').getHtml());

    $(".paginator-ajax").find('.paginator').find('a').click(function(){
        
        var href = $(this).attr('href');
        var $container = $(this).parents('#page-news');
        
        $(this).parent().parent().after($loader);
        
        $.ajax({
            url: href,
            dataType: 'json',
            success: function(container) {
                var $pageNews = $(container);
                $container.html($pageNews.html());
                $.getScript('/js/views/search.js');
                $.getScript('/js/more.js');
            },
            error: function() {
                alert('error');
            }
        });
        
        return false;
    });
    
    $("#search form:not(.no-ajax)").submit(function(){
        var searchUrl = getSearchUrl($(this));
        var $container = $('#page-news');
        $.fx.speeds._default = 500;
        modalDialog.setContent('trwa wyszukiwanie...').openDialog({
            modal: true,
            width: 900,
            height: 'auto',
            resizable: false,
            draggable: false,
            show: 'fade',
            hide: 'fade'
        });
        
        $.ajax({
            url: searchUrl,
            dataType: 'json',
            success: function(container) {
                var $pageNews = $(container);
                $container.html($pageNews.html());
                $("#search-submit").unbind('click');
                $.getScript('/js/views/search.js');
                $.getScript('/js/more.js');
                setTimeout(function(){
                    modalDialog.getDialog().dialog('close');
                }, 400);
                
            },
            error: function() {
                alert('error');
            }
        });
        return false;
    });
});

var getSearchUrl = function($object) {
    var $q1 = $object.find("input[name=q1]");
    var $q2 = $object.find("input[name=q2]");
    
    var url = '/szukaj/q1/' + $q1.val() + '/q2/' + $q2.val() ;
    
    return url;
}
