if (!Array.prototype.indexOf)
{
	Array.prototype.indexOf = function(elt /*, from*/)
	{
		var len = this.length >>> 0;

		var from = Number(arguments[1]) || 0;
		from = (from < 0)
		? Math.ceil(from)
		: Math.floor(from);
		if (from < 0)
			from += len;

		for (; from < len; from++)
		{
			if (from in this &&
				this[from] === elt)
				return from;
		}
		return -1;
	};
}


// Forces paginator to use ajax
function bindClickPaginator(ajaxContainerId)
{
	var lnk = [];
	$('.pages .container li a').each(function(i, el){
		el = $(el);
		lnk.push(el);
	});
	lnk.push($('.pages .nav_left a'));
	lnk.push($('.pages .nav_right a'));

	for(i in lnk)
	{
		lnk[i].click(function() {
			$('#'+ajaxContainerId).load($(this).attr('href'), function(){
				bindClickPaginator(ajaxContainerId)
			});
			return false;
		});
	}
}
	
// 
function rand(min, max)
{
	if(!min && !max) {
		min = 0; max = 2147483648;
	}
	return Math.round( Math.random() * (max - min) ) + min;
}

function paginatorScroll(opt)
{
	$('.'+opt.blockItemClass, '.'+opt.blockScrollClass).eq(0).attr('contentStatus', 2);
	var selector = opt.containerClass ? '.'+opt.containerClass : (opt.containerId ? '#'+opt.containerId : null);
	$(selector).paginator({
		total:opt.totalPages,
		animateScroll:true,
		onSelect: function(num){

			// TODO:
			// 		Don`t change this block now as it also being used in lightbox callback and gallery arrows scroll
			//		Paginator and scroll arrows should be refactoried
					var ajaxUrl = opt.ajaxUrl;
					var scroller = $('.'+opt.blockScrollClass);
					var page = $('.'+opt.blockItemClass, scroller).eq(num);
					if (!page.attr('contentStatus')) {
						page.attr('contentStatus', 1);
						page.load(ajaxUrl+'?page='+num, function(){
							page.attr('contentStatus', 2);
						});
					}
					$('.'+opt.containerClass).paginator('select', num);

					scroller.animate({marginLeft: (-num * $('.'+opt.blockItemClass, scroller).outerWidth(true))}, {queue: false, duration: 500});
			//

			if(opt.leftArrowClass && opt.rightArrowClass) {
				$.switchArrows(opt.leftArrowClass, opt.rightArrowClass, num);
			}

			paginatorScroll.activePage = num;
		}
	}).paginator('show');
}

function checkAndLoadImagesByTag(num, element) {
	var options;
	if (typeof($.loadOptions)=='undefined' || !(options=$.loadOptions[element.attr('id')]))
		return;
	var callback = callback;

	$('li img:animated', element).stop(true,true);
	$('li:active img', element).animate(options.inactiveCss, {queue: false, duration: options.duration});

	$('li', element).slice(0,num+2).each(function(i){
		var li = $(this);

		if (i == num)
			$('img', li).animate(options.activeCss, {queue: false, duration: options.duration});

		if (li.data('contentStatus'))
			return;

		li.data('contentStatus', 1); // loading

		$('li', element).slice(0,num+2).each(function(i){
			if (this.contentStatus)
				return;

			var page = i;
			$.ajax({
				async: true,
				dataType: 'html',
				success: function(result) {
					li.data('contentStatus', 2);
					li.html(result);
					if (li.hasClass('active'))
						$('img', li).css(options.activeCss);
					fontReplace();
					$('.lightbox', li).lightBox();
				},
				url: options.url,
				data: {	page: page }
			});
		});
	});
}

function switchImageInScroller(evt, num) {
	var element = $(evt.target);

	// stopping current animations
	$('li.scroller-li .oneImage:animated', element).stop(true,true);
	$('li.scroller-li .preview img:animated', element).stop(true,true);

	var oneImage_animation = {};
	var preview_animation = {};

	switch (evt.type) {
		case 'scrollerbeforeselect':
			oneImage_animation = element.scroller('option', 'animation').oneImage.deactivation;
			preview_animation = element.scroller('option', 'animation').preview.deactivation;
			break;

		case 'scrollerselect':
			oneImage_animation = element.scroller('option', 'animation').oneImage.activation;
			preview_animation = element.scroller('option', 'animation').preview.activation;
			break;
	}

	var liActive = element.scroller('getActive');
	$('.oneImage', liActive).animate(oneImage_animation, {queue: false, duration: element.scroller('option', 'animateScroll')});
	$('.preview img', liActive).animate(preview_animation, {queue: false, duration: element.scroller('option', 'animateScroll')});
}

function switchVideoInScroller(evt, num) {
	var element = $(evt.target);

	switch (evt.type) {
		case 'scrollerbeforeselect':
			
			// stopping current animations
			$('li.scroller-li .oneVideo:animated', element).stop(true,true);
			$('li.scroller-li .preview img:animated', element).stop(true,true);

			var liActive = element.scroller('getActive');

			// stopping player
			var player = $('object,embed', liActive);
			if (player.length>0) {
				player.hide();
				if (player.get(0).sendToActionScript)
					player.get(0).sendToActionScript('stop()');
			}

			// showing preview
			$('.preview', liActive).show();

			// animating oneVideo
			var oneVideo = $('.oneVideo', liActive);
			var oneVideo_animation = element.scroller('option', 'animation').oneVideo.deactivation;
			oneVideo.animate(oneVideo_animation, {queue: false, duration: element.scroller('option', 'animateScroll')});

			// animating preview
			var preview = $('.preview img', liActive)
			var preview_animation = element.scroller('option', 'animation').preview.deactivation;
			preview.animate(preview_animation, {queue: false, duration: element.scroller('option', 'animateScroll')});
			
			break;
		
		case 'scrollerselect':
			
			// stopping current animations
			$('li.scroller-li .oneVideo:animated', element).stop(true,true);
			$('li.scroller-li .preview img:animated', element).stop(true,true);

			var liActive = element.scroller('getActive');

			// animating oneVideo
			var oneVideo = $('.oneVideo', liActive);
			var oneVideo_animation = element.scroller('option', 'animation').oneVideo.activation;
			oneVideo.animate(oneVideo_animation, {queue: false, duration: element.scroller('option', 'animateScroll')});

			// animating preview
			var preview = $('.preview img', liActive)
			var preview_animation = element.scroller('option', 'animation').preview.activation;
			preview.animate(preview_animation, {queue: false, duration: element.scroller('option', 'animateScroll'), complete: function(){
				// showing player
				var player = $('object,embed', liActive);
				if (!player.length) {
					attachFlashPlayer($('.flash-player', liActive), null, oneVideo.outerWidth(), oneVideo.outerHeight(), null, null, null, true);
				} else {
					player.show();
				}

				player = $('object,embed', liActive);

				if (player.length>0) {
					// hiding preview
					$('.preview', liActive).hide();
					
					player = player.get(0);
					if (player.sendToActionScript)
						player.sendToActionScript('play()');
				}
			}});
		
			break;
	}
}

	function attachFlashPlayer(element, paths, width, height, flashvars, params, attributes, autostart) {
		var opts = {
			paths: $.extend({
				player: '/flash/player.swf',
				installer: '/flash/expressInstall.swf'
			}, paths || {}),
			width: width,
			height: height,
			flashvars: $.extend({settings_path: ''}, flashvars || {}),
			params: $.extend({
				menu: false,
				wmode: 'transparent',
				scale: 'exactfit',
				allowFullscreen: true,
				allowScriptAccess: 'always'
			}, params || {}),
			attributes: attributes
		};

		element.each(function(i) {
			if (!this.id)
				this.id = 'video-' + i + '-' + Math.round(Math.random() * 100000);

			if (!opts.width)
				opts.width = $(this).width();

			if (!opts.height)
				opts.height = $(this).height();

			if (!opts.flashvars.settings_path)
				opts.flashvars = $.extend(opts.flashvars || {}, {settings_path: $('span.flash-player-settings-path', element).text()});

			var attributes = $.extend({id: this.id + '-flash-video-player'}, opts.attributes);

			swfobject.embedSWF(
				opts.paths.player,
				this.id,
				opts.width,
				opts.height,
				'9.0.0',
				opts.paths.installer,
				opts.flashvars,
				opts.params,
				attributes
			);

			if (autostart) {
				var player = $('#'+attributes.id).get(0);
				if (player.sendToActionScript)
					player.sendToActionScript('play()');
			}

		});
	}

	function lightboxCalbPaging(settings) {
		var ajaxUrl				= $(".GalleryArrowParams").attr("ajaxUrl");
		var blockItemId			= $(".GalleryArrowParams").attr("blockItemId");
		var blockScrollClass	= $(".GalleryArrowParams").attr("blockScrollClass");
		var blockItemClass		= $(".GalleryArrowParams").attr("blockItemClass");
		var containerClass		= $(".GalleryArrowParams").attr("containerClass");
		var changePage			= false;

		if(lightboxCalbPaging.activePage === undefined) {
			lightboxCalbPaging.activePage = settings.activePage;
		}
		if(paginatorScroll.activePage !== undefined) {
			lightboxCalbPaging.activePage = paginatorScroll.activePage;
		}

		var activePage	= lightboxCalbPaging.activePage;
		var totalPages	= settings.totalPages;
		var totalImg	= settings.imageArray.length;
		var currImg		= settings.activeImage;
		var num			= lightboxCalbPaging.activePage;
		var prevEvt		= -1;
		var nextEvt		= totalImg;

		if(activePage > 0) {
			++prevEvt;
		}
		if(activePage < totalPages - 1) {
			--nextEvt;
		}

		if(currImg <= prevEvt) {
			--num;
			changePage = true;
			settings.prev = true;
		}
		if(currImg >= nextEvt) {
			++num;
			changePage = true;
			settings.next = true;
		}

		settings.activePage = lightboxCalbPaging.activePage = num;

		if(changePage) {
		
			var scroller = $('.'+blockScrollClass);
			var page = $('.'+blockItemClass, scroller).eq(num);
			
			if (!page.attr('contentStatus')) {
				page.attr('contentStatus', 1);
				page.load(ajaxUrl+'?page='+num, function(){
					page.attr('contentStatus', 2);
				});
			}
			$('.'+containerClass).paginator('select', num);
			scroller.animate({marginLeft: (-num * $('.'+blockItemClass, scroller).outerWidth(true))}, {queue: false, duration: 500});
		}
	}

function articleHoverAndClick() {

	var announce = $('.AnnouncementItemText')
		.unbind('mouseenter mouseleave')
		.bind('mouseenter mouseleave', function(evt) {
			if ($.trim($(this).siblings('.AnnouncementItemUrl').text()))
				$(this).toggleClass('AnnouncementItemHover', evt.type == 'mouseenter');
		})
		.unbind('click')
		.bind('click', function(){

			var link = $.trim($(this).siblings('.AnnouncementItemUrl').text());

			if (link)
				document.location.href=link;
		});
	$('.AnnouncementItemText').click(function(){
			var link = $.trim($(this).siblings('.AnnouncementItemUrl').text());

			if (link)
				document.location.href=link;
	});
	
	$('a', announce).addClass('Agray');
	
	$('ul li a', announce).each(function(){
		var $this = $(this);
		var link = $.trim($(this).parents('.AnnouncementItemText').siblings('.AnnouncementItemUrl').text());
		if (link)
			$this.attr('href', link);
		else
			$this.lightBox();

		$('img', $this).each(function() {
			var img = $(this);
			var li = img.parent();
			while (String(li.get(0).tagName).toLowerCase() != 'li')
				li = li.parent();

			imageQuadratish(img, li);
		});
	});
}

function imageQuadratish(img, container) {
	img = $(img).css('border', '0');;
	container = $(container);

	var tmpImg = new Image();
	tmpImg.src = img.attr('src');

	if (!tmpImg.width || !container.width())
		return;

	var minsize = Math.min(tmpImg.width, tmpImg.height);

	var wCoef = container.width() / minsize;
	var w = Math.round(tmpImg.width * wCoef);

	var hCoef = container.height() / minsize;
	var h = Math.round(tmpImg.height * hCoef);

	img.width(w);
	img.height(h);

	if (w > container.width()) {
		img.css('marginLeft', Math.round((container.width() - w) / 2)+'px');
	}

	if (h > container.height()) {
		img.css('marginTop', Math.round((container.height() - h) / 2)+'px');
	}
}

function contentLightBoxQuadratish(container) {
	$(container).each(function(){
		$('a.lightbox img', this).each(function() {
			var img = $(this);

			var div = $('<div class="lightbox" />').insertBefore(img);
			img.appendTo(div);

			imageQuadratish(img, div);
		});
	});
}

function fontReplace(selector, hover) {

	if (!selector)
		selector = '.Adonis';

	if (typeof(Cufon)!='undefined') {
		Cufon.now();
		Cufon.replace(selector, {hover: hover || true});
	}
}

function correctVideoBlockHeight(totalPages, countItems)
{
	var basicH = 20;
	var blockH = 410;
	var clStr = ".BlockVideoGallery, .VideoGalleryPlayerBlock, .VideoGalleryPlayerBlockItem, .VideoGalleryPlayerBlockScroll";
	if(totalPages == 1) {
		$(clStr).css('height', (basicH + countItems * blockH)+'px');
	}
}
function openURL(url) {
	document.location.href=url;
}