$(function(){
	
	if ($('html').hasClass('js')) {
		$('#page').fadeIn();
	}
	
	$.fn.r3d_slider = function(prop) {
		var defaults = {
			speed: 500,
			interval: 7000,
			title: false
		}
		var opts = $.extend(defaults, prop);

		var slider = {};
		slider.self = $(this);
		slider.ul = slider.self.children('ul');
		slider.li = slider.ul.children('li');
		slider.count = slider.li.length;
		slider.title = slider.self.children('figcaption');
		slider.txt = slider.title.find('span');
		slider.titles = [];
		slider.now = 0;
		slider.next = 1;
		
		slider.update_txt = function() {
			if (opts.title) {
				slider.title.slideUp(opts.speed, function() {
					if (slider.titles[slider.next] !== '') {
						slider.txt.html(slider.titles[slider.next]);
						slider.title.slideDown(opts.speed);
					}
				});
			}
		}
		
		
		
		slider.show_slide = function(next) {
			
			slider.next = (next === undefined) ? (slider.now < slider.count - 1 ? slider.now + 1 : 0) : next;
				
			if (slider.now != slider.next) {
				
				var $now_li = $('#slide-'+slider.now);
				var $new_li = $('#slide-'+slider.next);
				
				slider.ul.append($new_li.detach());
				
				if ($('html').hasClass('canvas')) {
					// Канвас анимация
					$now_li.find('img').fadeOut(opts.speed, function(){
						$new_li.fadeIn(opts.speed, function(){
							$now_li.css('display','none').find('img').css('display','block');
						});
						
					});
				} else {
					// Обычная анимация
					$new_li.fadeIn(opts.speed, function(){
						$now_li.css('display','none').find('img').css('display','block');
					});
				}
				slider.update_txt();
				slider.now = slider.next;
			}
		}
		
		
		
		// Назначение имён и создание эффектов
		slider.li.each(function(i) {
			var li = $(this);
			li.attr('id','slide-'+i);
			setTimeout(function(){
				var img = li.find('img');
				slider.titles[i] = img.attr('alt');
				if ($('html').hasClass('canvas')) {
					img.before(Pixastic.process(img.clone().get(0),"sepia"));
				};
				img.attr('class','filled');
			},1700);
			
		});
		
		slider.li.click(function(){
			slider.show_slide();
		});
		
		

		slider.update_txt();

		var autoSlide = setInterval(function(){	slider.show_slide() },opts.interval);
		/*
		slider.self.hover(
			function(){	clearInterval(autoSlide)},
			function(){	autoSlide = setInterval(function(){	slider.show_slide() },opts.interval)}
		);
		*/
		
		
		return this;
	};
	
	$('#promo').r3d_slider({speed:300, interval: 5000});
	
	$('#promo-small1, #promo-small2, #promo-small3').hover(
		function(){
			$(this).find('.promo-wave').stop().animate({backgroundPositionX:300},500);
		},
		function(){
			$(this).find('.promo-wave').stop().animate({backgroundPositionX:0},500);
		}
	);
	
	$('#price tr').bind('mouseenter',function(){
		$('#price-hover').dequeue().animate({top: $(this).offset().top - 229, height: $(this).height()},200); //133 156->50
	});
	
	$('#sitemenu li:not(#sitemenu_left,#sitemenu_right,#sitemenu_active)').hover(
		function(){$(this).children('.sitemenu_bg').slideUp(200)},
		function(){$(this).children('.sitemenu_bg').slideDown(200)}
	);
	
	$('a.pagelink[href!=#]').click(function(){
		var href = $(this).attr('href');
		//$('#dialog').dialog('close');
		$('#content').fadeOut(500,function(){location.pathname = href});
		return false;
	});
	
	$('a.pagebutton.details').click(function(){
		$(this).next('article.details').slideToggle();
		return false;
	});
	
	
	$('.upbutton').click(function(){
		$('html, body').animate({scrollTop: 0}, 2000);
	});
	
	$('#figures').children('figure').hover(
		function(){ $(this).find('.figure-overlay').fadeTo(300,0); },
		function(){ $(this).find('.figure-overlay').fadeTo(300,0.5); }
	);
	
	$('#pano-preview').delay(4000).animate({backgroundPositionX:'+=1000'},10000);
	setInterval(function(){
		$('#pano-preview').animate({backgroundPositionX:'+=1000'},10000)
	},19000);
	
	
	if ($("#figures").hasClass("photo-list")) {
		$("#figures.photo-list a").prettyPhoto({
			show_title: true,
			slideshow: 5000,
			theme: 'light_rounded',
			markup: '<div class="pp_pic_holder"> \
						<div class="ppt">&nbsp;</div> \
						<div class="pp_content roundborder ui-widget-content"> \
							<div class="pp_loaderIcon"></div> \
							<div class="pp_fade"> \
								<div id="pp_full_res"></div> \
								<div class="pp_details clearfix"> \
									<div class="pp_nav"> \
										<button href="#" class="pp_arrow_previous">Следующая</button> \
										<p class="currentTextHolder">0/0</p> \
										<button href="#" class="pp_arrow_next">Предыдущая</button> \
										<!--<button href="#" class="pp_expand" title="На весь экран">На весь экран</button>--> \
										<button class="pp_close" href="#">Закрыть</button>\
									</div> \
								</div> \
							</div> \
						</div> \
					</div> \
					<div class="pp_overlay"></div>',
			gallery_markup: '',
			changepicturecallback: function() {
				$('.pp_expand,.pp_contract').button({icons: {primary: "ui-icon-arrow-4-diag"},text:false});
				$('.pp_play').button({icons: {primary: "ui-icon-play"},text:false}).data('paused',false);
				$('.pp_pause').button({icons: {primary: "ui-icon-pause"},text:false});
				$('.pp_play,.pp_pause').click(function(){
					if ($(this).data('paused')) {
						$(this).data('paused',false).button('option',{icons:{primary:'ui-icon-play'}});
					} else {
						$(this).data('paused',true).button('option',{icons:{primary:'ui-icon-pause'}});
					}	
				});
				
				$('.pp_arrow_previous,').button({icons: {primary: "ui-icon-seek-prev"},text:false});
				$('.pp_arrow_next').button({icons: {primary: "ui-icon-seek-next"},text:false});
				$('.pp_close').button({icons: {primary: "ui-icon-circle-close"},text:false});
			}
	
		});
	}

			
			
	
	
	/*
	$('#figures').children('figure').each(function(i) {
		var img = $(this).find('img');
		if ($('html').hasClass('canvas')) {
			img.after(Pixastic.process(img.clone().attr('class','figure-overlay').get(0),"glow",{amount:0.3,radius:1.0}));
		} else {
			img.after('<div class="figure-overlay filled" style="background-color: #EDB"></div>');	
		}
	});
	
	

	if ($('html').hasClass('canvas')) {
		$('#figures').children('figure').hover(
			function(){ $(this).find('.figure-overlay').fadeIn(300); },
			function(){ $(this).find('.figure-overlay').fadeOut(300); }
		);
	} else {
		$('#figures').find('.figure-overlay').fadeTo(300,0.5);	
		$('#figures').children('figure').hover(
			function(){ $(this).find('.figure-overlay').fadeTo(300,0); },
			function(){ $(this).find('.figure-overlay').fadeTo(300,0.5); }
		);
			
	}		
	*/
	
	
	
	$('#button-reservation').click(function(){
		$.when($.ajax('/media/js/libs/jquery.timepickr.fixed.min.js',{dataType:'script',cache:true}), $.ajax('/media/js/libs/jquery.simple-watermark.min.js',{dataType:'script',cache:true})).done(
			function(){
				$('#dialog').load('/dialog/reservation/').dialog({
					title: 'Резервирование',
					width: 440,
					height: 390,
					autoOpen: false,
					resizable: false
				}).dialog('open');
			});
		return false;
	});

	/*
	$('#button-map').click(function(){
		$('#dialog').load('/dialog/map/').dialog({
			title: 'Карта',
			width: 645,
			height: 530,
			resizable: false
		}).dialog('open');
		
	});
	*/
	
	

	/*
	$('#promo').click(function(){
		$('#dialog').load('/dialog/cowboy_show/').dialog({
			title: 'Ковбойская вечеринка',
			width: 500,
			height: 450,
			resizable: false
		}).dialog('open');
		
	});
	*/
	
	$("#comment-form").submit( function() {
		if ( ( $( "#name" ).val( ).length > 0 ) && ( $( "#message" ).val( ).length > 0 ) )
		{
			$.post( "/dialog/sendcomment/",
			{
				name: $( "#name" ).val(),
				message: $( "#message" ).val()
			}, function( data )
			{
				if ( -1 != data.indexOf( "ERRCODE_OK" ) )
				{
					$("#comment-form").parent().slideUp();
					$('#dialog').text('Ваш отзыв будет опубликован через некоторое время.').dialog({
						height: 150, resizable: false, title: 'Спасибо', buttons: {OK: function() {$(this).dialog('close');}}
					});
				}
				else if ( -1 != data.indexOf( "ERRCODE_NOTPARAMS" ) )
				{
					$('#dialog').text('Введите ваше имя и текст отзыва').dialog({
						height: 150, resizable: false, title: 'Ошибка', buttons: {OK: function() {$(this).dialog('close');}}
					});
				}
			} );
		}
		else 
		{
			$('#dialog').text('Введите ваше имя и текст отзыва').dialog({
				height: 150, resizable: false, title: 'Ошибка', buttons: {OK: function() {$(this).dialog('close');}}
			});
		}
		
		return false;
	} );
	
	
	$('#comment-submit').click(function(){
		$("#comment-form").submit();
	});
});


document.body.style.backgroundColor = 'transparent';
