function FeedbackPopupClass() {this.init();}
FeedbackPopupClass.prototype = new PopupWindow();
FeedbackPopupClass.prototype.constructor = FeedbackPopupClass;
FeedbackPopupClass.prototype.sent = false;
//FeedbackPopupClass.prototype.selector = $("#feedback_popup");
FeedbackPopupClass.prototype._show = function(event) {
	if (this.sent) {
		this.css({height: 'auto'});
		this.find('input[type=text], textarea').val('');
		this.find('.success').hide();
		this.find('.h1_book').show();
		this.find('.book_table').show();
		this.sent = false;
		this.show_submit_button();
	}
}

FeedbackPopupClass.prototype.init = function() {
	var self = this;
	this.current = null;
	this.__show = this.show;
	this.show = function(event) {
	    self._show(event);
	    self.__show(event);
	}
	
	$(document).ready(function() {
		this.window = $("#feedback_popup");
		this.window.find('div.notes div.hide').hide();
		this._init();
		this.ajax_animation(0);
		$('#feedback_book_open_link').hover(
			function() {$(this).addClass('jshover');},
			function() {$(this).removeClass('jshover');}
		).click(this.show.bind(this)).show();
		if ($.browser.msie) {
			this.window.find('input, textarea')
				.focus(function() {$(this).addClass('jsfocus');} )
				.blur(function() {$(this).removeClass('jsfocus');} );
		}
		this.find('input.emailfield')
			.focus(this.check_email_not_empty.bind(this))
			.keyup(this.check_email_not_empty.bind(this))
			.blur(this.check_email_not_empty.bind(this));
		this.check_email_not_empty();
		this.window.find('ul.post li a').click(this.select_theme.bind(this));
		this.window.find('form').submit(this.submit.bind(this));
	}.bind(this));
};

FeedbackPopupClass.prototype.ajax_animation = function(on) {
	on ? this.find('.ajaximg').show() : this.find('.ajaximg').hide();
};

FeedbackPopupClass.prototype.get_button = function() {
	return this.find('form button[type=submit]');
};

FeedbackPopupClass.prototype.check_email_not_empty = function() {
	if (this.find('.emailfield').val()) {
		this.find('.cant').css({visibility : 'hidden'});
	}
	else {
		this.find('.cant').css({visibility : 'visible'});
	}
};

FeedbackPopupClass.prototype.deselect_current = function() {
	if (this.current) {
		var li = this.window.find('ul.post > li.'+this.current[1]);
		li.attr('class', this.current[0]);
		li.find('a').removeClass('chosen');
		this.current = null;
	}
};

FeedbackPopupClass.prototype.select_theme = function(event) {
	event.preventDefault();
	var active = $(event.currentTarget).hasClass('chosen');
	this.deselect_current();
	var cl = $(event.target).closest('li').attr('class');
	if(active) {
		this.get_button().attr('class', 'default');
		this.get_button().html(this.find('ul.post li.default span').html());
		this.show_note('default');
	}
	else {
		var cl1 = cl + '1';
		$(event.target).closest('li').attr('class', cl1).find('a').addClass('chosen');
		this.show_note(cl);
		this.find('form input[name=theme]').val(cl);
		this.get_button().attr('class', cl).html($(event.currentTarget).find('span').html());
		this.current = [cl, cl1];
	}
};

FeedbackPopupClass.prototype.show_note = function(cl) {
	this.window.find('div.notes div:visible').hide();
	this.window.find('div.notes div.'+cl).show();
};

FeedbackPopupClass.prototype.show_submit_button = function() {
	this.get_button().removeAttr('disabled').show();
};
FeedbackPopupClass.prototype.hide_submit_button = function() {
	this.get_button().attr('disabled', 'disable').hide();	
};

FeedbackPopupClass.prototype.submit = function(event) {
	event.preventDefault();
	this.ajax_animation(1);
	this.hide_submit_button();
	
	$.ajax({
		type: 'POST',
		data: $(event.currentTarget).serialize(),
		context: this,
		url: js_options.feedback_url,
		dataType: 'json',
		success: function(json, textStatus) {
			this.ajax_animation(0);
			t = textStatus;
			if (textStatus == 'success') {
				if (typeof json == 'object') {
					if (json.status && json.status =='ok')
					{
						this.find('.book_table').hide();
						this.css({height: 'auto'});
						this.find('.success').show();
						this.find('.h1_book').hide();
						this.sent = true;
					}
					else
					{
						if (json.errors)
							alert(json.errors);
						else
							this.alert_failure();
						this.show_submit_button();
					}
				}
				else {
					this.alert_failure();
					this.show_submit_button();
				}
			}
			else {
				this.alert_failure();
				this.show_submit_button();
			}
		},
		error: function(response, textStatus, e) {
			this.ajax_animation(0);
			this.alert_failure();
			this.show_submit_button();
		}
	});
	
};

FeedbackPopupClass.prototype.alert_failure = function() {
	alert("Не получилось отправить сообщение. Попробуйте еще раз.");
};

var FeedbackPopup = new FeedbackPopupClass();
