if ($.browser.msie) var ie = true;
if ($.browser.msie && $.browser.version.substr(0,1)<7) var ie6 = true;
if (!curPage) var curPage = '';
if (!curLang) var curLang = 'ru';



$(document).ready(function() {

	if (ie6 == true) {
		DD_belatedPNG.fix('body.index #menu a img, #logo img');
	}

	$('a').live('mousedown, mouseout',function(){
		$(this).blur();
	});

	$('#about').css({width:126*$('#menu a').size()});
		
	$('a').filter(function(){
		var link = $(this).attr('href');
		if (
			link.indexOf('http://') >= 0 &&
			link.indexOf('cinelab') < 0
		) {return this;}
	}).addClass('partnerLink').click(function(e){
		e.preventDefault();
		window.open(this.href,'_blank')
	});

/* Menu animation purposes */
	$('#menu a').each(function(){
		var img = $('img', $(this)).attr('src');
		var img = $('<img/>',{
			src : img
		});
		$(this).find('ins').html(img);

		var link = $(this).attr('href');
		if(link.indexOf(curPage) > 0) {
			$(this).addClass('active');
		}
	});

	if (ie == true) {
		$('#menu a ins').addClass('ie');
		$('#menu a').not('.active').hover(function(){
			$('ins img', this).css({display:'block'});
		}, function(){
			$('ins img', this).css({display:'none'});
		});

		$('#menu a').click(function(e){
			e.preventDefault();
			window.location.href = $(this).attr('href');
		});

	} else {
		$('#menu a').not('.active').find('ins').animate({opacity:0},0);
		$('#menu a').not('.active').find('ins').hover(function(){
			$(this).animate({opacity:1},400);
		}, function(){
			$(this).animate({opacity:0},400);
		});
	}

/* END Menu animation */

/* Content trees */
	$('#catalogue dl dd, #descriptions dd').filter(function(){
		if ($(this).hasClass('only')){
			return false;
		} else {
			return this;
		}
	}).slideUp(0);
	$('#catalogue dl').slideUp(0);

	$('#catalogue h3 a, #descriptions dt a').click(function(e){
		e.preventDefault();
		$('#right-content').html('');
		if ($(this).data('opened') != true) {
			var link = $(this).attr('href');
			if (link.indexOf('#') >= 0 && link.length > 1) {
				window.location.hash = link;
			}
			$('#catalogue dl, #descriptions dd').slideUp(0,'linear');
			$('#catalogue h3 a, #descriptions dl dt a').removeClass('opened').removeData('opened');
			$('dl',$(this).parents('li')).slideDown(0,'linear',function(){$(this).css({overflow:'visible'})});
			$('dd',$(this).parents('#descriptions dl')).slideDown(0,'linear',function(){$(this).css({overflow:'visible'})});
			$(this).addClass('opened').data({opened:true});
			$('dd.only tr:first-child td.name a',$(this).parents('li')).trigger('click');
		} else {
			window.location.hash = '!';
			$('dl',$(this).parents('li')).slideUp(0,'linear');
			$('dd',$(this).parents('#descriptions dl')).slideUp(0,'linear');
			$(this).removeClass('opened').removeData('opened');
		}
	});

	$('#catalogue dt a').click(function(e){
		e.preventDefault();
		if ($(this).data('opened') != true) {
			$('#catalogue dd').slideUp(0);
			$('#catalogue dt a').removeClass('opened').removeData('opened');
			$('dd',$(this).parents('dl')).slideDown(0,function(){
				$(this).parents('dl').find('dd tr:first-child td.name a').trigger('click');
			});
			$(this).addClass('opened').data({opened:true});
		} else {
			$('dd',$(this).parents('dl')).slideUp(0);
			$(this).removeClass('opened').removeData('opened');
		}
	});
/* END Content trees */

/* Catalog items */

	$('#catalogue td.name a, #right-content a').live('click',function(e){
		e.preventDefault();
		var link = $(this).attr('href');
		window.location.hash = link;
		var visited = $('#catalogue').find('a[href="'+link+'"]');

		if ($('h3 a', visited.parents('li')).data('opened') != true){
			$('dl',visited.parents('li')).slideDown(0,'linear',function(){$(this).css({overflow:'visible'})});
			visited.parents('li').find('h3 a').addClass('opened').data({opened:true});
		}

		if ($('dt a', visited.parents('dl')).data('opened') != true){
			$('dd',visited.parents('dl')).slideDown(0,'linear',function(){$(this).css({overflow:'visible'})});
			visited.parents('dl').find('dt a').addClass('opened').data({opened:true});
		}

		//var link = link + ' .good-details';
		$('#catalogue td.name a').removeClass('opened');
		visited.addClass('opened visited');
		if (curLang == 'ru') {
			pleaseWait = 'Подождите';
			errorMessage = 'К&nbsp;сожалению, информация не&nbsp;найдена';
		} else if (curLang == 'en') {
			pleaseWait = 'Please wait';
			errorMessage = 'Unfortunately data could not be loaded.';
		}

		$("#right-content").html('<div class="loading-indicator"><img src="/_global/images/loading.gif" width="28" height="28"><br />&nbsp;&nbsp;' + pleaseWait + '&hellip;</div>').load(link, function (responseText, textStatus, req) {
				if (textStatus == "error" || textStatus == 'timeout') {
					$("#right-content").html('<div class="loading-error">' + errorMessage + '</div>');
					$('#catalogue td.name a').removeClass('opened');
				}

		if (getScroll() >= 305) {
			$('#right-content').css({paddingTop:getScroll()-253});
		}

		});

	});

	var padding = 52;

	$(window).scroll(function(){
		var newTop = getScroll()-250;
		var curTop = parseInt($('#right-content').css('padding-top'));
		if (newTop < curTop && newTop > 52) {
			$('#right-content').css({paddingTop:newTop});
		} else if (newTop > curTop){
		} else if (newTop <= 52) {
			$('#right-content').css({paddingTop:52});
		}
	});

/* END Catalog items */

/* Hash links triggering */
	var hash = window.location.hash;
	if (hash != '' && hash != '#!' && hash != '#') {

		if (hash.indexOf('#') == 0) {
			hash = hash.substr(1);
		} else if (hash.indexOf('/#') == 0) {
			hash = '/' + hash.substr(2);
		}

		$('a').filter(function(){
			var link = $(this).attr('href');
			if (link == hash || link == '#'+hash || link == '/#'+hash || link == '/'+hash) {
				return this;
			}
		}).trigger('click');

	} else {
		$('#about-dl dt a').trigger('click');
	}

/* END Hash links triggering */

});

function getScroll() {
	var scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		scrOfY = window.pageYOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		scrOfY = document.body.scrollTop;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		scrOfY = document.documentElement.scrollTop;
	}
	return scrOfY;
}
