// jQuery script by marty (c) 2011-01-27

$(document).ready(function(){
    
    /* prepinac cen se slevou a bez slevy */
    if( $('#id-price-discount-toggle1').length ){
        $('#id-price-discount-toggle1,#id-price-discount-toggle2').live('click',function(){
            $('.price-discount-toggle,.price-nodiscount-toggle').toggle();
            /* alert($('.price-discount-toggle').css('display')); */
            jQuery.ajax({
                type:  "GET",
                url:   root+'_rsrc/jquery.setupdiscount.php',
                data:  'showdiscount='+$('.price-discount-toggle').css('display'),
                error: function (xhr, ajaxOptions, thrownError){ /* alert(xhr.status); alert(thrownError); */ },       
                success: function (response){ /* alert(response); */ }
            });
        });
    }
    
    /* quick choice */
    if( $('#frm-quick').length){
        
        /* AJAX na nacitani mest ve vybranem regionu */
        $('#id-oblast').bind('change',function(){
            if( $(this).val() > 0 ){
                jQuery.ajax({
                    type:  "GET",
                    url:   root+'_rsrc/jquery.townlist.php',
                    data:  'rid='+$(this).val(),
                    error:function (xhr, ajaxOptions, thrownError){ /* alert(xhr.status); alert(thrownError); */ },
                    success: function (data){
                        var items = [];
                        items.push('<option value="0">- ' + select_town_default_value + '- </option>');
                        $.each(data, function(key, val) {
                            items.push('<option value="' + val.id + '">' + val.name + '</option>');
                        });
                        $('#id-town').html(items.join(''));
                    }
                });
                $('#id-town').removeAttr('disabled'); 
            }else{
                $('#id-town').attr('disabled','disabled');
            }           
        });
        
        /* AJAX na nacitani dalsich polozek */
        var loading = false;
        $(window).scroll(function(event){
            $('#log').html(
                'scroll: '+$(window).scrollTop()+'<br />'
               +'loader:'+$('#quickchoiceloader').offset().top+'<br />'
               +'window.height:'+$(window).height()+'<br />'
               +'document.height:'+$(document).height()+'<br />'
            );
            if( ($(document).height() - $(window).height() - $(window).scrollTop() ) < 300 && !loading && quick.cont>0){
                //$('#quickchoicecontent').append('<div>ajax</div>');
                loading = true;
                $('#quickchoiceloader').fadeIn();
                jQuery.ajax({
                        type:  "GET",
                        url:   'quick_choice.php',
                        data:  'ajax=1&quick[objekt]='+quick.objekt
                              +'&quick[oblast]='+quick.oblast
                              +'&quick[town]='+quick.town
                              +'&quick[datefrom]='+quick.datefrom
                              +'&quick[dateto]='+quick.dateto
                              +'&quick[pricefrom]='+quick.pricefrom
                              +'&quick[priceto]='+quick.priceto
                              +(quick.available!='' ? '&quick[available]='+quick.available : '')
                              +(quick.call!='' ? '&quick[call]='+quick.call : '')
                              +'&page='+(quick.page+1),
                        error:function (xhr, ajaxOptions, thrownError){ /* alert(xhr.status); alert(thrownError); */ },
                        success: function (html){
                            $('#quickchoicecontent').append('<div>'+html+'</div>');
                            loading = false;
                            $('#quickchoiceloader').fadeOut();
                        }
                }); 
            }
        });
    }
    
    $('#id-search-keyword').addClass(searchFieldConf.klas).val(searchFieldConf.hint);
    $('#id-search-keyword').focus(function(){
        if( $(this).val()==searchFieldConf.hint) {
            $(this).removeClass(searchFieldConf.klas).val('');
        }
    }).blur(function(){
        if(!$(this).val() || $(this).val()==searchFieldConf.hint) {
            $(this).addClass(searchFieldConf.klas).val(searchFieldConf.hint);
        }
    });

});
