(function($) {

    $.fn.extend({
        bulka: function(config) {
            return this.each(function(){ new $.bulka(this, config); });
        }
    });
    
    $.bulka = function(el, config) {
    
        this.el = $(el);
    
        this.options = $.extend(this.defaults, config || {});
        
        this.init();
    }
    
    $.bulka.prototype.extend = $.extend;
    
    $.bulka.prototype.extend({
    
        // defaults config
        defaults: {
            timeHide: 300,
            tpl:
            '<div class="bulka-box">' +
                '<div class="bulka-top-left"></div>' +
                '<div class="bulka-top-right"></div>' +
                '<div class="bulka-top-center"></div>' +
                '<div class="bulka-bottom-left"></div>' +
                '<div class="bulka-bottom-right"></div>' +
                '<div class="bulka-bottom-center"></div>' +
                '<div class="bulka-left"></div>' +
                '<div class="bulka-right"></div>' +
                '<div class="bulka-center"></div>' +
            '</div>',
            heightMin: 13,
            widthMin: 50,
            html: null,
            htmlurl: null
        },
        
        init: function() {
            this.html = this.options.html;
            if (this.html == null)
                this.html = this.el.attr('html');
            
            this.htmlurl = this.options.htmlurl;
            if (this.htmlurl == null)
                this.htmlurl = this.el.attr('htmlurl');
            
            this.box = null;
            this.timer = null;
            
            var self = this;
            this.showFunc = function() { self.show(); };
            this.hideFunc = function() { self.hide(); };
            this.hideSlowFunc = function() { self.hideSlow(); };
            this.clearTimerFunc = function() { self.clearTimer(); };
            
            this.el.bind('mouseover', this.showFunc);
            this.el.bind('mouseout', this.hideSlowFunc);
        },
        
        show: function() {
            this.clearTimer();
            
            if (this.box != null)
                return;
                
            this.box = $(this.options.tpl).appendTo('body');
            this.box.bind('mouseover', this.clearTimerFunc);
            this.box.bind('mouseout', this.hideSlowFunc);
        
            this.content = $('<div/>').appendTo($('div.bulka-center', this.box));
            
            if (this.html) {
                this.content.html(this.html);
            } else if (this.htmlurl) {
                var self = this;
                this.content.addClass('bulka-loading');
                $.ajax({
                    type: 'GET',
                    url: this.htmlurl,
                    dataType: 'html',
                    complete: function(xhr, status) {
                        if (status != 'success')
                            self.hide();    // ajax result error
                    },
                    success: function(data) {
                        self.content.removeClass('bulka-loading').html(data).css({height: 'auto', width: 'auto'});
                        self.calcSizes();
                    }
                });
            }
            
            this.calcSizes();
            
        },
        
        clearTimer: function() {
            window.clearTimeout(this.timer);
        },
        
        hideSlow: function() {
            this.clearTimer();
            this.timer = window.setTimeout(this.hideFunc, this.options.timeHide);
        },
        
        hide: function() {
            this.clearTimer();
            if (this.box != null) {
                this.box.remove();
                this.box.unbind('mouseover').unbind('mouseout');
            }
            this.box = null;
        },
        
        calcSizes: function() {
            var ch = this.content.height();
            var cw = this.content.width();
            if (ch < this.options.heightMin) {
                ch = this.options.heightMin;
                this.content.height(ch);
            }
            if (cw < this.options.widthMin) {
                cw = this.options.widthMin;
                this.content.width(cw);
            }
            
            // set sizes for borders
            // all numbers calculated from css
            var h = this.box.height();
            var w = this.box.width();
            $('div.bulka-top-center', this.box).width(w);
            $('div.bulka-bottom-center', this.box).width(w - 27);
            $('div.bulka-left', this.box).height(h);
            $('div.bulka-right', this.box).height(h);
            
            h = h + 18 + 2;  // from css
            w = w + 10;   // from css
            
            var offset = this.el.offset();
            
            offset.top -= h;
            offset.left -= 15;
            
            this.box.css(offset);
        }
    });
    
})(jQuery);    
    
    
    
    

jQuery(function($){
	
    $('div.menu-top-box ul.menu li').each(function(){
        $(this).prepend('<span class="left"></span><span class="center"></span><span class="right"></span>');
        if ($('a', this).width() < 100)
            $('a', this).width(100);
        var w = $(this).width() - 14;
        $('span.center', $(this)).width(w);
    });
    
    $('a.button').each(function(){
        var wa = $('span.text', this).width();
        $(this).width(wa);
        $(this).prepend('<span class="left"></span><span class="center"></span><span class="right"></span>');
        var w_l = $('span.left', $(this)).width();
        var w_r = $('span.right', $(this)).width();
        var w = $(this).width() - w_l - w_r + parseInt($(this).css('padding-right')) + parseInt($(this).css('padding-left'));
        if (w < 0)
            w = 1;
        $('span.center', $(this)).width(w).css({'left': w_l + 'px', 'height': $(this).height()});
    });
    
    $('a.submit').click(function(){
        var el = $(this);
        el.attr('href', 'javascript:void(0);');
        while (el && !el.is('form') && !el.is('body')) {
            el = el.parent();
        }
        if (el.is('form'))
            el.submit();
    });
    
    $('ul.menu-left > li > div').each(function(){
        $(this).prepend('<span class="top"></span><span class="center"></span><span class="bottom"></span>');
        var h = $(this).height() - 9;
        if ($.browser.msie && $.browser.version < 7)
            h -= 1;
        if (h < 0)
            h = 0;
        $('span.center', $(this)).height(h);
    });
    
    $('.bulka').bulka();
    
    resize();
	$(window).resize(function(){resize();});

});



function resize() {
	var w = parseInt(($(window).width() - 1003) / 2);
    var h = $('div.body').height();
    
	$('div.bg-out-left,div.bg-out-right').width(w).height(h);
	$('div.bg-out-top,div.bg-out-bottom').width(w);
}


function clearOption(optionMenu) {
	if(optionMenu && optionMenu.options)
	for (var i=optionMenu.options.length; i >= 0;  i--) {
		optionMenu.options[i] = null;
	}
}

function addOptions(optionList, optionMenu) {
	//clear
	if(optionMenu.options)
	for (var i=optionMenu.options.length; i >= 0;  i--) {
		optionMenu.options[i] = null;
	}
	if(!optionList) return;
	optionMenu.disabled='';
	
	optionList=optionList.ol;
	//add
	for (var i=0; i < optionList.length; i++) {
		optionMenu[i] = new Option(optionList[i]['n'], optionList[i]['i']);
		if(optionList[i]['s']=='1')
			optionMenu.selectedIndex=i;
	}
}
	function updateSid(el,val,sel) {
		switch(el) {
			case 'region_id':
					clearOption(document.getElementById('district_id'));
			case 'district_id':
					clearOption(document.getElementById('street_id'));
			case 'street_id':
					clearOption(document.getElementById('house_id'));
					break;		
		}
		optionMenu = document.getElementById(el);
		
		if(optionMenu) {
			clearOption(optionMenu);
			optionMenu.disabled = 'disabled';
			optionMenu[0] = new Option('- Загрузка -', 0);		
		}
		
		jQuery.ajaxSetup({async: false});
		$.post('/school/address/', {action: el, id:val, selected:sel}, function(text) {
					if(text)
						addOptions(eval('('+text+')'),optionMenu);
					else
						addOptions('',optionMenu);
					return true;
					}
				);
		jQuery.ajaxSetup({async: true});

		return true;
	}

