/**
 * @Perfect World Entertainment
 * @class Page
 * @requires document
 *
 * Page handles page initialization as well as running functions onload.  It also stores URL data and login methods
 * To add a function to be run on page load use the queue method
*/

Page = {

	onLoadQueue: [], //queues functions to run on page load
	initQueue: [], //queues functions to run after the header has loaded

	config: {LOCATION: location.href, HOST: location.host, MODULE: null, SECTION: null, SUB_SECTION: null, QUERY_STRING: decodeURI(location.search).replace('?', '')},
	
	//global functions that should load on Page load should be here
	
	init: function(){
		
		for(var i=0; i<Page.initQueue.length; i++) Page.initQueue[i]();		
	
	},

	//all functions that should be run on Page load should be added here

	onLoad: function(){
				
		for(var i=0; i<Page.onLoadQueue.length; i++) Page.onLoadQueue[i]();

	},

	//queue method to queue functions to be run later, if the second param is 'init' it will run after the header as loaded, else defaults to page load

	queue: function(fn, type){
		
			(type == 'init')? Page.initQueue.push(fn) : Page.onLoadQueue.push(fn);
	},

	//login function (legacy)

	login: function(elem){

		 elem.action = 'https://account.perfectworld.com/login';
         elem.submit();

	},

	//checks if user hits enter on this site (legacy)

	checkEnter: function(e, elem){

		if (!e) var e = window.event
        if (e && e.keyCode == 13) Page.login(elem);
        
	},

	removeFocus: function(){

		if(document.getElementsByTagName) {
           
			var a = document.getElementsByTagName("a");
           
			for(var i = 0; i < a.length; i++) a[i].onfocus = function(){this.blur();};
       
       }

	}
	
}

/* @Perfect World Entertainment Tabs Framework
 * @class Tabs
 * @requires Page, document, jQuery
 *
 * This binds a click event to every element with the class Tabs.config.CLASS.  
 * Once clicked it looks for the container with the ID this.id + Tabs.config.SUFFIX.
 * It then hides all other containers with the class Tabs.config.CONTENT_CLASS and 
 * shows the container with the id this.id + Tabs.config.SUFFIX.  Assumes the tabs parent.parent is
 * a container element that also holds the tab-content containers.
 */ 

Tabs = {
	
	config: {CLASS: 'tab', SUFFIX: '-tab', CONTENT_CLASS: 'tab-content', HIDDEN_CLASS: 'hidden'},
	Tabs: null,
	
	
	init: function(){
			
		Tabs.tabs = $('.'+Tabs.config.CLASS);
		Tabs.bindEvents();		
		
	},
	
	bindEvents: function(){
		
		$(Tabs.tabs).bind('click', Tabs.switchTab);	
		
	},
	
	switchTab: function(){
		
		var parent = $(this).parent().parent();
		var id = $(this).attr('id');
		var container = $('#'+id+Tabs.config.SUFFIX);		
		$('.selected').removeClass('selected');	
	
		$(parent).find('.'+Tabs.config.CONTENT_CLASS).addClass(Tabs.config.HIDDEN_CLASS);
		$(container).removeClass(Tabs.config.HIDDEN_CLASS);
		$(this).addClass('selected');
	}

}

/* @Perfect World Entertainment Lightbox Framework
 * @class LB
 * @requires Page, document, jQuery
 *
 * This binds a click event to every element with the class LB.config.CLASS.
 * Once clicked it finds the container with the id this.id + LB.config.SUFFIX.
 * it resizes the lightbox background and repositions the lightbox container 
 * then fades in both of them. Assumes the lightbox background and container are position absolute and hidden;
*/

LB = {

    config: {CLASS: 'lb', BG_ID: 'lbbg', SUFFIX: '-lb', LB_VISIBLE: 'lb-visible', LB_CLOSE: 'close', FADE_TIME: 300},
    isActive: false,
    to: null, //used with IE window resize
    sc: null, //used on window scroll
	curLightBox: null,

	//runs on Page load

	init: function(){

		$('.'+LB.config.CLASS).bind('click', LB.show);

    	$('#'+LB.config.BG_ID).bind('click', LB.hide);

    	(document.all)? $(document).bind('keydown', LB.testEscape) : $(window).bind('keydown', LB.testEscape);
    	(document.all)? $(window).bind('resize', LB.set) : $(window).bind('resize', LB.reposition);
    	$(window).bind('scroll', LB.setScroll);

	},

    show: function(){
		
		if(Page.config.MODULE == 'dqrewards'){

			var server = $('#server_id').val();
			var char = $('#char_id').val();

			if(server == -1 || char == -1)  return;	

		}


        LB.isActive = true;

        var id = $(this).attr('id');
        var LBox = $('#'+id+LB.config.SUFFIX);

		LB.curLightBox = LBox;	

        if($(LBox).length == 0) return;

        $('#'+LB.config.BG_ID).width(Utils.calculateScreenWidth()).height(Utils.calculateScreenHeight());
        $('#'+LB.config.BG_ID).fadeIn(LB.config.FADE_TIME);

        $(LBox).css('left', Utils.calculateCenterX(LBox)).css('top', Utils.calculateCenterY(LBox));

        $(LBox).fadeIn(LB.config.fadeTime).addClass(LB.config.LB_VISIBLE);

        $(LBox).find('.'+LB.config.LB_CLOSE).bind('click', LB.hide);

    },

    hide: function(){

        $('.'+LB.config.LB_VISIBLE).fadeOut(LB.config.FADE_TIME);
        $('.'+LB.config.LB_VISIBLE).removeClass(LB.config.LB_VISIBLE);
        $('#'+LB.config.BG_ID).fadeOut(LB.config.FADE_TIME);
		$(LB.curLightBox).fadeOut(LB.config.FADE_TIME);

        LB.isActive = false;

    },

	//runs when window is resized

	reposition: function(){

        if(!LB.isActive) return;

        $('.'+LB.config.LB_VISIBLE).each(function(){

            $(this).animate({

                left: Utils.calculateCenterX($(this)),
                top: Utils.calculateCenterY($(this))

            }, LB.config.FADE_TIME);


        })

        $('#'+LB.config.BG_ID).width(Utils.calculateScreenWidth()).height(Utils.calculateScreenHeight());


    },

	//sets timeout on window resize

    set: function(){

        clearTimeout(LB.to);
        LB.to = setTimeout("LB.reposition()", 100);

    },
	
	//when window is scrolled

    setScroll: function(){

        if(!LB.isActive) return;

        clearTimeout(LB.sc);
        LB.sc = setTimeout("LB.reposition()", 100);

    },

	//if user hits escape while lightbox is active

    testEscape: function(e){

        if(!LB.isActive) return;

        var kc = e.keyCode;

        if(kc == 27) LB.hide();

    }

}

/* @Perfect World Entertainment
 * @Class DD
 * @requires Page, document, jQuery
 *
 * Custom Dropdown Framework, finds all elements with the class config.DD_CLASS, targets the first child div as the button. 
 * Toggles visibility on the UL inside the parent div.  When a LI is clicked looks for the attribute config.VALUE_ATTR
 * and finds the hidden input with the id of the same id as the element with class .dd minus the '-dd'.  Example:
 * <div id="myInput-dd" class="dd"></div> => <input type="hidden" id="myInput" name="myName />
 */

DD = {
	
	config: {DD_CLASS: 'dd', SUFFIX: '-dd', VALUE_ATTR: 'value'},
	
	init: function(){
	
		if($('.'+DD.config.DD_CLASS).length > 0) DD.bindEvents();
		
	},
	
	bindEvents: function(){
		
		$('.'+DD.config.DD_CLASS).find('div').eq(0).bind('click', DD.toggleDD);
		$('.'+DD.config.DD_CLASS).find('li').bind('click', DD.setValue);		
		
	},
	
	toggleDD: function(){
		
		var parent = $(this).parent();
		var menu = parent.find('ul');
		
		(menu.is(':visible'))? menu.hide() : menu.show();

	},
	
	setValue: function(){
		
		var val = $(this).attr(DD.config.VALUE_ATTR);
		var inputId = $(this).parent().parent().attr('id').replace(DD.config.SUFFIX, '');
		
		$('#'+inputId).val(val);
	}
	
}

//------------- end global.js objects ----------------------------//

Page.queue(Tabs.init);
Page.queue(LB.init);
Page.queue(Utils.getURLComponents, 'init');
Page.queue(DD.init);

$(document).ready(Page.onLoad);	


