$(function(){
	
	// pop video modal
	$('#viewVideo a').click(function(e){
		e.preventDefault();
		il.modal.init('auto', '<iframe width="425" height="344" src="' + il.videos["1"] + '" frameborder="0" allowfullscreen></iframe>');
	});
	
	// pop second video modal
	$('#viewVideo2 a').click(function(e){
		e.preventDefault();
		il.modal.init('auto', '<iframe width="425" height="344" src="' + il.videos["2"] + '" frameborder="0" allowfullscreen></iframe>');
	});
	
	// handle request for seminar information form
	$('#register_form').submit(function(e){
		e.preventDefault();
		var self = $(this);
		
		// verify form fields
		if($('#name').val() == ''){
			alert('Please enter your full name.');
			return false;
		} else if($('#country').val() == ''){
			alert('Please enter your country of residence.');
			return false;
		} else if(!$('#phone').val().replace(/\s+/g, '').match(il.phRE)){
			alert('Please enter a valid phone number.');
			return false;
		} else if(!$('#sem_email').val().match(il.emRE)){
			alert('Please enter a valid email address.');
			return false;
		}
		
		// send email
		$.ajax({
			url: 'mail.php',
			type: 'POST',
			data: self.serialize(),
			success: function(data){
				if(!data.match('OK')){
					il.modal.init('small', '<p>We\'re sorry, there was an error sending your email. Please try again later.</p>');
				} else {
					il.modal.init('small', '<p>Thank you!  Your request has been sent.');
				}
			}
		});
	});
		
	// handle request for contact us form
	$('#contact_form').submit(function(e){
		e.preventDefault();
		var self = $(this);

		// verify form fields
		if($('#name').val() == ''){
			alert('Please enter your full name.');
			return false;
		} else if($('#country').val() == ''){
			alert('Please enter your country of residence.');
			return false;
		} else if(!$('#phone').val().replace(/\s+/g, '').match(il.phRE)){
			alert('Please enter a valid phone number.');
			return false;
		} else if(!$('#con_email').val().match(il.emRE)){
			alert('Please enter a valid email address.');
			return false;
		}

		// send email
		$.ajax({
			url: 'mail.php',
			type: 'POST',
			data: self.serialize(),
			success: function(data){
				if(!data.match('OK')){
					il.modal.init('small', '<p>We\'re sorry, there was an error sending your email. Please try again later.</p>');
				} else {
					il.modal.init('small', '<p>Thank you! Your request has been sent.');
				}
			}
		});
			
	});
	
});

(function(){
	
	window.il ? 0 : window.il = {};
	
	// storage for illumiwave youtube urls
	il.videos = {
	  "1":"http://www.youtube.com/embed/UHgczbmXIMQ",
	  "2":"http://www.youtube.com/embed/iJL7SMxiZH8"
	},
	
	// utility object for modal dialog windows
	il.modal = {

		// entry point for modal functions
		// - 'auto' for regular centered dialog (video)
		// - 'small' for smaller centered dialog (message)
		init: function(type, html){
			il.modal.dialog(type, html);
		},

		// grey out the screen to prepare for dialog
		screen: function(){
			var screen = $('#screen');
			if(screen.length < 1){
				$('body').append('<div id="screen"></div>');
				screen = $('#screen');
				screen.animate({opacity:0.75,height:$(window).height()}, 600);
			} else if(!screen.is(':visible')){
				screen.animate({opacity:0.75,height:$(window).height()}, 600);
			} else {
				screen.fadeOut();
			}
			screen.click(function(){
				il.modal.kill();
			});
		},

		// pop actual dialog box
		dialog: function(type, html){
			il.modal.screen();

			var dialog = $('#dialog');
			if(dialog.length < 1){
				$('body').append('<div id="dialog"><img class="close" src="images/close-button.png"/></div>');
				dialog = $('#dialog');
				dialog.append(html);
			} else if(!dialog.is(':visible')){
				dialog.fadeIn();
			}
			
			$('#dialog .close').click(function(){
			  il.modal.kill();
			});

			if(type == 'small'){
				var offsetLeft = Math.floor((parseInt($(window).width(), 10) - 300) / 2),
					offsetTop = Math.floor((parseInt($(window).height(), 10) - 50) / 2);
				$('#dialog').css({'left':offsetLeft, 'top':offsetTop, 'width':'300px'});
			} else if(type == 'auto'){
				var offsetLeft = Math.floor((parseInt($(window).width(), 10) - 425) / 2),
					offsetTop = Math.floor((parseInt($(window).height(), 10) - 344) / 2);
				$('#dialog').css({'left':offsetLeft, 'top':offsetTop});
			}
		},

		// kill screen & dialog
		kill: function(){
			$('#screen').remove();
			$('#dialog').remove();
		}

	};
	
	il.phRE = /^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/;
	il.emRE = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
	
})();
