$(document).ready(function(){

	if ( $("#slider").length > 0 ) {
		$("#slider").easySlider({
			auto: true,
			continuous: true,
			nextText: 		'&rarr;',
			prevText: 		'&larr;'
		});
	}

	var email = '';
	var phone = '';
	var form = '';

	/* Окно по нажатию ссылки "отписаться" */
	$('a.unsubscribe').live( 'click', function(){
		$.post( '/?page=unsubscribe', { 'action' : 'form'}, function( data ){
			$.fancybox( data, {
				'autoDimensions' : false,
				'width': 500,
				'height' : 300
			});
		});
	});

	/* Отписка от СМС */
	$('input.unsubscribePhoneButton ').live( 'click', function(){
		phone = $('input.unsubscribePhone').val();
		if ( form == '' )
			form = $('#unsubscribeForm').html();

		if ( phone.length < 5 )
			return;

		$('#fancybox-content').html( '<div class="alignCenter"><br><br><br><img src="/img/loading.gif" alt="подождите" width="16" height="16"></div>' );

		$.getJSON("/?page=unsubscribe",
			{
				'action' : 'phone',
				'phone': phone
			},

			function( data ){
				$('#fancybox-content').html( data + form );
			}
		);
	});

	/* Отписка от email-рассылки */
	$('input.unsubscribeEmailButton ').live( 'click', function(){
		email = $('input.unsubscribeEmail').val();
		if ( form == '' )
			form = $('#unsubscribeForm').html();

		if ( email.length < 5 )
			return;

		$('#unsubscribeForm').html( '<div class="alignCenter"><br><br><br><img src="/img/loading.gif" alt="подождите" width="16" height="16"></div>' );

		$.getJSON("/?page=unsubscribe",
			{
				'action' : 'email',
				'email': email
			},

			function( data ){
				$('#fancybox-content').html( data + form );
				$('input.unsubscribeEmail').val( email );
			}
		);
	});

	/* Нажатие кнопки "подписаться" */
	$('#subsrciptionButton').live( 'click', function(){
		email = $('input.emailSubscription').val();
		phone = $('input.smsSubscription').val();

		form = $('div.subscriptions').html();

		if ( email.length == 0 && phone.length == 0 ){
			$('div.error').html( 'Введите Email или телефон' );
			return;
		}

		$('div.subscriptions').html( '<div class="alignCenter"><br><img src="/img/loading.gif" alt="подождите" width="16" height="16"></div>' );

		$.getJSON("/?page=subscriptions",
			{
				'email': email,
				'phone': phone
			},

			function( data ){
				if ( data.error != '' ){
					$('div.subscriptions').html( form );
					$('input.emailSubscription').val( email );
					$('input.smsSubscription').val( phone );
					$('div.error').html( data.error );
				}
				else{
					$('div.subscriptions').html( '<div class="marginDiv">' + data.msg + '</div>' );
				}
			}
		);
		
	});

	// отправка заявки на аккредитацию
	$('#submitAccreditation').click(function(){
		var correct = true;
		if ( !$('#pressShowsList').val() )
			correct = false;

		if ( !$('#smi_name').val() )
			correct = false;

		if ( !$('#coordinator_fio').val() )
			correct = false;

		if ( !$('#coordinator_phone').val() )
			correct = false;

		if ( !$('#coordinator_email').val() )
			correct = false;

		if ( !$('#announce').val() )
			correct = false;

		if ( !$('#people').val() )
			correct = false;

		if ( !$('#people_contacts').val() )
			correct = false;

		if ( !correct )
			alert( 'Все поля обязательны для заполнения' );
		else{
			$.post("/?page=forpress",
				{
					show: $('#pressShowsList').val(),
					smi_name: $('#smi_name').val(),
					coordinator_fio: $('#coordinator_fio').val(),
					coordinator_phone: $('#coordinator_phone').val(),
					coordinator_email: $('#coordinator_email').val(),
					announce: $('#announce').val(),
					people: $('#people').val(),
					people_contacts: $('#people_contacts').val()
				},

				function(data){
					$('#forpressForm').html('Ваша заявка успешно отправлена');
				});
		}
	});

	//** Разделы для Бара **//

	$barDivs = $.find('div.bar');
	if ( $barDivs.length > 0 ){
		$('div.bar').hide();
		$('#alcohol').show();
		$('#barMenu li[class="alcohol"]').css('background-color', '#000');
		$('#barMenu li[class="alcohol"]').css('color', '#fff');
	}

	var barSection = '';
	$('#barMenu li').click( function(){
		barSection = $(this).attr('class');
		$('div.bar').hide();


		$('#barMenu li').each(function(){
			$(this).css('background', 'none');
			$(this).css('color', '#000');
		});

		$(this).css('background-color', '#000');
		$(this).css('color', '#fff');

		$('#' + barSection ).fadeIn();
	});

    //** Флэш-элементы **//

    $('#avrora').html('').flash({
		swf: '/swf/avrora.swf',
		params: {
			allowFullScreen: "true",
			allowscriptaccess: "always"
		},
		width: 311,
		height: 60,
			wmode: 'transparent'
    });

    //Банер телефона заказа билетов
    $('#order-banner').html('').flash({
		swf: 'swf/zakaz.swf',
		params: {
			wmode: "transparent"
		},
		width: 284,
		height: 45
    });

    //Банер канала на youtube
    $('#youtube-banner').html('').flash({
		swf: 'swf/youtube.swf',
		params: {
			wmode: "transparent"
		},
		width: 311,
		height: 171
    });

    //** Банеры концертов **//
    $('.banners').cycle({
		fx: 'fade',
		speed: 1500,
		timeout: 5000
    });
	
	$('.banners-big').cycle({
		fx: 'fade',
		speed: 1500,
		timeout: 5000
    });

	//** Fancybox для галереи **//
	$('a[rel="galleryAlbum"], a.fancyImg').click().fancybox({
		'titlePosition'		: 'outside',
		'overlayColor'		: '#000',
		'overlayOpacity'	: 0.9
	});

	// отрисовка формы заказа билета
	$( 'a.orderTicketLink' ).click( function(){
		var show = parseInt( $(this).attr( 'rel' ).replace("show", '') );
		$.fancybox.showActivity();

		$.post( '/?page=orderticket', {'show' : show, 'action' : 'orderform'}, function( data ){
			$.fancybox( data, { 
				'autoDimensions' : false,
				'width': 500,
				'height' : 300
			});
		});
	});

	// обработка формы заказа билета
	$('input.orderTicketSubmit').live( 'click', function(){
		var ready = true;
		var color = '#d2effd';

		$( '#fancybox-content div.orderFormElement input[type="text"]' ).each( function(){
			if ( $(this).val() == '' ){
				$(this).css( 'background-color', color );
				ready = false;
			}
			else
				$(this).css( 'background', 'none' );
		});

		if ( !ready )
			return;

		var fio = $('input.orderTicketFIO').val();
		var phone = $('input.orderTicketPhone').val();
		var email = $('input.orderTicketEmail').val();
		var show = $('select.orderTicketShow').val();

		$('#fancybox-content').html( '<div class="alignCenter"><br><br><br><img src="/img/loading.gif" alt="подождите" width="16" height="16"></div>' );

		$.post( 
			'/?page=orderticket',
			{
				'fio' : fio,
				'phone' : phone,
				'email' : email,
				'show' : show,
				'action' : 'orderticket'
			},
			function(){
				$('#fancybox-content').html('<h1>Спасибо, Ваш заказ принят. В ближайшее время мы с вами свяжемся.</h2>');
			}
		);

	});

});

