/**
 * @author Erland Wiencke <erland@apt.no>
 * @since 20090610 
 */

$(document).ready(function(){
	replaceFonts();
	initCallbackForm();
	initArticleNav();
	
	/*if($.browser.msie) {
		$('#flashwrapper').hide();
	}
	
	$('.news.dialog').show().click(function(e) {
		e.preventDefault();
		$('#flashwrapper').show();
		$(this).remove();
	});*/
});

/**
 * Replaces selected texts with the profile font
 * @return void
 */
function replaceFonts(){
	Cufon.replace('h2, h3, h4, .label, span.module, div.article-navigation a', {fontFamily: 'Futura'});	
}

// Posts callbackform via ajax
function initCallbackForm() {
	$('#callback_form').submit(function(e) {
		e.preventDefault();
		var form = $(this);
		
		$.ajax({
			url: form.attr('action'),
			data: form.serializeArray(),
			type: 'POST',
			success: function(data) {
				var datajson = eval('('+data.split('<')[0]+')');
				var returnel;
				if(datajson.error) {
					var c = 'error';
				} else {
					var c = 'info';
				}
				
				var messages = form.parent().find('.ajaxmessage');
				messages.hide();
				messages.removeClass('error').removeClass('info').addClass(c).fadeIn();
				messages.html(datajson.message);
			}
		});
	});
}

// Makes article-navigation hover effect
function initArticleNav() {
	$('div.article-navigation').each(function() {
		var articlenav 	= $(this);
		var activelink 	= articlenav.find('a.active');
		var arrow 		= articlenav.find('.arrow');
		var timeout;
		
		articlenav.addClass('has_js');
		articlenav.find('a').mouseenter(move);
		arrow.css('top', activelink.position().top+7);
		articlenav.mouseleave(function() {
			move(null, activelink);
		});
		
		function move(e, element) {
			var el = element || this;
			arrow.stop(true,false).animate({'top': $(el).position().top+7}, 80);
		}
	});
}