$(document).ready(function () {
	if ($('#coloredPicturesPanel').length == 0) return;
	var h = {
		settings: {
			interval: 7.5,
			ratio: 16 / 10
		},
		arrow: $('#subPanel #arrow'),
		container: $('#coloredPicturesPanel'),
		containerOffset: $('#coloredPicturesPanel').offset(),
		activeElement: $('#coloredPicturesPanel ul').filter(':visible').children('li:first'),
		imageArea: $('#coloredPicturesPanel div.image'),
		flashContainer: $('#flashTrailer'),
		leftNav: $('#leftNav'),
		preload: function (b) {
			if (typeof b == 'undefined') return;
			$(b).each(function () {
				var a = this,
					img = $('<img/>');
				img.attr({
					class: 'preloadImg',
					src: this.src
				}).load(function () {
					$(a).parents('li').attr('loaded', true).find('img.aLoader').remove();
					if ($('li:not([loaded])', h.container).length == 0) h.start();
					$(this).remove()
				}).appendTo('body')
			});
			return h
		},
		cookie: {
			set: function (a, b, c) {
				var d = new Date();
				if (!c) c = 1;
				d.setTime(d.getTime() + 1000 * 60 * 60 * 24 * c);
				document.cookie = a + "=" + escape(b) + ";expires=" + d.toGMTString()
			},
			get: function (a) {
				var b = document.cookie,
					prefix = a + "=";
				var c = b.indexOf("; " + prefix);
				if (c == -1) {
					c = b.indexOf(prefix);
					if (c != 0) return null
				} else c += 2;
				var d = document.cookie.indexOf(";", c);
				if (d == -1) d = b.length;
				return unescape(b.substring(c + prefix.length, d))
			}
		},
		showTrailer: function (a) {
			if (typeof a == 'undefined') return;
			$('#subPanel').stop().animate({
				left: -15,
				opacity: .75
			},
			500);
			h.clearTimer();
			clearTimeout(h.panelTimer);
			if (typeof h.imageHeight == 'undefined') h.imageHeight = parseInt(294 / 607 * h.imageWidth);
			var b = "<object width='" + h.imageWidth + "' height='" + h.imageHeight + "'><param name='movie' value='http://www.kinopoisk.ru/js/player9.swf' /><param name='wmode' value='opaque' /><param name='allowFullScreen' value='true' /><param name='flashVars' value='" + a + "&__W=" + h.imageWidth + "&__H=" + h.imageHeight + "&__autoplay=false' /><embed src='http://www.kinopoisk.ru/js/player9.swf' type='application/x-shockwave-flash' wmode='opaque' width='" + h.imageWidth + "' height='" + h.imageHeight + "' allowfullscreen='true' flashvars='" + a + "&__W=" + h.imageWidth + "&__H=" + h.imageHeight + "&__autoplay=false' /></object>";
			try {
				h.flashContainer.show().html(b)
			} catch(e) {};
			$('img', h.imageArea).hide()
		},
		gc: function (_, v, o, i, d) {
			return true
		},
		markActiveNext: function () {
			var e = h.activeElement.next('[loaded]');
			if (e.length == 0) e = $('#coloredPicturesPanel ul').filter(':visible').children('li[loaded]:first');
			h.markActive(e)
		},
		setTimer: function () {
			h.clearTimer().timer = setInterval(h.markActiveNext, h.settings.interval * 1000);
			return h
		},
		clearTimer: function () {
			clearInterval(h.timer);
			h.timer = 0;
			return h
		},
		closePanel: function () {
			$('#subPanel').stop().animate({
				left: -15,
				opacity: .75
			},
			500)
		},
		start: function (f) {
			f = $('li', h.container).filter(':visible:first');
			var f = $('img.changeImage', f).attr('src');
			h.imageContainer.append('<img src="' + f + '" alt="" class="bg" /><img src="' + f + '" alt="" class="fg" />');
			h.markActive().setTimer();
			return h
		},
		resizeBlock: function () {
			var a = parseInt((h.container.width() - 160) / h.settings.ratio) - 10,
				leftNavDiv = h.leftNav.children('div'),
				h4 = $('h4.active:first-of-type', h.leftNav),
				li = h4.next('ul').children('li'),
				newListHeight = parseInt((a - h4.height()) / li.length);
			li.css('height', '');
			if (leftNavDiv.length == 0) h.leftNav.wrapInner(leftNavDiv = $('<div></div>').css('float', 'left'));
			a = Math.max(a, leftNavDiv.height());
			h.container.css('height', a);
			newListHeight -= 20;
			newListHeight = Math.max(newListHeight, 20);
			li.css('height', newListHeight).css('line-height', newListHeight + 'px')
		},
		markActive: function (e) {
			if (typeof e == 'undefined') e = h.activeElement.get(0);
			if (h.flashContainer.is(':visible')) {
				h.flashContainer.hide();
				$('img', h.imageArea).show()
			}
			$(h.activeElement).css('margin-left', 0);
			clearTimeout(h.marqueeTimer);
			clearTimeout(h.panelTimer);
			$('#subPanel').stop().animate({
				left: -15,
				opacity: .75
			},
			500);
			$('span', h.arrow).removeClass('reverse');
			h.setTimer();
			if ($('a', e).width() > 170) {
				$(e).css('width', $('a', e).width());
				var k = 1;
				h.marqueeTimer = setInterval(function () {
					$(e).css('margin-left', parseInt($(e).css('margin-left')) - k * 1);
					if (($('a', e).width() + parseInt($(e).css('margin-left'))) <= 140) k = -1;
					if (parseInt($(e).css('margin-left')) == 0) k = 1
				},
				30)
			}
			var a = $(e).offset();
			if (h.activeElement) h.activeElement.removeClass('currentElement');
			h.activeElement = $(e).addClass('currentElement');
			h.arrow.stop().animate({
				'paddingTop': parseInt(a.top - h.containerOffset.top + $(e).height() * .5 - 4)
			},
			400);
			var b = $('img.bg', h.imageArea);
			b.attr('src', $('img.changeImage', e).attr('src')).next('img.fg').stop().animate({
				opacity: 0
			},
			300, function () {
				$(this).attr('src', b.attr('src')).css('opacity', 1)
			});
			return h
		}
	},
		isIE = $.browser.msie,
		openBtn = $('<a href="#" id="openBtn">Развернуть</a>').click(function () {
		h.container.slideDown('fast');
		h.cookie.set('sliderHide', 0, 365 * 10);
		$(this).remove();
		return false
	}),
		closeBtn = $('<a href="#" id="closeBtn"></a>').css('opacity', 0).appendTo(h.container).hover(function () {
		$(this).animate({
			opacity: 1
		},
		'fast')
	},
	function () {
		$(this).animate({
			opacity: .5
		},
		'fast')
	}).click(function () {
		h.container.slideUp(1000, function () {
			$(this).after(openBtn)
		});
		h.cookie.set('sliderHide', 1, 365 * 10);
		return false
	}).hide();
	h.container.hover(function () {
		closeBtn.animate({
			opacity: .5
		},
		'fast')
	},
	function () {
		closeBtn.animate({
			opacity: 0
		},
		'fast')
	});
	h.imageContainer = $('div.image', h.container);
	if (h.preload($('img.changeImage', h.container).each(function () {
		$(this).parents('li:first').prepend('<img src="pic/load.gif" class="aLoader" alt="" />')
	})).gc()) $('h4', h.leftNav).click(function () {
		if ($(this).is('.active')) return false;
		$(this).parent().find('h4.active').removeClass('active').next('ul').slideUp('fast');
		$(this).addClass('active');
		$(this).next('ul').slideDown('fast', function () {
			h.markActive($('li', this).get(0));
			h.resizeBlock()
		})
	});
	var j = h.container.width();
	h.imageWidth = j - h.leftNav.width();
	if (isIE) $('img', h.imageArea).css('width', h.imageWidth);
	$('li', h.leftNav).hover(function () {
		h.clearTimer();
		h.markActive(this)
	},
	function () {
		h.setTimer()
	});
	h.arrow.hover(function () {
		var b = $(this).parent();
		h.clearTimer();
		$('div.content:first-of-type', h.container).html($('div', h.activeElement).html()).find('a.icon').click(function () {
			if ($(this).is('a.trailer')) {
				var a = this.href.substring(this.href.indexOf('#') + 1);
				h.showTrailer(a)
			} else open(this.href);
			return false
		});
		if (parseInt(b.css('left')) == -15) {
			$('span', h.arrow).addClass('reverse');
			b.stop().animate({
				left: 170,
				opacity: .9
			},
			500)
		}
	});
	h.resizeBlock();
	var l = 0;
	window.onresize = function () {
		clearTimeout(l);
		l = setTimeout(function () {
			h.resizeBlock()
		},
		10)
	};
	if (h.cookie.get('sliderHide') == 1) h.container.hide().after(openBtn)
});
