$(document).ready(function(){
	selects('.customSelectWrap .assyst_menu');	
	rememberLastLayer();

	$('.pager').each(function() {
		if ($(this).children().length === 1) {
			$(this).hide();
		}
	});	
		
	$('.slided').hide();
	
	$('.pressToSlide').bind('click', function() {
		$(this).next('.slided').slideToggle();
	});

});

function rememberLastLayer() {
	$('.newstyle form').each(function() {
		var form = $(this);
		$(form).bind('submit', function(){
			var lastLayer = document.location.hash;
			setCookie('lastLayer', lastLayer);
		});	
	});	
}

function selects(givenClass)  {
	var elt = givenClass + ' ul li';

	$('.thing').each(function(i) {
		$(this).hide();
		var thingId = 'elt' + i;
		$(this).attr('id', thingId);
	});

	$(elt).each(function(i) {
		var eltName = 'elt' + i;
		$(this).attr('name', eltName);
	});

	var myHash = document.location.hash;
	
	if (givenClass === '.lpp_menu') {
		var ifredirected = getCookie('lastLayer');
		if (ifredirected !== null) ifredirected = ifredirected.substring(3);
		if (ifredirected !== '' && ifredirected !== null && myHash === '') {
			myHash = '#' + ifredirected;
		}
		setCookie('lastLayer', '');
		//ifredirected = '';
	}



 var selects = $('div.customSelectWrap');
        selects.each(function(){
			var self = $(this), visible, hidden;
			visible =  self.find('.customSelectView');
			hidden =  self.find('.customSelectHidden');
			self.toggle(function(){
				hidden.show();
				return false;
			}, function(){
				hidden.hide();
				return false;
			});
			hidden.find('li').click(function(){ 																			   
				visible.find('input:hidden').val($(this).attr('data-value'));									 
				var str = $(this).text();
				visible.find('input:visible').val(str);
			}) 
        });


	
	if (myHash !== '') {
		$(myHash).css('display', 'block');
		var cnt = myHash.replace('#','');
		$(elt+'[name='+cnt+']').addClass('active');	
	} else {
		var firstli = $(elt)[0];
		$(firstli).addClass('active');
		var firstThing = $('.thing')[0];
		$(firstThing).css('display', 'block');
		myHash = '#' + $(firstThing).attr('id');
	}
	
	//document.location.hash = myHash;
	
	//var arr = [0];

	$(elt).each(function(i) {
		$(this).find('span').css('border-bottom', '1px dashed #333');
		if ($(this).hasClass('active')) $(this).find('span').css('border-bottom', 'none');
		$(this).bind("click", function() {
			if (!$(this).hasClass('active')) {
				
				var anchorName = $(this).attr('name');
				document.location.hash = anchorName;
					
				$(elt).removeClass('active');
				$(elt).find('span').css('border-bottom', '1px dashed #333');
					
				$('.thing').hide();
				
				$(this).addClass('active');
				$(this).find('span').css('border', 'none');
				 
				$('.thing[id=' + $(this).attr('name') + ']').toggle();
				if (!jQuery.support.cssFloat) { //проверка на браузер, можно выставить любое свойство
					$(document).scrollTop(0);
				}
			}
		});
			
		$(this).bind("mouseover",function() {
			if (!$(this).hasClass('active')) {
				$(this).css('cursor', 'pointer');
				$(this).find('span').css('border-bottom', 'none');
			}
		});
		
		$(this).bind("mouseout",function() {
			if (!$(this).hasClass('active')) {
				$(this).css('cursor', '');
				$(this).find('span').css('border-bottom', '1px dashed #333');
			}
		});  
	});
}

