rocknoon.namespace("Eccky.Page.Layout");

(
function(){
	
	/**
	 * Layout Page Manager
	 */
	var layout = rocknoon.Class({
		
		_dialog : null,
		_dialog_content : null,
		_dialog_iframe : null,
		_dialog_close : null,
		_userNameDefault : null,
		_dialog_close_callback : null,
		
		initialize : function(){
		
			this._initCtl();
			this._initEvent();
			
		},
		
		show : function(){

			if( !__IS_LOGIN__ ){
				this._showLoginForm();
			}else{
				this._showUserInfo();
			}
			
		},
		
		login : function(response){
			
			if(response.status){
				  var data = {};
			      data.user_id 		= response.info.user_id;
			      data.user_name 	= response.info.user_name;
			      data.diamonds    	= response.info.diamonds;
			      data.coins    	= response.info.coins;
			      data.user_level    	= response.info.user_level;
			      data.messages_count    	= response.info.messages_count;
			      data.character_pic    	= response.info.character_pic;
			      data.shead_shot    	= response.info.shead_shot;
			      data.lhead_shot    	= response.info.lhead_shot;
			      data.token    	= response.info.token;
			      data.from    		= response.info.from;
			      data.is_first    		= response.info.isFirst;
			      data.status    		= response.info.status;
			      data.is_membership    = response.info.is_membership;
			      
			      Eccky.Ajax.User.Login( data , rocknoon.callback( this , "_onLogin" ) );
			
			      //如果是Play 页面的话 从新加载 Play Flash 非常 HACK 的做法 不推荐
			      if( __CONTROLLER__ == "thegame" && __ACTION__ == "play" ){
			    	  Eccky.Api.BackService.Play( "flashContainer" , data.token , data.from, data.is_first  , data.status, __LANGUAGE_TAG__ );
			      }
			
			}else{
				this._showError(response.info );
			}
			
		},
		
		/**
		 * data = {
		 * 
		 * 	diamonds
		 *  coins
		 *  user_level
		 *  message_count
		 *  character_pic
		 *  shead_shot
		 *  lhead_shot
		 *  
		 * }
		 * 
		 * notice : 改变之后 要更新Session里边的用户数据
		 */
		profile : function( data ){
			this._changeProfile( data );
			
			data.user_id = __LOGIN_USER__.id;
			Eccky.Ajax.User.Update( data , rocknoon.callback( this , "_onUpdate" ) );
		},
		
		showDialog : function( url , width , height ){
			
			$('#douse').fadeIn(200);
			focusBox( this._dialog );
			
			//specify width and height for iframe
			this._dialog_iframe.width(width);
			this._dialog_iframe.height(height);
			this._dialog_iframe.attr('src', url);
			
			this._dialogZoomInEffect(width , height);
			
			
		},
		
		setCloseDialogCallback : function( callbackfunction ){
			
			this._dialog_close_callback = callbackfunction;
			
		},
		
		closeDialog : function(){
			this._dialog.hide().css({width:'60px',height:'60px'});
			$('#douse').fadeOut(300);
			this._dialog_content.hide();
			
			if( this._dialog_close_callback ){
				this._dialog_close_callback();
			}
		},
		
		_changeProfile : function( data ){
			
			if( data.user_name != undefined ){
				$("#userinfo_username").html( data.user_name );
			}
			
			if( data.character_pic != undefined ){
				$("#userinfo_character_pic").attr( "src" , data.character_pic );
			}
			
			if( data.diamonds != undefined ){
				$("#userinfo_diamonds").html(data.diamonds);
			}
			
			if( data.coins != undefined ){
				$("#userinfo_coins").html(data.coins);
			}
			
			if( data.messages_count != undefined ){
				$("#userinfo_messages_count").html(data.messages_count);
			}
			
			if( data.level != undefined ){
				$("#userinfo_level").html(data.level);
			}
			
		},
		
		_dialogZoomInEffect :　function( toWidth, toHeight){
			var hackThis = this;
			
			var windowW = $('body').width();
			var windowH = $(window).height();
			toTop = windowH/2-toHeight/2-8+$(document).scrollTop();
			toLeft = windowW/2-(toWidth + 24)/2;
			this._dialog.animate({height:toHeight,width:toWidth,top:toTop,left:toLeft},800, function(){
				hackThis._dialog_content.fadeIn();
			});
		},
		
		_initCtl : function(){
			
			this._dialog = $('#dialog');
			this._dialog_iframe  = $('#dialog_iframe');
			this._dialog_close  = $('#dialog_close');
			this._dialog_content  = $('#dialog_content');
			
		},
		
		_initEvent : function(){
			/**
			 * hyves & facebook login button
			 */
			$("#hyves_login")	.click( rocknoon.callback( this , "_doHyvesLogin") );
			$("#facebook_login").click( rocknoon.callback( this , "_doFacebookLogin") );
			
			/**
			 * eccky login button
			 */
			$("#eccky_login").click( rocknoon.callback( this , "_doEcckyLogin") );
			
			
			
			$("#userinfo_loginout_button").click( rocknoon.callback( this , "_doLogout") );
		
			//dialog close
			this._dialog_close.click( rocknoon.callback( this , "closeDialog") );
		},
		
		_doLogout : function(){
			Eccky.Ajax.User.Logout(rocknoon.callback( this , "_onLogout" ) );			
		},
		
		_showUserInfo : function(){
			$("#userinfo").show();
		},
		
		_showLoginForm : function(){
			$("#loginForm").show();
		},
		
		
		/**
		 * when click facebook login button
		 */
		_doFacebookLogin : function(){

			Eccky.Api.BackService.FacebookLogin();
			
		},
		
		_doHyvesLogin : function(){
			
			Eccky.Api.BackService.HyvesLogin();
			
		},
		
		/**
		 * when click facebook login button
		 */
		_doEcckyLogin : function(){
			
			var username = $("#naam").val();
			var password = $("#wachtwoord").val();
			
			if( rocknoon.validate.is_empty( username ) || username == this._userNameDefault ){
				this._showError( layout.Error.ShoudInputUserName );
				return;
			}
			
			if( rocknoon.validate.is_empty( password ) ){
				this._showError( layout.Error.ShoudInputPassword );
				return;
			}
			
			//show loading status
			this._loadingEcckyLogin(true);
			
			
			var data = {};
			data.username = username;
			data.password = password;
			
			Eccky.Api.BackService.EcckyLogin( data , rocknoon.callback( this , "_afterEcckyLogin") );
		},
		
		_afterEcckyLogin : function( response ){
			
			//change status
			this._loadingEcckyLogin(false);
			
			//login process is done by backservice, this is not a good way to implement like this.
			
		},
		
		_onUpdate : function( response ){
			
			var rst = rocknoon.ajax.prase(response);
			if( rst.status ){
				
			}else{
				this._showError(rst.info);
			}
			
		},
		
		_onLogin : function( response ){
			
			var rst = rocknoon.ajax.prase(response);
			if( rst.status ){
				this._successLogin(rst.info);
				//location.reload();
			}else{
				this._showError(rst.info);
			}
		},
		
		_onLogout : function( response ){
			var rst = rocknoon.ajax.prase(response);
			if( rst.status ){
				
				this._successLogout(rst.info);
				
				//如果是Play 页面的话 从新加载 Play Flash 非常 HACK 的做法 不推荐
			      if( __CONTROLLER__ == "thegame" && __ACTION__ == "play" ){
			    	  location.href = __BASE_URL__ ;
			      }
				
			}else{
				this._showError(rst.info);
			}
		},
		
		_loadingEcckyLogin : function( flag ){
			if( flag ){
				$("#eccky_login").hide();
				$("#eccky_login_loading").show();
			}else{
				$("#eccky_login").show();
				$("#eccky_login_loading").hide();
			}
			
		},
		
		_successLogin : function( userInfo ){
			
			$("#userinfo_username").html( userInfo.user_name );
			$("#userinfo_character_pic").attr( "src" , userInfo.character_pic );
			$("#userinfo_diamonds").html(userInfo.diamonds);
			$("#userinfo_coins").html(userInfo.coins);
			$("#userinfo_messages_count").html(userInfo.messages_count);
			$("#userinfo_level").html(userInfo.level);
			
			
			if( userInfo.is_membership == "1" || userInfo.is_membership == 1 ){
				$("#userinfo_is_membership").show();
			}else{
				$("#userinfo_is_membership").hide();
			}
			
			$("#userinfo").show();
			$("#loginForm").hide();
			$("#registerForm").hide();
			
		},
		_successLogout : function( userInfo ){
			$("#userinfo").hide();
			$("#loginForm").show();
			$("#registerForm").show();
		},
		
		_showError:function(error){
			alert(error);
			
		}
	});
	
	
	/**
	 * Singel method
	 */
	layout.Instance = function(){
		if( layout._instance == undefined ){
			layout._instance = new layout();
		}
		return layout._instance;
	};
	
	layout.Error = {};
	
	
	Eccky.Page.Layout = layout;
	
}		
)();

