/* @Perfect World Entertainment
 * @class PB (PWE Banner)
 * @requires document, Page, jQuery
 * @description Main portal banner
 *
 */

PB = {
	banners: null,
	backgrounds: null,
	thumbs: null,
	gameLink: null,
	regLink: null,
	videoBox: null,
	mblbbg: null,
	mbvidContainer: null,
	curBanner: 0,
	interval: null,
	timeout: null,
	lock: false,
	bgLocation: '',
	curGame: '',	
	lbVisible: false,
	videoTimeout: false,

	init: function(){	
		PB.banners = $('#banners').find('a.banner');
		PB.thumbs = $('#thumbs').find('li');
		PB.gameLink = $('#bannerGameLink');
		PB.regLink = $('#bannerRegLink');
		PB.videoBox = $('#videoLink');		
		PB.mblbbg = $('#mblbbg');
		PB.mbvidContainer = $('#mbvid-container');
		PB.bgLocation = PB.videoBox.attr('bglocation');
		PB.curGame = PB.banners.eq(0).attr('game');		
		PB.backgrounds = $('.banner-bg');

		PB.bindEvents();
		PB.formatBanners();	
		PB.restartTimer();	

		for(var i = 1; i<PB.backgrounds.length; i++) PB.backgrounds.eq(i).hide().css('left', 0);
	},

	bindEvents: function(){
		PB.thumbs.bind('click', PB.goToBanner);
		$(Page.win).bind('resize', PB.resizeInit);
		$(Page.win).bind('scroll', PB.resizeInit);
		PB.banners.hover(PB.stopTimer, PB.restartTimer);
		PB.videoBox.find('#videoPH').bind('click', PB.showLightBox);
		PB.mblbbg.bind('click', PB.hideLightBox);
		$('#mbclose').bind('click', PB.hideLightBox);	
	},

	formatBanners: function(){
		for (var i = 1; i<PB.backgrounds.length; i++) PB.banners.eq(i).hide();
		PB.banners.css('left', 0).width(Utils.calculateScreenWidth() - 17);		
	},

	// say no to code duplication!
	littleGreenFrog: function(){ // say yes to creative function names!
		PB.lock = true;
		PB.curGame = PB.banners.eq(PB.curbanner).attr('game');		

		PB.thumbs.removeClass('selected');
		PB.thumbs.eq(PB.curBanner).addClass('selected');

		PB.banners.hide().css('z-index', 4);
		PB.banners.eq(PB.curBanner).show().css('z-index', 6);

		PB.backgrounds.css('z-index', 2).hide();
		PB.backgrounds.eq(PB.curBanner).css('z-index', 3).fadeIn(350);

		PB.updateVideoBox();

		if ('en' != 'en') {
			var frame = $('.facebook-users iframe').contents();
			frame.find('.fbWrap').children().hide();
			frame.find('.fbfan').eq(PB.curBanner).show();

			for (var type in {'url_fb':1, 'url_tw':2, 'url_yt':3}) {
				var url = PB.banners.eq(PB.curBanner).attr(type);
				$('#col02 .' + type).attr('href', url);
			};
		}
	},

	//thumb is clicked
	goToBanner: function(e){
		var idx = PB.thumbs.index(this);
		if (idx == PB.curBanner || PB.lock) return;

		PB.curBanner = idx;
		PB.restartTimer();
		PB.littleGreenFrog();
	},

	//on interval
	nextBanner: function(){
		PB.curBanner = (PB.curBanner == PB.banners.length - 1) ? 0 : PB.curBanner + 1;

		PB.littleGreenFrog();
	},

	//updates video box on banner change

	updateVideoBox: function(){

		PB.videoBox.hide();
	
		var bg = 'black url('+PB.bgLocation+'th-'+PB.banners.eq(PB.curBanner).attr('game')+'.jpg) no-repeat';

		PB.gameLink.attr('href', PB.banners.eq(PB.curBanner).attr('href'));
		PB.regLink.attr('href', PB.banners.eq(PB.curBanner).attr('reg_url'));
		PB.videoBox.find('#videoPH').css('background', bg);

		setTimeout("PB.showVideoBox()", 350);
			
	},

	showVideoBox: function(){

		if($.browser.msie && $.browser.version == '8.0' || $.browser.msie && $.browser.version == '7.0'){

			PB.videoBox.show();

		}else{

			PB.videoBox.fadeIn();

		}

		PB.lock = false;

	},

	//on window scroll/resize

	resizeInit: function(){

		if(Page.doc.all){

			clearTimeout(PB.timeout);
			PB.timeout = setTimeout("PB.resize()", 10);

		}else{

			PB.resize();

		}

	},

	resize: function(){
		
		PB.banners.width(Utils.calculateScreenWidth());
		PB.mblbbg.width(Utils.calculateScreenWidth());
		PB.mbvidContainer.animate({

			top: 0,
			left: 0,
			marginLeft: Utils.calculateCenterX(PB.mbvidContainer),
			marginTop: Utils.calculateCenterY(PB.mbvidContainer)

		}, 300).dequeue();

	},

	stopTimer: function(){

		clearInterval(PB.interval);

	},

	restartTimer: function(){

		clearInterval(PB.interval);
		PB.interval = setInterval('PB.nextBanner()', 6000); 

	},

	//video image is clicked on the banner

	showLightBox: function(){

		if(PB.lbVisible) return;
		PB.lbVisible = true;

		PB.stopTimer();
		
		PB.mblbbg.hide().width(Utils.calculateScreenWidth()).height(Utils.calculateScreenHeight()).show();		
		
		PB.mbvidContainer.css('left', Utils.calculateCenterX(PB.mbvidContainer)).css('top', Utils.calculateCenterY(PB.mbvidContainer));

		PB.mbvidContainer.css('margin', '0px').animate({

			height: 373,
            width: 573,
			marginLeft: -286,
			marginTop: -186

		}, 350).dequeue();

		PB.videoTimeout = setTimeout("PB.writeVideo()", 500);

	},

	hideLightBox: function(){

		setTimeout("PB.mbvidContainer.animate({ marginLeft: 0, marginTop: 0, height: 0, width: 0 }, 350, PB.unlock).dequeue(); PB.mblbbg.hide();", 10);

		PB.removeVideo();

		PB.restartTimer();

	},

	unlock: function(){

		PB.lbVisible = false;

	},

	//used for video lightbox

	writeVideo: function(){

		var video = '<embed width="540" height="304" src="http://www.youtube.com/v/'+PB.banners.eq(PB.curBanner).attr('video')+'?version=3&amp;hl=en_US&amp;rel=0&autoplay=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" bgcolor="#000000"></embed>';
		PB.mbvidContainer.prepend(video);
		$('#mbclose').fadeIn(300);
		PB.mbvidContainer.find('iframe').eq(PB.curBanner).addClass('show-iframe');
	},

	removeVideo: function(){

		PB.mbvidContainer.find('embed').remove();
		$('#mbclose').hide();
		PB.mbvidContainer.find('iframe').removeClass('show-iframe');
	}

}

/* @class GameList
 * @requires document, Page, jQuery
 * @description controls the hover effects for the game list
 *
 */

GameList = {

	config: {OPEN_HEIGHT: 80, BASE_MARGIN_TOP: 100},
	lang: 'en',
	games: null,

	init: function(){

		if(GameList.lang == 'en') return;

		GameList.games = $('.gameListWrap').find('a');		
		GameList.bindEvents();

	},

	bindEvents: function(){

		GameList.games.each(function(){

			if($(this).find('.game-langs').length > 0) $(this).hover(GameList.showLangs, GameList.hideLangs);

		})

	},

	showLangs: function(){

		$(this).find('.game-langs').animate({

			height: GameList.config.OPEN_HEIGHT,
			marginTop: GameList.config.BASE_MARGIN_TOP - GameList.config.OPEN_HEIGHT

		}, 200).dequeue();		

	},

	hideLangs: function(){
	
		$(this).find('.game-langs').animate({

            height: 0,
            marginTop: GameList.config.BASE_MARGIN_TOP

        }, 200).dequeue();

	}

}


Page.queue(GameList.init);

