(function($){
	$.CMS = {
		URL_ROOT : $('script[url-root]').attr('url-root')
	};
})(jQuery);


jQuery(function($){
	/* Detect Calibri font */
	var test1, test2;
	test1 = $('<span>', {
		text : 'Test',
		css : {
			fontFamily : 'Calibri,Arial,sans-serif',
			fontSize : '100px',
			position : 'absolute',
			left : '-9999px',
			top : '-9999px'
		}
	}).appendTo('body');
	test2 = $('<span>', {
		text : 'Test',
		css : {
			fontFamily : 'Arial,sans-serif',
			fontSize : '100px',
			position : 'absolute',
			left : '-9999px',
			top : '-9999px'
		}
	}).appendTo('body');
	if(test1.height() == test2.height() && test1.width() == test2.width()) {
		$('body').addClass('alt-font');
	}
	test1.remove();
	test2.remove();

	/* Support <input placeholder="Text"> */
	$('body').delegate('input[type=text][placeholder]', 'focusin focusout', function(e){
		var that = $(this), placeholder = that.data('placeholder');
		if(!placeholder) {
			that.data('placeholder', placeholder = that.attr('placeholder'));
		}
		if(e.type == 'focusin') {
			if(that.val() == placeholder) {
				that.val('').removeClass('placeholder');
			}
		} else {
			if(that.val() === '') {
				that.val(placeholder).addClass('placeholder');
			}
		}
	});
	$('input[type=text][placeholder]').trigger('focusout');
	$('body').delegate('form', 'submit', function() {
		$('input.placeholder', this).val('');
	});
	
	/* Frontpage news scroller */
	var fpNews = $('#fp-news ul'), fpNewsAuto = true;
	$('#fp-news-prev').click(function() {
		fpNews.animate({
			marginTop: -30
		}, 600, 'linear', function() {
			fpNews.find('li:first').appendTo(fpNews.css('margin-top', 0));
		});
		return false;
	});
	$('#fp-news-next').click(function() {
		fpNews.find('li:last').prependTo(
			fpNews.css('margin-top', -30).animate({
				marginTop: 0
			}, 600, 'linear')
		);
		return false;
	});
	$('#fp-news').hover(function() {
		fpNewsAuto = false;
	}, function() {
		fpNewsAuto = true;
	});
	setInterval(function() {
		if(fpNewsAuto) {
			$('#fp-news-prev').click();
		}
	}, 5000);
	
	/* Fix frontpage columns */
	var fpCols = $('#fp-cols'),
		fpSmall = fpCols.is('.small'),
		fpN = fpSmall ? 6 : 4,
		fpH = {};
	fpCols.find('.fp-col').each(function(index) {
		var that = $(this);
		if(index < fpN) {
			fpH[index] = [that.height(), that];
		} else {
			var smallest = null;
			for(i in fpH) {
				if(!smallest || smallest[0] > fpH[i][0]) {
					smallest = fpH[i];
				}
			}
			smallest[0] += that.height();
			smallest[1].append(that.children());
		}
	});
	
	/* Main menu */
	var mainMenuTO,
		mainMenuCloseTO,
		mainMenuOpen,
		mainMenuOpenItem,
		mainMenuOpenMenu;
	setTimeout(function() {
		$('#menu>ul>li:has(ul)>a').hover(function() {
			var menuItem = $(this).parent();
			if(mainMenuTO) {
				clearTimeout(mainMenuTO);
			}
			mainMenuTO = setTimeout(function() {
				if(mainMenuCloseTO) {
					clearTimeout(mainMenuCloseTO);
					mainMenuCloseTO = 0;
				}
				if(mainMenuOpen) {
					mainMenuOpen.hide();
				}
				var offset = menuItem.offset();
				if(!mainMenuOpen) {
					mainMenuOpen = $('<div id="main-menu-open"></div>').appendTo('body');
					mainMenuOpenItem = $('<div id="main-menu-open-item"><div></div></div>');
					mainMenuOpen.append(mainMenuOpenItem);
					mainMenuOpenItem = mainMenuOpenItem.find('div');
					mainMenuOpenMenu = $('<div id="main-menu-open-l"><div id="main-menu-open-t"><div id="main-menu-open-tr"></div></div><div id="main-menu-open-r"><div id="main-menu-open-menu"></div></div><div id="main-menu-open-b"><div id="main-menu-open-br"></div><div id="main-menu-open-bl"></div></div></div>');
					mainMenuOpen.append(mainMenuOpenMenu);
					mainMenuOpenMenu = $('#main-menu-open-menu', mainMenuOpenMenu);
					mainMenuOpen.hover(function() {
						if(mainMenuCloseTO) {
							clearTimeout(mainMenuCloseTO);
							mainMenuCloseTO = 0;
						}
					}, function() {
						mainMenuCloseTO = setTimeout(function() {
							mainMenuOpen.hide();
						}, 500);
					});
				}
				mainMenuOpen.toggleClass('right', !menuItem.next().length);
				mainMenuOpenMenu.empty();
				var items = menuItem.find('a').length,
					menus = menuItem.find('>ul>li').length,
					holder = $('<div>').addClass('group first').appendTo(mainMenuOpenMenu),
					current = 0,
					curMen = 0,
					col = 1,
					cols = Math.max(1, Math.min(Math.min(4, items / 6), Math.ceil(menus / 1.5))|0);
				menuItem.find('>ul>li').each(function() {
					if((current > items / cols * col - 5 || curMen > menus / cols * col) && col < cols) {
						holder = $('<div>').addClass('group').appendTo(mainMenuOpenMenu);
						col++;
					}
					var adding = $(this).children().clone()
					holder.append(adding);
					current += adding.find('a').length;
					curMen++;
					holder.append(adding);
				});
				holder.addClass('last');
				mainMenuOpenMenu.add('#main-menu-open-t,#main-menu-open-b').width(cols * 180);
				mainMenuOpenItem.empty().append(menuItem.children('a').clone());
				mainMenuOpen.show().css({
					left : offset.left - 5,
					top : offset.top,
					width : mainMenuOpenItem.width()
				});
			}, 150);
		}, function() {
			if(mainMenuTO) {
				clearTimeout(mainMenuTO);
				mainMenuTO = null;
			}
		});
	}, 100);
	
	/* Convert select fields */
	$('.form-field-select select').each(function() {
		var select = $(this).css({
			position: 'absolute',
			left: '-9999px'
		});
		var repl = $('<div class="select"></div>');
		select.find('option').each(function() {
			var option = $(this), val = option.val();
			$('<div class="item"></div>').text(option.text()).appendTo(repl).click(function() {
				if(repl.is('.open')) {
					$(this).addClass('selected').siblings().removeClass('selected');
					repl.removeClass('open');
					select.val(val);
					return false;
					select.focus();
				}
			});
		});
		select.bind('change keyup', function() {
			repl.find('.item').eq(select.find('option[selected]').prevAll().length).addClass('selected').siblings().removeClass('selected');
		});
		repl.click(function() {
			$('.form-field-select .select.open').removeClass('open');
			repl.addClass('open');
			return false;
		}).appendTo($('<div class="select-holder"></div>').insertAfter(select)).find('.item:first').addClass('selected');
	});
	$('body').click(function() {
		$('.form-field-select .select.open').removeClass('open');
	});
	
	/* 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() == '') {
				ok = false;
				field.addClass('required');
			} else {
				field.removeClass('required');
			}
		});
		if(!ok) {
			return false;
		}
	});
	
	var title = $('#title'), titleh1 = title.find('h1'), fontSize = parseInt(titleh1.css('font-size'), 10), i = 0;
	while(titleh1.height() > 30) {
		fontSize *= 0.98;
		titleh1.css('font-size', fontSize.toFixed(2)+'px');
		i++;
		if(i > 40) {
			break;
		}
	}
});

