FLIR.init({ path: '/js/facelift-1.2/' });

var prependContent = '<table cellpadding="0" cellspacing="0">';
	prependContent += '<tr><td class="corner_top_left"></td><td class="top_bor"></td><td class="corner_top_right"></td></tr>';
	prependContent += '<tr class="mid">';
	prependContent += '<td class="left_bor"></td>';
	prependContent += '<td class="text_content">';

var appendContent = '</td>';
	appendContent += '<td class="right_bor"></td>';
	appendContent += '</tr>';
	appendContent += '<tr><td class="corner_bottom_left"></td><td class="bottom_bor"></td><td class="corner_bottom_right"></td></tr>';
	appendContent += '</table>';

function checkMail(src) {
	var emailReg = "^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$";
	var regex = new RegExp(emailReg);
	return regex.test(src);
}
	
$(document).ready( function () {
	var requiredText = '<span class="ui-state-error" style="border: 0;"><span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em;margin-top: 12px;"></span></span>';
	
	$('#contact .check').after(requiredText);
	$('#contact .ui-state-error').hide();
	
	$('.content_right').each(function() {
		$(this).html(prependContent + $(this).html() + appendContent);
		$('.border_top', this).attr('width', 420);
		$('.border_bottom', this).attr('width', 420);
	});
	
	$('.content_full').each(function() {
		$(this).html(prependContent + $(this).html() + appendContent);
		$('.border_top', this).attr('width', 620);
		$('.border_bottom', this).attr('width', 620);
	});
	
	$('#items a').each(function() {
		FLIR.replace(this, new FLIRStyle({ cFont:'OratorStd', realFontHeight:true }));
	});
	
	$('h1').each(function() {
		FLIR.replace(this, new FLIRStyle({ cFont:'OratorStd', realFontHeight:true }));
	});
	
	$('#footer').each(function() {
		FLIR.replace(this, new FLIRStyle({ cFont:'OCR A Extended', realFontHeight:true }));
	});
	
	$(function() {
		$('#dialog_plan').dialog({
			bgiframe: true,
			autoOpen: false,
			height: 580,
			width: 630,
			modal: true,
			resizable: false,
			buttons: {
				'Fermer': function() {
					$(this).dialog('close');
				}
			}
		});
		
		$('#dialog_confirm').dialog({
			bgiframe: true,
			autoOpen: false,
			height: 220,
			width: 400,
			modal: true,
			resizable: false,
			buttons: {
				'OK': function() {
					$(this).dialog('close');
				}
			}
		});
	});
	
	$('#plan').click(function() {
		$('#dialog_plan').dialog('open');
	});
	
	function sendMailContact() {
		var script = '/ajax.php';
		$.post(
			script,
			{func:'sendMailContact',
			 r_type:$('#r_type').val(),
			 r_firm:$('#r_firm').val(),
			 r_name:$('#r_name').val(),
			 r_firstname:$('#r_firstname').val(),
			 r_mail:$('#r_mail').val(),
			 r_tel:$('#r_tel').val(),
			 r_msg:$('#r_msg').val()},
			function(data, textStatus) {
				if ( data ) 
					$('#dialog_confirm').dialog('open');
			}
		);
	}
	
	$('#contact').submit(function(data) {
		var ok = true;
		$('#contact span.ui-state-error').hide();
		$('#contact .check').each(function() {
				if ( $(this).val() == "" || ($(this).attr("name") == "r_mail" && !checkMail($(this).val())) ) {
					if ( $(this).attr("name") == "r_mail" ) {
						alert('Vous devez saisir un mail correct.');
						$(this).focus();
					}
					$(this).next().show().effect('pulsate',{},500);
					$('#info').removeClass('ui-state-highlight').addClass('ui-state-error');
					$('#info_icon').removeClass('ui-icon-info').addClass('ui-icon-alert');
					ok = false;
				}
			}
		);
		if ( ok ) {
			$('#info').addClass('ui-state-highlight').removeClass('ui-state-error');
			$('#info_icon').addClass('ui-icon-info').removeClass('ui-icon-alert');
			sendMailContact();
			$('label').next().val('');
		}
		return false;
	});
	
	$('.caddie').click(function() {
		document.location.href='/contact.html?caddie=' + $(this).attr('id');
	});
});
