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


jQuery(function($){
	var header = $('#header'), headerPics = header.data('pics'), openPic = header.data('pic-nr'), headerGallery = $('#header-gallery'), headerGalleryTimeout,
		headerPic = $('#header-pic'),
		headerIn = $('#header-pic-in'),
		picCache = [];
	$.each(headerPics, function(i, url) {
		picCache.push($('<img>').attr('src', url));
		$('<div/>').addClass('pic-nr-'+i).toggleClass('open', openPic == i).click(function() {
			changePic(url);
		}).appendTo(headerGallery);
	});
	function changePic(newUrl, nr) {
		if(newUrl) {
			if(headerGalleryTimeout) {
				clearTimeout(headerGalleryTimeout);
				headerGalleryTimeout = 0;
			}
			if(typeof nr != 'number') {
				$.each(headerPics, function(i, url) {
					if(newUrl == url) {
						nr = i;
						return;
					}
				});
			}
			headerGallery.children().eq(nr).stop().fadeTo(200, 1).siblings().stop().fadeTo(400, 0.3);
			headerIn.css('background-image', 'url('+newUrl+')').fadeTo(0, 0).animate({
				opacity : 1
			}, 500, function() {
				headerPic.css('background-image', 'url('+newUrl+')');
			});
		} else {
			openPic = openPic < headerPics.length - 1 ? openPic + 1 : 0;
			changePic(headerPics[openPic]);
			headerGalleryTimeout = setTimeout(changePic, 5000);
		}
	}
	headerGalleryTimeout = setTimeout(changePic, 4000);
	
	$('#gallery a').fancybox({
		transitionIn : 'elastic',
		transitionOut : 'elastic'
	});
	
	$('.view-plan,.view-plan-2').click(function() {
		$('body').addClass('apartment-plan-view');
		$(this).closest('tr').addClass('open').next('.plan').addClass('open').prevAll('.korrus').first().addClass('open').siblings('.korrus').removeClass('open');
		$(window).scrollTop($(this).closest('tr').position().top - 70);
	});
	$('.hide-plan').click(function() {
		$('body').removeClass('apartment-plan-view');
		$(this).closest('tr').removeClass('open').next('.plan').removeClass('open');
		return false;
	});
	$('body').delegate('.plan.open .print', 'click', function() {
		window.print();
		return false;
	});
	
	$('.package .biglink').click(function() {
		var pack = $(this).closest('.package');
		pack.addClass('open').siblings().removeClass('open');
		pack.find('.more,.cover').fadeIn();
	});
	
	$('.package-gallery .nav .next').click(function() {
		var gal = $(this).closest('.package-gallery'), pics = gal.children('img'), open = pics.filter('.open'), next, ind;
		if(!open.length) {
			open = pics.first();
		}
		open.removeClass('open');
		next = open.next('img');
		if(!next.length) {
			next = pics.first();
		}
		ind = parseInt(open.css('z-index'), 0) || 0;
		next.css('z-index', ind + 1).hide().fadeIn().addClass('open');
		gal.find('.text').text(next.attr('title') || '');
		return false;
	});
	$('.package-gallery .nav .prev').click(function() {
		var gal = $(this).closest('.package-gallery'), pics = gal.children('img'), open = pics.filter('.open'), prev, ind;
		if(!open.length) {
			open = pics.last();
		}
		open.removeClass('open');
		prev = open.prev('img');
		if(!prev.length) {
			prev = pics.last();
		}
		ind = parseInt(open.css('z-index'), 0) || 0;
		prev.css('z-index', ind + 1).hide().fadeIn().addClass('open');
		gal.find('.text').text(prev.attr('title') || '');
		return false;
	});
	$('.package-gallery').each(function() {
		var that = $(this);
		that.find('img:not(:first)').hide();
		that.find('.nav .text').text(that.find('img:first').attr('title') || '');
		that.find('img').each(function() {
			picCache.push($('<img>').attr('src', $(this).attr('src')));
		});
	});
	$('.package .cover, .package .bignav .close').click(function() {
		$(this).closest('.package').removeClass('open').find('.cover,.more').fadeOut();
	});
	$('.package .bignav .next').click(function() {
		var that = $(this), pack = that.closest('.package'), next = pack.next();
		if(!next.length) {
			next = pack.siblings('.package').first();
		}
		next.addClass('open').find('.cover,.more').show();
		pack.removeClass('open').find('.cover,.more').hide();
	});
	$('.package .bignav .prev').click(function() {
		var that = $(this), pack = that.closest('.package'), prev = pack.prev();
		if(!prev.length) {
			prev = pack.siblings('.package').last();
		}
		prev.addClass('open').find('.cover,.more').show();
		pack.removeClass('open').find('.cover,.more').hide();
	});

	if(location.hash) {
		$(location.hash).click();
	}
	
	
	var win = $(window), headerFixed = false;
	win.scroll(function() {
		if(win.scrollTop() >= 480) {
			if(!headerFixed) {
				header.css({
					position : 'fixed',
					top : -480
				});
				headerFixed = true;
			}
		} else {
			if(headerFixed) {
				header.css({
					position : 'absolute',
					top : 0
				});
				headerFixed = false;
			}
		}
	});
});

