$(document).ready(function(){
	
	/*
	// заставляем работать hover в IE6, укажите через запятую классы у которых не работает hover
	var classes = ['someclass','someotherclass']; 
	// при наведении мыши на объект с классом someclass или someotherclass
	// к объекту будет насильно применяться класс someclass_hover или someotherclass_hover соответственно
	
	if($.browser.msie)
	for(c in classes)
	$('.'+classes[c]).each(function(){
		var cl = classes[c];
		$(this).bind('mouseover',function(){
			$(this).addClass(cl+'_hover');
		}).bind('mouseout',function(){
			$(this).removeClass(cl+'_hover');
		})
	});
	*/

	//initCollapseParagraphs();
	
	initCatMenuImagesRolover();
	
	preuploadCatMenuImages();
	
	initCatMenuClick();
	
	setTimeout('initLoginForm()',1000);
	
	initSearchForm();
	
});

function initCollapseParagraphs() {
	$('div.collapse').each(function(){
		$(this).children().not('.first-child').wrapAll('<span class="collapser"></span>');
		//alert(this.innerHTML);
		$('span.collapser',$(this)).hide();
		$('.first-child',$(this)).bind('click',function(){
			
			if ($('span.collapser:visible',$(this.parentNode)).length) {
				$('span.collapser',$(this.parentNode)).slideUp('fast',function(){
					$(this).removeClass('collapser-active');
				});
				$(this).removeClass('first-child-active');

			}
			else {
				$('span.collapser',$(this.parentNode)).slideDown('fast',function(){
					$(this).addClass('collapser-active');
				});
				$(this).addClass('first-child-active');
			}
			
			
		})
	});
}


function initCatMenuImagesRolover() {
	
	$('a.jscatmenuroloverlink').each(function(){
		var oldimage = $(this).parents('div.nav').children('img.jscatmenuimage')[0];
		var id = $(this).attr('id');
		id = id.substring(11);
		//alert(id);
		$(this).unbind('mouseover').bind('mouseover',function(){
			catMenuImagesRolover(oldimage,catmenuimages[id]);
		});
	});
	
}

function preuploadCatMenuImages() {
	if(typeof(catmenuimage)=='undefined') return;
	
	var img = [];
	for(i in catmenuimages) {
		img[i] = Image();
		img[i].src = catmenuimages[i];
	}
}

function catMenuImagesRolover(oldimg,newimgsrc) {
	/*
	var newimg = $(oldimg).clone();
	$(newimg).attr('src',newimgsrc);
	*/
	$(oldimg).attr('src',newimgsrc);
}

function initCatMenuClick() {
	$('a.jscatmenureloadlink').each(function(){
		var parent = $(this).parents('div.nav')[0];
		var id = $(this).attr('id');
		id = id.substring(11);
		$(this).unbind('click').bind('click',function(){
			reloadCatMenu(parent,id);
			return false;
		});
	});
	
	$('select.jscatmenuselect').attr('onchange','');
	
	$('select.jscatmenuselect').each(function(){
		var parent = $(this).parents('div.nav')[0];
		$(this).unbind('change').bind('change',function(){
			var option = $('option[value='+this.value+']',$(this));
			if(!$(option).hasClass('jscatmenureloadlink')) {
				document.location = this.value;
				return;
			}
			var id = option.attr('id');
			id = id.substring(13);
			reloadCatMenu(parent,id);
			return false;
		});
	});
	
}

function reloadCatMenu(container,page_id) {
	var url = '__ajax/catmenu/';
	//alert(container);
	//alert(page_id);
	$(container).fadeTo('fast',0.1);
	
	$.get(url,{'pid':page_id},function(data){
		//alert(data);
		$(container).html(data);
		$(container).fadeTo('fast',1);
		initCatMenuImagesRolover();
		initCatMenuClick();
	});
	
}


function initLoginForm() {
	
	$('input[name=login]').each(function(){
		if(this.value=='') this.value = 'логин:';
		
		$(this).bind('focus',function(){
			if(this.value=='логин:') this.value = '';
		});
		$(this).bind('blur',function(){
			if(this.value=='') this.value = 'логин:';
		});
		
	});
	
	$('input[name=password]').each(function(){
		if(this.value=='') {
			//$(this).attr('type','text');
			this.value = 'пароль:';
		}
		
		$(this).bind('focus',function(){
			if(this.value=='пароль:') this.value = '';
			//$(this).attr('type','password');
		});
		$(this).bind('blur',function(){
			if(this.value=='') {
				//$(this).attr('type','text');
				this.value = 'пароль:';
			}
			else {
				//$(this).attr('type','password');
			}
		});
	});
		
}

function initSearchForm() {
	
}
