$(document).ready(function(){

	//последний элемент меню верхнего уровня (у него особенности в стиле)
	$('ul.level_1 > li:last-child').addClass('last');
	
	//выпадающее меню (некоторые пункты не должны показываться)
	$("li.item").hover(function(){
		if ($(this).children("ul").hasClass("level_2") && !$(this).children("ul").hasClass("no_show") ){
			$(this).children("ul").fadeIn(200);
		}		
	},
	function(){
		$(this).children("ul").fadeOut(100);
	})
	
	//убираем дефолтное слово в окне поиска при фокусе
	def_text=$('input.search').val();
	
	$('input.search').focus(function(){
		if ($(this).val() == def_text) {	
			$(this).val('');			
		}	
	});
			
	$('input.search').blur(function(){
		if ($(this).val()==""){
			$(this).val(def_text);			
		}	
	});	
		
	//изменение размера изображения по заданной ширине и высоте контейнера
	
	function resize_img(obj,obj_frame){
		if ($(obj).length>0){
			$(window).load(function(){
				ww=$(obj_frame).width();
				//alert(ww);
				hh=$(obj_frame).height();
				//alert(hh);
				$(obj).each(function(ind){
					w=$(this).width();
					h=$(this).height();			
					if (w != ww){
						h=h*ww/w;
						w=ww;
					}
					if (h < hh){				
						w=w*hh/h;
						h=hh;				
						ml=(w-ww)/2;
						$(this).css('margin-left',-ml);				
					}
					else{				
						if (h > hh){
							mt=(h-hh)/2;
							$(this).css('margin-top',-mt);					
						}
					}				
					$(this).width(w);
					$(this).height(h);	
								
							
				})
			})
		}	
	}	
	
	resize_img('.slider_main_img','.block_img_text');
	resize_img('.slider_top','.head_right');
	resize_img('.img_last img','.img_last');
	
	//привязка кнопки к форме
	$('.div_button').click(function(){
		if ($(this).parents('form').length){
			$(this).parents('form').submit();
		}		
	});
	
	
	//скрипт для фотогалереи (классы для кнопок в навигации и кол-во изображений на странице)
	function nav_full_photo(){
		$('.photo_this').text(cur_photo + 1);
		$('.photo_all').text(len);
		
		if (cur_photo == 0){
			$('.photo_prev').addClass('disabled');
		}
		else{
			$('.photo_prev').removeClass('disabled');
		}
		
		if (cur_photo == (len -1) ){
			$('.photo_next').addClass('disabled');
		}
		else{
			$('.photo_next').removeClass('disabled');
		}		
	}
	
	
	if ($('.cont_photo_full').length){
		
		len = $('.photo_small').length;	
		
		//кэшируем изображения
		/*var imgs = new Array();	
		for(var i=0;i<len;i++)
		{
			imgs[imgs.length] = new Image();			
			imgs[imgs.length - 1].src = $('.photo_small a').eq(i).attr('type');
		}*/
		
		var cur_photo_str=$('.photo_small a.active').attr('id');	
		if (!cur_photo_str){cur_photo=0;}
		else{cur_photo=cur_photo_str.substring(6,cur_photo_str.length)-1;}	
		nav_full_photo();
		//изменение размера маленьких изображений
		resize_img('.photo_small img','.photo_small span');
		//изменение размера большой фотографии
		resize_img('.photo_inline img','.photo_inline');
		
		
		$('.photo_small').click(function(){
			if ($(this).hasClass('active')){				
			}
			else{				
				var new_cur_photo_str=$(this).attr('id');
				new_cur_photo=new_cur_photo_str.substring(6,new_cur_photo_str.length)-1;
				var prev_cur_photo=cur_photo;
				//var src=$(this).children("a").attr('type');
				var title=$(this).children("a").attr('title');				
				$('.title_photo .text').text(title);				
				$(this).addClass('active');
				$('.photo_small').eq(prev_cur_photo).removeClass('active');	
				
				//new_photo_str='<img style="display:none;" src="'+ src +'" alt="'+ title +'">';
				//$('.photo_inline').append(new_photo_str);				
				
				//$('.photo_size img:first-child').fadeOut(200,function(){$(this).remove();});
				//$('.photo_size img:last-child').fadeIn(200);
				$('.photo_size img').eq(new_cur_photo).fadeIn(500,function(){$(this).addClass('show')});
				$('.photo_size img').eq(prev_cur_photo).fadeOut(500,function(){$(this).removeClass('show')});
				
				cur_photo=new_cur_photo;
				nav_full_photo();
				//изменение размера большой фотографии
				//resize_img('.photo_inline img','.photo_inline');
			}
			
			return false;
		})
		
		$('.photo_full_nav .photo_nav').click(function(){
			if (!$(this).hasClass('disabled')){
				
				if ($(this).hasClass('photo_next')){
					var new_cur_photo=cur_photo + 1;
				}
				else{
					var new_cur_photo=cur_photo - 1;					
				}		
								
				var prev_cur_photo=cur_photo;
				var src=$('.photo_small').eq(new_cur_photo).children("a").attr('type');				
				var title=$('.photo_small').eq(new_cur_photo).children("a").attr('title');				
				$('.title_photo .text').text(title);				
				$('.photo_small').eq(new_cur_photo).addClass('active');
				$('.photo_small').eq(prev_cur_photo).removeClass('active');		
				
				
				//new_photo_str='<img src="'+ src +'" alt="'+ title +'">';
				//$('.photo_inline').prepend(new_photo_str);			
			
				//проявление (в случае двойного клика нужно удалять все фото, кроме первого)
				//$('.photo_size img:gt(0)').fadeOut(200,function(){$(this).remove();});
				//$('.photo_size img:first-child').fadeIn(200);
				
				$('.photo_size img').eq(new_cur_photo).fadeIn(500,function(){$(this).addClass('show')});
				$('.photo_size img').eq(prev_cur_photo).fadeOut(500,function(){$(this).removeClass('show')});
				
				cur_photo=new_cur_photo;
				nav_full_photo();
				//изменение размера большой фотографии
				//resize_img('.photo_inline img','.photo_inline');
			}
			
		})
		
	}
	
	//для гостевой книги
	if ($('.guestbook').length){
		$('.theme_link').click(function(){
			$(this).next('.hide').fadeIn(600);			
			$(this).css('display','none');
		})
		$('.theme').click(function(){
			$(this).parent('.hide').fadeOut(200);			
			$(this).parent('.hide').prev('.theme_link').css('display','block');
		})
	}
	
		
})
