(function(window){
	
function GlobalTop(){ this.initialize.apply(this, arguments); }

	var p = GlobalTop.prototype;
	
	p.COOKIE_NAME = 'anteprima_global_top_lang_cookie';
	p.IMG_WIDTH = 899;
	p.IMG_HEIGHT = 1200;
	p.RIGHT_MARGIN = 36;
	
	p.figContainer;
	p.fig;
	p.img;
	
	p.initialize = function(){
		var owner = this;
		var cookie = $.cookie(this.COOKIE_NAME);
		if(cookie == 'en' || cookie == 'jp' || cookie == 'cn' || cookie == 'hk'){
			var url = 'http://'+domain_prefix+cookie+base_domain+web_root;
			location.href = url;
			return;
		}
		
		$('#global_top').css('visibility', 'visible');
		this.figContainer = $('div');
		this.figContainer.css({
			overflow : 'hidden',
			height : '100%',
			right : this.RIGHT_MARGIN
		});
		
		this.fig = this.figContainer.find('figure');
		this.fig.css({
			position : 'relative',
			width : '100%', height : '100%'
		});
		this.img = this.figContainer.find('img');
		this.img.css({
			opacity : 0,
			position : 'absolute',
			right : 0
		});
		
		$(window).bind({
			resize : function(){
				owner.onResize();
			}
		});
		this.initHover("#global_top ul");
		this.onResize();
		
		this.show();
	}
	
	p.show = function(){
		this.figContainer.css({
			visibility : 'visible'
		});
		
		this.onResize();
		this.img.stop().animate({
			opacity : 1
		},  2600, 'easeInOutQuart');
	}
	
	p.initHover = function(e){
		var owner = this;
		var container = $(e);
		container.find("a").bind({
			mouseenter : function(){
				$(this)
				.css("width",$(this).find("img").width())
				.stop()
				.animate({backgroundPositionX:'13px'},200)
				.fadeTo("50","0.3")
			},
			mouseleave : function(){
				$(this)
				.stop()
				.css("backgroundPositionX","-241px")
				.fadeTo("50","1")	
			},
			click : function(){
				var href = $(this).attr('href');
				var lang = $(this).data('lang');
				$.cookie(owner.COOKIE_NAME, lang, { expires : 365 });
				location.href = href;
				return false;
			}
		});
		
		
			
	}
	
	p.onResize = function(){
		var ww = $(window).width();
		var wh = $(window).height();
		var wwh = ww >> 1;
		
		var per = wh / this.IMG_HEIGHT;
		var tmpW = this.IMG_WIDTH * per;
		var right = (wwh - tmpW) >> 1;
		
		if(right > this.RIGHT_MARGIN){
			this.figContainer.css({
				right : right
			});
		}else{
			this.figContainer.css({
				right : this.RIGHT_MARGIN
			});
		}
		
		if(((tmpW - wwh)>>1) > 0){
			this.figContainer.width(wwh);
			this.img.css({
				right : (wwh - tmpW)>>1
			});
		}else{
			this.figContainer.width(tmpW);
			this.img.css({
				right :0
			});
		}
		
		this.img.attr({
			width : tmpW,
			height : wh
		});
	}
	

window.GlobalTop = GlobalTop;
}(window));



$(function(){
var globalTop = new GlobalTop();
});
