(function ($) {

/**
 * ErrorReporting
 * @constructor jQuery
 */
$.widget('ui.ErrorReporting', $.extend({}, $.ui.Component.prototype, {
    type: 'ErrorReporting',
    init: function () {
        var self = this;

        $('html').live('keydown', function (e) {
        	var code = e.keyCode ? e.keyCode : (e.which ? e.which : e.charCode);
        	if (code == 13 && e.ctrlKey) {
        		if (document.selection || document.getSelection || window.getSelection) {
        			var txt = '';
					if (document.getSelection) {
						txt = document.getSelection();
					} else if (document.selection) {
						txt = window.getSelection();
					} else if (document.selection) {
						txt = document.selection.createRange().text;
					} else if ('selectionStart' in e) {
						txt = e.value.substr(e.selectionStart, l);
					}
					if (txt.length) {
						self.reload({
				    		showPopup: 1,
				    		description: txt
				    	});
					} else {
	        			alert('Выделите текст с ошибкой, а затем нажмите Ctrl+Enter');
	        		}
        		}
        	}
        });
        
        $('#errorReportingForm select[name="type"]').live('change', function () {
        	if ($(this).val() == 'error reporting syntax') {
        		$('#ctrlEnterText').css('display', 'inline');
        	} else {
        		$('#ctrlEnterText').css('display', 'none');
        	}
        });
        
        $('a.errorReportingButton').live('click', function () {
        	self.reload({
        		showPopup: 1
        	});
        });
        
        $('#errorReportingForm .submitBtn').live('click', function (e) {
            self.reload({
            	email: $('#errorReportingForm input.textInput[name="email"]').val(),
            	name: $('#errorReportingForm input.textInput[name="name"]').val(),
            	description: $('#errorReportingForm textarea[name="description"]').val(),
            	type: $('#errorReportingForm select[name="type"]').val(),
            	url: location.href,
            	userAgent: navigator.userAgent,
            	errorReporting: 'errorReporting'
            });
            return false;
        });
		
		$('div.popup_container div.title a.close.reporting').live('click', function (e) {
			self.reload({
				isCloseWindow: 1
			});
            return false;
		});

		$('div.popup_container #errorReportedButton').live('click', function (e) {
			self.reload({
				isCloseWindow: 1
			});
            return false;
		});
    }
}));

})(jQuery);