Main = function()
{
	var h_resize = 0;
	var descr_overlay = null;

	function wrap_image(el)
	{
		var subclass = el.className.match(/(?:^|[ ])image\-([a-z]+)/)[1];

		var cls = el.className.replace('image-' + subclass, '').trim();
		if (cls != '') cls = ' ' + cls;

		el.className = '';

		jQuery([
			'<table cellspacing="0" class="tbl-image tbl-image-{0}{1}">'.format(subclass, cls),
				'<tr><td class="tl">&nbsp;</td><td class="up">&nbsp;</td><td class="tr">&nbsp;</td>',
				'<tr><td class="lt">&nbsp;</td><td class="ins"></td><td class="rt">&nbsp;</td></tr>',
				'<tr><td class="bl">&nbsp;</td><td class="dn">&nbsp;</td><td class="br">&nbsp;</td>',
			'</tr></table>'
		].join('')).insertBefore(el).find('td.ins').append(el);
	}

	function wrap_title(el)
	{
		jQuery([
			'<table class="tbl-title"><tr>',
				'<td class="lt"></td>',
				'<td class="mid"></td>',
				'<td class="rt"></td>',
			'</tr></table>'
		].join('')).insertBefore(el).find('td.mid').html(el.innerHTML);

		jQuery(el).remove();
	}

	function on_prod_link_over(el)
	{
		if (descr_overlay === null)
		{
			descr_overlay = jQuery('<div class="descr-overlay"><div class="descr-back"></div><div class="descr-text"></div></div>').appendTo('.products');
			descr_overlay.hide();
		}

		descr_overlay.stop(true, true);

		descr_overlay.css('bottom', (jQuery('.products').get(0).offsetHeight - el.offsetTop + (S.is_ie ? 83 : 57)) + 'px');
		descr_overlay.css('left', (el.offsetLeft + (S.is_ie ? 120 : 140)) + 'px');
		descr_overlay.find('.descr-text').html(jQuery(el).find('.descr').html());

		if (S.is_ie)
		{
			descr_overlay.show();
			descr_overlay.find('.descr-back').height(descr_overlay.find('.descr-text').height() + 20);
			descr_overlay.hide();
		}

		descr_overlay.show(200);
	}

	function on_prod_link_out()
	{
		if (descr_overlay != null)
		{
			descr_overlay.stop(true, true);
			descr_overlay.hide(200);
		}
	}

	function bind_prod_events(el)
	{
		var link = jQuery(el).find('a');

		link.bind('mouseover', function(){ on_prod_link_over(el) });
		link.bind('mouseout', on_prod_link_out);
	}

	function replace_elements()
	{
		jQuery('img.image-left').add('img.image-right').add('img.image-norm').map(function(){ wrap_image(this); });

		jQuery('a.image-left').add('a.image-right').add('a.image-norm').map(function(){
			if (this.href.match(/\.(jpg|gif|png)$/i)) jQuery(this).attr('rel', 'lightbox');
			wrap_image(this);
		});

		jQuery('.products .item h4').map(function(){ wrap_title(this); });
		jQuery('.products .item').map(function(){ bind_prod_events(this); });

		(S.is_ie ? Main.on_resize_ie() : Main.on_resize());
	}

	function lightbox_it()
	{
		jQuery('.text a[rel="lightbox"]').lightBox({
			imageLoading: IMG + 'lightbox-ico-loading.gif',
			imageBtnPrev: IMG + 'lightbox-btn-prev.gif',
			imageBtnNext: IMG + 'lightbox-btn-next.gif',
			imageBtnClose: IMG + 'lightbox-btn-close.gif',
			imageBlank: IMG + 'lightbox-blank.gif'
		});
	}

	return {
		on_resize: function()
		{
			var st_wrap = S.get('wrap').style;
			var st_bg_top = S.get('bg-top').style;

			st_wrap.height = '100px';
			st_wrap.width = '100px';
			st_bg_top.width = '100px';

			var hgt = S.info.page.height();

			st_wrap.height = (hgt + 20) + 'px';

			var wdt = S.info.page.width() - (S.is_ie ? 21 : 0);

			st_wrap.width = wdt + 'px';
			st_bg_top.width = wdt + 'px';

			if (S.is_ie)
			{
				jQuery('.cont #sheet .text').get(0).style.zoom = '100%;';
				jQuery('.cont #sheet .text').get(0).style.zoom = 1;

				var el_sheet = S.get('sheet');
				var st_sheet_bg = S.get('sheet-bg').style;
				var st_sheet_left = S.get('sheet-left').style;
				var st_sheet_right = S.get('sheet-right').style;

				var sh = el_sheet.offsetHeight;
				var sw = el_sheet.offsetWidth;

				st_sheet_bg.height = sh - 480;
				st_sheet_bg.width = sw - 100;
				st_sheet_left.height = sh - 480;
				st_sheet_right.height = sh - 480;

				h_resize = 0;
			}
		},

		on_resize_ie: function()
		{
			if (h_resize > 0) clearTimeout(h_resize);
			h_resize = setTimeout(Main.on_resize, 10);
		},

		opt_ie6: function()
		{
			document.write('<style type="text/css">\n');
			document.write('@import url("' + ROOT + 'css/ie6.css");\n');
			document.write('</style>');

//			document.write('<'+'script type="text/javascript">\n');
//			document.write('var IE6UPDATE_OPTIONS = {');
//			document.write('icons_path: "http://static.ie6update.com/hosted/ie6update/images/",');
//			document.write('message: "Ваш браузер устарел. Нажмите сюда для обновления..."');
//			document.write('}\n');
//			document.write('<'+'/'+'script>');
//			document.write('<'+'script type="text/javascript" src="http://static.ie6update.com/hosted/ie6update/ie6update.js"><'+'/'+'script>');
		},

		opt_opera: function()
		{
			document.write('<style type="text/css">\n');
			document.write('@import url("' + ROOT + 'css/opera.css");\n');
			document.write('</style>');
		},

		opt: function()
		{
			if (window.opera) Main.opt_opera();
			if (window.ie6) Main.opt_ie6();
		},

		main: function()
		{
			S.get('wrap').style.display = '';
			Main.on_resize();
		},

		on_load: function()
		{
			Main.on_resize();
			replace_elements();
			lightbox_it();
		}
	};
}();

S.add_handler(window, 'load', Main.on_load);
S.add_handler(window, 'resize', (S.is_ie ? Main.on_resize_ie : Main.on_resize));

function FixOverlay()
{
	var el_overlay = S.get('jquery-overlay');
	var el_box = S.get('jquery-lightbox');

	if (!el_overlay || !el_box || el_overlay.style.display=='none' || el_box.style.display=='none') return;

	var hgt = el_box.offsetTop + el_box.offsetHeight + 32;
	if (document.body.offsetHeight > hgt) hgt = document.body.offsetHeight;

	if (el_overlay.offsetHeight < hgt) el_overlay.style.height = Math.round(hgt) + 'px';

	if (S.is_ie)
	{
		var top = S.get('lightbox-container-image-box');
		var bot = S.get('lightbox-container-image-data-box');

		if (top && bot) bot.style.width = top.offsetWidth + 'px';
	}
}

setInterval(FixOverlay, 500);

