/* scripts for myhome.ru
 *
 * Coded by Denis Arkhipov
 * vikhlun@gmail.com
 *
 * Version: 0.3
 * Date: Oct 04 2011
 */

if (!$.support.opacity) $('html').addClass('oldies');
$(function(){
	
	var ios = (navigator.userAgent.match(/like Mac OS X/i)) ? true : false;
	if (ios) $('html').addClass('ios');
	
	var touchDevice = ('ontouchstart' in document.documentElement) ? true : false;
	if (touchDevice) $('html').addClass('touch');
	
	var oldies = (!$.support.opacity) ? true : false;
	
	// placeholders for inputs
	$('.textInput-placeholder').each(function(){
		$(this).attr('data-placeholder',$(this).val());
		$(this).focusin(function(){
			if ($(this).val() == $(this).attr('data-placeholder')) {
				$(this).val('').removeClass('textInput-placeholder');
			};
		}).focusout(function(){
			if ($(this).val() == '') {
				$(this).val($(this).attr('data-placeholder')).addClass('textInput-placeholder');
			};
		});
	});
	
	/*фидбэк*/
	$('#feedback-handler').click(function(){
		showPopup('popup-feedback');
		return false;
	});
	/*----фидбэк*/
	
	/*поиск в шапке*/
	$('#header .where').click(function(){
		var w = $(this);
		var list = w.find('ul');
		if (!list.is(':visible')) {
			list.slideDown(100);
			$(document).mousedown(function(e){
				var target = $(e.target);
				if (target.hasClass('where') || target.parents('.where').length) {} else {
					w.find('span').trigger('click');
				};
			});
		} else {
			list.hide();
			$(document).unbind('mousedown');
		};
		list.find('li').click(function(){
			var item = $(this);
			w.find('input').val(item.attr('data-rel'));
			list.find('.active').removeClass('active');
			item.addClass('active');
			w.find('span').text(item.text());
		});
	});
	/*----поиск в шапке*/
	
	
	/*логин*/
	$('#header .button-login').click(function(){
		showPopup('popup-login');
		return false;
	});
	/*----логин*/

	/*показать/скрыть меню авторизованного пользователя*/
	$('#header .auth .user-menu').mouseenter(function(){
		var inner = $(this).parent().find('.user-menu-inner');
		inner.show();
	}).mouseleave(function(){
		var inner = $(this).parent().find('.user-menu-inner');
		inner.hide();
	});
	/*----меню авторизованного пользователя*/
	
	
	/* Исправил навешивание клика на Live */
	$('.user-photo .toggler').live('click', function(){
		$('.user-photo .user-photo-controls').slideToggle(100);
		return false;
	});

	/* Временно коментим согласие на правила
	$('.fpa-agreed input[type="checkbox"]').click(function(){
		if ($(this).is(':checked')) {
			$('.form-project-add .fpa-submit button').removeAttr('disabled').removeAttr('title').removeClass('button2-disabled');
		} else {
			$('.form-project-add .fpa-submit button').attr('disabled','disabled').attr('title', 'Чтобы продолжить, ознакомтесь с пользовательским соглашением.').addClass('button2-disabled');
		}
	});*/
	
	if ($('.sortby').length) {
		$('.pathBar .sortby').click(function(){
			var filterItem = $("#filter [name='sortby']");
			var w = $(this);
			var list = w.find('ul');
			if (!list.is(':visible')) {
				list.slideDown(100);
				$(document).mousedown(function(e){
					var target = $(e.target);
					if (target.hasClass('sortby') || target.parents('.sortby').length) {} else {
						w.find('span').trigger('click');
					};
				});
			} else {
				list.hide();
				$(document).unbind('mousedown');
			};
			list.find('li').click(function(){
				var item = $(this);
				//$("#filter [name='filter']").val(0);
				filterItem.val(item.attr('data-rel'));
				$("#filter").submit();
//				list.find('.active').removeClass('active');
//				item.addClass('active');
//				w.find('span').text(item.text());
			});
		});
	};
	var h = $(window).height();
	//alert(h);
	$(".wrapper-inside .content").css("min-height", h-350+"px");

	
	/*изменение типа помещения на странице добавления интерьера*/
	$('.fpa-space-edit').live('click', function(){
		var item = $(this).parents('.header');
		item.find('strong').hide();
		item.find('select').show();
		item.find('.fpa-space-edit').addClass('fpa-space-save').html('Сохранить');
		
		return false;
	});
	$('.fpa-space-save').live('click', function(){
		var item = $(this).parents('.header');
		item.find('strong').html( item.find('select option:selected').html() ).show();
		item.find('select').hide();
		item.find('.fpa-space-save').html('Изменить').removeClass('fpa-space-save');
	});
	/*----изменение типа помещения*/
	
	/* События для показа подсказок в форме создания идей */
	$('.form-project-add .show_hint').live('mouseenter', function(){
		$(this).prev('p.hint').show();
	}).live('mouseleave', function(){
		$(this).prev('p.hint').hide();
	});
	
	$('.fpa-submit .fpa-later').click(function(){
		$('form#idea-making-form-step-2')
		.append('<input type="hidden" name="later" value="yes">')
		.submit();
	});
	/*----События для показа подсказок в форме создания идей */
	
	// Скрытие успешного сохранения
	$(".good-title").fadeOut(3000);
	
	
	/*промоблок*/
	 if ($('.showcase').length) {
		(function(){
			var s = $('.showcase');
			var items = s.find('.item');
			var qnt = items.length,
				isAnimated = false,
				current = 0,
				timer;
				
			/**
			 * Запуск таймера на перелистывание рекламных 
			 * слоганов для дизайнеров.
			 */
			function startSwitchAdv()
			{
				return setInterval((function(){
					var index = current + 1;
					index = (index>=qnt) ? 0 : index;
					go(index);
				}), 4000);
			}
			
			if (qnt > 1) {
				
				var thumbnails = '<div class="thumbs">';
				items.each(function(){
					var item = $(this);
					thumbnails += '<div></div>';
				});
				thumbnails += '</div>';
				s.append(thumbnails);
				thumbnails = s.find('.thumbs div');
				thumbnails.eq(0).addClass('active');
				
				thumbnails.click(function(){
					clearTimeout(timer);
					var a = $(this);
					if (!a.hasClass('active') && !isAnimated) {
						var index = s.find('.thumbs div').index($(this));
						thumbnails.filter('.active').removeClass('active');
						a.addClass('active');
						go(index);
					};
				});
				
				items.css({opacity:0});
				items.eq(0).addClass('active').show().css({opacity:1});
				timer = startSwitchAdv();
				// При получении фокуса страницы, стартим таймер
				window.onfocus = function(){
					if (!timer) {
						timer = startSwitchAdv();
					}
				}
				
				// При потери фокуса останалвиваем таймер, и стираем ID таймера
				window.onblur = function(){
					clearTimeout(timer);
					timer = undefined;
				};
			} else {
				items.eq(0).addClass('active').show().css({opacity:1});
			};
			
			function go(index) {
				isAnimated = true;
				thumbnails.filter('.active').removeClass('active');
				thumbnails.eq(index).addClass('active');
				items.eq(index).css({zIndex:2}).show().animate({opacity:1},300,function(){
					$(this).css({zIndex:1});
					isAnimated = false;
				});
				items.eq(current).animate({opacity:0},300,function(){$(this).hide();});
				current = index;
			};
			
		})();
	};
	
	/*----промоблок*/
	
	/*ставим фокус на textarea*/
	$('.leave-comment-link a').click(function(){
		$('.comments textarea').focus();
		return false;
	});
	/*----ставим фокус на textarea*/
	
	/*подсвечиваем рейтинг*/
	if ($('.rating-leave').length) {
		(function(){
			var r = $('.rating-leave');
			var s = r.find('i');
			s.mouseenter(function(){
				var index = s.index(this);
				s.filter(':lt('+(index+1)+')').addClass('hovered');
				s.filter(':gt('+(index)+')').removeClass('hovered');
			}).click(function(){
				var index = s.index(this);
				s.filter(':lt('+(index+1)+')').addClass('active');
				r.find('input').val(index+1);
			});
			r.mouseenter(function(){
				s.removeClass('active');
			}).mouseleave(function(){
				if (!r.find('input').val() == '') {
					s.filter(':lt('+(r.find('input').val())+')').addClass('active');
				};
				s.removeClass('hovered');
			});
		})();
	};
	/*----подсвечиваем рейтинг*/
	
	/*вызов плеера фоток*/
	if ($('#player').length) {
		photoPlayer.init();
		
		if ($('.idea-listing').length) {
			$('.idea-listing a').click(function(){
				var a = $(this);
				if (!a.hasClass('active')) {
					$('.idea-listing .active').removeClass('active');
					a.addClass('active');
					var r = a.attr('data-rel')*1;
					photoPlayer.load(r);
					$('.idea-features').hide();
					$('.idea-features-'+a.attr('data-rel')).show();
					if (r != 0) {
						$('.idea-authors').hide();
					} else {
						$('.idea-authors').show();
				        };
				};
				return false;
			});
		};
	};
	/*----вызов плеера фоток*/
	
	
	/* Удаление проекта */
	$('.gallery-item-remove').click(function(){
		var item = $(this).parents('.item');
		var id = $(this).attr('data-id');
		$('body').append('<div class="popup popup-confirm" id="popup-confirm">'+
		'	<div class="popup-header">Удаление проекта</span></div>'+
		'	<div class="popup-body">'+
		'		<p><strong>Вы действительно хотите удалить проект?</strong></p>'+
		'		<p>'+
		'			<a href="#" class="button2 handler-yes">Да</a>'+
		'			<a href="#" class="button2 handler-no">Нет</a>'+
		'		</p>'+
		'	</div>'+
		'</div>');
		$('#popup-confirm').find('.handler-yes').click(function(){
			$.get(
				'/idea/create/delete/id/'+id,
				function(response){
					if (response.success) {
						item.animate({width:0},200,function(){
							item.remove();
						});
					}
				},
				'json'
			);
			
			closePopup( $('#popup-confirm') );
			$('#popup-confirm').remove();
			return false;
		});
		$('#popup-confirm').find('.handler-no').click(function(){
			closePopup( $('#popup-confirm') );
			$('#popup-confirm').remove();
			return false;
		});
		showPopup('popup-confirm');
		return false;
	});
	
	$('a.bugs-handler').click(function(){
		showPopup('popup-feedback');
		return false;
	});
	/* ----Удаление проекта */
	
	/*Изменение стиля ссылки "изменить фото" аватарки пользователя */
	$('.file_load').live('mouseover', function(){
		$('a.pseudo-change').addClass('pseudo-file');
	});
	$('.file_load').live('mouseout', function(){
		$('a.pseudo-change').removeClass('pseudo-file'); 
	});
	
	if ($('.mh-new').length) {
		$('.mh-new a').click(function(){
			showPopup('popup-message');
			var offset = $('.messages-header').offset();
			$('#popup-message').css({
				top:offset.top,
				left:offset.left
			})
			return false;
		})
	};
	/*---- изменение стиля*/
	
	if ($('.registration').length) {
		$('.registration .switcher a').click(function(){
			var a = $(this);
			if (!a.parent().hasClass('active')) {
				$('.registration .switcher .active').removeClass('active');
				a.parent().addClass('active');
				if (a.attr('data-rel') == 'user') {
					$('.form-user').show();
					$('.form-designer').hide();
				} else {
					$('.form-designer').show();
					$('.form-user').hide();
				};
			};
			return false;
		});
                 /* Внес небольшие изменения. */
		$('.form-designer .fd-person .selectInput').change(function(){
			var val = $(this).val();
			if (val == 'Designer') {
				$('.form-designer').removeClass('form-designer-jur').addClass('form-designer-phys');
				$('.fd-company label').html('Имя <span class="required">*</span>').next('span').hide();
			} else {
				$('.form-designer').removeClass('form-designer-phys').addClass('form-designer-jur');
				$('.fd-company label').html('Название компании <span class="required">*</span>').next('span').show();
			};
		});
		
	};
	
	/* Модифицировал блок кода под нужные нужды. */
	if ($('.kapitel-list').length) {
		$('.kapitel-list input').click(function(){
			var input = $(this);
			if (input.is(':checked')) {
				input.parent().addClass('kl-item-checked');
			} else {
				input.parent().removeClass('kl-item-checked');
				
				if ($('.pathBar .view a.active').find('.view-checked').length)
					input.parent().slideUp(200);
			};
			$('.pathBar .view .view-checked').text($('.kapitel-list .kl-item-checked').length);
			
			// запрос на смену статуса
			$.get('/idea/goldcapitel/select/id/'+input.attr('data-id'), function(response){
				if ( ! response.success)
					alert("Произошла ошибка!\nСкорее всего ваши изменения не применятся.");
			}, 'json');
		});
		
		$('.pathBar .view a').click(function(){
			var a = $(this);
			
			if (!a.hasClass('active') && a.find('em').text() != 0) {
				
				$('.pathBar .view .active').removeClass('active');
				a.addClass('active');
				$ulWorks = $('ul.userWorks');
				$kapitelList = $('.kapitel-list');
				
				if (a.find('.view-checked').length) {
					$ulWorks.prev('h2').hide();
					$ulWorks.hide();
					$ulWorks.find('li').hide();
					
					$ulWorks.each(function(index, elem){
						var ul = $(elem);
						var li = ul.find('li.kl-item-checked').show();
						if (li.length) {
							ul.prev('h2').slideDown(200);
							ul.slideDown(200);
						}
					});
					
				} else if (a.find('.view-capitel').length) {
					
					$ulWorks.prev('h2').hide();
					$ulWorks.hide();
					$ulWorks.find('li').hide();
										
					$ulWorks.each(function(index, elem){
						var ul = $(elem);
						var li = ul.find('li.kl-capitel').show();
						if (li.length) {
							ul.prev('h2').slideDown(200);
							ul.slideDown(200);
						}
					});
					
				} else {
					
					$ulWorks.prev('h2').hide();
					$ulWorks.hide();
					$ulWorks.find('li').hide();
					
					
					$ulWorks.each(function(index, elem){
						var ul = $(elem);
						var li = ul.find('li:not(.kl-capitel)').show();
						if (li.length) {
							ul.prev('h2').show();
							ul.slideDown(200);
						}
					});
					
				}
			}
			
			return false;
		});
	}
	
	/*вакансии*/
	if ($('.vacancies-list').length) {
		(function(){
			$('.vacancies-list h2 a').click(function(){
				$(this).parents('.item').trigger('click');
				return false;
			});
			$('.vacancies-list .item').click(function(){
				var a = $(this);
				
				if (!a.hasClass('active')) {
					$('.vacancies-list .active').removeClass('active');
					a.addClass('active');
					setHash(a.attr('id').replace('vacancy-',''));
				} else {
					a.removeClass('active');
					setHash('0');
				}
			});
			if (getHash()) {
				var item = $('#vacancy-'+getHash());
				if (item.length) {
					item.trigger('click');
					$(document).scrollTop(item.offset().top-12);
				}
			}
		})();
	}
	/*----вакансии*/
	
	if ($('.accordion-list').length) {
		(function(){
			$('.accordion-list h2 a').click(function(){
				$(this).parents('.item').trigger('click');
				return false;
			});
			$('.accordion-list .item').click(function(){
				var a = $(this);
				
				if (!a.hasClass('active')) {
					$('.accordion-list .active').removeClass('active');
					a.addClass('active');
					setHash(a.attr('id').replace('id-',''));
				} else {
					a.removeClass('active');
					setHash('0');
				}
			});
			if (getHash()) {
				var item = $('#id-'+getHash());
				if (item.length) {
					item.trigger('click');
					$(document).scrollTop(item.offset().top-12);
				}
			}
		})();
	}
	
	$('.c-hinter').hinter();
	
	$('.project-docs a.handler').live('click',function(){
		if (!$(this).parent("div").hasClass('pd-active')) {
			$(this).parent("div").addClass('pd-active');
					$(document).mousedown(md);
				} else {
					$(document).unbind('mousedown',md);
			$(this).parent("div").removeClass('pd-active');
				};
				return false;
			});
		function md(e) {
			var target = $(e.target);
		//alert(target.hasClass('pd-certificate'));
		if ( $(e.target).closest(".pd-active").length ) {} else {
			if($('.pd-certificate').hasClass("pd-active")){
				$('.pd-certificate').removeClass('pd-active');
			}
			if($('.pd-history').hasClass("pd-active")){
				$('.pd-history').removeClass('pd-active');
			}
			//alert(111);
			};
		};
	
	
	
	//меню проекта 
	bindGaleryItems();
});
/*----$(document).ready();*/


function bindGaleryItems()
{
	if ($('.gallery').length) {
		$('.gallery:not(.gallery-unpublic)').each(function(){
			var g = $(this);
			g.find('.item .info').css({opacity:0});
			g.find('.item').mouseenter(function(){
				//$(this).find('.info').stop().fadeTo(100,1);
				$(this).find('.info').stop().animate({
					top:230-$(this).find('.info').outerHeight(),
					opacity:1
				},200);
				if (g.hasClass('gallery-controls')) {
					$(this).find('.gallery-item-controls').animate({
						opacity:1
					},200);
				};
				if (g.hasClass('gallery-kapitel')) {
					$(this).find('.gallery-item-kapitel').addClass('gallery-item-kapitel-hovered').animate({
						opacity:1
					},200);
				};
			}).mouseleave(function(){
				//$(this).find('.info').stop().fadeTo(200,0);
				$(this).find('.info').stop().animate({
					top:230,
					opacity:0
				},200);
				if (g.hasClass('gallery-controls')) {
					$(this).find('.gallery-item-controls').animate({
						opacity:0.5
					},200);
				};
				if (g.hasClass('gallery-kapitel')) {
					$(this).find('.gallery-item-kapitel').removeClass('gallery-item-kapitel-hovered').animate({
						opacity:0.5
					},200);
				};
		        });
			
			if (g.hasClass('gallery-kapitel')) {
				g.find('.gallery-item-kapitel input').click(function(){
					var input = $(this);
					if (input.is(':checked')) {
						input.parents('.gallery-item-kapitel').addClass('gallery-item-kapitel-checked');
						input.parent().find('span').text('Проект участвует в');
					} else {
						input.parents('.gallery-item-kapitel').removeClass('gallery-item-kapitel-checked');
						input.parent().find('span').text('Отправить на участие в');
					};
				});
			};
	
		});
	};
}


/*фотоплеер*/
var photoPlayer = (function(){
	
	var p,slideshow,items,list,photos,next,nav,hide,counter,photo,info,inner,isRolling,author,
		current = 0,
		qnt = 0;
	
	function init(n) {
		// Добавил.
		// При инициализации надо загружать всегда первую фотку.
		current = 0;
		p = $('#player');
		author = (p.attr('data-author')) ? true : false;
		items = p.find('a');
		list = items;
		
		create();
	};
	
	function loadGallery(n) {
		current = 0;
		qnt = 0;
		clearTimeout(slideshow);
		
		if (n) {
			list = items.filter('.player-gallery-'+n);
			create();
		} else {
			list = items;
			create();
		};
	};
	
	function create() {
		p.empty().append('<div class="player-photo"></div>'+
		'	<div class="player-nav player-prev"><i></i></div>'+
		'	<div class="player-nav player-next"><i></i></div>'+
		((author) ? '<div class="player-copy"><a href="#" class="player-copy-handler" target="_blank">Найти дубликаты изображения<br>в интернете</a><div class="c-hinter"><i></i><p class="c-hinter-text">С помощью этого сервиса вы можете проверить, не используются ли изображения ваших проектов на сторонних ресурсах.</p></div></div>' : '')+
		'	<div class="player-photos">'+
		'		<div class="player-photos-wrapper"><div class="player-photos-inner"></div></div>'+
		'	</div>'+
		'	<div class="player-info"></div>');
		p.find('.player-photos-inner').html(list);
		list = p.find('.player-photos');
		
		photos = new Array(); // photos array [url,title,loading indicator,gallery]
		list.find('a').each(function(i){
			var a = $(this);
			if (i==0) $(this).addClass('active');
			qnt = i+1;
			photos[i] = {
				url:a.attr('href'),
				title:a.attr('title'),
				loaded:false,
				copy:(author) ? a.attr('data-copy') : false
			};
		});
		list.find('a:eq(0)').addClass('active');
		
		photo = p.find('.player-photo');
		info = p.find('.player-info');
		
		if (qnt > 1) {
			var currentRoll = 0;
			var size=12; // preview counter
			
			nav = p.find('.player-nav');
			nav.click(function(){
				clearTimeout(slideshow);
				p.find('.player-slideshow i').removeClass('active');
				
				var next = ($(this).hasClass('player-prev')) ? current-1 : current+1;
				next = (next<0) ? qnt-1 : ( (next>(qnt-1)) ? 0 : next );
				
				if (qnt > 14) {
					
					if (!isRolling && !$(this).hasClass('disabled')){
						if (next < currentRoll || next > (currentRoll+size-1) ){
							var val;
							if (next > current){
								val= (next-size+1)<0 ? 0 : (next-size+1);
								inner.animate({
									marginLeft:-val*50+'px'
								},200,function(){
									isRolling = false;
								});
								currentRoll=val;
								checkPNav();
							} else if (next < current) {
								val= next>(qnt-size+1) ? qnt-size+1 : next;
								inner.animate({
									marginLeft:-(val)*50+'px'
								},200,function(){
									isRolling = false;
								});
								currentRoll=val;
								checkPNav();
							}
						}
						
					}
				}
				
				load(next);
			});
			
			list.find('a').click(function(){
				if (!$(this).hasClass('active')) {
					var index = list.find('a').index(this);
					load(index);
					
					if (qnt>16) {
						p.find('.player-list').trigger('click');
					};
				};
				
				return false;
			});
			
			if (qnt > 14) {
				p.find('.player-photos-wrapper').addClass('player-photos-wrapper-nav');
				inner = p.find('.player-photos-inner');
				list.append('<div class="pp-prev disabled"><i></i></div><div class="pp-next"><i></i></div>');
				inner.width(qnt*50);
				
				list.find('.pp-prev').click(function(){
					if (!isRolling && !$(this).hasClass('disabled')) {
						var val=currentRoll>=4 ? 4 : currentRoll;
						isRolling = true;
						inner.animate({
							marginLeft:'+='+(val*50)+'px'
						},200,function(){
							isRolling = false;
						});
						currentRoll-=val;
						checkPNav();
					}
				});
				list.find('.pp-next').click(function(){
					if (!isRolling && !$(this).hasClass('disabled')) {
						var val=(qnt-currentRoll-size-1)>4 ? 4 : qnt-currentRoll-size;
						isRolling = true;
						inner.animate({
							marginLeft:'-='+(val*50)+'px'
						},200,function(){
							isRolling = false;
						});
						currentRoll+=val;
						checkPNav();
					}
				});
				function checkPNav() {
					if (currentRoll <= 0) {
						list.find('.pp-prev').addClass('disabled');
						list.find('.pp-next').removeClass('disabled');
					} else if (currentRoll >= qnt-size) {
						list.find('.pp-prev').removeClass('disabled');
						list.find('.pp-next').addClass('disabled');
					} else {
						list.find('.pp-prev').removeClass('disabled');
						list.find('.pp-next').removeClass('disabled');
					}
				}
			}
			
		} else {
			p.find('.player-photos').remove();
			p.find('.player-nav').remove();
		}
			
		load(current);
	}
	
	
	
	function load(index) {
		current = index;
		preload(current);
		
		var prevWrapper = photo.find('div:not(:last)');
		setTimeout((function(){
			prevWrapper.remove();
		}),100);
		
		var wrapper = $('<div><span></span></div>').css({opacity:0});
		photo.append(wrapper);
		
		if (photos[index].loaded) {
			wrapper
				.find('span').append('<img src="'+photos[index].url+'" alt="">').end()
				.animate({opacity:1},300);
		} else {
			wrapper.animate({opacity:1},300);
			var img = $(new Image())
				.load(function(){
					if (index == current) {
						img
							.css({opacity:0})
							.appendTo(wrapper.find('span'))
							.animate({opacity:1},300);
					};
				})
				.attr('src',photos[index].url);
		};
		
		info.text(photos[index].title);
		list.find('.active').removeClass('active');
		list.find('a:eq('+current+')').addClass('active');
		p.find('.player-copy-handler').attr('href',photos[index].copy);
		
	};
	
	function getNext() {
		return ((current+1)>(qnt-1)) ? 0 : (current+1);
	};
	
	function preload(index) {
		var start = index-1;
		preloadImage(index);
		
		if (qnt > 3) {
			for (var i=start;i<(start+3);i++) {
				if (i != index) {
					preloadImage((i < 0) ? qnt+i : ( (i > (qnt-1)) ? i-qnt : i ));
				};
			};
		} else {
			for (var i=0;i<qnt;i++) {
				if (i != index) {
					preloadImage(i);
				};
			};
		};
		
		function preloadImage(i) {
			if (photos[i].loaded == false) {
				$(new Image()).load(function(){
					photos[i].loaded = true;
				}).attr('src',photos[i].url);
			};
		};
	};
	
	return {
		init:init,
		load:loadGallery
	};
	
})();

/*----плеер*/

/*подсказки в профиле*/
$.fn.hinter = function(){
	$(this).each(function(){
		var p = $(this),
			handler = p.find('i'),
			index = (p.offset().top*p.offset().left).toString();
		
		p.attr('data-index',index);
		handler.click(function(){			
			if (!p.hasClass('c-hinter-active')) {
				p.addClass('c-hinter-active');
				$(document).mousedown(md);
			} else {
				$(document).unbind('mousedown',md);
				p.removeClass('c-hinter-active');
			};
		});
		function md(e) {
			var target = $(e.target);
			if (target.attr('data-index') == index || target.parents('.c-hinter').attr('data-index') == index) {} else {
				handler.trigger('click');
			};
		};
	});
};
/**/

/*popup*/
function setCenter(item) {
	windowHeight = document.documentElement.clientHeight;
	currentOffset = document.documentElement.scrollTop || document.body.scrollTop;
	currentOffset = currentOffset + parseInt((windowHeight - $(item).outerHeight()) / 2);
	currentOffset = (currentOffset < 10) ? 10 : currentOffset;
	pLeft = parseInt(($(window).width() - item.outerWidth()) / 2);
	item.css({top:currentOffset,left:pLeft});
}
function showPopup(popup) {
	popup = $('#'+popup);
	setCenter(popup);
	createBlind(popup);
	popup.css({opacity:0}).show().animate({opacity:1},300);
	popup.find('.popup-close').click(function(){
		closePopup(popup);
		//return false;
	});
	$(document).bind('keydown',function(e){
		if (e.which == 27) {
			closePopup(popup);
		};
	});
};
function createBlind(popup) {
	var blind = $('<div class="blind"></div>');
	blind.css({opacity:0}).height($(document).height()).appendTo('body').animate({opacity:0.6},200);
	blind.click(function(){
		closePopup(popup);
		//return false;
	});
}
function closePopup(popup) {
	$('.blind').remove();
	popup.hide();
	$(document).unbind('keydown');
	return false;
}
/*----popup*/

$.fn.list = function() {
	$(this).each(function(i){
		var list = $(this),
			inner = list.find('.list-inner');
		
		list.find('.bar-wrapper').remove();
		if (inner.height() > list.height()) {
			$('<div class="bar-wrapper"><b></b><div class="bar"></div></div>').appendTo(list);
			var bar = list.find('.bar'),
				min = inner.height() - list.height(),
				max = list.height()-150;
				ratio = min / max;
			bar.css('marginTop',parseInt(inner.css('marginTop'))/-ratio);
				
			bar.unbind('mousedown').mousedown(function(e){
				var y = e.pageY;
				var pos = Math.abs(parseInt(inner.css('marginTop')))/ratio;
				$(document).mousemove(function(e){
					var dif = e.pageY - y;
					dif = (dif + pos);
					dif = (dif < 0) ? 0 : ( (dif > max) ? max : dif );
					bar.css('marginTop',dif);
					inner.css('marginTop',-dif*ratio);
				});
				$(document).mouseup(function(e){
					$(document).unbind('mousemove');
					$(document).unbind('mouseup');
				});
			});
			list.unbind('mousewheel').mousewheel(function(e, delta) {
				
				var ev = e.originalEvent;
				if (!ev)
					ev = window.event;
				if (ev.wheelDelta)
					delta = ev.wheelDelta/120;
				else
					delta = ev.detail/-3;
				
				var dir = -60/ratio*delta,
					pos = Math.abs(parseInt(inner.css('marginTop')))/ratio,
					dif = (dir	 + pos);
				
					dif = (dif < 0) ? 0 : ( (dif > max) ? max : dif );
					bar.css('marginTop',dif);
					inner.css('marginTop',-dif*ratio);
				return false;
			});
		};
		
	});
};

function getHash() {
	var www = window.location.toString();
	var hash = null;
	if (www.indexOf('#') >= 0) {
		hash = www.substring(www.indexOf('#')+1);
	};
	return hash;
};

function setHash(hash) {
	var www = window.location.toString();
	if (www.indexOf('#') >= 0) {
		www = www.substring(0,www.indexOf('#'));
	};
	window.location.replace(www+'#'+hash);
};

/**
 * Замена стандартного confirm.
 * Пример использования
 * <a href="/delete/344" onclick="return doAction(this, 'Вопрос','Заголовок');">удалить</a>
 */
doAction = function (link, question, title)
{
	if (question === undefined) question = 'Выполнить операцию?';
	if (title === undefined) title = 'Подтверждение действия';
	
	
	$('body').append('<div class="popup popup-confirm" id="popup-confirm">'+
	'	<div class="popup-header">'+title+'</span></div>'+
	'	<div class="popup-body">'+
	'		<p><strong>'+question+'</strong></p>'+
	'		<p>'+
	'			<a href="#" class="button2 handler-yes">Да</a>'+
	'			<a href="#" class="button2 handler-no">Нет</a>'+
	'		</p>'+
	'	</div>'+
	'</div>');
	$('#popup-confirm').find('.handler-yes').click(function(){
		closePopup( $('#popup-confirm') );
		$('#popup-confirm').remove();

		location = link.href;

		return false;
	});
	$('#popup-confirm').find('.handler-no').click(function(){
		closePopup( $('#popup-confirm') );
		$('#popup-confirm').remove();

		return false;
	});

	showPopup('popup-confirm');
	return false;

};
/*----Замена стандартного confirm.*/


/* Copyright (c) 2010 Brandon Aaron (http://brandonaaron.net)
 * Licensed under the MIT License (LICENSE.txt).
 *
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 * Thanks to: Seamus Leahy for adding deltaX and deltaY
 *
 * Version: 3.0.4
 *
 * Requires: 1.2.2+
 */
(function(c){var a=["DOMMouseScroll","mousewheel"];c.event.special.mousewheel={setup:function(){if(this.addEventListener){for(var d=a.length;d;){this.addEventListener(a[--d],b,false)}}else{this.onmousewheel=b}},teardown:function(){if(this.removeEventListener){for(var d=a.length;d;){this.removeEventListener(a[--d],b,false)}}else{this.onmousewheel=null}}};c.fn.extend({mousewheel:function(d){return d?this.bind("mousewheel",d):this.trigger("mousewheel")},unmousewheel:function(d){return this.unbind("mousewheel",d)}});function b(i){var g=i||window.event,f=[].slice.call(arguments,1),j=0,h=true,e=0,d=0;i=c.event.fix(g);i.type="mousewheel";if(i.wheelDelta){j=i.wheelDelta/120}if(i.detail){j=-i.detail/3}d=j;if(g.axis!==undefined&&g.axis===g.HORIZONTAL_AXIS){d=0;e=-1*j}if(g.wheelDeltaY!==undefined){d=g.wheelDeltaY/120}if(g.wheelDeltaX!==undefined){e=-1*g.wheelDeltaX/120}f.unshift(i,j,e,d);return c.event.handle.apply(this,f)}})(jQuery);
