var Complaints = {
	/**
	 * Variables
	 */
	WinForm: {},
	/**
	 * Initialization
	 */
	Init: function() {
		Complaints.OtherReasonDescription = $('other_reason_description');
		Complaints.ComplaintsForm = $('complaint_form');
		Complaints.ComplaintReasonSelect = $('complaint_reason');
		/**
		 * Is there any topics?
		 */
		$$('.topic .voting a.minus').each(function(minus){
			minus.addEvent('click', Complaints.ShowComplaintWindow(/lsVote.vote\('(\d+)',this,-1,'topic'\); return false;/.exec(minus.get('onclick'))[1], 'topic'));
			minus.erase('onclick');
		});
		/**
		 * Is there any comments?
		 */
		$$('.comment .voting a.minus').each(function(minus){
			minus.addEvent('click', Complaints.ShowComplaintWindow(/lsVote.vote\((\d+),this,-1,'comment'\); return false;/.exec(minus.get('onclick'))[1], 'comment'));
			minus.erase('onclick');
		});
		/**
		 * Show textarea for describing users's own reason of complaint
		 */
		Complaints.ComplaintReasonSelect.addEvent('change', Complaints.ToggleOtherComplaintsField);
		/**
		 * Adding handler on sending form
		 */
		$('complaint_form').addEvent('submit', Complaints.SendMessage);
		Complaints.OtherReasonDescription.addEvent('focus', Complaints.HideDefaultTextAreaMessage);
		/**
		 * Adding title messages on minus anchors
		 */
		$$('.topic .voting a.minus').set('title', aComplaintsLang['complaints_add_complaint']);
		$$('.comment .voting a.minus').set('title', aComplaintsLang['complaints_add_complaint']);
	},
	ShowComplaintWindow: function(iObjectId, sObjectType) {
		return function(event) {
			event.stop();
			Complaints.ResetComplaintsForm();
			/**
			 * Creating window with complaint form
			 */
			Complaints.WinForm = new StickyWin.Modal({
					content: $('complaint_form_div'),
					closeClassName: 'close-block',
					useIframeShim: false,
					relativeTo: $(this),
					offset: {
						x: 300,
						y: -50
					}
			});
			Complaints.WinForm.hide();
			Complaints.WinForm.show();
			/**
			 * Saving actual information about complaint origin
			 */
			$('complaint_form').store('object_id', iObjectId);
			$('complaint_form').store('origin_type', sObjectType);
		};
	},
	ShowCommentWindow: function(event) {
		event.stop();
		Complaints.WinForm.show();
	},
	ToggleOtherComplaintsField: function() {
		Complaints.OtherReasonDescription.setStyle('display', (this.options[this.selectedIndex].value == 'other') ? 'block' : 'none');
		if (this.options[this.selectedIndex].value == 'other' && Complaints.OtherReasonDescription.value == '') {
			Complaints.OtherReasonDescription.set('value', aComplaintsLang['complaints_describe_reason_here']);
			Complaints.OtherReasonDescription.setStyle('color', 'grey');
		}
	},
	HideDefaultTextAreaMessage: function() {
		if ($(this).value == aComplaintsLang['complaints_describe_reason_here']) {
			$(this).set('value', '');
			$(this).setStyle('color', 'black');
		}
	},
	ResetComplaintsForm: function() {
		Complaints.OtherReasonDescription.set('value', aComplaintsLang['complaints_describe_reason_here']);
		Complaints.OtherReasonDescription.setStyle('color', 'grey');
		Complaints.OtherReasonDescription.setStyle('display', 'none');
		Complaints.ComplaintReasonSelect.selectedIndex = 0;
		
	},
	SendMessage: function(event) {
		event.stop();
		$('submit_complaint').set('disabled', true);
		sComplaintText = (Complaints.ComplaintReasonSelect.options[Complaints.ComplaintReasonSelect.selectedIndex].value == 'other') 
				? Complaints.OtherReasonDescription.value 
				: Complaints.ComplaintReasonSelect.options[Complaints.ComplaintReasonSelect.selectedIndex].get('text');
		if (sComplaintText.length < 1 || sComplaintText == aComplaintsLang['complaints_describe_reason_here']) {
			msgErrorBox.alert(aComplaintsLang['error'], aComplaintsLang['complaints_no_text_in_complaint']);
			$('submit_complaint').set('disabled', false);
			return;
		}
		if (sComplaintText.length > 250) {
			msgErrorBox.alert(aComplaintsLang['error'], aComplaintsLang['complaints_text_is_too_large']);
			$('submit_complaint').set('disabled', false);
			return;
		}
		JsHttpRequest.query(
			'POST '+aRouter['complaints'] + 'ajaxcomplaint',
			{ object_id: $('complaint_form').retrieve('object_id'), origin_type: $('complaint_form').retrieve('origin_type'), 
				complaint_text: sComplaintText, security_ls_key: LIVESTREET_SECURITY_KEY },
			function(result, errors) {
				if (!result) {
					msgErrorBox.alert(aComplaintsLang['error'],'Please try again later');
				}
				if (result.bStateError) {
					msgErrorBox.alert(result.sMsgTitle,result.sMsg);
				} else {
					msgNoticeBox.alert(result.sMsgTitle,result.sMsg);
				}
				Complaints.WinForm.hide();
				Complaints.ResetComplaintsForm();
				$('submit_complaint').set('disabled', false);
			},
			true
		);
	}
};
window.addEvent('domready', Complaints.Init);

