function addHandler(object, event, handler, useCapture) {
	 if (object.addEventListener) {
	object.addEventListener(event, handler, useCapture ? useCapture : false);
	 } else if (object.attachEvent) {
	object.attachEvent('on' + event, handler);
	 } else alert("Add handler is not supported");
	 }
	// Добавляем обработчики
	 /* Gecko */
	addHandler(window, 'DOMMouseScroll', wheel);
	/* Opera */
	addHandler(window, 'mousewheel', wheel);
	/* IE */
	addHandler(document, 'mousewheel', wheel);
	// Обработчик события
	function wheel(event) {
		
	 var delta; // Направление скролла
	 // -1 – скролл вниз
	 // 1 – скролл вверх
	event = event || window.event;
	
	if(event && event.type!="mousewheel") return false;
	// Opera и IE работают со свойством wheelDelta
	if (event && event.wheelDelta) {
	delta = event.wheelDelta / 120;
	// В Опере значение wheelDelta такое же, но с противоположным знаком
	// if (window.opera) delta = -delta;
	// В реализации Gecko получим свойство detail
	}else if (event && event.detail){
		
		delta = -event.detail / 3;
	}
	 
	// Запрещаем обработку события браузером по умолчанию
	//if (event.preventDefault) event.preventDefault();
	//event.returnValue = false;
	 return delta;
	}

	$(document).ready(function(){
		//$('#select_date').datepicker();
		//$('#show_calendar').click(function(){
		//	$('#select_date').datepicker("show");
		//});
		
		$('.scroll-arrow').jScrollPane({showArrows: true});
		$('.scroll-simple').jScrollPane({verticalDragMaxHeight:30});
		$('#open_map').prettyPhoto({animation_speed:'slow',theme:'light_square',navigation:false,autoplay_slideshow: false, autoplay: false, social_tools: false, deeplinking: false});
		$('#svet .container').scrollable({horizontal:true,size:4,speed:500,scrollwheel:false});
		
		var now_scroll_pos=0;
		$(document).scroll(function(){
			now_scroll_pos=$(document).scrollTop();
		});
		

		var mouse_now_down=false;
		$('.jspDrag').mousedown(function(){
			mouse_now_down=true;
		});
		
		$(document).mouseup(function(){
			mouse_now_down=false;	
		});
		
		
		$('.scroll-simple, .scroll-arrow').scroll(function(){
				
				if(wheel()=='1')
					$(document).scrollTop($(document).scrollTop()-100);
				else if(wheel()=='-1')
					$(document).scrollTop($(document).scrollTop()+100);
		});
		

		
		selected_text=($('#city option:selected').length)?$('#city option:selected').text():$('#city option:first').text();
		$('#city').before('<div id="styled_city"><div class="cont"><span class="select">'+selected_text+'</span></div><div class="items"></div></div>');
		
		$('#city option').each(function(){
			$('#styled_city .items').append('<a href="'+$(this).val()+'" rel="'+$(this).val()+'">'+$(this).text()+'</a>');
		});
		
		$('#styled_city .cont').click(function(e){
			$('#styled_city .items').slideToggle();
			e.stopPropagation();	
		});
		
		$('body').click(function(){
			$('#styled_city .items').slideUp('slow');	
		});
		
		$('#styled_city .items a').click(function(){
			$('#styled_city .select').text($(this).text());
			$('#city option[value="'+$(this).attr('rel')+'"]').attr('selected',true);	
		});
		
		$("#search_text").autocomplete({
			source: "/ajax/search_suggest/",
			minLength: 1
		});
		
		$('.menu_select .current').click(function(){
			$(this).parent().children('.select_list').slideToggle();			
		});
		
		$('.menu_select').mouseleave(function(){
			$('.menu_select .select_list').slideUp();
		});
		
  		$('.mini_gall div a').click(function(){
        	$('#full_view_'+$(this).attr('rel')).click();
    	});
		
		$('.linkjs').click(function(){
			$('#'+$(this).attr('rel')).click();	
		});
		
		$(".full_gall a[rel^='prettyPhoto']").prettyPhoto({default_width:800,animation_speed:'slow',theme:'light_square',slideshow:3000, autoplay_slideshow: false, autoplay: false, social_tools: false, deeplinking: false});
		$("#blitz a[rel^='prettyPhoto']").prettyPhoto({animation_speed:'slow',theme:'light_square',navigation:false,autoplay_slideshow: false, autoplay: false, social_tools: false, deeplinking: false});
		$('#send_quest_link').prettyPhoto({animation_speed:'slow',theme:'light_square',navigation:false,autoplay_slideshow: false, autoplay: false, social_tools: false, deeplinking: false});
		
		$('.show_on_main').click(function(){
			$('.show_on_main').removeClass('selected');
			$(this).addClass('selected');
			
			$('#fotorep_main').html('<img src="'+$(this).attr('rel')+'" alt="" style="display:none" />');
			loading('#fotorep_main');
			var img=$('#fotorep_main').find('img');
			img.load(function(){
				img.fadeIn('slow');
				unloading('#fotorep_main');					
			});
			
		});
		
			
		
		var i=0;
		$('.content img').each(function(){
			var clear=$(this).css('float'),
				margin='',
				full_size=false,
				img_title=false;
				++i;
			
			if(clear=='left') margin='margin-right:10px;float:'+clear;
			if(clear=='right') margin='margin-left:10px;float:'+clear;
			
			//$(this).css('max-width','560');
			
			var img_html='<img src="'+$(this).attr('src')+'" alt="'+$(this).attr('alt')+'" style="'+$(this).attr('style')+'" />';
		    
		    if($(this).attr('title') && $(this).width()>=560){
		    	img_html='<a class="zoom" href="#zoom" rel="'+i+'"></a><a id="show_full_'+i+'" href="'+$(this).attr('title')+'">'+img_html+'</a>';
				full_size=true;	
	    	}
	    	else if($(this).width()>=560){
		    	img_html='<a class="zoom" href="#zoom" rel="'+i+'"></a><a id="show_full_'+i+'" href="'+$(this).attr('src')+'">'+img_html+'</a>';
				full_size=true;	
	    	}
			
			if($(this).attr('alt')){
				img_html='<div style="'+margin+'">'+img_html+'<div class="img_alt">'+$(this).attr('alt')+'</div>'+'</div>';
				img_title=true;
			}
			
			if(img_title || full_size){
   				$(this).replaceWith(img_html);	
   			}
   			
			
   			
   			if(full_size){
   				$("#show_full_"+i).prettyPhoto({default_width:800,animation_speed:'slow',theme:'light_square',slideshow:3000, autoplay_slideshow: false, autoplay: false, social_tools: false, deeplinking: false});
   				$("#show_full_"+i).parent().children('.zoom[rel="'+i+'"]').click(function(){
   					$("a#show_full_"+i).click();
   				});
		    }
		    
		});
		
		$('.content blockquote').each(function(){
			$(this).html('<div class="blockquote">'+$(this).html()+'</div>');		
		});
		
/*photo*/
	var last_scroll_position=0;

	$('#photo_day').hover(function(){
		$(this).children('.desc').show();
	},function(){
		$(this).children('.desc').hide();	
	});
	
	function close_lightbox(){
		$(document).scrollTop(last_scroll_position);
		$('.lightbox_back').fadeOut('slow');
		$('.lightbox').fadeOut('slow');		
	}
	
	function open_lightbox(url,param){
			last_scroll_position=$(document).scrollTop();
			$(document).scrollTop(0);
			
			$('.lightbox_back').fadeIn('slow');
			$('.lightbox').show('scale');
			loading('.lightbox');
			$.ajax({
				url: url,
				data: param,
				dataType: 'html',
				success: function(data) {
			    	$('.lightbox').html('<a href="#close" class="close"></a>'+data);
			    	
    				$('.lightbox .close').click(function(){
						close_lightbox();
					});
				}
			});
	}
	
	$(document).keyup(function(e){
		if(e.keyCode==27)
        	close_lightbox();
    });
	
	
	$('body').append('<div class="lightbox_back"></div><div class="lightbox"></div>');
	$('.lightbox_back').click(function(){close_lightbox();});
	$('#photo_day .link a').click(function(){
		open_lightbox('/ajax/photo/','id='+$(this).attr('rel'));
	});
	$('#photo_day_pre ul a').click(function(){
		open_lightbox('/ajax/photo/','id='+$(this).attr('rel'));
	});
	
	$('#photo_archive a').click(function(){
		open_lightbox('/ajax/photo/','id='+$(this).attr('rel'));
	});
/*!photo*/
		
    /*scroll_block*/    
    $('.scroll_block').each(function(){
       $(this).html('<span>'+$(this).html()+'</span>');
       $(this).css({'display':'block','overflow':'hidden'});
       var scroll_text=$(this).children('span');
       scroll_text.css({'white-space':'nowrap','position':'relative','width':'auto'});
       scroll_text.bind('mouseenter', function() {
                        var text_width   =   $(this).width(),
                            elem_width   =   $(this).parent().width(),
                            elem_scroll  =   $(this),
                            scroll_speed =   25,
							move_to;		 
                            if(text_width > elem_width) {
								move_to =	elem_width-text_width;
                              	$(elem_scroll).animate(
                                    {left: move_to},
                                    {
										duration : scroll_speed * text_width,
                                        easing   : "linear",
	                                	complete : function(){
	                                    	$(elem_scroll).animate(
	                                          {left:0},
	                                          {
												duration : scroll_speed * text_width,
                                                easing   : "linear",
		                                        complete : function(){ $(elem_scroll).mouseover(); }
	                                          }
	                                      	);
										}
                                    }
                                )
                            }
                    });
                    
       scroll_text.bind('mouseleave', function(){
            $(this).stop();
            $(this).css("left",0);  
       });
       
    });
    
    /*
    $('.mesta_view').mouseover(function(){
   		$(this).parent().children('.mesta_preview').fadeIn();
    });
    
    $('.mesta_view').mouseleave(function(){
    	$(this).parent().children('.mesta_preview').hide();
    });
    */
    
	$('.mesta_view').hover(
	function(){
		$(this).parent().children('.mesta_preview').show();
	},
	function(){
		$(this).parent().children('.mesta_preview').hide();
	});
	

    
    /*!scroll_block*/ 
		
		/*ajax-links*/
		/*
		function set_ajax(selector){
			$(selector).click(function(e){
				if($(this).attr('href').match(/\/ajax\/(\w+)(.*)/g))
				{
					$.ajax({
						url: $(this).attr('href'),
						success: function(data){
							var insert_into=$(this).attr('rel');
							$(insert_into).html(data);
						} 
					});
					e.preventDefault();
				}	
			});
		}
		
		set_ajax('a');
		*/
	});
	

/*functions*/
function unloading(elem){
	$(elem).children('.load').remove();
}
		
function loading(elem){
	unloading(elem);
	$(elem).prepend('<div class="load"><div class="loader"></div></div>');
	$(elem).children('.load').css({'width':$(elem).width(),'height':$(elem).height()}).children('.loader').css({'width':$(elem).width(),'height':$(elem).height()});
}

function nl2br(str){
	return str.replace(/([^>])\n/g, '$1<br/>');
}

