var application = {
	fancybox:{
		init:function(){
			$("a.fancybox-group").fancybox({
				'transitionIn'	     :	'elastic',
				'transitionOut'	     :	'elastic',
				'titlePosition'      :   'inside',
				'speedIn'		     :	600, 
				'speedOut'		     :	200,
				'cyclic'             : true,
				'hideOnOverlayClick' : true,
				'showCloseButton'    : true,
				'margin'             : 30,
				'width'              : 'auto',
				'height'             : 'auto'
			});
		}
	},
	tabs:{
		init:function(ClassApplication){
			var self = this;
			$(".ui-tabs a").click(function(){
				self.toggleTabs($(this),self);
                return false;
            });
		},
		toggleTabs: function(link,self){
			$('.ui-tabs,.tabs').find('.current').each(function(){
				$(this).removeClass('current');
			});
			var href = link.attr('href');
			$(''+href).addClass('current');
			link.addClass('current');
		}
	},
	placeholders:{
		init:function(ClassApplication){
			$('.form_placeholder').each(function(){
				var input = $(this);
				var title = input.attr('title');
				if (input.length){
					input.focus(function(){
						if (input.hasClass('placeholder_text'))
							input.removeClass('placeholder_text').val('');
					}).blur(function(){
						if ($.trim(input.val()) == '')
							input.addClass('placeholder_text').val(title);
					});
					if ($.trim(input.val()) == ''){
						input.addClass('placeholder_text').val(title);
					}
				}
			});
			$('form').submit(function(){
				$(this).find('.placeholder_text').val('');
			});
		}
    },
	ajaxMail:{
		current:{
            sending:false
        },
		init: function(ClassApplication){
            var self = this;
			var orderButton = $('#main_big_order_button');
			if(orderButton.length){
				self.orderButton(orderButton);
				orderButton.fancybox({
					'transitionIn'	     :	'elastic',
					'transitionOut'	     :	'elastic',
					'speedIn'		     :	400, 
					'speedOut'		     :	400,
					'hideOnOverlayClick' : true,
					'showCloseButton'    : true,
					'width'              : 'auto',
					'height'             : 'auto',
					'padding'			 : 0,
					'titleShow'          : false,
					'onComplete'         : function() {
						self.initMail(self);
					}
				});
			}else{
				return false;
			}
			return true;
        },
		initMail: function(self){
			$('#order_submit').click(function(){
				self.ajaxSendMail($('#order_form'),self);
                return false;
            });
		},
		orderButton:function(orderButton){
			button = orderButton;
			href = button.attr('href').replace(/#ajax#/g,'/ajax-order-form/');
			button.attr('href',href);
			button.show();
			return true;
		},
		ajaxSendMail:function(form, self){
            if(self.current.sending) return false;
            self.current.sending = true;
			
            form.find('.order_form_error').removeClass('order_form_error');
            form.find('.order_form_success').hide();
            form.find('.order_form_errors').hide();
            
			var send_data = form.serializeArray();
			send_data[0].value = document.location.href;
			
            $.ajax({
                type: "POST",
                data: send_data,
				dataType: 'json',
                url: '/webforms/custom_order_send/',
                success: function(data){
                    if(data.status){
                        self.errorMail(data, form, self);
                    }else{
                        self.okMail(form, self);
                    }
                    self.current.sending = false;
                },
				error: function(jqXHR, textStatus, errorThrown){
					console.log(jqXHR);
					console.log(textStatus);
					console.log(errorThrown);
				}
            });
            return false;
        },
		okMail:function(form, self){
            form.find('.order_form_success').show();
            form.get(0).reset();
        },
        errorMail:function(error, form, self){
            form.find('.order_form_errors').show();
            if(error.email){
                form.find('.order_email').addClass('order_form_error');
            };
            if(error.name){
                form.find('.order_name').addClass('order_form_error');
            };
        }
	},
	price:{
		init:function(ClassApplication){
			$('.price_types').hide();
			$('.'+$('#price_select').val()).show();
			$('#price_select').change(function() {
				$('.price_types').hide();
				$('.'+$(this).val()).show();
			});
		}
    }
}
$(document).ready(function(){
    (function() {
        this.fancybox.init(this);
		this.placeholders.init(this);
		this.ajaxMail.init(this);
		this.tabs.init(this);
		this.price.init(this);
    }).call(application);
});
