Giant = function(){
	this.jPtr = $("#giant");
	if(this.jPtr.size()){
		this.init();
	}
}

Giant.prototype = {

	ANI_DURATION : 13, // кадры
	ANI_INTERVAL : { // % (css)
		start : 140,
		finish : 50
	},
	ANI_HEIGHT_DELTA : 25, // пиксели

	init : function(){
		this.updateWindowHeight();
		this.domInit();
		this.updateWindowWidth();
		this.tweenInit();
		this.attachEvents();
		this.enable();
	},

	updateWindowHeight : function(){
		this.iWindowHeight = document.documentElement.clientHeight || window.innerHeight;
	},

	updateWindowWidth : function(){
		this.jPicture.css(
			"width",
			this.jPicture.find("img[class!=hidden]").attr("width")
		);
	},

	domInit : function(){
		this.jOpen = $("#zoom");
		this.jPicture = this.jPtr.find(".picture");
		this.jPicture.css("right", this.ANI_INTERVAL.start + "%");
		this.jPtr.removeClass("hidden");
		this.iPictureHeight = this.jPicture[0].offsetHeight;
		this.jPtr.addClass("hidden");
		this.jPtr.find(".fog").removeClass("hidden");
		this.jClose = this.jPtr.find(".close");
	},

	tweenInit : function(){
		var that = this;
		this.twOpen = new Tween(this.jPicture, "", EEQ.Quartic.easeInOut, 0, 1, this.ANI_DURATION);
		this.twOpen.stop();

		this.twClose = new Tween(this.jPicture, "", EEQ.Quartic.easeInOut, 0, 1, this.ANI_DURATION);
		this.twClose.stop();

		this.twOpen.onMotionFinished = function(){
			that.onAniFinish();
		};

		this.twClose.onMotionFinished = function(){
			that.onAniFinish();
			that.onCloseFinish();
		};

		this.twOpen.onMotionChanged = this.twClose.onMotionChanged = function(elem){
			Hitek.tween.apply(this, [elem, function(sName, iValue){
				that.animate(iValue);
			}]);
		};
	},

	attachEvents : function(){
		var that = this;
		this.jOpen.click(
			function(){
				that.open();
			}
		);
		this.jClose.click(
			function(){
				that.close();
			}
		);
		this.jPicture.click(
			function(evt){
				evt.stopPropagation();
			}
		);
		$(document).click(
			function(){
				that.close();
			}
		)
		.keydown(
			function(evt){
				if(evt.keyCode === 27){ // Escape
					that.close();
				}
			}
		);
		$(window).resize(
			function(){
				that.updateWindowHeight();
			}
		);
	},

	animate : function(iPercentOffset){
		this.jPicture.css("right", iPercentOffset + "%");
	},

	getOffsetTop : function(){
		var iScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
		if(this.iWindowHeight >= this.iPictureHeight + this.ANI_HEIGHT_DELTA * 2){ // если блок с картинкой по высоте влезает с некоторым запасом в окно, то центрируем
			return (this.iWindowHeight - this.iPictureHeight) / 2 + iScrollTop;
		}
		else{ // если нет, то просто показываем с отступом сверху
			return this.ANI_HEIGHT_DELTA + iScrollTop;
		}
	},

	open : function(){
		if(!this.bOpened && this.bEnabled){
			this.onAniStart();
			this.onOpenStart();
			this.twOpen.props = {
				right : {
					s : this.ANI_INTERVAL.start,
					f : this.ANI_INTERVAL.finish
				}
			}
			this.twOpen.play();
		}
	},

	close : function(){
		if(this.bOpened && this.bEnabled){
			this.onAniStart();
			this.twClose.props = {
				right : {
					s : this.ANI_INTERVAL.finish,
					f : this.ANI_INTERVAL.start
				}
			}

			this.twClose.play();
		}
	},

	onAniStart : function(){
		this.disable();
		this.jPtr.removeClass("hidden");
	},

	onAniFinish : function(){
		this.enable();
	},

	onOpenStart : function(){
		this.bOpened = true;
		this.jPicture.css("top", this.getOffsetTop());
	},

	onCloseFinish : function(){
		this.bOpened = false;
		this.jPtr.addClass("hidden");
	},

	enable : function(){
		this.bEnabled = true;
	},

	disable : function(){
		this.bEnabled = false;
	}


}

var oGiant = false;

$(window).load(
	function(){
		oGiant = new Giant();
	}
);
