(function ($){
	jQuery.fn.popup = function (options){
		
		var options = $.extend({
			width: 400,
			height: 400,
			title:''
		}, options);

		
		$(this).click(function(event){
			event.preventDefault();
			var $link = $(this),
				$popup = $('<div />').addClass('popup'),
				$overlay = $('<div />').addClass('overlay'),
				$popupContent = $('<div />').addClass('popup-content').appendTo($popup),
				$close = $('<a />').addClass('close-popup').attr('href', 'javascript:void(0)').html('<span>Закрыть</span>').appendTo($popupContent),
				$title = $('<div />').addClass('title').html('<span>'+options.title+'</span>').appendTo($popupContent),
				$iframe = $('<iframe />').appendTo($popupContent),
				$document = $(document), $window = $(window),
				ws, wh,	ph, pw,
				url = $link.attr('href');
				
			ws = $window.scrollTop();
			wh = $window.height();				
			$iframe.attr({
				src: url,
				frameborder: 0,
				scrolling : "no"
			}).width(options.width).height(options.height);	
				
			$popup
		      	.hide()
		      	.appendTo('body')
		      	.fadeIn(200);
			pw = $popup.outerWidth();
			ph = $popup.outerHeight();
			
	      	$popup.css({
	      		marginLeft : - pw / 2,
	      		top: ws + (ph && (wh - 50 > ph) ? (wh - ph)/2 : 25) 
	      	});

		        
			$overlay
				.hide()
		      	.appendTo('body')
		      	.width($document.width()).height($document.height())
		      	.fadeIn(200);
		      
		    $overlay.add($close).click(function (){
		    	$popup.fadeOut(200, function (){$popup.remove()});
		    	$overlay.fadeOut(200, function (){$popup.remove()});
		    });
			      
				
		});
		
		return this
		
	};
	
	$(function (){
		var $orderLinks = $('a.ortder-link');
		if ($orderLinks.length){
			$orderLinks.popup({
				width: 420,
				height: 450,
				title: 'Заказ продукции'
			});
		}
		$('a.question-form').popup({
			width: 420,
			height: 480,
			title: 'Задать вопрос'
		});

		$orderLinks = $('a.ortder2-link');
		if ($orderLinks.length){
			$orderLinks.popup({
				width: 420,
				height: 450,
				title: 'Заказ продукции'
			});
		}
		$('a.question-form').popup({
			width: 420,
			height: 480,
			title: 'Задать вопрос'
		});
	
		var $frontcats = $("#front-cats"),
			$manufs  = $("#logos").find(' > a');
		if ($frontcats.length){
			$frontcats.find('.item').each(function (){
				var $item = $(this),
					$link = $item.find('a.title'),
					itemsids = $link.attr('rel'), i,
					$highlightItems;
				if (itemsids){
					itemsids = itemsids.split(',');
					
					$highlightItems = $manufs.filter(function (index){
						var $item = $(this),
							link = $item.attr('href'), id, match;
						match = link.match(/filters_equal\[13\]=(\d+)/i);
						id = match[1];
						return $.inArray(id, itemsids) != -1;
					});
					$item.data('items', $highlightItems);
				}
			}).hover(function (){
				var $items = $(this).data('items');
				if ($items)	$items.addClass('highlight');
			}, function (){
				var $items = $(this).data('items');
				if ($items) $items.removeClass('highlight');
			});
			
		}
		
		
	});
})(jQuery);
