var slideshowTimer = null;
$(document).ready(function(){
    initContactForm();
    
	if($('#rotator')){
		doSlideshow('rotator', true, 5000);
	}
    
    $('IMG.alignright').parent().css('margin','0');
    

});
function isVisible(obj) { return (obj.css('display') == 'block'); } 

function doSlideshow(objid, start, time){
	if(!$('#' + objid) || !isVisible($('#' + objid))){
		if(slideshowTimer != null){
			clearTimeout(slideshowTimer);
			slideshowTimer = null;
		}
		return;
	}
	
	if(start){
		clearTimeout(slideshowTimer);
		slideshowTimer = null;
	}
	
	var currObj = $('#' + objid + ' li.current');
	var nextObj = currObj.next();
	if($('#' + objid + ' li:last').hasClass('current')){ 
		nextObj = $('#' + objid + ' li:first');
	}

	nextObj.addClass('current');
	nextObj.css('opacity', 0).css('z-index','2');
	currObj.removeClass('current').css('z-index','1');
	currObj.animate({opacity:0}, 2000);
	nextObj.animate({opacity:1}, 2000);
	
	slideshowTimer = setTimeout('doSlideshow("' + objid + '", false, ' + time + ');', time);
}

function initContactForm(){

	$('#contact').submit(function(){
		var form = $(this);
		var formData = $(this).serialize();
		var error = '';
        $('#thank-you').hide();
        $('#error').hide();
        $.ajax({
			type: 'POST', url: form.attr('action'), data: formData, 
			success: function ( responseData ){
				if((error = Process(responseData)) != ''){
					$('#error').html(error);
                    $('#error').css('color','red');
                    $('#error').show();
				} else{
					document.getElementById('contact').reset();
					form.hide('normal');
					$('#thank-you').show('normal');					
				}
			}
		});
		
		return false;
	});
}

function Process(str){
	list = str.split(':');
	if(list[0].toLowerCase() != 'done'){
		return list[1];
	} else {
		return '';
	}
}