var feedback;

var department = {
	com: 'Коммерческий отдел',
	tech: 'Техническую поддержку'
}

$(function() {
	
	//FeedBack - объект для управления высплывающим окном
	function FeedBack() {
		var container_id = 'fb_container';
		var overlay_id = 'fb-overlay';
		var content_id = 'fb-content';
		$('body').append(this.BuildContainer());
		this.container = $('#fb-container');
		this.overlay = $('#fb-overlay');
		this.content = $('#fb-content');
		this.SetEvents(this);
		this.isShow = false;
	}
	
	FeedBack.prototype.SetEvents = function(feedback) {
		$('#fb-open').click(function(){
			feedback.Show();
			return false;
		});
				
		$('#fb-close').click(function(){
			feedback.Hide();
		});
	}
	
	FeedBack.prototype.BuildContainer = function() {
		var container = '<div id="fb-overlay"></div>';		
		container += '<div id="fb-container">';
		container += '<div id="fb-box"><div id="fb-close"></div>'+this.BuildTable()+'</div>';
		container += '</div>';
		return container;
	}
	
	FeedBack.prototype.BuildTable = function() {
		var table = '<table class="fb-table">';
		table += '<tr class="fb-top"><td class="fb-left"></td><td class="fb-center"></td><td class="fb-right"></td></tr>';
		table += '<tr class="fb-middle"><td class="fb-left"></td><td class="fb-center"><div id="fb-content"></div></td><td class="fb-right"></td></tr>';
		table += '<tr class="fb-bottom"><td class="fb-left"></td><td class="fb-center"></td><td class="fb-right"></td></tr>';
		table += '</table>';
		return table;
	}
	
	FeedBack.prototype.BuildContent = function() {
		var content = '<a id="fb-mail">Написать письмо</a>';
		return content;
	}
	
	FeedBack.prototype.GetAjaxOptions = function(feedback,pars) {
		var options = {
			//url: 'index.php?id='+FB_Ajax,
			//url: 'index.php',
			url: 'ajax/dierafeedback.html',
			data: pars,
			type: 'post',
			dataType: 'html',
			timeout: 30000,
			cache: false,
			beforeSend: function(data) {
				//feedback.PrintContent('<div class="fb-loader"></div>')
				$('#fb-form-overlay').show();
			},
			success: function(data) {
				$('#fb-form-overlay').hide();
				feedback.PrintContent(data);
			},
			error: function(data) {
				$('#fb-form-overlay').hide();
				feedback.PrintContent('<div class="fb-text">Извините, произошла ошибка.</div>');
			}
		}
		return options;
	}
	
	FeedBack.prototype.SetAjaxEvents = function(feedback) {
		//$('#fb-mail').click(function() {
		//	var command = 'mail';
		//	feedback.Ajax(command);
		//});
		var pars = {id: FB_Ajax};
		var options = this.GetAjaxOptions(this, pars);
		$('#fb-form').ajaxForm(options);
		$('#fb-file').bind('change',function(event) {
			var value = $('#fb-file').val();
			if (value.length > 0) {
				$('#fb-fake-file').val(value.substring(0,30) + ((value.length < 31) ? '' : '...')).css('color','#000');
			}
		}).bind('mouseover',function(event) {
			var value = $('#fb-file').val();
			if (value.length > 0) {
				$('#fb-fake-file').val(value.substring(0,30) + ((value.length < 31) ? '' : '...')).css('color','#000');
			}
		});
		$('#fb-file').hover(function() {
			$(this).parent().find('.fb-attach').addClass('fb-attachHover');
		}, function() {
			$(this).parent().find('.fb-attach').removeClass('fb-attachHover');
		});
		$('#fb-department-options').bind('change',function(event){
			var key = $('#fb-department-options').val();
			var value = department[key];
			$('#fb-department').val(value).css('color','#000');
		});
		$('#fb-department-options').hover(function() {
			$('.fb-arrow').addClass('fb-arrowHover');
		}, function() {
			$('.fb-arrow').removeClass('fb-arrowHover');
		});
		$('#fb-department-options').focus(function() {
			$('.fb-arrow').addClass('fb-arrowFocus');
		});
		$('#fb-department-options').blur(function() {
			$('.fb-arrow').removeClass('fb-arrowFocus');
		}).change(function() {
			$('.fb-arrow').removeClass('fb-arrowFocus');
		});
	}
	
	FeedBack.prototype.PrintContent = function(content) {
		this.ClearContent();
		this.content.html(content);
		var recaptcha_id = 'fb-recaptcha';
		if (document.getElementById(recaptcha_id)) this.ShowRecaptcha();
		this.SetAjaxEvents(this);
	}
	
	FeedBack.prototype.DeleteAjaxEvents = function() {
		$('#fb-mail').unbind();
		$('#fb-form').ajaxFormUnbind();
		$('#fb-file').unbind();
		$('#fb-department-options').unbind();
	}
	
	FeedBack.prototype.ClearContent = function() {
		this.DeleteAjaxEvents();
		this.content.html();
	}
	
	FeedBack.prototype.Ajax = function(command) {
		var pars = {
			id: FB_Ajax,
			action: command
		};
		var options = this.GetAjaxOptions(this,pars);
		$.ajax(options);
	}
	
	FeedBack.prototype.Show = function() {
		if (this.isShow) return;
		$('#recaptcha_image').attr('id','no_recaptcha_image');
		$('#recaptcha_response_field').attr('id','no_recaptcha_response_field');
		$('#recaptcha_challenge_field').attr('id','no_recaptcha_challenge_field');
		$('#recaptcha_challenge_field_holder').attr('id','no_recaptcha_challenge_field_holder');
		//this.PrintContent(this.BuildContent());
		feedback.PrintContent('<div class="fb-loader"></div>');
		this.Ajax('mail');
		var window_height = $(window).height();
		var container_height = this.container.height();
		var container_top = Math.round((window_height - container_height) / 2);
		if (container_top < 0) container_top = 0;
		this.isShow = true;
		this.container.css('top',container_top);
		this.overlay.height($(document).height());
		this.overlay.show();
		this.container.show();
	}
	
	FeedBack.prototype.Hide = function() {
		this.container.hide();
		this.overlay.hide();
		this.isShow = false;
		$('#no_recaptcha_image').attr('id','recaptcha_image');
		$('#no_recaptcha_response_field').attr('id','recaptcha_response_field');
		$('#no_recaptcha_challenge_field').attr('id','recaptcha_challenge_field');
		$('#no_recaptcha_challenge_field_holder').attr('id','recaptcha_challenge_field_holder');
	}
	
	FeedBack.prototype.ShowRecaptcha = function() {
		var options = {
			theme: "custom"
		}
		Recaptcha.create(FB_Recaptcha_Public_Key, null, options);
		$('#fb-captcha-reload').click(function(){
			Recaptcha.reload();
		});
	}
	
	feedback = new FeedBack();
	
})
