Ubisoft.GamePage = {
	element : null,
	settings : {
		transition : {
			duration : '200'
		},
		offset : 20
	},
	currentTab : null,
	currentThumb : null,
	disabled : false,
	screenshot : null,
	thumbIndex : 0,
	thumbCount : 0,
	screenshotObj : null,
	currentDownloadTab : null,
	ssHeight : null,
	ssWidth : null,
	init : function () {
		$$('div.nav').each( function(el,i){
			var menu = el.getElement('div.nav-wrapper');
			if (menu || el.hasClass('dropmenu')){
				el.addEvent('mouseenter',function(){
					if (Ubisoft.Localization.isOpen) return;
					Surrounder.hide();
					var element = this.getElement('div.nav-wrapper');
					element.setStyles({
						'height':element.getChildren()[0].getSize().y,
						'visibility':'visible'
					});
					
					this.addClass("on");
					this.addClass("white");
				});
				el.addEvent('mouseleave',function(){
					var fn = Ubisoft.Navigation.closeItem.bind(this.getElement('div.nav-wrapper'));
					fn();
				});
			} else {
				el.addEvent('mouseenter',function(){
					if (Ubisoft.Localization.isOpen) return;
					Surrounder.hide();
				});
			}
		});
		$('game-content').getElement('.tabs').getElements('li').each( function(el,i){
			if (el.hasClass('selected')){
				Ubisoft.GamePage.selectTab(el);
			}
			el.addEvent('click',function(){
				if (this.hasClass('selected')) return;
				Ubisoft.GamePage.selectTab(this);
			});
			if (BrowserObject.Engine.trident4){
				el.addEvent('mouseenter',function(){
				});
				el.addEvent('mouseleave',function(){
				});
			}
		});
		
		if ($('screenshots')){
			$$('.thumb').each( function(el,i){
				el.getElement('.overlay').setStyle('opacity','0.5');
				el.addEvent('click',function(){
					if (Ubisoft.GamePage.disabled||this.hasClass('on')) return;
					Ubisoft.GamePage.selectThumb(this);				
				});
				el.addEvent('mouseenter',function(){
					Ubisoft.GamePage.turnOn(this);
				});
				el.addEvent('mouseleave',function(){
					Ubisoft.GamePage.turnOff(this);
				});
				if (i==0){
					Ubisoft.GamePage.currentThumb = el;
					Ubisoft.GamePage.turnOn(el);
					el.addClass('on');
				}
			});
			this.thumbCount = $$('.thumb').length;
			
			$('thumbs-inner-wrapper').set('morph',{
				duration:500,
				transition:Fx.Transitions.Quart.easeOut
			});
			$$('.scroll').each(function(el){
				if (Ubisoft.GamePage.thumbCount<=4){
					el.addClass('disabled');
				}
				el.addEvent('click',function(){
					if (Ubisoft.GamePage.thumbCount<=4){
						return false;
					}
					if (this.id=='scrolldownbtn'){
						if (Ubisoft.GamePage.thumbIndex<=-1*Ubisoft.GamePage.thumbCount+4) return false;
						Ubisoft.GamePage.thumbIndex--;
						$('scrollupbtn').removeClass('disabled');
						if (Ubisoft.GamePage.thumbIndex<=-1*Ubisoft.GamePage.thumbCount+4) this.addClass('disabled');
					} else 
					if (this.id=='scrollupbtn'){
						if (Ubisoft.GamePage.thumbIndex==0) return false;
						Ubisoft.GamePage.thumbIndex++;
						$('scrolldownbtn').removeClass('disabled');
						if (Ubisoft.GamePage.thumbIndex==0) this.addClass('disabled');
					}
					$('thumbs-inner-wrapper').morph({
						'margin-top':Ubisoft.GamePage.thumbIndex*94
					})
					return false;
				});
			});
		}
		
		if ($('download-tabs')){
			Ubisoft.GamePage.currentDownloadTab = $('download-tabs').getElement('a.selected');

			$('download-tabs').getElements('a').each(function(el){
				el.addEvent('click',function(){
					if (this.hasClass('selected')) return false;
					if ($(Ubisoft.GamePage.currentDownloadTab)){
						Ubisoft.GamePage.currentDownloadTab.removeClass('selected');
						$(Ubisoft.GamePage.currentDownloadTab.id+'-tab').setStyles({
							'display':'none'
						});
					}
					
					Ubisoft.GamePage.currentDownloadTab = this;
					this.addClass('selected');
					$(this.id+'-tab').setStyles({
						'display':'block'
					});
					return false;
				});
			});
		}
		this.screenshotObj = $('screenshot-image-wrapper');
		
		if ($('screenshot-image')){
			this.ssHeight = $('screenshot-image').getStyle('height');
			this.ssWidth = $('screenshot-image').getStyle('width');
		}
	},
	selectTab : function(el){
		if ($(this.currentTab)){
			$(this.currentTab.get('label')).setStyle('display','none');
			this.currentTab.removeClass('selected');
		}
		this.currentTab = el;
		$(this.currentTab.get('label')).setStyle('display','block');
		this.currentTab.addClass('selected');
	},
	selectThumb : function(el){
		if ($(this.currentThumb)){
			this.currentThumb.removeClass('on');
			this.turnOff(this.currentThumb);
		}
		this.currentThumb = el;
		this.disabled = true;
		if (BrowserObject.Engine.trident4){
			$('screenshot-loader').setStyles({
				opacity:1
			});
		} else {
			$('screenshot-loader').morph({
				opacity:1
			});
		}
		this.screenshot = new Asset.image(el.getElement('img').get('label'),{
			id:'screenshot-image',
			height:Ubisoft.GamePage.ssHeight,
			width:Ubisoft.GamePage.ssWidth,
			onload:function(){
				Ubisoft.GamePage.showScreenshot();
			}
		});
		this.currentThumb.addClass('on');
	},
	turnOn : function(el){
		if (Ubisoft.GamePage.disabled||el.hasClass('on')) return;
		var obj = el.getElement('.overlay');
		obj.morph({
			opacity:0,
			'border-color':'#000'
		});
		var obj = el.getElement('.image');
		obj.morph({
			'border-color':'#fff'
		});
	},
	turnOff : function(el){
		if (Ubisoft.GamePage.disabled||el.hasClass('on')) return;
		var obj = el.getElement('.overlay');
		obj.morph({
			opacity:0.5,
			'border-color':'#666'
		});
		var obj = el.getElement('.image');
		obj.morph({
			'border-color':'#000'
		});
	},
	showScreenshot : function(){
		this.screenshotObj.empty();
		this.screenshotObj.innerHTML = '<img src="'+this.screenshot.src+'" height="'+Ubisoft.GamePage.ssHeight+'" width="'+Ubisoft.GamePage.ssWidth+'" id="screenshot-image" />';
		if (BrowserObject.Engine.trident4){
			$('screenshot-loader').setStyles({
				opacity:0
			});
		} else {
			$('screenshot-loader').morph({
				opacity:0
			});
		}
		this.disabled = false;
	}
};

window.addEvent('domready',function(){
	Ubisoft.GamePage.init();
});

