/*
 * Notamedia 2009
 */

$(function(){

	var self = this;

	var flashvars = {
			videoURL:'/static/video/gromov.mov',
			playFunction:'$.gromovPlay',
			stopFunction:'$.gromovStop',
			pauseFunction:'$.gromovPause'
		};
	var params = {
			wmode:'transparent',
			allowscriptaccess:'always'
		};
	var attributes = {
		id:'swfobject_index',
		name:'swfobject_index'
	};

	swfobject.embedSWF("/static/swf/index.swf?12", "swfobject_index", "100%", "489", "9.0.0", "/static/swf/expressInstall.swf", flashvars, params, attributes);


	var self = this,
		$lenta = $('#lenta2'),
		$prevLnk = $('#arrow .prev'),
		$nextLnk = $('#arrow .next'),
		$curB = $lenta.find('>.def').eq(0),
		playing = false;

	
	$prevLnk.click(function(){
		var $b = $curB.next();
		if ($b.length) {
			$curB = $b;
			self.openBlock();
		}
	});

	$nextLnk.click(function(){
		var $b = $curB.prev();
		if ($b.length) {
			$curB = $b;
			self.openBlock();
		}
	});
	
	$nextLnk.addClass('disabled');
	
	$lenta.find('>.def').click(function(){
		$curB = $(this);
		self.openBlock();
	});
	
	this.openBlock = function(){
		
		if(document['swfobject_index'] && playing){
			document['swfobject_index'].stopVideo();
			return;
		}
		
		
		var $b = $curB;
			$pre = $b.prevAll(),
			r = 300 - ($pre.length * 180);


		var $bn = $curB.next(),
			$bp = $curB.prev();
		if(!$bn.length){
			$prevLnk.addClass('disabled');
			$nextLnk.removeClass('disabled');
		} else if(!$bp.length){
			$nextLnk.addClass('disabled');
			$prevLnk.removeClass('disabled');
		} else {
			$nextLnk.removeClass('disabled');
			$prevLnk.removeClass('disabled');
		}

		$lenta.animate({ right:r }, 400, function(){
			self.closeBlocks();
			if($b.hasClass('video')){
				$b.append('<div class="flash"><a href="http://get.adobe.com/flashplayer">Загрузить флэш плеер</a><div id="swfobject_video"></div></div>');
				$b.css({ width:320 }).find('.shad').removeClass('shad').addClass('shad2');
				
				var $bigPic = $b.find('.bigPic');
				
				var flashvars = {
						videoURL: $bigPic.attr('rel'), //'/static/video/gromov.flv',
						imageURL: $bigPic.attr('src')  //'/static/images/pic1.jpg'
					};
				var params = {
						wmode:'window',
						allowfullscreen:true
					};
				var attributes = {};
			
				swfobject.embedSWF("/static/swf/video.swf", "swfobject_video", "320", "240", "9.0.0", "/static/swf/expressInstall.swf", flashvars, params, attributes);
			} else {
				$b.css({ width:320 }).find('.shad').removeClass('shad').addClass('shad2');
			}
			
			$lenta.find('>.def').removeClass('act');
			$curB.addClass('act');
		});

	}
	
	this.closeBlocks = function(){
		var $bs = $lenta.find('>.def');
		$bs
			.removeClass('act')
			.css({ width:'' })
			.find('.flash')
				.remove()
			.end()
			.find('.shad2')
				.removeClass('shad2')
				.addClass('shad');
	}

	$.gromovPlay = function(){
		playing = true;
		$lenta.animate({ top:100 }, 300);
		self.closeBlocks();
	}
	
	$.gromovStop = $.gromovPause = function(){
		playing = false;
		$lenta.animate({ top:0 }, 300);
		self.openBlock();
		
	}

	this.openBlock();

});

