(function($) {

var RU_DOF = window.RU_DOF = new Array('Вс','Пн','Вт','Ср','Чт','Пт','Сб');
var RU_MONTHS = window.RU_MONTHS = new Array('января','февраля','марта','апреля','мая','июня','июля','августа','сентября','октября','ноября','декабря');

$(window).bind('load', function() {
    window.setTimeout(function() { Visuals.setupTV(); }, 400);
});

$(document).ready(function() {
    Visuals.setupClocks();
    Interfaces.setup();
});

var Visuals = window.Visuals = new Object();
Visuals.setupClocks = function() {
    this.clocks = $('#tv .time');
    window.setInterval(function() { Visuals.printClockTime(); }, 1000);
    Visuals.printDate();
}

Visuals.printClockTime = function() {
    var time = new Date();
    time.setTime = time.getTime()+1000*60*60*2;
    var m = time.getMinutes();
    var h = time.getHours();
    m = (m<10) ? "0"+m : m;
    h = (h<10) ? "0"+h : h;
    var spanClass = (time.getSeconds() % 2 == 0)?'':'h';
    this.clocks.html(time.getDate()+' '+RU_MONTHS[time.getMonth()]+' '+time.getFullYear()+' &nbsp;&nbsp;&nbsp; '+h+"<span class='"+spanClass+"'>.</span>"+m+' &nbsp; '+RU_DOF[time.getDay()]);
}
Visuals.printDate = function() {
    var time = new Date();
    time.setTime = time.getTime()+1000*60*60*2;
    var m = time.getMinutes();
    var h = time.getHours();
    m = (m<10) ? "0"+m : m;
    h = (h<10) ? "0"+h : h;
    var spanClass = (time.getSeconds() % 2 == 0)?'':'h';
    $('#__date').html(time.getDate()+' '+RU_MONTHS[time.getMonth()]+' '+time.getFullYear());
}

Visuals.setupTV = function() {
    $('#tv .ripples').fadeOut(400);
}

var Interfaces = window.Interfaces = new Object();
Interfaces.setup = function() {
    var $outer = $('#outer');
    $().ajaxError(function(event, request, settings){
        _hideLoader();
    });
    var $cells = $('table.estates td div');
    $cells
        .hover(
            function() {
                $(this).hoverCell();
            },
            function() {
                $(this)._hoverCell();
            }
        )
        .click(
            function() {
                window.location = $(this).find('a').get(0).href;
            }
        )
        .each(function() {
            $('a', this).click(function(e) {
                if (e.stopPropagation) e.stopPropagation();
                e.returnValue = false;
                e.cancelBubble = true;
            });
            var $ribbon = $(this).find('ins.ribbon');
            if ($ribbon.length) {
                var pos = $(this.getElementsByTagName('img')[0]).position();
                $ribbon.css({ left: pos.left, top: pos.top });
            }
        });

    $('.estate-page tr:even').addClass('e');
    $('.estate-gallery ul').gallery($outer);
    $('#dan-search form').danSearch('#dan-search-res');
    if (window._className == 'districts') {
        if (window.DANDistricts) DANDistricts.init();
    }

}
jQuery.fn.extend({
    hoverCell: function() {
        this.addClass('hover');
    },
    _hoverCell: function() {
        this.removeClass('hover');
    }
});

var loaderCount = 0;

window._showLoader = function() {
    loaderCount ++;
    if (!window._loaderP) {
        window._loaderP = $(document.createElement('div')).addClass('loader-p').html('Загрузка ...').css({opacity: 0.6}).appendTo('#outer');
    } else {
        window._loaderP.show();
    }
    if ((jQuery.browser.msie) && (jQuery.browser.version<7)) {
        window._loaderP.css({top: document.documentElement.scrollTop});
    }
}
window._hideLoader = function() {
    loaderCount --;
    if (loaderCount < 0) loaderCount = 0;
    if ((window._loaderP) && (loaderCount == 0))
        window._loaderP.hide();
}

window.$el = function(tag) {
    return $(document.createElement(tag));
}

window._getTextContent = function(xmlElement) {
    if (xmlElement.text != undefined) return xmlElement.text;
    else if (xmlElement.textContent != undefined) return xmlElement.textContent;
    else if (xmlElement.firstChild != undefined) return xmlElement.firstChild.nodeValue;
}


})(jQuery);

