	
var gallery = new Array();

$(document).ready(function(){
	$('.photogallery').each(function(){
		gallery.push(new Gallery(this));
	})
	
	for(var i=0; i < gallery.length; i++) {
		gallery[i].init();
	};
})

/************************************* 
	Gallery Object
	
	attrs:
	  gallery - jQuery объект указывающий на контейнер фотогалереи
	  bg      - jQuery объект контейнера с фоном
	
*************************************/
function Gallery(gallery){
	this.oGallery = gallery;
	this.oBack = $('#gallery-bg');
	this.oFader = $('#gallery-fader');
	this.oLogoInitial = $('#logo');
	this.oMainContent = $('#main_content');
	
	this.aPhoto = new Array()
	this.sFindClass = 'photo';
	this.sFindCtrl = 'controls';
	
	this.isShow = false;
	this.notHide = false;
	this.speed = 300;
}

Gallery.prototype.init = function(){
	this.findPhoto()
	this.initPhoto();
	this.initControls();
	this.initEvents();
}

Gallery.prototype.initEvents = function(){
	var _my = this;
	$(document.body).click(function(){
		if(!_my.notHide && _my.isShow){
			_my.hidePhoto();
			_my.hideGallery();
		}
		_my.notHide = false;
	})

	$(this.oBack).click(function(){
		_my.notHide = true;
	})
	
	$(this.oGallery).click(function(){
		_my.notHide = true;
	})
	
	$(window).resize(function(){
		for(var i=0; i < _my.aPhoto.length; i++) {
			if(_my.aPhoto[i].isShow){
				_my.aPhoto[i].correct();
			}
		};
		_my.correct();
	})
	
	this.ctrlPrev.click(function(){ _my.prevPhoto(); })
	this.ctrlNext.click(function(){ _my.nextPhoto(); })
	this.ctrlClose.click(function(){ _my.hideGallery(); })
	
	this.addHover(this.ctrlPrev);
	this.addHover(this.ctrlNext);
	this.addHover(this.ctrlClose);
	
}

Gallery.prototype.addHover = function(obj){
	obj.mouseover(function(){ $(this).addClass('hover') });
	obj.mouseout(function(){ $(this).removeClass('hover') });
}

Gallery.prototype.initControls = function(){
	/* Блоки управления фотографиями */
	this.oControls = $('.'+this.sFindCtrl, this.oGallery);
	this.ctrlPrev = $('.prev', this.oControls);
	this.ctrlClose = $('.close', this.oControls);
	this.ctrlNext = $('.next', this.oControls);
	
	/* Логотип */
	this.oLogo = $('.logo', this.oBack);
	
}

Gallery.prototype.findPhoto = function(){
	var _my = this;
	this.aPhoto = new Array();
	$('.'+this.sFindClass, this.oGallery).each(function(){
		_my.aPhoto.push(new Photo(this, _my));
	})
}

Gallery.prototype.initPhoto = function(){
	for(var i=0; i < this.aPhoto.length; i++) {
		this.aPhoto[i].init()
	};
}

Gallery.prototype.prevPhoto = function(){
	var current = 0;
	for(var i=0; i < this.aPhoto.length; i++) {
		if(this.aPhoto[i].isShow){
			this.notHide = true;
			this.aPhoto[i].hide();
			current = i;
		}
	};
	current--;
	if(current == -1) current = this.aPhoto.length-1;
	this.aPhoto[current].show()
}

Gallery.prototype.nextPhoto = function(){
	var current = 0;
	for(var i=0; i < this.aPhoto.length; i++) {
		if(this.aPhoto[i].isShow){
			this.notHide = true;
			this.aPhoto[i].hide();
			current = i;
		}
	};
	current++;
	if(current == this.aPhoto.length) current = 0;
	this.aPhoto[current].show()
}

Gallery.prototype.showGallery = function(){
	this.isShow = true;
	var _my = this;

	this.oLogoInitial.hide();
	this.oFader.show();
	this.oBack.css({ top: -556, left: 0 }).show();
	this.oBack.animate({ top: this.bClone ? 1 : this.oMainContent.height() - this.oBack.height() + 1 }, _my.speed, 'easeout');

	this.correct();

	setTimeout(
		function(){
			_my.oControls.css({ opacity: 0 }).show().animate({ opacity: 1 }, _my.speed);
			_my.oLogo.css({ opacity: 0 }).show().animate({ opacity: 1 }, _my.speed);
		},
		_my.speed
	);
}

Gallery.prototype.fixGallery = function(iIndex){
	var bClone = false;

	if($(this.oGallery).parents("#gallery").length == 0){
		bClone = true;

		this.hidePhoto();

		for(var i=0; i < this.aPhoto.length; i++) {
			this.aPhoto[i].notOver = true;
			this.aPhoto[i].previewPhoto.removeClass("hover");
			this.aPhoto[i].setPreviewDefault(true);
		};

		this.bClone = true;
		this.oGalleryOld = this.oGallery;

		this.aPhotoOld = new Array();
		for(var i=0; i < this.aPhoto.length; i++) {
			this.aPhotoOld[i] = this.aPhoto[i];
		}

		this.oGallery = $(this.oGallery).clone();
		this.oGallery.appendTo("#gallery");

		this.findPhoto()
		this.initPhoto();
		this.initControls();

		var _my = this;

		$(this.oGallery).click(function(){
			_my.notHide = true;
		})

		this.ctrlPrev.click(function(){ _my.prevPhoto(); })
		this.ctrlNext.click(function(){ _my.nextPhoto(); })
		this.ctrlClose.click(function(){ _my.hideGallery(); })

		this.addHover(this.ctrlPrev);
		this.addHover(this.ctrlNext);
		this.addHover(this.ctrlClose);

		this.aPhoto[iIndex].show();
	}

	return bClone;
}

Gallery.prototype.correct = function(){
	var ctrlCoords = Common.Dom.getAbsoluteCoords(this.oGalleryOld || this.oGallery);
	this.oControls.css({ left: ctrlCoords.iLeft-19 });
}

Gallery.prototype.check = function(){
	var isHide = true;
	for(var i=0; i < this.aPhoto.length; i++) {
		if(this.aPhoto[i].toShow){
			isHide = false;
		}
	}
	if(isHide && !this.notHide){
		this.hideGallery();
	}
}

Gallery.prototype.hideGallery = function(){
	this.isShow = false;
	var _my = this;

	this.hidePhoto();

	this.oLogoInitial.show();
	this.oFader.hide();
	this.oBack.animate({ top: -556 }, _my.speed, 'easeout');

	this.oControls.animate({ opacity: 0 }, _my.speed, function(){
		$(this).hide();
		_my.oLogo.hide();
	});

	if(this.bClone){
		this.oGallery = this.oGalleryOld;
		this.aPhoto = this.aPhotoOld;
		$("#gallery").empty();

		for(var i=0; i < this.aPhoto.length; i++) {
			this.aPhoto[i].notOver = false;
		}
	}
}

Gallery.prototype.hidePhoto = function(){
	for(var i=0; i < this.aPhoto.length; i++) {
		if(this.aPhoto[i].isShow){
			this.aPhoto[i].hide();
		}
	};
}


/************************************* 
	Photo Object
	
	attrs:
	  ph      - jQuery объект указывающий на контейнер с фото
	  gallery - указатель на объект родитель
	
*************************************/
function Photo(photo, gallery){
	this.oGallery = gallery;
	this.oPhoto = photo;
	this.sFindBigClass = 'big';
	this.sFindPreviewClass = 'preview';
	this.isShow = false;
	this.toShow = false;
	this.notShow = false;
	this.notOver = false;
}

Photo.prototype.init = function(){
	this.findPhotos()
	this.initEvents();
	this.preload = new Preloader(this.bigPhoto.find('img').attr('src'));
}

Photo.prototype.findPhotos = function(){
	_my = this;
	
	this.bigPhoto = $('.'+this.sFindBigClass, this.oPhoto);
	this.previewPhoto = $('.'+this.sFindPreviewClass, this.oPhoto);
	
	this.previewSize = { w: this.previewPhoto.find('img').width(), h: this.previewPhoto.find('img').height() };
	this.previewPhoto.find('a').css({ width: _my.previewSize.w-2, height: _my.previewSize.h-2 }).find('img').css({ position: 'absolute', left: -1, top: -1 });
	
}

Photo.prototype.initEvents = function(){
	var _my = this;
	$('a', this.previewPhoto).click(function(evt){ _my.fClick(evt); return false; });
	this.previewPhoto.mouseover(function(){
		if(!_my.isShow && !_my.notOver){
			$(this).addClass('hover');
			_my.setPreviewBig(true);
		}
	})
	this.previewPhoto.mouseout(function(){
		if(!_my.isShow){
			$(this).removeClass('hover');
			_my.setPreviewDefault(true);
		}
	})
}

Photo.prototype.setPreviewBig = function(bool){
	_my = this;
	if(bool)
		this.previewPhoto.find('img').css({ top: -3 });
	this.previewPhoto.find('img').css({ left: -3, width: _my.previewSize.w+4, height: _my.previewSize.h+4 });
}

Photo.prototype.setPreviewDefault = function(bool){
	_my = this;
	if(bool)
		this.previewPhoto.find('img').css({ top: -1 });
	this.previewPhoto.find('img').css({ left: -1, width: _my.previewSize.w, height: _my.previewSize.h });
}

Photo.prototype.fClick = function(evt){

	if(!this.notOver){

		if(this.isShow){
			this.notShow = true;
			this.hide()
		}{
			if(!this.notShow)
				this.show()
			this.notShow = false;
		}
	}

	return false;
}

Photo.prototype.show = function(){
	var iIndex = 0;
	for(var i = 0; i < this.oGallery.aPhoto.length; i++){
		if(this.oGallery.aPhoto[i] == this){
			iIndex = i;
			break;
		}
	}

	var bFix = this.oGallery.fixGallery(iIndex);

	if(!bFix){

		if(!this.oGallery.isShow){
			this.oGallery.showGallery();
		}

		var _my = this;
		this.toShow = true;

		this.setPreviewDefault(true);

		this.oGallery.hidePhoto();

		this.bigPhoto.css({ zIndex: 2 });

		if($.browser.msie){
			this.bigPhoto.css({ width: document.body.clientWidth })
		}

		this.previewPhoto.find('img').css({ top: 0 }).animate({ top: -$(this.oPhoto).height() - 1 }, _my.oGallery.speed, 'easeout');
		this.bigPhoto.find('img').css({ top: 556 }).end().show();

		var limits = this.oGallery.oBack.width()*0.8
		var iLeft = this.getPosition(limits);

		this.bigPhoto.find('img').css({ left: iLeft }).animate({ top: 1 }, _my.oGallery.speed, 'easeout', function(){
			$(this).css({ top: 1 });
			_my.bigPhoto.find('div').css({ opacity: 0 }).show().animate({ opacity: 1 }, _my.speed);
		});

		this.previewPhoto.removeClass('hover');

		this.isShow = true;
		this.notShow = false;
	}
}

Photo.prototype.getPosition = function(limits){

	var mPreview = Common.Dom.getAbsoluteCoords(this.previewPhoto.get(0));
	var bigWidth = this.bigPhoto.find('img').width();
	var bigLeft = mPreview.iLeft-(bigWidth/2)+this.previewPhoto.find('img').width()/2;

	if(bigLeft < 0)
		bigLeft = 0;

	if(bigLeft+bigWidth > limits)
		bigLeft=limits-bigWidth;

	return bigLeft;
}

Photo.prototype.hide = function(){
	this.bigPhoto.css({ zIndex: 1 });

	var _my = this;
	this.setPreviewDefault(false);
	this.previewPhoto.find('img').animate({ top: 0 }, _my.oGallery.speed, function(){   });
	this.bigPhoto.find('img').animate({ top: 556 }, _my.oGallery.speed, 'easeout', function(){ $(this).css({ top: 556 }); _my.bigPhoto.hide() });
	this.bigPhoto.find('div').animate({ opacity: 0 }, _my.speed, function(){ $(this).hide() });
	
	this.isShow = false;
	this.toShow = false;

	this.oGallery.check();
}

Photo.prototype.correct = function(){
	var limits = this.oGallery.oBack.width()*0.8
	var iLeft = this.getPosition(limits);

	/* Для IE пересчитываем размеры блока */
	if($.browser.msie){
		this.bigPhoto.css({ width: document.body.clientWidth })
	}

	this.bigPhoto.find('img').css({ left: iLeft });
}

function Preloader(src){
	this.src = src;
	this.isLoad = false;
	
	this.init();
}

Preloader.prototype.init = function(){
	_my = this;
	
	this.oImage = document.createElement('img');
	this.oImage.src = this.src;
	
	$(this.oImage).load(function(){
		_my.isLoad = true;
	});
}