var levelMenuSwitcher = function(){
(function($){
	$('div.levelMenu ul').each(function(){
		var there = this;
		var active = $('li.active', there);
		if( active.length ){
			$(active).prev('li:first').each(function(){
				$(this).append('<div class="active-bottom"></div>');
			});
			$(active).next('li:first').each(function(){
				$(this).append('<div class="active-top"></div>');
			});
		}
	});
	$('div.mainMenu ul').each(function(){
		var there = this;
		var active = $('li.active', there);
		if( active.length ){
			$(active).prev('li:first').each(function(){
				$(this).append('<div class="active-bottom"></div>');
			});
			$(active).next('li:first').each(function(){
				$(this).append('<div class="active-top"></div>');
			});
			if( $(active).hasClass('first') ){
				$(active).append('<div class="active-first"></div>');
			}
			if( $(active).hasClass('last') ){
				$(active).append('<div class="active-last"></div>');
			}
		}
	});
	var pnStack = [];
	var parentPN = undefined;
	$('form.styleForm').each(function(){
		var there = this;
		pnStack.push( $('.pnRow', there) );
		$('.pnRow', there).each(function(){
			var there = this;
			parentPN = there;
			var onfocus = function(){
				var empted = 0;
				var there = $(this).parents('.pnRow:first');
				for( var i = 0; i < pnStack.length; i++ ){
					if( typeof pnStack[i] != 'undefined' ){
						if( $('.input1 input', pnStack[i]).val() == '' && $('.input2 input', pnStack[i]).val() == '' ){
							++empted;
						}
					}
				}
				if( empted <= 1 && ( $('.input1 input', there).val() == '' && $('.input2 input', there).val() == '' ) ){
					var clone = $(parentPN).clone();
					$('input', clone).focus(onfocus).blur(onblur).val('');
					$('#pnWrapper').append( clone );
					pnStack.push( clone );
				}
			}
			var onblur = function(){
				var empted = 0;
				var emptedId = false;
				for( var i = 0; i < pnStack.length; i++ ){
					if( typeof pnStack[i] != 'undefined' ){
						if( $('.input1 input', pnStack[i]).val() == '' && $('.input2 input', pnStack[i]).val() == '' ){
							++empted;
							emptedId = i;
						}
					}
				}
				if( empted > 1 ){
					for( var i = 0; i < pnStack.length; i++ ){
						if( typeof pnStack[i] != 'undefined' ){
							if( $('.input1 input', pnStack[i]).val() == '' && $('.input2 input', pnStack[i]).val() == '' && i != emptedId ){
								$( pnStack[i] ).remove();
								delete pnStack[i];
							}
						}
					}
				}
			}
			$('input', there).focus(onfocus).blur(onblur);
		});
	});
})(jQuery)
}
catalogMenuActive = function(){
	(function($){
		var parent = $('div.catalog-levelMenu');
		var arrows = {};
		arrows.parent = $('.clm-arrows', parent);
		arrows.left = $('.clm-a-left', parent);
		arrows.right = $('.clm-a-right', parent);
		var list = {};
		list.parent = $('.clm-list', parent);
		list.table = $('table', list.parent);
		var pos = {};
		pos.left = 0;
		pos.index = 0;
		pos.items = [];
		pos.wndCount = 0;
		var clickTo = function(dir){
			var val = {};
			val.index = pos.index;
			val.left = 0;
			switch(dir){
			case 'left':
				val.index--;
			break;
			case 'right':
				val.index++;
			break;
			default:
				return false;
			}
			if( val.index > pos.items.length - pos.wndCount ) val.index = pos.items.length - pos.wndCount;
			if( val.index < 0 ) val.index = 0;
			pos.index = val.index;
			for( var i = 0; i < val.index; ++i ){
				val.left += $(pos.items[i]).width();
			}
			
			list.table.animate({'marginLeft':(val.left * -1)+'px'}, {duration:300, queue:false});
		}
		if( list.parent.width() < list.table.width() ){
			arrows.parent.removeClass('hidden');
			pos.items = list.table.find('td');
			pos.wndCount = Math.round( list.parent.width() / $(pos.items[0]).width() );
			arrows.left.click(function(){
				clickTo('left');
			});
			arrows.right.click(function(){
				clickTo('right');
			});
			for( var i = 0; i < pos.items.length; ++i ){
				if( $(pos.items[i]).hasClass('active') ) pos.index = i;
			}
			if( pos.index >= pos.wndCount ){
				clickTo('left');
			}
		}
	})(jQuery)
}
jQuery(document).ready(function(){
	levelMenuSwitcher();
	catalogMenuActive();
});
