$.fn.extend({
	mmenu: function () {
		
		var $this = $(this),
			currentOl = null;
		
		var showOl = function () {
			
			if (currentOl) {
				currentOl.show().parent().addClass('expanded');				
			}
		}
	
		var hideOl = function () {
			if (currentOl) {
				currentOl.hide().parent().removeClass('expanded');
			}
		}
		
		$this
			.find('a').mouseover(function () {	
							
				hideOl();
			}).end().find('.expandable a').each(function (num, el) {
				currentOl = $(el).next('.dd');
				
				var olHd = currentOl.find('.hd'),
					hideTO,
					timeout = 0.5;
			
				olHd.width(currentOl.width() - $(el).width() - 2);
				
				currentOl
					.mouseover(function () {
						
						clearTimeout(hideTO);
						currentOl = $(el).next('.dd');
						showOl();
					})
					.mouseout(function () {
						hideTO = setTimeout(function () {
							currentOl = $(el).next('.dd');
							hideOl();
						}, timeout * 1000);
					});
				
				$(el)
					.mouseover(function () {
						clearTimeout(hideTO);
						currentOl = $(el).next('.dd');
						showOl();
					})
					.mouseout(function () {
						hideTO = setTimeout(function () {
							currentOl = $(el).next('.dd');
							hideOl();
						}, timeout * 1000);
					});

			});
		
	},
	contacts: function () {
		
		var $this = $(this);
		
		$this.find('.form input[type=text], .form textarea').focus(function () {
			
			var type = this.tagName.toLowerCase(),
				errClassName = type + '-error',
				parent = $(this).parent();
			
			if (parent.hasClass(errClassName)) {
				parent.removeClass(errClassName);
				this.value = '';
			}
			
		});
		
	},
	guestteasers: function (config) {
		
		var config = config || {},
			me = jQuery(this),
			viewport = jQuery(this).find('.viewport'),
			previous = jQuery(this).find('.nav .prev'),
			next = jQuery(this).find('.nav .next'),
			element = viewport.find('.items'),
			items = element.children(),
			numElmts = items.length,
			itemHeight = items.height(),
			shownInViewport = Math.round(viewport.height() / itemHeight),
			increment = 1,
			firstElementOnViewPort = 1,
			isAnimating = false;
		
		var scrollPrev = function () {
			
			if (isAnimating) {
				return false;
			}
			
			if (firstElementOnViewPort == 1) {
				return false;
			}
			
			if (firstElementOnViewPort - increment <= 0) {
				jQuery(element).css('top', "-" + numElmts * itemHeight + "px");
				firstElementOnViewPort = numElmts - (increment - 1);
			} else {
				firstElementOnViewPort -= increment;
			}
			
			jQuery(element).animate({
				top: "+=" + itemHeight * increment,
				y: 0,
				queue: true
			}, "swing", function() {isAnimating = false;});
			
			isAnimating = true;
			
			updateNav();
			
		}
		
		var scrollNext = function () {
			
			if (isAnimating) {
				return false;
			}
			
			if (firstElementOnViewPort + shownInViewport > numElmts) {
				return false;
			}
			
			if (firstElementOnViewPort > numElmts) {
				firstElementOnViewPort = 1 + increment;
				jQuery(element).css('top', "0px");
			} else {
				firstElementOnViewPort += increment;
			}
			
			jQuery(element).animate({
				top: "-=" + itemHeight * increment,
				y: 0,
				queue: true
			}, "swing", function() {isAnimating = false;});
			
			isAnimating = true;
			
			updateNav();
			
		}
		
		var updateNav = function () {
			
			if (firstElementOnViewPort == 1) {
				previous.addClass('prev-disabled');
				next.removeClass('next-disabled');
			} else if (1 < firstElementOnViewPort && firstElementOnViewPort + shownInViewport <= numElmts) {
				previous.removeClass('prev-disabled');
				next.removeClass('next-disabled');					
			} else {
				previous.removeClass('prev-disabled');
				next.addClass('next-disabled');										
			}
			
		}
		
		if (numElmts <= shownInViewport) {
			next.hide()
			previous.hide()
		}
		
		previous.click(function(event){
			event.preventDefault();
			scrollPrev()
		});
		
		next.click(function(event){
			event.preventDefault();
			scrollNext();
		});
		
		return {
			scrollNext: function () {
				scrollNext();
			},
			scrollPrev: function () {
				scrollPrev();
			}
		}
		
	},
	seoblock: function () {
		
		var me = jQuery(this),
			link = me.find('.toggle a'),
			text = me.find('.text'),
			visible = false;
		
		link.click(function () {
			text.slideToggle();
			link.text((link.text() == "Развернуть") ? "Свернуть" : "Развернуть");
			link.parent().toggleClass('opened');
			visible = !visible;
		});
		
	}
});
