ZigZag = {

	POPUP_DIMENSIONS : {
		width : 500,
		height : 550
	},

	init : function(){
		this.domInit();
	},

	domInit : function(){
		this.jPage = $("#TopPhone");
	}
}

$(document).ready(
	function(){
		ZigZag.init();
	}
);


Giant = function(){
	this.domInit();
	this.attachEvents();
	this.updateFontSize();
}

Giant.prototype = {
	
	ANI_DURATION : 0.5,
	OFFSET_TOP : 100,
	WIDTH_TO_FONT_SIZE : 35,

	domInit : function(){
		this.jPtr = $("#giant_phone");
		this.jHolder = this.jPtr.find(".holder");
		this.jPhone = this.jPtr.find(".middle");
		this.jOpen = $(".head_contacts .t_tel");
		this.jClose = this.jPtr.find(".close");
	},
	
	attachEvents : function(){
		var that = this;
		this.jOpen.click(
			function(evt){
				if(!that.bOpened){
					that.open();
					evt.stopPropagation();
				}
			}
		);
		
		this.jHolder.click(
			function(evt){
				evt.stopPropagation();
			}
		);
		
		$(document).add(this.jClose).click(
			function(evt){
				if(that.bOpened){
					that.close();
					evt.stopPropagation();
				}
			}
		);
		
		$(window).resize(
			function(){
				if(that.bOpened){
					that.updateFontSize();
				}
			}
		);
	},
	
	open : function(){
		var that = this;
		
		that.jPtr.
			css("opacity", 0).
			addClass("ani").
			show();
		
		this.positionTop();
		this.updateFontSize();
			
		jTweener.removeTween(this.jPtr);	
		jTweener.addPercent(
			this.jPtr,
			{
				time : this.ANI_DURATION,
				transition : "linear",
				
				fade : function(position){
					that.jPtr.css("opacity", position);
				},
				
				
				onComplete : function(){
					that.jPtr.
						css("filter", "none").
						removeClass("ani");
				}
			}
		);

		this.bOpened = true;
	},
	
	close : function(){
		var that = this;
		
		this.jPtr.
			css("opacity", 1).
			addClass("ani");
		
		if($.browser.msie){
			this.jPtr[0].style.filter="alpha(opacity='100')"
		}
		
		jTweener.removeTween(this.jPtr);
		jTweener.addPercent(
			{
				time : this.ANI_DURATION,
				transition : "linear",
				fade : function(position){
					that.jPtr.css("opacity", 1 - position);
				},
				onComplete : function(){
					that.jPtr.
						hide().
						removeClass("ani");
				}
			}
		);
				
		this.bOpened = false;
	},
	
	positionTop : function(){
		this.jHolder.css("top", this.getOffsetTop());
	},
	
	getOffsetTop : function(){
		var
			iScrollTop = document.documentElement.scrollTop || document.body.scrollTop,
			iWindowHeight = document.documentElement.clientHeight || window.innerHeight,
			iPhoneHeight = this.jHolder.height();
		
		if(iWindowHeight >= iPhoneHeight + this.OFFSET_TOP * 2){
			return (iWindowHeight - iPhoneHeight) / 2 + iScrollTop;
		}
		else{
			return this.OFFSET_TOP + iScrollTop;
		}
	},
	
	updateFontSize : function(){
		var iFontSize = Math.round(ZigZag.jPage.width() / this.WIDTH_TO_FONT_SIZE) + "px";
		this.jPhone.css("fontSize", iFontSize);
		
		if($.browser.mozilla){
			this.mozillaFix();
		}
	},
	
	mozillaFix : function(){
		var that = this;
		this.jPhone.hide();
		setTimeout(
			function(){
				that.jPhone.show();
			},
			1
		);
	}
	
}

$(document).ready(
	function(){
		new Giant();
	}
);
