$(function(){
	/**
	 * check video
	 */
	// if has pages
	function set_arr_act(id_title, id_cont) {
		// set buttons activation
		$(id_title+' .new_prev').removeClass('no_act');
		$(id_title+' .new_next').removeClass('no_act');
		if ($(id_cont+' .pages span').is(':first-child')) {
			$(id_title+' .new_prev').addClass('no_act');
		} else if ($(id_cont+' .pages span').is(':last-child')) {
			$(id_title+' .new_next').addClass('no_act');
		}
	}
	
	if ($('#video_block .pages a').length) {
		set_arr_act('#video_title', '#video_block');		
		
		$('#video_title').delegate('.new_prev', 'click', function(){
			if ($('#video_block .pages span').length) {
				var url = $('#video_block .pages span').prev('a').attr('href');
			}
			set_arr_act('#video_title', '#video_block');
			if (url != undefined) {
				$.ajax({
					type: "GET",
					url: url,
					beforeSend: function(){
						$('#video_block').html('<div class="block_overlay"></div>');
						$('#video_block .block_overlay').show();
					},
					success: function(data){
						$('#video_block .block_overlay').hide();
						$('#video_block').html($('#video_block', data).html());
						set_arr_act('#video_title', '#video_block');
					}
				});
			}
			return false;
		});
		
		// click NEXT
		$('#video_title').delegate('.new_next', 'click', function(){
			if ($('#video_block .pages span').length) {
				var url = $('#video_block .pages span').next('a').attr('href');
			}
			set_arr_act('#video_title', '#video_block');
			if (url != undefined) {
				$.ajax({
					type: "GET",
					url: url,
					beforeSend: function(){
						$('#video_block').html('<div class="block_overlay"></div>');
						$('#video_block .block_overlay').show();
					},
					success: function(data){
						$('#video_block .block_overlay').hide();
						$('#video_block').html($('#video_block', data).html());
						set_arr_act('#video_title', '#video_block');
					}
				});
			}
			return false;
		});
		
	} else {
		$('#video_title .new_prev').addClass('no_act');
		$('#video_title .new_next').addClass('no_act');
		
		$('#video_title').delegate('.new_prev', 'click', function(){
			return false;
		});
		
		$('#video_title').delegate('.new_next', 'click', function(){
			return false;
		});
	}
	
	$('#video_block .vitem').click(function(){
		$('#video_block .vitem').removeClass('cur');
		var $this = $(this);
		$this.addClass('cur');
		var video_src = $this.find('.vide_src').attr('href');
		
		var str = '<object width="452" height="269" id="bx_flv_player_11350" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">'
		+'<param value="/bitrix/components/bitrix/player/mediaplayer/player.swf" name="movie">'
		+'<param value="high" name="quality">'
		+'<param value="transparent" name="wmode">'
		+'<param value="file='+video_src+'&amp;autostart=true&amp;logo.hide=true&amp;bufferlength=10&amp;dock=true&amp;provider=video&amp;abouttext=Bitrix Media Player&amp;aboutlink=http://www.bitrixsoft.com/products/cms/features/player.php#tab-main-link" name="flashvars">'
		+'<param value="always" name="allowscriptaccess">'
		+'<param value="true" name="allowfullscreen">'
		+'<embed width="452"  height="269" flashvars="file='+video_src+'&amp;autostart=true&amp;logo.hide=true&amp;bufferlength=10&amp;dock=true&amp;provider=video&amp;abouttext=Bitrix Media Player&amp;aboutlink=http://www.bitrixsoft.com/products/cms/features/player.php#tab-main-link" wmode="transparent" menu="true" allowfullscreen="true" allowscriptaccess="always" type="application/x-shockwave-flash" src="/bitrix/components/bitrix/player/mediaplayer/player.swf" name="bx_flv_player_11350_embed" id="bx_flv_player_11350_embed">'
		+'</object>';
		
		$('#video').html(str);
		return;
	});
	

	/**
	 * main news listing
	 */
	if ($('.news_block .pages a').length) {
		set_arr_act('#news_title', '.news_block');		
		
		$('.news_block').height($('.news_block').height()+'px');
		
		$('#news_title').delegate('.new_prev', 'click', function(){
			if ($('.news_block .pages span').length) {
				var url = $('.news_block .pages span').prev('a').attr('href');
			}
			set_arr_act('#news_title', '.news_block');
			if (url != undefined) {
				$.ajax({
					type: "GET",
					url: url,
					beforeSend: function(){
						$('.news_block').html('<div class="block_overlay"></div>');
						$('.news_block .block_overlay').show();
					},
					success: function(data){
						$('.news_block .block_overlay').hide();
						$('.news_block').html($('.news_block', data).html());
						set_arr_act('#news_title', '.news_block');
					}
				});
			}
			return false;
		});
		
		// click NEXT
		$('#news_title').delegate('.new_next', 'click', function(){
			if ($('.news_block .pages span').length) {
				var url = $('.news_block .pages span').next('a').attr('href');
			}
			set_arr_act('#news_title', '.news_block');
			if (url != undefined) {
				$.ajax({
					type: "GET",
					url: url,
					beforeSend: function(){
						$('.news_block').html('<div class="block_overlay"></div>');
						$('.news_block .block_overlay').show();
					},
					success: function(data){
						$('.news_block .block_overlay').hide();
						$('.news_block').html($('.news_block', data).html());
						set_arr_act('#news_title', '.news_block');
					}
				});
			}
			return false;
		});
		
	} else {
		$('#news_title .new_prev').addClass('no_act');
		$('#news_title .new_next').addClass('no_act');
		
		$('#news_title').delegate('.new_prev', 'click', function(){
			return false;
		});
		
		$('#news_title').delegate('.new_next', 'click', function(){
			return false;
		});
	}
});
