/* @Perfect World Entertainment
 * @class CB (Content Banner)
 * @requires document, Page, jQuery
 * @description rotating banner for the landing page
 *
 */


CB = {

	banners: null, //holds banners
	thumbs: null, //holds thumbs
	descs: null, //holds containers with text
	curBanner: 0, //the current banner
	interval: null, //holds setInterval function object
	timeout: null,
	lock: false,

	init: function(){

		CB.banners = $('#main-banner').find('img').parent();
		CB.thumbs = $('#banner-thumbs').find('li');
		CB.descs = $('#banner-message').find('.banner-desc');

		CB.formatVideoBanners();
		CB.bindEvents();
		CB.startBannerInterval();

	},

	bindEvents: function(){

		CB.thumbs.bind('click', CB.switchBanner);

		CB.banners.each(function(){

			if($(this).attr('videodata')) $(this).bind('click', CB.showBannerVideo);

		});		
	
		$('#bvlbbg, #bv-close').bind('click', CB.hideBannerVideo);
	},

	switchBanner: function(){

		var idx = $(this).attr('id').replace('th', '');

		if(idx == CB.curBanner || CB.lock) return;

		CB.lock = true;

		CB.stopBannerInterval();
		CB.startBannerInterval();

		CB.banners.eq(CB.curBanner).fadeOut(300).dequeue();
		
		CB.curBanner = idx;

		CB.banners.eq(CB.curBanner).fadeTo(300, 1).dequeue();

		CB.updateThumbs();
		CB.updateMessages();

		setTimeout("CB.lock = false", 300);

	},

	rotateBanner: function(){
	
		CB.banners.eq(CB.curBanner).fadeOut(300).dequeue();
		
		(CB.curBanner < 4)? CB.curBanner++ : CB.curBanner = 0;

		CB.banners.eq(CB.curBanner).fadeTo(300, 1).dequeue();

		CB.updateThumbs();
		CB.updateMessages();

	},

	updateThumbs: function(){
		
		CB.thumbs.removeClass('selected');
		CB.thumbs.eq(CB.curBanner).addClass('selected');

	},

	updateMessages: function(){

		CB.descs.each(function(){

			if($(this).is(':visible')) $(this).fadeOut(300);
	
		});

		CB.descs.eq(CB.curBanner).fadeTo(300, 1).dequeue();

	},

	stopBannerInterval: function(){

		clearInterval(CB.interval);

	},

	startBannerInterval: function(){

		CB.interval = setInterval("CB.rotateBanner()", 4000);

	},

	showBannerVideo: function(){

		CB.stopBannerInterval();

		var vidUrl = $(this).attr('videodata');
		var c = $('#banner-video-container');		

		CB.writeVideo(c, vidUrl);

		$('#bvlbbg').hide().width(Utils.calculateScreenWidth()).height(Utils.calculateScreenHeight()).fadeIn(300);

		c.css('left', Utils.calculateCenterX(c)).css('top', Utils.calculateCenterY(c)).show();	

		$(Page.win).bind('resize', CB.setScroll);
		$(Page.win).bind('scroll', CB.setScroll);

	},

	hideBannerVideo: function(){

		$('#bvlbbg').fadeOut(300);
		$('#banner-video-container').hide();
		CB.removeVideo();
		CB.startBannerInterval();
	
		$(Page.win).unbind('resize', CB.setScroll);
        $(Page.win).unbind('scroll', CB.setScroll);

	},

	writeVideo: function(cont, url){

		cont.prepend('<embed src="'+url+'" type="application/x-shockwave-flash" width="488" height="304" allowscriptaccess="always" allowfullscreen="true"></embed>');

	},

	removeVideo: function(){

		$('#banner-video-container').find('embed').remove();

	},

	formatVideoBanners: function(){
		
		for(var i = 0; i<5; i++){

			if($('.banner-desc').eq(i).hasClass('has-video')){
				
				CB.banners.eq(i).removeAttr('href');				
				CB.banners.eq(i).find('span').addClass('video-icon');
				CB.thumbs.eq(i).find('div').addClass('video-thumb');				
			}

		}

	},

	setScroll: function(){

		clearTimeout(CB.timeout);
		CB.timeout = setTimeout("CB.scroll()", 10);

	},

	scroll: function(){

		var b = $('#banner-video-container');

		b.animate({

			top: Utils.calculateCenterY(b),
			left: Utils.calculateCenterX(b)

		}, 350).dequeue();

	}

}

/* @class BlogAccordion
 * @requires document, Page, jQuery
 * @description Accordion script for front page blogs
 *
 */

BlogAccordion = {

	blogs: null,
	height: 150, //open height
	closedHeight: 17, //closed height

	init: function(){
		
		BlogAccordion.blogs = $('.closed');
		BlogAccordion.bindEvents();

	},

	bindEvents: function(){

		BlogAccordion.blogs.find('h2').bind('click', BlogAccordion.slideDown);
		
	},

	slideUp: function(e){

		if($(this).attr('class') == 'blog-title') return; //if the title <a> is clicked do nothing since the user is leaving the page

		var blog = $(this).parent().parent();

		$(this).unbind('click', BlogAccordion.slideUp);

		blog.animate({ height: BlogAccordion.closedHeight}, 250);

        blog.find('h2').find('span').html('&#43;');	

		e.stopPropagation(); //stops click event from bubbling to the h2 so  slideDown doesnt fire

	},

	slideDown: function(e){
			
		if(e.target.href) return; //if the title <a> is clicked do nothing since the user is leaving the page
	
		var blog = $(this).parent();
		$(this).find('span').bind('click', BlogAccordion.slideUp);

		blog.animate({height: BlogAccordion.height}, 250);

		blog.find('h2').find('span').html('&#45;');
	}
}

/* @class GameTheater
 * @requires document, Page, jQuery
 * @description Main Video Theater for game headers
 * !IMPORTANT! This script is formatted for one video with the facebook like button
 */

GameTheater = {

	config: {OPEN_HEIGHT: 360, CLOSED_HEIGHT: 200, THEATER_CONTAINER_WIDTH: 472},
	container: null,
	theaterContainer: null,
	theaterLeft: null,
	displayVideo: null,
	videoData: null,
	character: null,
	buttons: {newBtn: null, playBtn: null, freeBtn: null, regBtn: null, closeBtn: null},
	isIE: false,
	isIE7: false,
	lock: false,
	firstVisit: false,

	init: function(){

		GameTheater.setElementReferences();
		GameTheater.bindEvents();
		GameTheater.checkIE();

		if(!GameTheater.firstVisit) GameTheater.theaterContainer.hide().width(0).css('left', Utils.calculateCenterX(GameTheater.theaterContainer) + 50);

	},

	setElementReferences: function(){

		GameTheater.container = $('.bg-media');
        GameTheater.theaterContainer = $('#theater-container');
        GameTheater.theaterLeft = $('#theater-btns-left');
        GameTheater.character = $('.bg-character');
        GameTheater.buttons.newBtn = $('.btn-playnew');
        GameTheater.buttons.playBtn = $('.btn-playgame');
        GameTheater.buttons.closeBtn = $('.btn-theaterclose');
		GameTheater.buttons.regBtn = $('.btn-closedbeta');
        GameTheater.displayVideo = $('#display-video');
        GameTheater.videoData = $('#video-info').attr('videodata');

	},

	bindEvents: function(){

		GameTheater.buttons.newBtn.bind('click', GameTheater.openTheater);
		GameTheater.buttons.closeBtn.bind('click', GameTheater.closeTheater);
		$(Page.win).bind('resize', GameTheater.onChange);

	},

	//This is here because fading transparent .png's in IE7/8 causes transparency to turn black, also reduces animation in IE7 
	//due to peformance issues

	checkIE: function(){

		if(document.all){

			if(parseInt($.browser.version) < 9){

				 GameTheater.isIE = true;

				if($.browser.version == '7.0') GameTheater.isIE7 = true;

			}

		}

	},

	openTheater: function(){
			
		if(!GameTheater.lock){

			GameTheater.hideCharacter();
			GameTheater.lock = true;
	
		}

	},

	closeTheater: function(){

		if(!GameTheater.lock){

			GameTheater.animateTheaterContainer(0);
			GameTheater.lock = true;		
		
		}

	},

	hideCharacter: function(){

		if(GameTheater.isIE){

			GameTheater.character.hide();
			setTimeout("GameTheater.toggleContainer()", 100);

		}else{

			GameTheater.character.fadeOut(300);		
			setTimeout("GameTheater.toggleContainer()", 400);

		}

	},

	showCharacter: function(){

		(GameTheater.isIE)? GameTheater.character.show() : GameTheater.character.fadeIn(300);

		if(GameTheater.firstVisit){

			GameTheater.theaterContainer.css('left', Utils.calculateCenterX(GameTheater.theaterContainer) + 50);
			GameTheater.firstVisit = false;

		}

		setTimeout("GameTheater.lock = false", 300);

	},

	toggleContainer: function(key){

		if(key != 0){

			if(!GameTheater.isIE7){

				GameTheater.container.animate({

            		height: GameTheater.config.OPEN_HEIGHT

        		}, 256);

				setTimeout("GameTheater.animateTheaterContainer()", 400);

			}else{

				GameTheater.container.height(GameTheater.config.OPEN_HEIGHT);

				GameTheater.animateTheaterContainer();

			}

			GameTheater.buttons.playBtn.hide();
            GameTheater.buttons.regBtn.hide();

		}else{

			if(!GameTheater.isIE7){

				GameTheater.container.animate({

                	height: GameTheater.config.CLOSED_HEIGHT

            	}, 256);
				
				setTimeout("GameTheater.showCharacter()", 500);

			}else{

				GameTheater.container.height(GameTheater.config.CLOSED_HEIGHT);
                GameTheater.showCharacter();

			}

			GameTheater.buttons.newBtn.show();
            GameTheater.buttons.regBtn.show();
			GameTheater.removeMovie();

		}

	},

	animateTheaterContainer: function(key){
		
		if(key != 0){

			if(!GameTheater.isIE7){

				GameTheater.theaterContainer.width(0).show().animate({

            		width: GameTheater.config.THEATER_CONTAINER_WIDTH,
            		marginLeft: 0 - GameTheater.config.THEATER_CONTAINER_WIDTH/2

        		}, 256);

				setTimeout("GameTheater.writeMovie(0); GameTheater.lock = false;", 350);

			}else{

				GameTheater.theaterContainer.width(GameTheater.config.THEATER_CONTAINER_WIDTH).show().css('margin-left', 0 - GameTheater.config.THEATER_CONTAINER_WIDTH/2);

                GameTheater.writeMovie(0);
				GameTheater.lock = false;

			}

			GameTheater.buttons.newBtn.hide();
            GameTheater.buttons.closeBtn.show();
            GameTheater.theaterLeft.show();

		}else{

			if(!GameTheater.isIE7){

				GameTheater.theaterContainer.animate({

                	width: 0,
                	marginLeft: 0

            	}, 256);

				setTimeout("GameTheater.toggleContainer(0)", 350);

			}else{

				GameTheater.theaterContainer.hide();
                GameTheater.toggleContainer(0);

			}

			GameTheater.buttons.newBtn.hide();
            GameTheater.buttons.closeBtn.hide();
			GameTheater.theaterLeft.hide();

		}

	},

	writeMovie: function(idx){

		GameTheater.displayVideo.html(GameTheater.videoData);

	},

	removeMovie: function(){

		GameTheater.displayVideo.find('embed').remove();

	},

	onChange: function(){

		clearTimeout(GameTheater.timeout);
		GameTheater.timeout = setTimeout("GameTheater.reposition(236)", 10);

	},

	reposition: function(){

		GameTheater.theaterContainer.css('left', $('.bg-media').offset().left + 540);

	}

}


//Page.queue(GameTheater.init);
Page.queue(BlogAccordion.init);

