// this is the main startup script
$(function () {

	// wire rotator (home page)
	$("#rotator").rotator({
		itemFade: 500,
		pagerFade: 0,
		itemShow: 3000,
		autoPlay : true,
		showPager : true
	});
	
	// wire superfish navigation (all pages)
	$('#nav')
		.superfish({
			delay: 300,
			speed: 100,
			//animation: {height:'show'},
			dropShadows: false
		});

	// IE6 bgiframe fix - do it only if bgIframe is loaded (all pages)
	if($.fn.bgIframe)
		$('#nav').find('ul').bgIframe({opacity:false});

	// replace heading fonts (all pages)
	Cufon.replace('#nav li a, #content h1, #content h2, #content h3, #content h4, #content ul.imglist h3, #footer h6, #rotator p.title', {
        fontFamily: 'Aller',
		hover: true,
		forceHitArea: true
	});

	// wire fancybox (projects page)
	$("#projects .project a.box").fancybox({
		zoomSpeedIn: 300,
		zoomSpeedOut:	300,
		zoomOpacity: true,
		imageScale: true,
		overlayShow: true
	});

	// contact form hide success/error messages (contact page)
	$('#contactform div.success, #contactform div.error').hide();
	// define ajax submit handler
	var AjaxSubmit = function(){
		var btnText=$('#contactform .submit').val();
		// inform client that data is been sent:
		$('#contactform .submit').val('Sending...');
		$.ajax({
			type: 'POST',
			url: $('#contactform').attr('action'),
			data: $('#contactform').serialize(),
			// successful POST - display result in success div:
			success: function(msg){
				$('#contactform .form').slideUp(500,function(){
					$('#contactform div.success').fadeIn(500);
				});
			},
			// error on server, or wrong url - display status text in error div:
			error: function(response) {
				$('#contactform .submit').val(btnText);
				$('#contactform div.error').html(response.statusText).slideDown(500);
			}
		});
		// prevent default submit button behaviour:
		return false;
	}
	// wire client validation
	$("#contactform").validate({
		submitHandler: AjaxSubmit
	});

});
