(function($){
	var URL_ROOT = $('script[url-root]').attr('url-root');
	$.CMS = {
		URL_ROOT : URL_ROOT,
		URL_DATA : URL_ROOT+'cms-data/'
	};
})(jQuery);


jQuery(function($){
	$('#menu>ul>li>a').pir({
		font : 1,
		color : 'fff',
		hoverColor : 'b5bbc0',
		openColor : '569FC9'
	});
	$('.sidebar-block h2').pir({
		font : 1,
		color : 'fff'
	});
	$('.top-pic h1').pir({
		font : 1,
		color : 'fff',
		wrap : true
	});
	$('#fp-content h1').pir({
		font : 1,
		color : '2e1d1b'
	});
	
	function fixBG() {
		var bg = $('#background'),
			img = bg.find('img'),
			w = bg.width(),
			h = bg.height(),
			iw = img.width(),
			ih = img.height();
		
		if(iw && ih && w && h) {
			if(iw/ih > w/h) {
				img.css({
					width : iw/ih*h,
					height : h,
					left : (w-iw/ih*h)/2,
					top : 0
				});
			} else {
				img.css({
					width : w,
					height : ih/iw*w,
					left : 0,
					top : (h-ih/iw*w)/2
				});
			}
		}
	}
	var img = $('#background img')[0];
	if(img.complete || img.readyState == 4) {
		fixBG();
	}
	$('#background img').load(fixBG);
	$(window).resize(fixBG);
	
	$('.pakett>h2').click(function() {
		var pakett = $(this).closest('.pakett');
		pakett.toggleClass('open').find('.content')[pakett.is('.open') ? 'slideDown' : 'slideUp']();
	});
	
	$('.saun .pic a, #gallery-pics a').fancybox({
		titlePosition : 'over'
	});
	
	$('#news-years .year').click(function() {
		$('.news-block').hide();
		$('.news-block.year-'+$(this).text()).show();
		$(this).addClass('open').siblings().removeClass('open');
	}).first().click();
	
	$('input[type=text][placeholder]').bind('focus blur', function(e){
		var that = $(this), placeholder = that.data('placeholder');
		if(!placeholder) {
			that.data('placeholder', placeholder = that.attr('placeholder'));
		}
		if(e.type == 'focus') {
			if(that.val() == placeholder) {
				that.val('').removeClass('placeholder');
			}
		} else {
			if(that.val() === '') {
				that.val(placeholder).addClass('placeholder');
			}
		}
	}).blur();
	$('#sidebar form').submit(function() {
		$('input.placeholder', this).val('');
	});
	
	
	$('#check-map a').click(function() {
		if(!$('#map').length) {
			$('<div>', {
				id : 'map-container',
				css : {
					position : 'absolute',
					right : 20,
					bottom : 40,
					width : 720,
					height : 480,
					border : '2px solid #ccc',
					backgroundColor : '#fff',
					padding : 8,
					zIndex : 100,
					'box-shadow' : '0 0 30px #000',
					'-moz-box-shadow' : '0 0 30px #000',
					'-webkit-box-shadow' : '0 0 30px #000'
				}
			}).append($('<div>', {
				id : 'map',
				css : {
					width : '100%',
					height : '100%'
				}
			})).appendTo('#content').click(function() {
				return false;
			});
			var myLatlng = new google.maps.LatLng(59.4341,24.7585);
			var myOptions = {
				zoom: 15,
				center: myLatlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};
			var map = new google.maps.Map(document.getElementById('map'), myOptions);
		
			var marker = new google.maps.Marker({
				position: myLatlng, 
				map: map,
				title: 'City Spa'
			});
		}
		return false;
	});
	$('body').click(function() {
		$('#map-container').remove();
	});
	
	$('#mail-subscribe').submit(function(e) {
		e.preventDefault();
		var that = $(this), field = that.find('input[name=email]'), email = field.val();
		if(field.is('.placeholder')) {
			email = '';
		}
		if(!email) {
			alert(that.attr('data-error-1'));
			return;
		}
		$.get(that.attr('action'), { email : email, list : 2 }, function(data) {
			if(!data) {
				alert(that.attr('data-error-2'));
				return;
			}
			that.find('p').text(that.attr('data-ok'));
			field.val('');
		}, 'json');
	});
});


