var _img;
var _okno;
var galleryInstance; 

$(function(){

	_okno = $('#vizual');
	_img = $('<img src="images/vizual.jpg" alt="" id="img" />').appendTo(_okno);
	var _form = $('#form');
	
	$(window).bind('resize', function(){
		winResize();
	});
	winResize();
	
	$('.home #one, .two #one').live('click', function(){
		$('body').removeClass().addClass('one');
		_form.hide();
		makeScroll();
	});
	$('.home #two, .one #two').live('click', function(){
		$('body').removeClass().addClass('two');
		_form.hide();
		makeScroll();
	});
	$('header').live('click', function(){
		$('body').removeClass().addClass('home');
		_form.hide();
	});
	
	$('.gallery a').click(function(){
		_form.hide();
		galleryInstance = new Gallery( $(this) );
		return false;
	});
	
	$('.link').click(function(){
		$(this).prev('.description').slideToggle(function(){
			makeScroll();
		});
		text2Attr($(this), 'name');
	});
	
	$('.form').click(function(){
		$('#form').toggle();
		return false;
	});
	
	_form.submit(function(){
		_form.fadeTo(200,.8);
		$('.message span').slideUp(function(){
			$(this).empty();
		});
		$.ajax({
				url: 'send.php',
				type: 'post',
				data: $(this).serialize(),
				dataType: 'json',
				success: function(data){
					_form.find('.warn').removeClass('warn');
					if(data.warn) {
						$.each(data.warn, function(){
							_form.find('[name='+this+']').addClass('warn');
						});
					}
					if(data.status == "true") {
						$('<span class="ok" style="display:none;">'+data.message+'</span>')
							.appendTo('.message')
							.slideDown();
						_form.find('[type=text],textarea').val('');
						_form.find('[name=mail]').val('@');
					}
					if(data.status == "false") {
						$('<span class="warning" style="display:none;">'+data.message+'</span>')
							.appendTo('.message')
							.slideDown();
					}
					_form.fadeTo(200,1);
				}
			})
		return false;
	});
	
	_form.find('input,textarea').focus(function(){
		$(this).removeClass('warn');
	});
	
	_form.find('.close').click(function(){
		_form.hide();
	});


});

$(window).load(function(){
	//makeScroll();
});

function winResize() {
	var pomer_okno = _okno.width() / _okno.height();
	var pomer_obraz = 1920 / 1200;
	if( pomer_okno > pomer_obraz) {
		_img.css({
				'width': _okno.width() + 'px',
				'height': ( _okno.width() / pomer_obraz ) + 'px'
				});
	}
	else {
		_img.css({
				'height': _okno.height() + 'px',
				'width': ( _okno.height() * pomer_obraz ) + 'px'
				});
	}
	_img.css({
			'marginTop': '-' + ( parseInt( _img.height() ) / 2 ) + 'px',
			'marginLeft': '-' + ( parseInt( _img.width() ) / 2 ) + 'px'
		});
	$('.content, .jScrollPaneContainer').css({height: (parseInt(_okno.height())-164) + 'px'});
	makeScroll();
}

function makeScroll() {
	$('.content:visible').jScrollPane({scrollbarWidth:15, scrollbarMargin:15, dragMaxHeight: 64, dragMinHeight: 64});
}




Gallery = function(elm){
	this.source = elm.parent();
	this.active = elm;
	this.position = elm.index() + 1;
	this.count = this.source.find('a').length;
	this.makeGallery();
	this.loadPhoto();
};
Gallery.prototype.makeGallery = function(){
	var that = this;
	this.gallery = $('<div id="gallery" />');
	this.next = $('<span class="next" />');
	this.prev = $('<span class="prev" />');
	this.preloader = $('<span class="preloader" />');
	this.close = $('<span class="close" />');
	this.img = $('<img id="gallery-img" />');
	this.loader = $('<img id="gallery-loader" />');
	this.gallery
		.appendTo('body')
		.append(this.prev)
		.append(this.next)
		.append(this.preloader)
		.append(this.close)
		.append(this.img)
		.append(this.loader);
	this.close.click(function(){
		that.gallery.remove();
	});
	this.img.click(function(){
		that.gallery.remove();
	});
	this.prev.click(function(){
		that.active = that.active.prev();
		that.position -= 1;
		that.preloader.show();
		that.loadPhoto();
	});
	this.next.click(function(){
		that.active = that.active.next();
		that.position += 1;
		that.preloader.show();
		that.loadPhoto();
	});
};
Gallery.prototype.loadPhoto = function(){
	var that = this;
	this.preloader.show();
	this.photoUrl = this.active.attr('href');
	this.loader.attr('src', this.photoUrl).load(function(){
		that.img.attr('src', that.loader.attr('src'));
		that.resize();
		$(window).resize(function(){
			that.resize();
		});
		that.preloader.hide();
	});
	this.prev.show();
	this.next.show();
	if(this.position == 1) {
		this.prev.hide();
	}
	if(this.position == this.count) {
		this.next.hide();
	}
}
Gallery.prototype.resize = function(){
	var pomer_okno = this.gallery.width() / this.gallery.height();
	var pomer_obraz = this.img.width() / this.img.height();
	if( pomer_okno > pomer_obraz) {
		this.img.css({
			'width': this.gallery.width() + 'px',
			'height': ( this.gallery.width() / pomer_obraz ) + 'px'
		});
	}
	else {
		this.img.css({
			'height': this.gallery.height() + 'px',
			'width': ( this.gallery.height() * pomer_obraz ) + 'px'
		});
	}
	this.img.css({
			'marginTop': '-' + ( parseInt( this.gallery.height() ) / 2 ) + 'px',
			'marginLeft': '-' + ( parseInt( this.gallery.width() ) / 2 ) + 'px'
		});
}


function text2Attr(obj, attr) {
	var _text = obj.text();
	var _attr = obj.attr(attr);
	obj.attr(attr, _text);
	obj.text(_attr);
}
