$(document).ready(function() {
	NeueMedia.init();
	$("#newsletter_form_2").submit(function(){
		emailVal = $("#email_address").val();
		if(!/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(emailVal)) {
			NeueMedia.Global.showFlashMessage("alert", "Email is incorrect!");
			return false;
		}
	});
});

var sTmOut = false;

var NeueMedia =
{
	init: function()
	{

		NeueMedia.Global.init();
		NeueMedia.Home.init();
		NeueMedia.Clients.init();
		NeueMedia.Contact.init();
		NeueMedia.Callback.init();

	},
	Global: {
		init: function() {
			NeueMedia.Global.fixIE6Hover();
			NeueMedia.Global.prepareForms();
		},
		fixIE6Hover: function() {
			$("div#primary_navigation li").hover(
					function() {
						$(this).addClass("hover");
					},
					function() {
						$(this).removeClass("hover");
					}
					);
		},
		prepareForms: function() {
			$.prePopulate();
		},
		showFlashMessage : function(type, message)
		{
			$("#notifications").slideDown(250, function()
			{
				var sTmOut = setTimeout(NeueMedia.Global.hideFlashMessage, 5000);
				$("#message").html(message).fadeIn('slow');
			});
		},
		hideFlashMessage : function()
		{
			if ( sTmOut ) {
				clearTimeout(sTmOut);
			}
			$("#message").fadeOut(250, function(){
				$("#notifications").slideUp(250, function(){
					$("#message").html("");
				});
			});
		}
	},
	Home:
	{
		init: function()
		{
			if ($("body").attr("id") == "section_home")
			{
				NeueMedia.Home.flashHeader();
				NeueMedia.Home.callbackModal();
			}
			$("#newsletter_form").submit(NeueMedia.Home.newsletter);

		},
		newsletter : function()
		{
			/// when php will be enabled
			/*
			 $.ajax({
			 type : $(this).attr("method"),
			 url  : $(this).attr("action"),
			 data : $(this).serialize(),
			 dataType:"json",
			 success:NeueMedia.Home.handleNewsletter
			 })
			 return false;
			 */
			jsonResponse = new Object();
			jsonResponse.message_type = 'confirm';
			jsonResponse.message = 'Thank you for submitting your request';

			NeueMedia.Home.handleNewsletter(jsonResponse);
			return false;
		},
		handleNewsletter : function(jsonResponse)
		{
			NeueMedia.Global.showFlashMessage(jsonResponse.message_type, jsonResponse.message);
			//reset the input if email is valid
			if (jsonResponse.message_type == 'confirm')
			{
				NeueMedia.Global.prepareForms();
			}
		},
		flashHeader: function() {
			$("div#flash_intro").flash({
				src: "/flash/home_placeholder.swf",
				width: 930,
				height: 445,
				wmode: "opaque"
			});
		},
		callbackModal: function() {
			$("div#callback_modal").jqm({
				overlay: 70
			});
			$("a#callback_modal_trigger").click(function() {
				$("div#callback_modal").jqmShow();
				$('#call_back_request').show();
				$('#confirmationmessage').hide();
				return false;
			});
		}
	},
	Clients: {
		init: function() {
			if ($("body").attr("id") == "section_clients") {
				$("ul#clients li").hover(
					function() {
						$(this).addClass("hover");
					},
					function() {
						$(this).removeClass("hover");
					}
				);
			}
		}
	},
	Contact:{
		init:function()
		{
			$("#contact_form").submit(function()
			{
				$.ajax({
				type : $(this).attr("method"),
				url  : $(this).attr("action"),
				data : $(this).serialize(),
				dataType:"json",
				success:NeueMedia.Contact.handleContact
				})
				return false;
			});
		},
		handleContact : function(jsonResponse) {
			NeueMedia.Global.showFlashMessage(jsonResponse.message_type, jsonResponse.message);
			//reset the input if email is valid
			if (jsonResponse.message_type == 'confirm')
			{
				$("#contact_form input[type=text], #contact_form textarea").val("");
				NeueMedia.Global.prepareForms();
			}
		}
	},
	Callback:{
		init:function(){
			$('#call_back_request').submit(function(){
				$.ajax({
				type : $(this).attr("method"),
				url  : $(this).attr("action"),
				data : $(this).serialize(),
				dataType:"json",
				success:NeueMedia.Callback.handleCallback
				})
				return false;
			});
		},
		handleCallback : function(jsonResponse) {
			if(jsonResponse.message=="ok") {
				$('#call_back_request').toggle();
				$('#confirmationmessage').toggle();

				$('#call_back_request input[type=text]').val('');
				$('#call_back_request label span').remove();
			}
			else {
				$('#call_back_request label span').remove();

				$("#"+jsonResponse.field).prev("label").append(' <span style="color:#ff3300; font-weight:bold;">('+jsonResponse.message+')</span>');
				$("#"+jsonResponse.field).focus();
				//alert(jsonResponse.message)
			}
		}
	}


};