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


jQuery(function($){
	$('.grouped>h2').click(function() {
		var that = $(this), open = that.parent().is('.open');
		if(open) {
			that.next().slideUp(200, function() {
				that.parent().removeClass('open');
			});
		} else {
			that.parent().addClass('open');
			that.next().hide().slideDown(200);
		}
	});
	
	$('a.map-link[data-coords]').click(function(e) {
		e.preventDefault();
		var that = $(this), coords = that.attr('data-coords').split(';');
		if(coords.length < 2) {
			return;
		}
		try {
			var myLatlng = new google.maps.LatLng(coords[0], coords[1]);
		} catch(e) {
			return;
		}
		$('#map-container').remove();
		if(!$('#map').length) {
			$('<div>', {
				id : 'map-container'
			}).css('top', (that.position().top - 200)+'px').append($('<div>', {
				id : 'map',
				css : {
					width : '100%',
					height : '100%'
				}
			})).appendTo('#content').click(function() {
				return false;
			});
			var myOptions = {
				zoom: coords.length > 2 ? parseInt(coords[2]) : 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: ''
			});
		}
		return false;
	});
	$('body').click(function() {
		$('#map-container').remove();
	});
	
	$('a[rel]').fancybox({
		transitionIn : 'elastic',
		transitionOut : 'elastic'
	});
	
	/* Form submit cehck */
	$('form').submit(function() {
		var ok = true;
		$(this).find('input[type=text][data-required],textarea[data-required],select[data-required]').each(function() {
			var field = $(this);
			if(field.val() == '') {
				field.addClass('required');
				if(ok) {
					field.focus();
				}
				ok = false;
			} else {
				field.removeClass('required');
			}
		});
		if(!ok) {
			return false;
		}
	});
	
	/* IMG title's */
	$('body').delegate('a[title]:has(img)', 'mouseenter', function() {
		var pic = $('img', this),
			title = $('<div class="pic-title"></div>').text($(this).attr('title')).insertAfter(pic);
		title.css({
			left : pic.position().left + pic.width() - title.outerWidth(),
			top : pic.position().top + pic.height() - title.outerHeight()
		});
	}).delegate('a[title]:has(img)', 'mouseleave', function() {
		$(this).find('.pic-title').remove();
	});
});

