$(document).ready(function() {

	// target blank replacement
	$('a[rel="external"]').click(function() {
		window.open($(this).attr('href'));
		return false;
	});

	// fire cufon
	Cufon.replace('.cufon, .cufon_big', {
		hover: true,
		hoverables: { a: true, div: true }
	});

	// hide cufon flicker
	$('.ie7 .cufon, .ie8 .cufon, .ie7 .cufon_big, .ie8 .cufon_big').show();

	// ticker hide messages, no opacity for IE
	var length = $('#ticker h3').length;
	if ($.browser.msie && $.browser.version.substr(0,1) < 9) {
		$('#ticker h3').slice(1, length).css('display', 'none');
	} else {
		$('#ticker h3').slice(1, length).css({'opacity': '0.0', 'margin-top': '5px', 'display': 'none'});
	}

	// fix background
	/*
	TODO: fix vikings background
	var height = $('#background_middle').height();
	var margintop_ff_ie = height - 125;
	var height_ff_ie = height - 125;
	$('.ie8 #background_repeat, .gecko #background_repeat').css({'height': height_ff_ie + 'px', 'margin-top': '-' + margintop_ff_ie + 'px'});
	$('.webkit #background_repeat').css({'height': height + 'px', 'margin-top': '-' + height + 'px', 'background-position': 'center 156px'});
	*/

	// form post
	$('#email2').hide();
	function submitform() {
		var error = false;
		if ($('#name').val() == '') {
			$('#name').css({'background': '#ff2727', 'color': '#ffffff'});
			error = true;
		} else {
			$('#name').css({'background': '#ffffff', 'color': '#300e04'});
			error = false;
		}
		/*var email = $('#email').val();
		var regex = /^[A-Za-z0-9\._%+\-]+\@([A-Za-z0-9\-]+\.)+([A-Za-z0-9]{2,4}|museum)$/;
		if (email != '' && email.match(regex)) {
			$('#email').css({'background': '#ffffff', 'color': '#300e04'});
		} else {
			$('#email').css({'background': '#ff2727', 'color': '#ffffff'});
			error = true;
		}*/
		if ($('#comment').val() == '') {
			$('#comment').css({'background': '#ff2727', 'color': '#ffffff'});
			error = true;
		} else {
			$('#comment').css({'background': '#ffffff', 'color': '#300e04'});
			error = false;
		}
		if (error == false) {
			$('form').submit();
		}
	}
	$('form div').click(function() {
		submitform();
	});

	// lightbox
	$('.lightbox').lightBox();

});

$(window).load(function() {

	/*
	ticker
	--------------------------------------------------
	*/
	var messages = $('#ticker h3').length - 1;
	var wait = 5000;

	function ticker(index) {
		if (index >= messages) {
			var next = 0;
		} else {
			var next = index + 1;
		}

		// fading cufon text in IE looks ugly
		if ($.browser.msie && $.browser.version.substr(0,1) < 9) {
			$('#ticker h3').eq(index).hide();
			setTimeout(function() {
				$('#ticker h3').eq(next).css('display', 'block');
			}, 250);
		} else {
			$('#ticker h3').eq(index).animate({
				opacity: 0.01,
				marginTop: '-5'
			}, 500, function() {
				$(this).css({'margin-top': '5px', 'display': 'none'});
				$('#ticker h3').eq(next).css('display', 'block').animate({
					opacity: 0.99,
					marginTop: '0'
				}, 500);
			});
		}

		setTimeout(function() { ticker(next); }, wait);
	}

	setTimeout(function() { ticker(0); }, wait);
	/*
	end ticker
	--------------------------------------------------
	*/

});

