$(document).ready(function()
{
	if ($('a.but-order[rel]').length>0) {
		os.require('modules/goods/cart-button');
	}
	
	$('#toggle-bottom-sitemap').on('click', function(){
		$('#bottom-sitemap').toggle();
		$(this).toggleClass('opened');
	});
	
	if ($('a span.hint').length>0)
	{
		js.include('jquery.qtip.min.js');
	
		$('a span.hint').each(function()
		{
			var $title = $(this).closest('a').clone().remove('img').html();
			var $hint = $(this).html();
			$(this).parent('a').qtip({
				show: {
					delay: 1
				},
				content: {
					title: {
						text: $title
					},
					text: $hint
				},
				style: {
					width: {
						min: 600
					},
					title: {
						'background-color': '#2967a2',
						'color': '#fff',
						'font-family': 'Arial',
						'font-size': '14px'
					},
					border: {
						radius: 5
					}
				},
				position: {
					target: 'mouse',
					adjust: {
						mouse: true
					}
				}
			}).removeAttr('title');
		});
	}

	$('select.autosubmit').change(function()
	{
		if (this.form)
			$(this.form).submit();
	});

	$('.content-inner img').each(function()
	{
		var $img = $(this);
		var $width = $img.width();
		var $height = $img.height();

		var $max_width = 700;
		if ($img.closest('#hivemind-comments').size() > 0)
		{
			var $max_width = 600;
		}

		if ($width > $max_width)
		{
			var $coeff = $max_width / $width;
			$width *= $coeff;
			$height *= $coeff;

			$img.width($width).height($height).closest('#content table, .content table').removeAttr('width').css({width: 'auto'});
		}
	});

	$('a[href]').each(function()
	{
		var $a = $(this);
		var $href = $a.attr('href');

		if ($href.match(/sap-press\.com|sappress\.com/))
		{
			if ($href.match(/\.html$/))
				$a.attr('href', $href + '/ref=2V0lTQUZGS0VZQ0hFQ0tTVU1fTVowODU5T');
		}
	});

	if ($("select[multiple]").not('.no-asmselect').size() > 0)
	{
		js.css('../js/bsmselect/jquery.bsmselect.css');
		os.require('bsmselect/jquery.bsmselect', function(){
			os.require('bsmselect/jquery.bsmselect.sortable', function(){
				$("select[multiple]").not('.no-asmselect').not('[disabled]').bsmSelect({
					addItemTarget: 'top',
					sortable: true,
					removeLabel: 'Убрать',
					highlightAddedLabel: 'Добавлено: ',
					highlightRemovedLabel: 'Убрано: ',
					plugins: [
						$.bsmSelect.plugins.sortable()
					]
				});
		
				$("select[multiple]").not('.no-asmselect').filter('[disabled]').attr('rows', 1);
			});
		});
	}

	if ($('.split').size() > 0)
	{
		js.include('autocolumn.min.js');
		$('.split').columnize({
			columns: 2,
			lastNeverTallest: true
		});
	}

	$('.content-inner .message').delay(4000).fadeOut(2000);
	
	if ($("input.date").length>0)
		js.include('dates.js');
});

$('input#search').ready(function()
{
	os.require('modules/search/common');
});

$('a.highslide').live('click', function(e){
	e.preventDefault();
	js.css('../lib/highslide/highslide.css');
	js.include('../lib/highslide/highslide-with-gallery.cpacked.js');
	js.include('../lib/highslide/highslide.config.js');
	
	return hs.expand(this);
});
