var url = location.protocol + '//' + location.host;

window.onresize = setWindowProperties;
window.onscroll = setWindowProperties;
function getClientWidth(){
    return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}
function getClientHeight(){   
    return document.documentElement.clientHeight;     
}
function setWindowProperties()
{
    scrollTop = document.documentElement.scrollTop;
    width = getClientWidth(); // ширинаЭкрана   
    height = getClientHeight(); // высотаЭкрана
    $('#trans_bg').css('width', width);
    $('#trans_bg').css('height', height);
    $('#trans_bg').css('top', scrollTop);
}

$(document).ready(function(){

    $(window).load(function() {
      $('#head_img').animate({opacity:1}, 1000);
    });
      
    $('#enter').click(function(){
        $('#enter_box').toggle();
        return false;    
    });
    
    //Настриваем поведение при AJAX
    $().ajaxStart(function(){
        setWindowProperties();
        $('#trans_bg').show();
    });    
    $().ajaxStop(function(){
        $('#trans_bg').hide(); 
    });        
    
    //Сообщения
    $("#name").blur(function(){
        if (this.value == '') {
            this.value = 'Ваше имя';
        }          
    });
    $("#name").focus(function(){
        if (this.value == 'Ваше имя') {
            this.value = '';    
        }    
    });
    
    $("#captcha_string").blur(function(){
        if (this.value == '') {
            this.value = 'Введите код';
        }      
    });
    $("#captcha_string").focus(function(){
        if (this.value == 'Введите код') {
            this.value = '';
        }    
    });
    $('.error').hide();     
    
    var commentAdd = ''; 
    if ($.cookie('commentIsAdd')) {
        $.cookie('commentIsAdd', '', {path: "/"});
        commentAdd = 'Сообщение успешно добавлено.<br />';
    }
    
    $('#content').everyTime(1000, function(i) {
        var i = $.cookie('time');
        if (i == 0) {
             $.cookie('time', '', {path: "/"}); 
             $('#submit').removeAttr("disabled");     
             $('#error_captcha').hide();   
        }
        //if ($.cookie('time')) {
            if (i > 0) {
                $.cookie('time', --i, {path: "/"});
                $('#error_captcha').html(commentAdd + 'Вы сможете добавить следующее сообщение через ' + i + ' сек.').show();
                $('#submit').attr('disabled', 'disabled');
            }        
        //}
    });
    
    jQuery("<img>").attr("src", url + '/images/loadinfo.gif');
    jQuery("<img>").attr("src", url + '/images/trans_bg.png');  
});

function error_message(position) {
    $('.error').hide();
    if (position == 'name') {
        msg = 'Введите имя';   
        $('#error_name').html(msg).hide().fadeIn('slow');     
    }
    else if (position == 'msg') {
        msg = 'Введите текст сообщения';    
        $('#error_msg').html(msg).hide().fadeIn('slow');
    }
    else if (position == 'captcha') {
        msg = 'Введен неверный код,<br />попробуйте еще раз';   
        $('#error_captcha').html(msg).hide().fadeIn('slow'); 
    }
    
    if (position == 'captcha') {         
        $('#no_margin').attr('src', url + '/page/pictureCaptcha').load();
    } 
}

function include(url) {
  var script = document.createElement('script');
  script.setAttribute('type', 'text/javascript')
  script.setAttribute('src', url);
  document.getElementsByTagName('head').item(0).appendChild(script);
}
