
$(document).ready( function() {
	
	$('#btn-appointment').hide();
	
	$('#btn-appointment').delay(1500).fadeIn();
	
	// List of practice areas
	var practice_areas = [
		{ "practice": "Personal Injury",                              "pic": "1",  "url": "personal-injury" },
		{ "practice": "Property Sales, Purchases & Re-mortgages",     "pic": "2",  "url": "property-sales-purchases-remortgages" },
		{ "practice": "Commercial Property & Construction",           "pic": "3",  "url": "construction-commercial-property" },
		{ "practice": "Landlord & Tenant",                            "pic": "4",  "url": "landlord" },
		{ "practice": "Wills & Probate",                              "pic": "5",  "url": "wills-probate" },
		{ "practice": "Family Law",                                   "pic": "1",  "url": "family-law" },
		{ "practice": "Employment Law",                               "pic": "2",  "url": "employment-law" },
		{ "practice": "Licensing Law",                                "pic": "3",  "url": "licensing-law" },
		{ "practice": "Alternative Dispute Resolution",               "pic": "4",  "url": "alternative-dispute-resolution" },
		{ "practice": "Debt Recovery",                                "pic": "5",  "url": "debt-recovery" },
		{ "practice": "Company Setup & Corporate Governance",         "pic": "1",  "url": "company-setup-corporate-governance" } ];
		
	var num_areas    = 11;
	var current_area = 1;
	var first        = true;

	$.timer(5000, function (timer) { $(this).change_text(); }); // Start timer	}
		
	jQuery.fn.change_text = function() {
	
		if(current_area == num_areas) { current_area = 1; } // Reset the count

		var the_link = $("#box a");
		the_link.fadeOut( function() {
			the_link.attr("href", "http://www.wams.ie/" + practice_areas[current_area].url );
			the_link.text(practice_areas[current_area].practice);
			$("#box a").fadeIn();
		});
		
		$('#ticker img').fadeOut( function() { 
			$(this).attr("src", "media/images/ticker/" + practice_areas[current_area].pic + ".jpg").fadeIn();
		} );
					
		current_area ++;
	}

	$("a[rel='callback']").colorbox({width:"550px", height:"370px", inline:true, href:"#callback-modal"});
	$("a[rel='appointment']").colorbox({width:"600px", height:"555px", inline:true, href:"#appointment-modal"});
	$("a[rel='appointment-1']").colorbox({width:"600px", height:"555px", inline:true, href:"#appointment-modal"});
	
	
	// Validate the forms
	$("#callback-form").validate();
	$("#appointment-form").validate({
		rules: {
    	tel: { required: function(element) { return $("#app-email").val() < 5; } },
    	"app-email": { required: function(element) { return $("#tel").val() < 7; } }
    }	
	});
	
	$("#callback #submit").click(function(e){
		if ( $("#callback-form").valid() ) {
			
			$.post("mail.php",
				   { name: $("#callback-form #name").val(),
				     telephone: $("#callback-form #telephone").val(),
				     reason: $("#callback-form #reason").val() },
				   function(data) {
					   
					   $("#callback").fadeOut( function() {
							 
							 if(data == "worked") { $("#callback-success").fadeIn(); return true; }
							 else { $("#callback-fail").fadeIn(); return false; }
					   
					   });
					   
				   },
				   "text");
			e.preventDefault();
		}
		else { return false; }
	});
	
	$("#appointment #submit").click(function(e){
		if ( $("#appointment-form").valid() ) {
			
			$.post("mail-appointment.php",
				   { name: $("#appointment-form #app-name").val(),
				     telephone: $("#appointment-form #tel").val(),
				     email: $("#appointment-form #app-email").val() ,
				     details: $("#appointment-form #details").val() },
				   function(data) {
					   
					   $("#appointment").hide();

					   if(data == "worked") { $("#appointment-success").show(); return true; }
					   else { $("#appointment-fail").show(); return false; }
				   },
				   "text");
			e.preventDefault();
		}
		else { return false; }
	});
		
});