// JavaScript Document


jQuery(document).ready(function($) {
	'use strict';

	/*save email for "notice me"*/
	$('#notice-me').submit(function() {
	   //return alert(ajaxurl);
		var form_el = $(this);
		var email = form_el.find('input[name=email]').val();

		if((email == '') || (typeof(email) == 'undefined')) {
			if(transforms3d_supported) {
				cube.find('.cube-slide-3').attr('title', '').html(form_el.data('email-not-set-msg'));
				cube.showBottom();
			}
			else
				cube.find('.return-msg').attr('title', '').html(form_el.data('email-not-set-msg')).fadeIn();
			return false;
		}

		form_el.find('.ajax-loader').fadeIn('fast');
        
        var post_data = {
            action          : 'precise_save_email_notice_via_ajax',
            email           : email
        }
        $.post(ajaxurl, post_data, function(message){
            //form_el.find('.ajax-loader').hide();
			if(message == '' || message == 'success' || (typeof(message) == 'undefined')) {
				if(transforms3d_supported) {
					cube.find('.cube-slide-1').html(form_el.data('success-msg'));
					cube.showTop();
				}
				else
					cube.find('.return-msg').attr('title', '').html(form_el.data('success-msg')).fadeIn();
			}
			else {
				if(transforms3d_supported) {
					cube.find('.cube-slide-3').attr('title', message).html(form_el.data('opening-error-msg'));
					cube.showBottom();
				}
				else
					cube.find('.return-msg').attr('title', message).html(form_el.data('opening-error-msg')).fadeIn();
			}
            form_el.find('.ajax-loader').fadeOut('fast');
        });
        
		return false;
	});
	
	
	/*save email for rss subscribe*/
	$('#rss-subscribe').submit(function() {
		var form_el = $(this);
		var email = form_el.find('input[name=email]').val();

		if((email == '') || (typeof(email) == 'undefined')) {
			form_el.find('.return-msg').attr('title', '').html(form_el.data('email-not-set-msg')).fadeIn();
			return false;
		}
		
		form_el.find('.ajax-loader').fadeIn('fast');
        
        var post_data = {
            action          : 'precise_save_email_notice_via_ajax',
            email           : email
        }
        $.post(ajaxurl, post_data, function(message){
            //form_el.find('.ajax-loader').hide();
			
            if(message == "" || message == 'success' || (typeof(message) == 'undefined')) {
				form_el.find('.return-msg').attr('title', '').html(form_el.data('success-msg')).fadeIn();
			}
			else {
				form_el.find('.return-msg').attr('title', message).html(form_el.data('opening-error-msg')).fadeIn();
			}
            
            form_el.find('.ajax-loader').fadeOut('fast');
        });
		return false;
	});
	
	
	/*send message on email*/
	$('#send-message').submit(function() {
		var form_el = $(this);
		var email = form_el.find('input[name=email]').val();
		var sender_name = form_el.find('input[name=sender_name]').val();
		var message = form_el.find('textarea[name=message]').val();

		if((email == '') || (sender_name == '') || (message == '') || (typeof(email) == 'undefined') || (typeof(sender_name) == 'undefined') || (typeof(message) == 'undefined')) {
			form_el.find('.return-msg').html(form_el.data('all-fields-required-msg')).fadeIn();
			return false;
		}
		
		form_el.find('.ajax-loader').fadeIn('fast');
        
        var post_data = {
            action          : 'precise_send_email_msg_via_ajax',
            email           : email,
            sender_name     : sender_name,
            message         : message
        }
        $.post(ajaxurl, post_data, function(message){
			if(message == "" || message == 'success' || (typeof(message) == 'undefined')) {
				form_el.find('.return-msg').html(form_el.data('success-msg')).fadeIn();
			}
			else {
				form_el.find('.return-msg').html(message).fadeIn();
			}
            form_el.find('.ajax-loader').fadeOut('fast');
        });
        
		return false;
	});
	
	$('.return-msg').click(function() {
        $(this).fadeOut('fast');
    });
});