(function(){

// Общие настройки модального окна
//******************************************************************************
$.modal({
	voileColor: '#000',
	voileOpacity: .2,
	body: '#popup-body',
	closer: '#popup-closer, .closer',
	window: '<div id="popup"><div id="popup-icon"/><div id="popup-head"><div id="popup-closer"/></div><div id="popup-body"/></div>',
	init: function(win) { // после инициализации
	},
	open: function(win) { // после открытия
		// фон
		if ($.ie) $('*', win).png();

		// тень
		win.bb({id: 'popup-bb'});
		$('button', win).decorate();
		$('a.button', win).bb();

		// попап с иконкой или без
		(win[0].className.search('popup-icon-') === -1) ? win.removeClass('popup-icon') : win.addClass('popup-icon');

		if (win.hasClass('no-closer')) $('#popup-closer').hide()
		else $('#popup-closer').show();
	}
});


// Показать алерт
//******************************************************************************
var alertN = 0;
$.fn.alert = function()
{
	this.modal({
		cls: this.attr('class'),
		afterClose: function() {
			++alertN;
			$('#alert-'+alertN).alert();
		}
	});
};



// DOM reary
//******************************************************************************
$(function(){
	
	if ($.browser.msie) {
		$('select').mousedown(function(){
			if ($('option', this).size() < 15) return;
			if (!this._opened) {
				var t = $(this),
					p = t.offset();
				
				if (!this._fake) {
					this._fake = $('<select/>')
						.click(function(){
							t.val($(this).val()).removeAttr('size').focus().change();
							t[0]._opened = false;
							$(this).hide();
						})
						.blur(function(){
							$(this).click();
						})
						.attr('size', 15)
						.css({position: 'absolute', display: 'none', width: t.width() + 2, height: 'auto', top: p.top, left: p.left, zIndex: 20})
						.appendTo('body')
				}
				
				t.attr('size', 2);
				this._fake.html(t.html()).show().val(t.val()).focus();
				this._opened = true;
			}
		});
	}

	// позицирование wrap
	var wrap = $('#wrap');
	function updateWrap()
	{
		if (!wrap.length) return;
		var h = ($(document).height() - 600) / 3;
		wrap.css('top', h);

	}
	$(window).resize(updateWrap);
	updateWrap();

	// menu
	$('#menu > li').hover(function(){
		$('ul', this).toggle();
	});

	// декорирование кнопок
	$('a.button').bb();
	$('button').decorate({cls: 'button'});


	// alert
	$('#alert').alert();

	// popup
	//******************************************************************************
	$('a.popup, button.popup').click(function(){
		var frame = $('<iframe name="popup-iframe" id="popup-iframe" frameborder="0" scrolling="no"/>')
			.load(function(){ $(this).addClass('loaded'); })
			.modal({cls: 'popup-iframe '+$(this).attr('class')});

		frame.removeClass('loaded');
		switch (this.nodeName) {
			case 'A':
				frame[0].src = this.href;
				return false;
			case 'BUTTON':
				this.form.target = 'popup-iframe';
				return true;
		}
	});

	// popupwindow closer
	if ($('body.popup').length) {
		$('.closer').click(function(){ window.parent.$.modal.close(); });
	};

	//error
	$('ul.error_list').each(function(){
		var e = $(this),
			r = e.prev();

		if (!r.length) return;

		e.appendTo('body');

		var f = function(){
			var p = r.offset();
			e.css({
				top: p.top + r.height(),
				left: p.left,
				width: r.width() - 2
			});
			e.show();
		};
		var h = function(){ e.hide(); };
		
		
		r.find('input, textarea, select').focus(f).blur(h);
		
		r.addClass('input-row-error')
			.hover(f, h)
			.change(function(){
				r.removeClass('input-row-error').unbind('mouseenter mouseleave');
				e.hide();
			});
	});


	// Игра
	//**************************************************************************
	$('a.game_photo').click(function(){
		w = window.open(this.href, 'game_photo', 'resizable=no,scrollbars=no,status=no,width=500,height=400');
		w.focus();
		return false;
	});
	$('a.game_calc').click(function(){
		w = window.open(this.href, 'game_calc', 'resizable=no,scrollbars=no,status=no,width=826,height=422');
		w.focus();
		return false;
	});
	$('a.game_tetris').click(function(){
		w = window.open(this.href, 'game_tetris', 'resizable=no,scrollbars=no,status=no,width=827,height=423');
		w.focus();
		return false;
	});
	$('a.game_holiday').click(function(){
		w = window.open(this.href, 'game_holiday', 'resizable=no,scrollbars=no,status=no,width=827,height=443');
		w.focus();
		return false;
	});

});

})();