$(document).ready(function(){


    $(function() {
    	var zIndexNumber = 1000;
    	$('body > *').each(function() {
    		$(this).css('zIndex', zIndexNumber);
    		zIndexNumber -= 10;
    	});
    });

	  makeWaves();

	  $(".validate").validate();

    $('.boat').css('position', 'absolute');
    $('.boat').css('z-index', '200');
    $('.boat').css('top', '0px');

    $('.rtl').css('left', '200%');
    $('.ltr').css('left', '-100%');

    $('#myuptownfun').css('top', '100px');
    $('#lazyday').css('top', '80px');


  	animateElement('#lyc',50,35, 21000);
  	animateElement('#raginuptown',90,51, 24000);
  	animateElement('#tequila1800',50,45, 30000);
  	animateElement('#threeolives',-55,47, 36000);
  	animateElement('#myuptownfun',-20,57, 45000);
  	animateElement('#elevatelifestyle',100,60, 48000);
  	animateElement('#lknfun',80,65, 42000);
  	animateElement('#bitflip',100,100,35000);
  	animateElement('#rockhouse',100,70, 48000);
  	animateElement('#foxhound',35,75, 30000);
  	animateElement('#lazyday',0,80, 39000);
  	animateElement('#kiss951',120,85, 18000);
  	animateElement('#mikeshard',5,85, 28000);
  	animateElement('#vwc',-60,90, 25000);
  	animateElement('#budlime',-25,25,27000);
  	animateElement('#qcbroker', 90,25,36000);


  	$("#submit").click(function (event)
    {
         event.preventDefault();
         ajaxProcessForm('reservation', 'reservation', 'process_form.php', 'POST');
    });


});

function animateElement(elementID, iTop, iLeft, speed) {

	speed = speed/2;

	$(elementID).animate({
		top: iTop+"px",
		left: iLeft+"%",
		},
		speed
	);
}


function getRand(range) {
	var rangeFix = range+1;
	return Math.floor(Math.random()*rangeFix);
}

function makeWaves() {

	for(i=0;i<100;i++) {
		$('#wave0').clone().appendTo('#waves')
		.css('padding-left', getRand(300)+'px')
		.css('padding-right', getRand(200)+'px')
		.css('padding-top', getRand(30)+'px');
	}

	animateWater();
}

function animateWater(){

	var step = 1+getRand(1);
	var direction = (step == 2) ? "-=2px" : "+=2px";
	var op = step*getRand(50)/100;

	$('#waves').animate({
		top: direction,
		left: direction,
		opacity: op
		},
		5000,
		function() {
			animateWater();
		}
	);
}

/*
$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};
*/

function ajaxProcessForm(form_id, form_name, form_action, form_method){

	//var formData = $('#'+form_id).serializeObject();
	var formData = $('#'+form_id).serialize();
	formData['form_url'] = form_name;
	var feedback = form_name+'_feedback';
	var ajaxresult = form_name+'_ajaxResult';

	$.ajax({
		url: form_action,
		type: form_method,
		dataType: 'json',
		data: formData,
		success: function(response) {
			if(response.error) {
				$('#'+feedback).html(response.message).show();
			}
			else {
				$('#'+feedback).hide();
				$('#'+ajaxresult).html(response.message);
				$('#contactFormTitle').html('Thank you for filling out the form');
				//$('#'+ajaxresult).dialog();
				$('#'+ajaxresult).css('display', 'block');
				$('#'+form_id).css('display', 'none');
			}
		}
	});

}
