
//名前空間用のオブジェクトを用意する
var STVIEW = {};

STVIEW.latitude = '';
STVIEW.longitude = '';

//Login Process
STVIEW.processLogin = function(cid,sid) {
	var requireChangePassword = 0;
	var skipPwdDate;
	
	var params = {
		cid: cid,
		sid: sid
	};
	
	// Get url to login
	var apiLoginUrl = ClientData.conf_apiLoginUrl();
	
	$('#main-error-message').html("loading...");
	$('#main-error-message').show();
	
	AVWEB.avwCmsApiWithUrl(apiLoginUrl, null, 'webClientStreamingLogin', 'GET', params, function (data) {
		//requirePasswordChange = data.requirePasswordChange;
		LOGIN.userinfo_sid = data.sid;
		LOGIN.userInfo_userName = data.userName;
		LOGIN.optionList = data.serviceOptionList;
		
		LOGIN.getServiceOptionList();
		
		if (data.result == 'success') {
			
			// Save retrieved info
			STVIEW.saveLoginInfo(data);
			
			// set number new push message to 0
			ClientData.pushInfo_newMsgNumber(0);
			
			//ストリーミングモード有効化
			ClientData.isStreamingMode(true);
			//GPS情報
			if( STVIEW.latitude != '' ){
				ClientData.latitude(STVIEW.latitude);
			}
			if( STVIEW.longitude != '' ){
				ClientData.longitude(STVIEW.longitude);
			}
			
			$('#main-error-message').css('display', 'none');
			if (data.requirePasswordChange == 0) {
				ClientData.userInfo_sid(ClientData.userInfo_sid_local());
				
				//$('#main-error-message').html("login ok move to:" + COMMON.ScreenIds.ContentViewStreaming);
				//$('#main-error-message').show();
				
				//コンテンツIDセット
				ClientData.contentInfo_contentId(cid);
				//ストリーミングのビューアへ移動
				AVWEB.avwScreenMove("abvw/" + COMMON.ScreenIds.ContentViewStreaming + "?__UPDATEID__");
				
			} else {
				$('#main-error-message').html(AVWEB.format(I18N.i18nText('msgLoginErrWrong'), 'E001'));
				$('#main-error-message').show();
				return;
			}
		}
		else {
            LOGIN.login_errorMessage = data.errorMessage;
            $('#main-error-message').html(AVWEB.format(I18N.i18nText('msgLoginErrWrong'), data.errorMessage).toString());
            $('#main-error-message').show();
            alert("Open Error1!");
        }
        
    }, function (xhr, statusText, errorThrown) {
    	
        if (xhr.responseText && xhr.status != 0) {
            LOGIN.login_errorMessage = JSON.parse(xhr.responseText).errorMessage;
            
        	alert("Open Error21!:" + LOGIN.login_errorMessage);
            
            $('#main-error-message').html(AVWEB.format(I18N.i18nText('msgLoginErrWrong'), JSON.parse(xhr.responseText).errorMessage).toString());
        } else {
        	
        	alert("Open Error22!:E001");
        	
            $('#main-error-message').html(AVWEB.format(I18N.i18nText('msgLoginErrWrong'), 'E001'));
        }
        $('#main-error-message').show();
    });
	
};

//check Save Login Info
STVIEW.saveLoginInfo = function(data) {

	var lang = I18N.getCurrentLanguage();
	// load language
	I18N.changeLanguage(lang);

	var accountPath = data.urlPath;
	var loginId = data.loginId;
	var date = new Date();
    ClientData.userInfo_accountPath(accountPath);
    ClientData.userInfo_loginId(loginId);
    ClientData.userInfo_accountPath_session(accountPath);
    ClientData.userInfo_loginId_session(loginId);
    ClientData.userInfo_userName(LOGIN.userInfo_userName);

    ClientData.userInfo_lastLoginTime(date.jpDateTimeString());
	ClientData.userInfo_sid_local(LOGIN.userinfo_sid);
	LOGIN.saveServiceUserOption();
	
	//alert("url:" + ClientData.userInfo_accountPath() + " loginId:" + ClientData.userInfo_loginId());
	
	// 不要なフラグ落とす
	ClientData.common_contentDataChkFlg(false);
	ClientData.userInfo_rememberLogin(false);
	
	//ページジャンプ設定をクリア
	ClientData.JumpQueue([]);
	ClientData.IsJumpBack(false);
	
};

//$(document).ready(function () {
STVIEW.ready = function(){
	
	//$('#main-error-message').html( window.location.href );
	//$('#main-error-message').show();
	//return;
	
	//引数の確認
	var sid = COMMON.getUrlParam('sid', '');
	var cid = COMMON.getUrlParam('cid', '');
	var page = COMMON.getUrlParam('page', '');
	var latitude = COMMON.getUrlParam('latitude', '');
	var longitude = COMMON.getUrlParam('longitude', '');
	var reload = COMMON.getUrlParam('reload', '');
	
	var isView = false;
	if( !sid || !cid ){
		//表示出来ない
		$('#main-error-message').html(AVWEB.format(I18N.i18nText('msgLoginErrWrong'), 'E001'));
		$('#main-error-message').show();
		return;
	}
	
	var isReload = false;
	if( reload != "" ){
		if( reload == "true" ){
			isReload = true;
		}
	}
	
	//---初期化
	if( isReload == false ){
		//セッションストレージクリア
		SessionStorageUtils.clear();
		AVWEB.avwUserSessionObj = null;
		// create new session
		AVWEB.avwCreateUserSession();
	}
	I18N.initi18n();

	var sysSettings = AVWEB.avwSysSetting(); // get info in conf.json
	
	//モード初期化
	ClientData.isGetitsMode(false);
	ClientData.isStreamingMode(false);
	
	//confのパラメータセット
	ClientData.conf_apiUrl( sysSettings.apiUrl );
	ClientData.conf_apiLoginUrl( sysSettings.apiLoginUrl );
	ClientData.conf_apiResourceDlUrl( sysSettings. apiResourceDlUrl );
	
	if( isReload == false ){
		//念のため前回までの閲覧ログは削除
		ClientData.ContentLogData([]);
	}
	
	//ページ番号指定有り
	if( page == "" || page == "0" || page == "1"){
		ClientData.common_prePageNo(null);
	} else {
		ClientData.common_prePageNo(page);
	}
	//GPS
	if( latitude != "" ){
		STVIEW.latitude = latitude;
	}
	if( longitude != "" ){
		STVIEW.longitude = longitude;
	}
	
	if( STVIEW.latitude != "" && STVIEW.longitude != "" ){
		STVIEW.processLogin(cid,sid);
	} else {
		setTimeout(function (){
			STVIEW.processLogin(cid,sid);
		}, 1000);
	}
};
//});


