HH.WorldCup = {};


HH.WorldCup._game_config = {
	1: {
		swf: '/static/flash/world_cup/game_motivation_1/main.swf',
		fv: {
			asset_root: '/assets/world_cup/game_motivation_1/',
			swf_root:   '/static/flash/world_cup/'
		}
	},
	2: {
		swf: '/static/flash/world_cup/game_health_1/main.swf',
		fv: {
			asset_root: '/assets/world_cup/game_health_1/',
			swf_root:   '/static/flash/world_cup/'
		}
	},
	3: {
		swf: '/static/flash/world_cup/game_skill_1/main.swf',
		fv: {
			asset_root: '/assets/world_cup/game_skill_1/',
			swf_root:   '/static/flash/world_cup/'
		}
	},
	4: {
		swf: '/static/flash/world_cup/game_motivation_2/main.swf',
		fv: {
			asset_root: '/assets/world_cup/game_motivation_2/',
			swf_root:   '/static/flash/world_cup/'
		}
	},
	5: {
		swf: '/static/flash/world_cup/game_skill_2/main.swf',
		fv: {
			asset_root: '/assets/world_cup/game_skill_2/',
			swf_root:   '/static/flash/world_cup/'
		}
	},
	6: {
		swf: '/static/flash/world_cup/game_health_2/main.swf',
		fv: {
			asset_root: '/assets/world_cup/game_health_2/',
			swf_root:   '/static/flash/world_cup/'
		}
	}
};


HH.WorldCup.init = function() {
	Common.log('HH.WorldCup.init() called');
	jQuery('#main').before('<div id="world_container"></div>');
	jQuery('#world_container').addClass('flash').height('464px').html('<div id="world_content"><div id="world"><div id="world_flash"></div></div></div>');
	jQuery('#world_content').height('460px');
	HH.flash({
		id: 'world_flash',
		src: '/static/flash/world_cup/holder.swf',
		width: '100%',
		height: 460,
		express_install: '/static/flash/express_install.swf',
		flash_vars: {
			lang: Cube.lang.currentLanguage(),
			is_ie: jQuery.browser.msie,
			asset_root: '/assets/world_cup/holder/',
			api_root: '/api/en/world_cup/',
			swf_root: '/static/flash/world_cup/'
		},
		transparent: true
	});
	jQuery('body').append('<div id="fancybox_hidden" style="display: none;"><a class="overlay-flash iframe" id="fancy_clicker" href="#"></a></div>');
};


HH.WorldCup.game = function(n) {
	Common.log('HH.WorldCup.game(' + n + ') called');
	if (!HH.WorldCup._game_config[n]) {
		return;
	}
	if (jQuery.browser.msie) {
		jQuery('#flash_world_flash').css('visibility', 'hidden');
	}

	// Defaults
	var width  = 650;
	var height = 400;

	// Overrides
	if (HH.WorldCup._game_config[n]['width']) {
		width = HH.WorldCup._game_config[n]['width'];
	}
	if (HH.WorldCup._game_config[n]['height']) {
		height = HH.WorldCup._game_config[n]['height'];
	}

	var url = '/en/cup-game-' + n;
	jQuery('#fancy_clicker').attr('href', url);
	jQuery('a#fancy_clicker').fancybox({
		padding: 0,
		imageScale: false,
		frameWidth: width,
		frameHeight: height,
		hideOnContentClick: false,
		overlayOpacity: 0.9,
		overlayShow: true,
		callbackOnClose: function() {
			HH.WorldCup.fancyboxClosed();
		},
		centerOnScroll: true
	});
	jQuery('#fancy_clicker').trigger('click').unbind('click');
};


HH.WorldCup.fancyboxClosed = function() {
	Common.log('HH.WorldCup.fancyboxClosed() called');
	if (!jQuery.browser.msie) {
		var flashObj = document.getElementById('flash_world_flash');
		if (flashObj && (typeof(flashObj.fancyboxClosed) !== 'undefined')) {
			flashObj.fancyboxClosed();
		}
	} else {
		jQuery('#flash_world_flash').css('visibility', 'visible');
	}
};


HH.WorldCup.loadExternalGame = function(n) {
	Common.log('HH.WorldCup.loadExternalGame() called');
	if (!HH.WorldCup._game_config[n]) {
		return;
	}

	// Defaults
	var width  = 650;
	var height = 400;
	var fv     = {};

	// Overrides
	if (HH.WorldCup._game_config[n]['width']) {
		width = HH.WorldCup._game_config[n]['width'];
	}
	if (HH.WorldCup._game_config[n]['height']) {
		height = HH.WorldCup._game_config[n]['height'];
	}
	if (HH.WorldCup._game_config[n]['fv']) {
		fv = HH.WorldCup._game_config[n]['fv'];
	}
	jQuery('#container')
		.width(width + 'px')
		.height(height + 'px');
	HH.flash({
		id: 'flash',
		src: HH.WorldCup._game_config[n].swf,
		width: width,
		height: height,
		express_install: '/static/flash/express_install.swf',
		flash_vars: fv,
		callback: function() {
			return;
		}
	});
};
