//hello Kitty
if (typeof getSelTextWorked == 'undefined'){
	var getSelTextWorked=function(){
		var txt = '';
		if (window.getSelection!=undefined){
			txt = window.getSelection();
		}else if (document.getSelection!=undefined){
			txt = document.getSelection();
		}else if (document.selection!=undefined){
			txt = document.selection.createRange().text;
		}
		return txt;
	}
}

if (typeof ctrlEnter == 'undefined'){
	var ctrlEnter=function(e)
	{
	    if(((e.keyCode == 13) || (e.keyCode == 10)) && (e.ctrlKey == true)){
			var descr = getSelTextWorked();
		    var feedbackInit = {
		    	'type' : 'error',
		    	'link' : location.href
		    }
		    if (descr != ''){
		    	feedbackInit.messageText = '%ERROR%: "' + descr + '"';
		    }
		    
		    $('a.feedback').feedback(feedbackInit);
	    }
	}
}

function validate_email(str){
	var email_regular = new RegExp(/^([a-z0-9_\-\.]+)@[a-z0-9\-]+(\.[a-z0-9\-]+)*(\.[a-z]{2,3})$/i);
	return email_regular.test(str);
}

$(document).ready(function() {
    //$(document).keypress(function(event){ctrlEnter(event)});
	$('#headerDropdown').click(function(){
		$(this).find('ul').show();
		$(this).addClass('active');
	});
	$('#headerDropdown').mouseleave(function(){
		$(this).find('ul').hide();
		$(this).removeClass('active');
	});

	$('#tochka_header .navigationBar .barMenu').mouseenter(function(){
		$('#portal_sections').show().addClass('activePopUp');
	});
	$('#tochka_header .navigationBar .barMenu').mouseleave(function(){
		$('#portal_sections').hide().removeClass('activePopUp'); 
	});

	/* MOUSE OVER AND PRESSDOWN
	$('input.submitButton,div.dropdown,input.button,a.red,a.button,div.button span,span.button,a.minimizeButton,a.refresh,a.otherCityDropDown,span.dropdownMenuItem img').mouseover(function(){
		$(this).addClass('MouseOver');
	});
	$('input.submitButton,div.dropdown,input.button,a.red,a.button,div.button span,span.button,a.minimizeButton,a.refresh,a.otherCityDropDown,span.dropdownMenuItem img').mousedown(function(){
		$(this).addClass('MousePressDown');
	});
	$('input.submitButton,div.dropdown,input.button,a.red,a.button,div.button span,span.button,a.minimizeButton,a.refresh,a.otherCityDropDown,span.dropdownMenuItem img').mouseup(function(){
		$(this).removeClass('MousePressDown');
	});
	$('input.submitButton,div.dropdown,input.button,a.red,a.button,div.button span,span.button,a.minimizeButton,a.refresh,a.otherCityDropDown,span.dropdownMenuItem img').mouseleave(function(){
		$(this).removeClass('MouseOver');
		$(this).removeClass('MousePressDown');
	});
	*/

	/*$('div.select').find('input').click(function(){
		$(this).parent().find('ul').show();
	});*/

		$('#QuickSearchGadget div.dropdown ul').click(function(){
			if($(this).hasClass("disable_old_events")) {
				return true;
			}
			$(this).toggleClass('active');
			$(this).find('li').toggle().filter(':first').show();
			$(this).parents('li.gadget').css('z-index','310');
			$(this).parents('li.gadget').next().css('z-index','300');
			return false;
		});
		
		$('#QuickSearchGadget div.dropdown ul').mouseleave(function(){
			if(!$(this).parent().hasClass("links")) {  
				$(this).removeClass('active');
				$(this).find('li').hide();
				if(!$(this).hasClass("disable_old_events")) {
					$(this).find('li').filter(':first').show();
				} else {
					$(this).find('li').filter('.selected').show();
				}
			}
		});
		if (typeof $.fn.feedback == 'undefined'){
		// Feedback


		$.fn.feedback = function(options){
			var domain = typeof mainDomainHttp != 'undefined' ? mainDomainHttp : 'http://tochka.net/',
				activeTab = typeof options != 'undefined' && typeof options.type != 'undefined' ? 'activeTab='+options.type : '',
				messageText = typeof options != 'undefined' && typeof options.messageText != 'undefined' ? 'messageText='+options.messageText : '',
				link = typeof options != 'undefined' && typeof options.link != 'undefined' ? 'fback_location='+options.link : '';
			$.getScript(domain+'feedbackform/?' + activeTab + '&' + messageText + '&' + link,
				function(){
					if ($('div.feedback').children().length == 0){
						$('body').append(feedback_form.data);
					} else {
						$('div.feedback').replaceWith(feedback_form.data);
					}
					$('div.feedback').each(setFeedback);
				}
			);
		};
		var setFeedback = function(){
			var self = this;
			var con = $(self).children(".c");
			$(self).children(".opacity").height($(document).height());
			//	$(window).resize(function(){
			//		$(self).height($(document).height());
			//	});
			$(con).css({
				marginTop: function(){
					return parseInt($(con).height()/-2);
				},
				marginLeft: '-240px'
		  	});
			$('div.feedback').find("a.fback_close, input.fback_close").live('click',function(){
				$('div.feedback').remove();
			});
			$(self).find("ul.tabsLeft").tabs();
		};
		/* Automatically apply to any a.feedback */
		$('a.feedback').click(function(){
			$(this).feedback();
		});
		
		// Feedback		
			$('#fback_submit').live('click',function(){
				var fback_text = $('#fback_text').val(),
					fback_type = $('div.feedback').find('ul.tabsLeft').find('li.active').attr('id'),
					fback_email = $('#fback_email').val();
				if (fback_text.trim() == ''){
					alert(typeof feedback_validate_message != 'undefined' ? feedback_validate_message : 'Поле "Текст сообщения" не заполнено');
					return false;
				}
				if (fback_email.trim() == '' || !validate_email(fback_email.trim())){
					alert(typeof feedback_validate_email != 'undefined' ? feedback_validate_email : 'Поле Email не заполнено или заполнено некорректно');
					return false;
				}
				$.post('/feedbacksend', {
					'fback_email': fback_email,
					'fback_type': fback_type,
					'fback_text': fback_text,
					'fback_location': location.href
				}, function(data){
					$('div.feedback').replaceWith(data);
					$('div.feedback').find('div.c').css({
						marginTop: function(){
							return parseInt($(this).height()/-2);
						},
						marginLeft:  function(){
							return parseInt($(this).width()/-2);
						}
				  	});
				});
			});
		
		}
		

});

