

var Site = {
	Common: {
		contact: function(e) {
			$('#email, textarea').focus(function () {
				$(this).removeClass('error');
				$('#def-contact .notify').empty();
			});

			$(function() {
				if (!Modernizr.input.placeholder) {
					var active = document.activeElement;
					$('#email, textarea').focus(function () {
						if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
							$(this).val('').removeClass('hasPlaceholder');
						}
					}).blur(function () {
						if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
							$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
						}
					});
					$('#email, textarea').blur();
					$(active).focus();
				}
			});

			$('.remove').remove();
			$('#def-contact').show();
			
			$.get('def_token.php', function(txt) {
				$('#def-contact fieldset').append('<input type="hidden" name="ts" value="' + txt + '" />');
			});
			
			function processRequest(formData, jqForm, options) {
				$('.ajax-loader').css({'visibility':'visible'});
				$('#def-contact .notify').empty();
				$('#def-contact .notify').attr('class', 'notify');
				$('#def-contact input').removeClass('error');
				$('#def-contact textarea').removeClass('error');
				
				var form = jqForm[0], ok = true;
				if(!form.email.value || form.email.value === $('#email').attr('placeholder')) {
					ok = false;
					$('#email').addClass('error');
					$('.ajax-loader').css({'visibility':'hidden'});
				}
				
				if(!form.message.value || form.message.value === $('#message').attr('placeholder')) {
					ok = false;
					$('#message').addClass('error');
					$('.ajax-loader').css({'visibility':'hidden'});
				}
				return ok;
			}
			
			function processResponse(data, statusText, xhr, $form) { 

				$('.ajax-loader').css({'visibility':'hidden'});
				$('#def-contact .notify').append(data.notify);
				
				if(data.success) {
					$('#def-contact .notify').addClass('success');
					$('#message').val('').blur();
					$('#email').val('').blur();
				} else {
					$('#def-contact .notify').addClass('error');
					if(data.error.length !== 0) {
						for(x in data.error){
							var field = '#' + data.error[x];
							$(field).addClass('error');
						}
					}
				}
			}
			
			function processError(jqXHR, textStatus, errorThrown) {
				$('.ajax-loader').css({'visibility':'hidden'});
				$('#def-contact .notify').append('Error: ' + errorThrown).addClass('error');
			}

			var options = { 
				dataType:	'json', 
				timeout:	3000,
				beforeSubmit:	processRequest,
				success:	processResponse,
				error:	processError
			}; 

			$('#def-contact').ajaxForm(options); 
		},
		
		youtube: function(e) {
			$(".youtube").click(function() {
				var options = {
				padding		: 0,
				autoScale		: false,
				transitionIn	: 'elastic',
				transitionOut	: 'none',
				title			: this.title,
				width			: 640,
				height		: 390,
				href			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				type			: 'swf',
				swf			: {
				   	 wmode		: 'transparent',
					allowfullscreen	: 'true'
				}
			};
		$.fancybox(options);
	
		return false;
	});
		},
		
		belatedPng: function(e) {
			if (typeof DD_belatedPNG !== 'undefined') {
				var PNG_fix_selectors = [
					'ul.social li a span'
				];
				DD_belatedPNG.fix(PNG_fix_selectors.join(','));
			}
		}
	}
};


$(document).ready(function() {

	Site.Common.contact();
	Site.Common.youtube();
	Site.Common.belatedPng();
	$("ul li:last-child").addClass("last");
	$("#mnc").fancyFlickset({photosetId: '72157624014946932'});
	$("#stuff").fancyFlickset({photosetId: '72157624015245930'});
}); 
