
$(function() {

    $('#login-form .input-text')
        .each(function() {
            $(this).data('def', this.value);
        })
        .focus(function() {
            if(this.value == $(this).data('def')) this.value = '';
        })
        .blur(function() {
            if(this.value == '') this.value = $(this).data('def');
        })
    ;

    $('form').submit(function(e) {

        var flag = true;

        $('.required', this).each(function() {

            var value = this.type == 'radio' ? $('input[name="'+ this.name +'"]:checked').val() : this.value;

            if(value == '' || value == '0' || typeof value == 'undefined') {

                this.focus();
                flag = false;
                return false;

            }

        });

        if(!flag) {

            alert('Пожалуйста, заполните все поля, отмеченные звездочкой');

            e.preventDefault();
            return false;

        }
        
    });

    $('.confirm').click(function(e) {
        if(!confirm('Вы уверены?')) {
            e.preventDefault();
            return false;
        }
    });

    $('#brands').change(function() {

        var models  = $('#models');
        var loading = $('#modelsLoading');

        if(!models.length) return false;

        models.html('')[0].disabled = true;
        loading.show();

        var any = $(this).hasClass('any') ? '/any' : '';

        $.getJSON(
            root + 'ajax/models/' + $(this).val() + any,
            function(data) {
                for(i in data) $('<option/>').attr('value', i).html(data[i]).appendTo(models);
                models[0].disabled = false;
                loading.hide();
            }
        )

    });

    $('#captcha-reload').click(function() {

        var img = $('#captcha');
        if(!img.data('url')) img.data('url', img[0].src);
        img[0].src = img.data('url') +'/'+ Math.random();

    });

    $('.thumbnailContainer').mouseover(function(e) {

        if(e.target.tagName != 'IMG') return false;

        var id = this.id.replace(/^imagediv-/gi, '');
        $('#mainPhoto').attr('src', root + 'photos/medium/'+ id);
        $('#mainLink').attr('href', root + 'photos/large/'+ id);

    });

    $('#moarToggle').click(function() {
        $(this).html($('#moar').is(':visible') ? 'подробный поиск' : 'скрыть подробности');
        $('#moar').slideToggle(300);
    })

    $('input[name="option[37]"], input[name="option[39]"]').change(function() {

        var obj = $('input[name="option[38]"]');
        if(this.checked) obj[0].checked = false;
        obj.attr('disabled', this.checked);
    });

    if($.fn.autocomplete) {

        $('#city').autocomplete(root + 'ajax/city', {
            autoFill : true,
            selectFirst : true
        });

    }

    var inp1 = $('input[name="option[37]"]');
    var inp2 = $('input[name="option[38]"]');
    var inp3 = $('input[name="option[39]"]');

    var func = function() {
        var flag = inp1[0].checked || inp3[0].checked;
        if(flag) inp2[0].checked = false;
        inp2[0].disabled = flag;
    }

    inp1.click(func);
    inp3.click(func);

    inp2.click(function() {
        if(this.checked) {
            inp1[0].checked = false;
            inp3[0].checked = false;
        }
        inp1[0].disabled = this.checked;
        inp3[0].disabled = this.checked;
    });

    setTimeout(function() {
        $('.notify, .errors').slideUp(500);
    }, 5000);

});



fillYear = function(x, y) {

    $('input[name="since"]').val(x);
    $('input[name="till"]').val(y);

}


function printpr()  
{  
var PROMPT = 1; 
var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>'; 
document.body.insertAdjacentHTML('beforeEnd', WebBrowser) 
WebBrowser1.ExecWB(7, PROMPT) 
WebBrowser1.outerHTML = "" 
}

function printit()
{ 
window.print() ; 
} 


function getPrint() {
 var ua = navigator.userAgent.toLowerCase();
 // Определим Internet Explorer
 if (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1) {
   printpr();
 }

 printit();
}

