/**
* Lisa Mowry Music - global.js
*
* Author: Andrew Kallemeyn
* When: September 2011
*
*/

var initialPlay = 0; //whether or not the user has initialized the CD media
var mobileDevice = 0;

function isMobile() {
    return (
		(navigator.userAgent.match(/Android/i) != null) ||
		(navigator.userAgent.match(/webOS/i) != null) ||
		(navigator.userAgent.match(/iPhone/i) != null) ||
		(navigator.userAgent.match(/iPod/i) != null) ||
		(navigator.userAgent.match(/iPad/i) != null)
    );
}

function isValidEmail(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

$(document).ready(function() {
	if(isMobile()) {
		$('body').addClass('mobile_device');
		mobileDevice = 1;
	}
	
	$('input, textarea').placeholder();
	
	if($('#navigation li.current a.home').length > 0) {
		$("#photo_feature a").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	300, 
			'speedOut'		:	300, 
			'overlayShow'	:	true,
			'overlayColor'	:	'#000', 
			'easingIn'      :	'easeOutQuad',
			'easingOut'     :	'easeInQuad',
			'padding'		:	0,
			'opacity'		:	true
		});
		
		$('#cd_cover a, #listen_now a').click(function() {
			$('#cd_cover, #listen_now a').fadeOut(function() {
				if(!mobileDevice && !initialPlay) {
					//play first song
					initialPlay = 1;
					$('#cd_menu li:first-child a').trigger('click');
				}		
			});
			
			$('#return_to_artwork a, #cd_menu').fadeIn();	
			return false;
		});
		
		$('#return_to_artwork a').click(function() {
			$('#cd_menu, #return_to_artwork a').fadeOut();
			$('#cd_cover, #listen_now a').fadeIn();
			
			$('#listen_now span').addClass('playlist').html('Return to <strong>Playlist</strong>');
			return false;
		});
		
		$('#cd_menu a').click(function() {
			return false
		});
		
		//initialize audio controls for CD media
		$(document).audiocontrol();
	}
	
	if($('#contact_form').length > 0) {
		$('#contact_form input, #contact_form textarea').focus(function(e) {
			$(this).parent().removeClass('error');
		});
		
		$('#contact_form button').width($('#contact_form button').width());
		
		$('#contact_form button').ajaxStart(function() {
			$(this).addClass('loading').html('<img src="/images/spinner.gif" />');
		});
		
		$('#contact_form button').ajaxStop(function() {
			$(this).removeClass('loading').text('Send');
		});
		
		$('#contact_form').submit(function() {
			try {
				//run some simple validation...
				var fieldsNotEmpty = ['contact_email','contact_message'];
				var errors = false;
				
				$('#contact_form fieldset li').removeClass('error');
				
				$(fieldsNotEmpty).each(function(index, value) {
					if($('#' + value).val() == '') {
						$('#' + value).parent().addClass('error');
						errors = true;
					}			
				});
				
				if(!isValidEmail($('#contact_email').val())) {
					errors = true;
					$('#contact_email').parent().addClass('error');
				}
				
				if(!errors) {
					$.ajax({
						url: $(this).attr('action') + '?ajax=true',
						type: $(this).attr('method'),
						data: $(this).serialize(),
						
						success: function(response) {
							response = $.trim(response);
							
							if(response == "success") {
								$('#contact_form').find('input, textarea').val('').placeholder();
								$('#contact_form_status').addClass('success').html('Your email was sent!');
													
							}else{
								$('#contact_form_status').addClass('error').html('There was an error sending :( Try again later.');
	
							}
						}
					});
				}
				
				return false;
				
			}catch(ex) {
				return true;
			}
		});
	}
});
