/* ---------------------------------
all_pages.js
***
Made by: Nikolai Khilkovsky
E-mail: khilkovn@gmail[dot]com
URL: http://nightraven.name
--------------------------------- */
var	elSize=new  Array(),
		animateSpeed=200,
		k=0;
$.tools.validator.localize("ru", {
	'*'             : 'Введите пожалуйста правильное значение',
	':email'        : 'Введите пожалуйста корректный адрес',
	':number'       : 'Введите пожалуйста число',
	'[max]'         : 'Введённое число не должно превышать $1',
	'[min]'         : 'Введённое число должно быть больше $1',
	'[required]'    : 'Поле обязательно для заполнения',
	'[data-equals]' : 'Введенные пароли не совпадают'
});
//Скрипт фонаря
var t=0,p0x=0,p1x=0,p2x=0,p0y=0,p1y=0,p2y=0,X=0,Y=0,M1X=new Array(),M1Y=new Array(),M2X=new Array(),M2Y=new Array();
for (var i=0;i<301;i++){
	if (i<151){
		t=i/150;p0x=1180;p1x=750;p2x=0;p0y=380;p1y=0;p2y=340;
		M1X[i]=Math.floor((1-t)*(1-t)*p0x+2*t*(1-t)*p1x+t*t*p2x)+'px';
		M1Y[i]=Math.floor((1-t)*(1-t)*p0y+2*t*(1-t)*p1y+t*t*p2y)+'px'
	}
	else {
		t=(i-150)/150;p0x=0;p1x=450;p2x=1180;p0y=340;p1y=950;p2y=800;
		M1X[i]=Math.floor((1-t)*(1-t)*p0x+2*t*(1-t)*p1x+t*t*p2x)+'px';
		M1Y[i]=Math.floor((1-t)*(1-t)*p0y+2*t*(1-t)*p1y+t*t*p2y)+'px'
	}
};
for (var i=0;i<301;i++){
	if (i<151){
		t=i/150;p0x=0;p1x=600;p2x=1180;p0y=800;p1y=925;p2y=325;
		M2X[i]=Math.floor((1-t)*(1-t)*p0x+2*t*(1-t)*p1x+t*t*p2x)+'px';
		M2Y[i]=Math.floor((1-t)*(1-t)*p0y+2*t*(1-t)*p1y+t*t*p2y)+'px'
	}
	else {
		t=(i-150)/150;p0x=1180;p1x=575;p2x=0;p0y=325;p1y=-100;p2y=200;
		M2X[i]=Math.floor((1-t)*(1-t)*p0x+2*t*(1-t)*p1x+t*t*p2x)+'px';
		M2Y[i]=Math.floor((1-t)*(1-t)*p0y+2*t*(1-t)*p1y+t*t*p2y)+'px'
	}
};
function lounchFonar(){
	var i=0;
	$('#fonar0').css({'top':'380px','left':'1180px'});
	$('#fonar1').css({'top':'800px','left':'0px'})
	$('#fonar').fadeIn(animateSpeed,function(){
		var intervalID = window.setInterval(function(){
			if ($('#effect_swicth span').hasClass('off')){
				$('#fonar').fadeOut(animateSpeed);
				clearInterval(intervalID)
			};
			if (i<301) {
				$('#fonar0').css({'top':M1Y[i],'left':M1X[i]});
				$('#fonar1').css({'top':M2Y[i],'left':M2X[i]});
				i+=1
			}
			else {
				$('#fonar').fadeOut(animateSpeed);
				clearInterval(intervalID);
				setTimeout(lounchFonar,2000)
			}
		},20)
	})
};
//Скрипт неонки
function startNeon() {
	var rand=Math.floor(Math.random()*6)+1;
	$('.neon').each(function(i){
		$(this).height($(this).parent().height());
		$(this).width($(this).parent().width());
		rand+=i;
		if (rand>6){rand=rand-6};
		$(this).flashembed({
			src:'flash/neon.swf',
			version: [10, 0],
			wmode:'transparent',
			allowfullscreen: false
		},{ran: rand})
	})
};
//Построительные скрипты
function RebuildFooter() {
	$('#wrap').removeAttr('style');
	if ($('#wrap').height()<$('#layout').height()) {$('#wrap').height($('#layout').height())};
	if ($.browser.msie && $.browser.version < 8) {
		$('#footer').hide(0);
		$('#footer').show(0)
	}
};
function topMenu(){$('#top_menu li a').each(function(i){$(this).addClass('top_menu_li_'+i).html('&nbsp;')})};
//Автозапуск скриптов 
$(document).ready(function() {
	$('[title]').not('#top_menu ul li a[title]').not('#effect_swicth span[title]').tooltip({
		position:'top center',
		offset:[-10,0]
	});
	$('#top_menu ul li a[title]').tooltip({
		position:'top center',
		offset:[20,0]
	});
	$('#effect_swicth span[title]').tooltip({
		position:'bottom left',
		offset:[0,0]
	});
	$('form').submit(function(){$(this).find('input[type="submit"]').attr('disabled','disabled');return true});
	$(":submit").dblclick(function(){return false});
	$('img[align="left"]').css('margin-left','0px');
	$('img[align="right"]').css('margin-right','0px');
//	Выключалка спецэффектов	
	var allcookies=document.cookie;
	var pos=allcookies.indexOf("projmod=");
	if(pos!=-1){
		var start=pos+8;
		var end=allcookies.indexOf(";",start);
		if(end==-1){end=allcookies.length};
		if(allcookies.substring(start, end)=='on'){
			if($('#effect_swicth span').hasClass('off')){$('#effect_swicth span').removeClass('off').addClass('on')}
		}
		else {
			if($('#effect_swicth span').hasClass('on')){$('#effect_swicth span').removeClass('on').addClass('off')}			
		}
	};
	$('#effect_swicth span').click(function(){
		if ($(this).hasClass('on')) {
			$(this).removeClass('on').addClass('off');
			document.cookie="projmod=off;";
			$('.neon').each(function(i){
				$(this).removeAttr('style').html('<img class="neon_left" src="img/neon'+(i+1)+'.png" alt="" /><img class="neon_right" src="img/neon'+(i+1)+'.png" alt="" />').find('img').css('display','block')
				$(this).find('img').css('display','block')
			})
		}
		else {
			$(this).removeClass('off').addClass('on');
			document.cookie="projmod=on;";
			startNeon();
			lounchFonar()
		}
	});
//	Всевозможные флешки
	$('#header .logo').flashembed({
		src:'flash/logo-main.swf',
		version: [10, 0],
		wmode:'transparent',
		w3c: true,
		width: '210px',
		height: '210px',
		allowfullscreen: false
	});
	flashembed('logo-right',{
		src: 'flash/logo_full.swf',
		version: [10, 0],
		wmode: 'transparent',
		w3c: true,
		width: '66px',
		height: '343px',
		allowfullscreen: false
	});
	if ($('#effect_swicth span').hasClass('on')){startNeon()}
	else {$('.neon img').css('display','block')};
//	Постеры
	if (document.getElementById('poster')) {
		$('#poster .items').tabs('div.panes > div.pane',{
			tabs:'img',
			effect:'fade',
			fadeInSpeed:animateSpeed,
			fadeOutSpeed:0,
			onClick:function(){
				$('.week').parent().removeAttr('style');
				$('.week').parent().parent().parent().prev().find('div.center').removeAttr('style');
				if ($('.week').parent().height()<$('.week').parent().parent().parent().prev().find('div.center').height()) {$('.week').parent().height($('.week').parent().parent().parent().prev().find('div.center').height())}
				else {$('.week').parent().parent().parent().prev().find('div.center').height($('.week').parent().height())};
				$('.neon').each(function(i){
					$(this).height($(this).parent().height());
					$(this).width($(this).parent().width())
				});
				RebuildFooter()
			}
		});
		$('#poster div.scrollable').scrollable({'size':4}).find('.item:first').addClass('active')
	};
//	Скроллинг с галереей в страничке "Об них"
	if ($('.photos div.scrollable').length>0){
		$('body').append('<div id="mini_overlay_photo" class="mini_overlay"><a class="close"></a><img class="loader" src="img/loading1.gif" alt="" /></div>')
		$('.photos div.scrollable').scrollable({'size':5,clickable:false}).find('a').overlay({
			fixed:false,
			target:'#mini_overlay_photo',
			top:'5%',
			mask:{
				color: '#000',
				opacity: 0.01
			},
			speed:animateSpeed,
			onClose:function(){
				$('#mini_overlay_photo img').not($('#mini_overlay_photo img.loader')).remove();
				$('#mini_overlay_photo').removeAttr('style')
			}
		}).gallery({speed:animateSpeed})
	};
//	Скроллинг видео на страничке галереи
	$('.video_list div.scrollable').scrollable({'size':3,clickable:false});
//	Галерея больших фоток
	if (document.getElementById('big_image')) {
		var GalleryName=$('h2').html(),
		    imageIDreg = /#(.+)/,
		    imageIDrez=imageIDreg.exec(document.location.href),
		    counfPhoto=$('#photo_list > .item').length;
		if (imageIDrez){imageID=imageIDrez[1]}
		else {imageID=$('#photo_list > .item:first').attr('id')};
		function showPhoto(imageID){
			var url=$('#'+imageID+' .src').html(),
			    alt=$('#'+imageID+' .name').html(),
			    img = new Image();
			$('#big_image img.loader').css('display','block');
			img.onload = function() {
				$('#big_image').fadeTo('fast', 0.3,function(){
					$('#big_image img.picture').removeAttr('style').attr('src', url);
					if ($('#big_image img.picture').height()>600){$('#big_image img.picture').height(600)};
					if ($('#big_image img.picture').width()>790){$('#big_image img.picture').width(790)};
					if ($('#big_image img.picture').height()<600){$('#big_image img.picture').css('margin-top',Math.floor((600-$('#big_image img.picture').height())/2)+'px')};
					$('#big_image').fadeTo('medium', 1,function(){$('#big_image img.loader').css('display','none')});
				})
			};
			img.src = url;
			RebuildFooter();
			$('#photo_list > .item').removeClass('current');
			$('#big_image a.browse').removeClass('disabled');
			$('#'+imageID).addClass('current');
			$('#photo_list > .item').each(function(i){
				if ($(this).hasClass('current')) {
					$('h2').html(GalleryName+' / Фотография '+(i+1)+' из '+counfPhoto);
					if (i==0){$('#big_image a.prevPage').addClass('disabled')};
					if (i==counfPhoto-1){{$('#big_image a.nextPage').addClass('disabled')}}
				}
			})
		};
		if (document.getElementById(imageID)) {showPhoto(imageID)};
		$('#big_image a.prevPage').click(function(){
			if (!$(this).hasClass('disabled')) {
				$('#photo_list > .item').each(function(i){
					if ($(this).hasClass('current')) {
						imageID=$(this).prev().attr('id');
						showPhoto(imageID);
						return false
					}
				})
			}
		});
		$('#big_image a.nextPage').click(function(){
			if (!$(this).hasClass('disabled')) {
				$('#photo_list > .item').each(function(i){
					if ($(this).hasClass('current')) {
						imageID=$(this).next().attr('id');
						showPhoto(imageID);
						return false
					}
				})
			}
		})
	};
//	Об них
	$('.about_club .heading').tabs('.about_club div.panes > div.pane',{
		tabs:'span',
		effect:'fade',
		fadeInSpeed:animateSpeed,
		fadeOutSpeed:0,
		onBeforeClick:function(){$('.about_club div.panes').height($('.about_club div.panes').height())},
		onClick:function(object,tabIndex){
			if ($('.about_club div.panes div.pane').eq(tabIndex).height()>$('.about_club div.panes').height()) {
				$('.about_club div.panes').removeAttr('style');
				$('.about_club div.panes').height($('.about_club div.panes').height());
				$('.neon').each(function(i){
					$(this).height($(this).parent().height());
					$(this).width($(this).parent().width())
				});
				RebuildFooter()
			}
		}
	});
//	Всплывалка карты
	if (document.getElementById('map')) {
		$('#map .image').click(function(e){
			var src=$(this).find('img').attr('src').replace('mini.jpg','big.png');
			elSize[100]=$(this).offset().top,
			elSize[110]=$(this).offset().left;
			$(this).addClass('active');
			$('body').append('<div id="mini_overlay_shadow" style="width:'+$('#layout').width()+'px;height:'+$('#layout').height()+'px;"></div>');
			$('body').append('<div id="mini_overlay_map" class="mini_overlay"><img src="'+src+'" alt="" /><a class="close"></a></div>');
			$('#mini_overlay_map').css({
				'left':elSize[110]+'px',
				'top':elSize[100]+'px',
				'display':'block'
			});
			$('#mini_overlay_map img').animate({height:'351px'},animateSpeed,'linear',function(){
				ClousePhoto=function(){
					$('#mini_overlay_map img').animate({height:'178px'},animateSpeed,'linear',function(){
						$('#mini_overlay_map').remove();
						$('#mini_overlay_shadow').remove();
						$('#map .active').removeClass('active')
					})
				};
				$('#mini_overlay_map .close').click(ClousePhoto);
				$('#mini_overlay_shadow').click(ClousePhoto);
				$(document).one('keydown', function(e,ClousePhoto){
					if (e.keyCode==27) {
						$('#mini_overlay_map img').animate({height:'178px'},animateSpeed,'linear',function(){
							$('#mini_overlay_map').remove();
							$('#mini_overlay_shadow').remove();
							$('#map .active').removeClass('active')
						})
					}
				})
			})
		});
	};
//	Форма контактов
	if (document.getElementById('contact_form')) {
		$('#open_contact_form').click(function(){
			if ($('#contact_form_block').css('display')=='none') {
				$('#contact_form_block').slideDown(animateSpeed,function(){
					$('#contact_form').validator({
						lang:'ru',
						messageClass:'valid_error',
						offset:[0, 5],
						onSuccess:function(){$('input[type="submit"]').removeAttr('disabled')}
					});
					$('.neon').each(function(i){
						$(this).height($(this).parent().height());
						$(this).width($(this).parent().width())
					})
				});
				$('#open_contact_form span.arrow').css('background','none')
			}
			else {
				$('.valid_error').remove();
				$('#contact_form_block .text-input').val('');
				$('#contact_form_block .textarea-input').val('');
				$('#contact_form_block .form-item .invalid').removeClass('invalid');
				$('#contact_form_block input[type="submit"]').removeAttr('disabled');
				$('#sendinf').html('');
				$('#contact_form_block').slideUp(animateSpeed,function(){
					$('.neon').each(function(i){
						$(this).height($(this).parent().height());
						$(this).width($(this).parent().width())
					});
					$('#open_contact_form span.arrow').removeAttr('style')
				})
			};
			RebuildFooter()
		})
	};
//	Форма отзывов
	if (document.getElementById('response_form')) {
		$('#response_form').validator({
			lang:'ru',
			messageClass:'valid_error',
			offset:[0, 5],
			onSuccess:function(){$('input[type="submit"]').removeAttr('disabled')}
		})
	}
});
if (window.addEventListener) {
	window.addEventListener('load',function(){
		ImageResize();
		RebuildFooter();
		if ($('#effect_swicth span').hasClass('on')){lounchFonar()}
	},false)
}
else {
	window.attachEvent('onload',function(){
		ImageResize();
		RebuildFooter();
		if ($('#effect_swicth span').hasClass('on')){lounchFonar()}
	})
};
$(window).resize(function(){RebuildFooter()});

//Плеер
function startPlayer(flag,volume,track){
	var playItem = track;
	var allcookies=document.cookie;
	var pos=allcookies.indexOf("playertrack=");
	if(pos!=-1){
		var start=pos+12;
		var end=allcookies.indexOf(";",start);
		if(end==-1){end=allcookies.length};
		playItem=parseInt(allcookies.substring(start, end),10)
	};
	var currentVolume=volume;
	var allcookies=document.cookie;
	var pos=allcookies.indexOf("playervolume=");
	if(pos!=-1){
		var start=pos+13;
		var end=allcookies.indexOf(";",start);
		if(end==-1){end=allcookies.length};
		currentVolume=allcookies.substring(start, end)
	};
	var myPlayList=new Array();
	$.ajax({url:"inc/playerlib.php",
		type:"POST",
		data: {funcName:"Get"},
		dataType:"json",
		async: false,
		success:function(data){myPlayList=data;}
	});
	$('#jquery_jplayer').jPlayer({
		ready: function(){
			displayPlayList();
			var allcookies=document.cookie;
			var pos=allcookies.indexOf("playermode=");
			if(pos!=-1){
				var start=pos+11,
				    end=pos+15,
				    modvalue=allcookies.substring(start, end);
				if(modvalue=="stop"){playListInit(false)}
				else {playListInit(true)}
			}
			else {playListInit(flag)}
		},
		nativeSupport: false,
		volume:currentVolume
	}).jPlayer('onSoundComplete', function() {playListNext()});
	if(currentVolume==0){
		$('#jplayer_volume_min').css('display','none');
		$('#jplayer_volume_restore').css('display','block')
	}
	$('#jplayer_previous').click(function(){
		playListPrev();
		$(this).blur();
		return false
	});
	$('#jplayer_stop').click( function() {
		document.cookie="playermode=stop;";
		$("#jquery_jplayer").jPlayer("stop")
	});
	$('#jplayer_play').click( function() {
		document.cookie="playermode=play;";
		$("#jquery_jplayer").jPlayer("play")
	});
	$('#jplayer_next').click( function() {
		playListNext();
		$(this).blur();
		return false
	});
	$('#jplayer_volume_min').click(function(){
		currentVolume=Math.floor($('#jplayer_volume_bar_value').width()*100/$('#jplayer_volume_bar').width());
		$(this).css('display','none');
		$('#jplayer_volume_restore').css('display','block');
		$('#jquery_jplayer').jPlayer('volumeMin');
		document.cookie="playervolume="+$('#jquery_jplayer').jPlayer("getData","volume")+";";
		return false
	});
	$('#jplayer_volume_bar').click(function(){
		$('#jplayer_volume_restore').css('display','none');
		$('#jplayer_volume_min').css('display','block')
		document.cookie="playervolume="+$('#jquery_jplayer').jPlayer("getData","volume")+";";
	});
	
	 $('#jplayer_volume_bar_value').click(function(){
        document.cookie="playervolume="+$('#jquery_jplayer').jPlayer("getData","volume")+";"
    });
	
	$('#jplayer_play_current').click(function(){
		if ($(this).hasClass('show-play-list-now')){
			$('#jplayer_playlist').slideUp(animateSpeed,function(){$('#jplayer_play_current').removeClass('show-play-list-now')})
		}
		else {
			$('#jplayer_playlist').slideDown(animateSpeed,function(){$('#jplayer_play_current').addClass('show-play-list-now')})
		}
	});
	function displayPlayList() {
		$('#jplayer_playlist ul').empty();
		for (i=0; i<myPlayList.length; i++) {
			var listItem=(i==myPlayList.length-1) ? '<li class="jplayer_playlist_item_last">' : '<li>';
			listItem+='<span id="jplayer_playlist_item_'+i+'" tabindex="1">'+ myPlayList[i].name +'</span></li>';
			$('#jplayer_playlist ul').append(listItem);
			$('#jplayer_playlist_item_'+i).data('index',i).click(function(){
				var index=$(this).data('index');
				if (playItem!=index){playListChange(index)}
				else {$('#jquery_jplayer').jPlayer('play')};
				$(this).blur();
				return false
			})
		}
	};
	function playListInit(autoplay) {
		if(autoplay){playListChange(playItem)}
		else {playListConfig(playItem)}
	};
	function playListConfig(index){
		$('#jplayer_playlist_item_'+playItem).removeClass('jplayer_playlist_current').parent().removeClass('jplayer_playlist_current');
		$('#jplayer_playlist_item_'+index).addClass('jplayer_playlist_current').parent().addClass('jplayer_playlist_current');
		$('#jplayer_play_current').html($('#jplayer_playlist_item_'+index).html());
		playItem = index;
		document.cookie="playertrack="+index+";";
		$('#jquery_jplayer').jPlayer('setFile', myPlayList[playItem].mp3)
	};
	function playListChange(index){
		playListConfig(index);
		$('#jquery_jplayer').jPlayer('play')
	};
	function playListNext(){
		var index = (playItem+1 < myPlayList.length) ? playItem+1 : 0;
		document.cookie="playertrack="+index+";";
		playListChange(index)
	};
	function playListPrev(){
		var index = (playItem-1 >= 0) ? playItem-1 : myPlayList.length-1;
		document.cookie="playertrack="+index+";";
		playListChange(index)
	}
};
