
var EditDataAccesser = function(editorInfo){

	var cur_editorInfo = editorInfo;

	//デジブックのID
	this.getEditorInfo = function() {
		return cur_editorInfo;
	}

	
	//デジブックのID
	this.getDigiBookId = function() {
		return cur_editorInfo.digibookid;
	}

	//デジブックのタイトル
	this.getDigiBookTitle = function() {
		return cur_editorInfo.digibooktitle;
	}
	this.setDigiBookTitle = function(digibooktitle) {
		return cur_editorInfo.digibooktitle = digibooktitle;
	}

	//デジブックのサブタイトル
	this.getDigiBookSubTitle = function() {
		return cur_editorInfo.digibooksubtitle;
	}
	this.setDigiBookSubTitle = function(digibooksubtitle) {
		return cur_editorInfo.digibooksubtitle = digibooksubtitle;
	}

	//ジャケットオプション：タイトルフォント
	this.getOptionJacketFontType = function() {
		return cur_editorInfo.optioncodeToValue.jacketFontType;
	}
	this.setOptionJacketFontType = function(jacketFontType) {
		return cur_editorInfo.optioncodeToValue.jacketFontType = jacketFontType;
	}

	//ジャケットオプション：タイトル文字色
	this.getOptionJacketTitleColor = function() {
		return cur_editorInfo.optioncodeToValue.jacketTitleColor;
	}
	this.setOptionJacketTitleColor = function(jacketTitleColor) {
		return cur_editorInfo.optioncodeToValue.jacketTitleColor = jacketTitleColor;
	}

	//ジャケットオプション：ジャケット色
	this.getOptionJacketBackColor = function() {
		return cur_editorInfo.optioncodeToValue.jacketBackColor;
	}
	this.setOptionJacketBackColor = function(jacketBackColor) {
		return cur_editorInfo.optioncodeToValue.jacketBackColor = jacketBackColor;
	}

	//デジブックのテーマ
	this.getThemeId = function() {
		return cur_editorInfo.themeid;
	}
	this.setThemeId = function(themeid) {
		return cur_editorInfo.themeid = themeid;
	}

	//テーマオプション：フォント
	this.getOptionFontType = function() {
		return cur_editorInfo.optioncodeToValue.fontType;
	}
	this.setOptionFontType = function(fontType) {
		return cur_editorInfo.optioncodeToValue.fontType = fontType;
	}

	//テーマオプション：フォント色
	this.getOptionFontColor = function() {
		return cur_editorInfo.optioncodeToValue.fontColor;
	}
	this.setOptionFontColor = function(fontColor) {
		return cur_editorInfo.optioncodeToValue.fontColor = fontColor;
	}

	//テーマオプション：フォントサイズ
	this.getOptionFontSize = function() {
		return cur_editorInfo.optioncodeToValue.fontSize;
	}
	this.setOptionFontSize = function(fontSize) {
		return cur_editorInfo.optioncodeToValue.fontSize = fontSize;
	}

	//テーマオプション：フォント表示位置水平方向
	this.getOptionFontPosH = function() {
		return cur_editorInfo.optioncodeToValue.fontPosH;
	}
	this.setOptionFontPosH = function(fontPosH) {
		return cur_editorInfo.optioncodeToValue.fontPosH = fontPosH;
	}

	//テーマオプション：フォント表示位置垂直方向
	this.getOptionFontPosV = function() {
		return cur_editorInfo.optioncodeToValue.fontPosV;
	}
	this.setOptionFontPosV = function(fontPosV) {
		return cur_editorInfo.optioncodeToValue.fontPosV = fontPosV;
	}

	//テーマオプション：再生速度
	this.getOptionSpeed = function() {
		return cur_editorInfo.optioncodeToValue.speed;
	}
	this.setOptionSpeed = function(speed) {
		return cur_editorInfo.optioncodeToValue.speed = speed;
	}

	//テーマオプション：背景色
	this.getOptionBackColor = function() {
		return cur_editorInfo.optioncodeToValue.backColor;
	}
	this.setOptionBackColor = function(backColor) {
		return cur_editorInfo.optioncodeToValue.backColor = backColor;
	}

	//デジブックのBGM
	this.getBgmId = function() {
		return cur_editorInfo.cur_bgmid;
	}
	this.setBgmId = function(cur_bgmid) {
		return cur_editorInfo.cur_bgmid = cur_bgmid;
	}

	//デジブックのBGM種別
	this.getBgmKind = function() {
		return cur_editorInfo.cur_bgmselect;
	}
	this.setBgmKind = function(cur_bgmselect) {
		return cur_editorInfo.cur_bgmselect = cur_bgmselect;
	}

	//マイBGM
	this.getUploadBgmTitle = function() {
		return	cur_editorInfo.upload_bgmtitle;
	}
	this.getUploadBgmResuuid = function() {
		return	cur_editorInfo.upload_bgmresuuid;
	}

	this.setUploadBgm = function( bgmresuuid, bgmtitle ) {
		cur_editorInfo.upload_bgmresuuid	=	bgmresuuid;
		cur_editorInfo.upload_bgmtitle		=	bgmtitle;
	}



	//ジャケットのフレーミング
	this.getIsJacketFraming = function() {
		return cur_editorInfo.isJacketFraming;
	}
	this.setIsJacketFraming = function(isJacketFraming) {
		return cur_editorInfo.isJacketFraming = isJacketFraming;
	}

	//ジャケットイメージ
	this.getJacketPhoto = function() {
		return cur_editorInfo.jacketphoto;
	}
	this.setJacketPhoto = function(jacketphoto) {
		return cur_editorInfo.jacketphoto = jacketphoto;
	}

	//ラベルイメージ
	this.getLabel = function() {
		return cur_editorInfo.label;
	}		
	this.setLabel = function(label) {
		return cur_editorInfo.label = label;
	}


	//画像
	this.getPhotos = function() {
		return cur_editorInfo.photos;
	}
	this.setPhotos = function(photos) {
		return cur_editorInfo.photos = photos;
	}

	//未使用画像
	this.getUnusePhotos = function() {
		return cur_editorInfo.unusePhotos;
	}
	this.setUnusePhotos = function(unusePhotos) {
		return cur_editorInfo.unusePhotos = unusePhotos;
	}


	//画像のbaseUrl
	this.getImageBaseUrl = function() {
		return cur_editorInfo.imageBaseUrl;
	}

	//マイBGMのbaseUrl
	this.getUploadBgmBaseUrl = function() {
		return	cur_editorInfo.mybgmBaseUrl;
	}

}

var EditorCom = function(editorInfo){
	var edtAccesser = new EditDataAccesser(editorInfo);
	var	funcAry_onSuccessSend_keyToValue =[];


	//デジブックのID
	this.getDigiBookId = function() {
		return edtAccesser.getDigiBookId();
	}

	//デジブックのタイトル
	this.getDigiBookTitle = function() {
		return edtAccesser.getDigiBookTitle();
	}
	this.setDigiBookTitle = function(digibooktitle) {
		return edtAccesser.setDigiBookTitle(digibooktitle);
	}

	//デジブックのサブタイトル
	this.getDigiBookSubTitle = function() {
		return edtAccesser.getDigiBookSubTitle();
	}
	this.setDigiBookSubTitle = function(digibooksubtitle) {
		return edtAccesser.setDigiBookSubTitle(digibooksubtitle);
	}

	//ジャケットオプション：タイトルフォント
	this.getOptionJacketFontType = function() {
		return edtAccesser.getOptionJacketFontType();
	}
	this.setOptionJacketFontType = function(jacketFontType) {
		return edtAccesser.setOptionJacketFontType(jacketFontType);
	}

	//ジャケットオプション：タイトル文字色
	this.getOptionJacketTitleColor = function() {
		return edtAccesser.getOptionJacketTitleColor();
	}
	this.setOptionJacketTitleColor = function(jacketTitleColor) {
		return edtAccesser.setOptionJacketTitleColor(jacketTitleColor);
	}

	//ジャケットオプション：ジャケット色
	this.getOptionJacketBackColor = function() {
		return edtAccesser.getOptionJacketBackColor();
	}
	this.setOptionJacketBackColor = function(jacketBackColor) {
		return edtAccesser.setOptionJacketBackColor(jacketBackColor);
	}


	//デジブックのテーマ
	this.getThemeId = function() {
		return edtAccesser.getThemeId();
	}
	this.setThemeId = function(themeid) {
		return edtAccesser.setThemeId(themeid);
	}

	//テーマオプション：フォント
	this.getOptionFontType = function() {
		return edtAccesser.getOptionFontType();
	}
	this.setOptionFontType = function(fontType) {
		return edtAccesser.setOptionFontType(fontType);
	}

	//テーマオプション：フォント色
	this.getOptionFontColor = function() {
		return edtAccesser.getOptionFontColor();
	}
	this.setOptionFontColor = function(fontColor) {
		return edtAccesser.setOptionFontColor(fontColor);
	}

	//テーマオプション：フォントサイズ
	this.getOptionFontSize = function() {
		return edtAccesser.getOptionFontSize();
	}
	this.setOptionFontSize = function(fontSize) {
		return edtAccesser.setOptionFontSize(fontSize);
	}

	//テーマオプション：フォント表示位置水平方向
	this.getOptionFontPosH = function() {
		return edtAccesser.getOptionFontPosH();
	}
	this.setOptionFontPosH = function(fontPosH) {
		return edtAccesser.setOptionFontPosH(fontPosH);
	}

	//テーマオプション：フォント表示位置垂直方向
	this.getOptionFontPosV = function() {
		return edtAccesser.getOptionFontPosV();
	}
	this.setOptionFontPosV = function(fontPosV) {
		return edtAccesser.setOptionFontPosV(fontPosV);
	}

	//テーマオプション：再生速度
	this.getOptionSpeed = function() {
		return edtAccesser.getOptionSpeed();
	}
	this.setOptionSpeed = function(speed) {
		return edtAccesser.setOptionSpeed(speed);
	}

	//テーマオプション：背景色
	this.getOptionBackColor = function() {
		return edtAccesser.getOptionBackColor();
	}
	this.setOptionBackColor = function(backColor) {
		return edtAccesser.setOptionBackColor(backColor);
	}

	//デジブックのBGM
	this.getBgmId = function() {
		return edtAccesser.getBgmId();
	}
	this.setBgmId = function(cur_bgmid) {
		return edtAccesser.setBgmId(cur_bgmid);
	}

	//デジブックのBGM種別
	this.getBgmKind = function() {
		return edtAccesser.getBgmKind();
	}
	this.setBgmKind = function(cur_bgmselect) {
		return edtAccesser.setBgmKind(cur_bgmselect);
	}

	//マイBGM
	this.getUploadBgmTitle = function() {
		return edtAccesser.getUploadBgmTitle();
	}
	this.getUploadBgmResuuid = function() {
		return edtAccesser.getUploadBgmResuuid();
	}


	//ジャケットのフレーミング
	this.getIsJacketFraming = function() {
		return edtAccesser.getIsJacketFraming();
	}
	this.setIsJacketFraming = function(isJacketFraming) {
		return edtAccesser.setIsJacketFraming(isJacketFraming);
	}

	//ジャケットイメージ
	this.getJacketPhoto = function() {
		return edtAccesser.getJacketPhoto();
	}
	//ジャケットイメージURL
	this.getJacketPhotoUrl = function() {
		var jacketPhoto = edtAccesser.getJacketPhoto();
		return jacketPhoto.url;
	}


	//ラベルイメージ
	this.getLabel = function() {
		return edtAccesser.getLabel();
	}		
	//ラベルイメージURL
	this.getLabelUrl = function() {
		var label = edtAccesser.getLabel();
		return label.url;
	}

	//画像
	this.getPhotos = function() {
		return edtAccesser.getPhotos();
	}
	this.setPhotos = function( photos ) {
		return edtAccesser.setPhotos( photos );
	}

	this.getUnusePhotos = function() {
//TODO
		return new Array();
	}
	this.setUnusePhotos = function( photos ) {
//TODO
		//return edtAccesser.setPhotos( photos );
	}
	
	this.sortPhotoOrder = function( orderPhotoPos_a ) {
		//未使用写真のコピーを作っておく
		var new_unusePhotos  = new Array();
		var unusePhotos  = this.getUnusePhotos();
		for( var i in unusePhotos ) {
			new_unusePhotos.push( unusePhotos[i] );
		}

		var photos  = this.getPhotos();

		//指定された順に使用中写真のコピーを作る。
		var	new_indexs = new Array(photos.length);
			//現写真と新写真のインデックスの対応配列
		for( i=0;i<new_indexs.length;i++ ) {
			new_indexs[i] = -1;
		}
		var new_photos = new Array(orderPhotoPos_a.length);
		for( i=0;i<orderPhotoPos_a.length;i++ ) {
			new_photos[i] = photos[orderPhotoPos_a[i]];
			new_indexs[ orderPhotoPos_a[i] ] = i;
		}

		//新写真に含まれなかった現写真は未使用写真列の末尾にコピーする
		for( var i in new_indexs ) {
			if( new_indexs[i]<0 ) {
				new_unusePhotos.push( photos[i] );
			}
		}

		//書き込み
		this.setPhotos(new_photos);
		this.setUnusePhotos(new_photos);
	}

	//画像のbaseUrl
	this.getImageBaseUrl = function() {
		return	edtAccesser.getImageBaseUrl();
	}

	//マイBGMのbaseUrl
	this.getUploadBgmBaseUrl = function() {
		return	edtAccesser.getUploadBgmBaseUrl();
	}

	//画像の変更,追加
	this.addImageResource = function(editorInfo_diff) {

		var diff = false;
		//ジャケット画像の差し替え
		if( editorInfo_diff.jacketphoto!='' ) {
			var jacketphoto = editorInfo_diff.jacketphoto;
			edtAccesser.setJacketPhoto(jacketphoto);
			diff = true;
		}
		if( editorInfo_diff.label!='' ) {
			var label = editorInfo_diff.label;
			edtAccesser.setLabel(label);
			diff = true;
		}
	}

	//マイBGM情報を追加する
	//	IE用マイBGMアップロードswfがアップロード完了を通知してきたときに呼び出す。
	//	アップロード完了直後は、サーバ上にはマイBGMが登録されているがeditorInfoにはマイBGM用の情報が入っていない状態。
	//	この関数内でローカルに保持しているeditorInfoを書き換え、サーバに送信することで、マイBGMがDigiBookに組み込まれる。
	this.addMyBgmResource = function(editorInfo_diff) {
logger.info("addMyBgmResource");
		edtAccesser.setUploadBgm( 
			editorInfo_diff.upload_bgmresuuid,
			editorInfo_diff.upload_bgmtitle
		);
	}

	//--
	this.openJacketTab = function() {
		var cur_editorInfo_json= getEditorInfo_stringify();
		var digibookid = this.getDigiBookId();
		this.exec('openEditTab_Jacket', {digibookid:digibookid, editorInfo:cur_editorInfo_json});
	}
	this.openPhotoTab = function() {
		var cur_editorInfo_json= getEditorInfo_stringify();
		var digibookid = this.getDigiBookId();
		this.exec('openEditTab_Photo', {digibookid:digibookid, editorInfo:cur_editorInfo_json});
	}
	this.openThemeTab = function() {
		var cur_editorInfo_json= getEditorInfo_stringify();
		var digibookid = this.getDigiBookId();
		this.exec('openEditTab_Theme', {digibookid:digibookid, editorInfo:cur_editorInfo_json});
	}
	this.openBgmTab = function() {
		var cur_editorInfo_json= getEditorInfo_stringify();
		var digibookid = this.getDigiBookId();
		this.exec('openEditTab_Bgm', {digibookid:digibookid, editorInfo:cur_editorInfo_json});
	}
	this.openUpload = function() {
		var cur_editorInfo_json= getEditorInfo_stringify();
		var digibookid = this.getDigiBookId();
		this.exec('openEditUpload', {digibookid:digibookid, editorInfo:cur_editorInfo_json});
	}
	this.saveEdit = function() {
		var cur_editorInfo_json= getEditorInfo_stringify();
		var digibookid = this.getDigiBookId();
//		window.top.DBmask.cover_('loading',null,false,false);
		this.exec('saveEdit', {digibookid:digibookid, editorInfo:cur_editorInfo_json});
	}
	this.volatileSaveEdit = function() {
		var cur_editorInfo_json= getEditorInfo_stringify();
		var digibookid = this.getDigiBookId();
		this.send('volatileSaveEdit', { digibookid:digibookid, editorInfo:cur_editorInfo_json });
	}
	this.volatileSaveEditPhoto = function() {
		var cur_editorInfo_json= getEditorInfo_stringify();
		var digibookid = this.getDigiBookId();
		this.send('volatileSaveEditPhoto', { digibookid:digibookid, editorInfo:cur_editorInfo_json });
	}
	this.volatileSaveEditLabel = function() {
		var cur_editorInfo_json= getEditorInfo_stringify();
		var digibookid = this.getDigiBookId();
		this.send('volatileSaveEditLabel', { digibookid:digibookid, editorInfo:cur_editorInfo_json });
	}
	this.volatileSaveEditJacket = function() {
		var cur_editorInfo_json= getEditorInfo_stringify();
		var digibookid = this.getDigiBookId();
		this.send('volatileSaveEditJacket', { digibookid:digibookid, editorInfo:cur_editorInfo_json });
	}
	this.volatileSaveEditBgm = function() {
		var cur_editorInfo_json= getEditorInfo_stringify();
		var digibookid = this.getDigiBookId();
		this.send('volatileSaveEditBgm', { digibookid:digibookid, editorInfo:cur_editorInfo_json });
	}


	this.cancelEdit = function() {
		editor_window.location.href = "/images/ajax-loader.gif";
		window.top.DBmask.remove_();
		window.top.onCancelEdit();
	}

	this.registCallBackOnSuccessSendKeyToValue = function(key, func) {
		funcAry_onSuccessSend_keyToValue[key] = func;
	}
	
	var submit_form = undefined;
	this.setSubmitForm = function( submit_form_ ) {
		submit_form = submit_form_;
	}

	var editor_window = undefined;
	this.setEditorWindow = function( editor_window_ ) {
		editor_window = editor_window_;
	}

	//Exec
	this.exec = function( actioncode, arg ) {

		try{
//			document.getElementById('iframe_editor').contentWindow.editorSubmit( actioncode, arg );
			editor_window.editorSubmit( actioncode, arg );
			return false;
		}catch (e) {
			var f = submit_form;
	//		var f = document.getElementById("editorSubmit");
			var e;
			e=document.createElement('input');
			e.type='hidden';
			e.name='actioncode';
			e.value=actioncode;
			f.appendChild(e);
			
			for( var i in arg ) {
				e=document.createElement('input');
				e.type='hidden';
				e.name=i;
				e.value=arg[i];
				f.appendChild(e);
			}
			
			f.method='post';
			f.action='/';
			f.submit();
			return false;
		}
	}

	this.send = function( commandcode , arg ) {
		var digibookid = this.getDigiBookId();
		var	data={
			"commandcode":		commandcode,
			"command_arg":	JSON.stringify(arg)
		};
		$.ajax({
			url:	'/ajax.php',
			type:	"POST",
			data:	data,
			dataType:	"text",
			cache:	false,
			timeout:	15*1000	/*milisec*/,
			success:	function(data, status) {
				try{
if(status=='success') {
					for( var i=0; i<funcAry_onSuccessSend_keyToValue.length; i++ ) {
						funcAry_onSuccessSend_keyToValue[i]();
					}
					if(commandcode === 'volatileSaveEditPhoto') {
						// editor/editPhoto.tmpl を開いていることを期待
						// ほんとはコールバックっぽいことしたいよねー
						funcAry_onSuccessSend_keyToValue[commandcode]();
//						startEditPhoto();
					} else if (commandcode === 'volatileSaveEditJacket') {
						funcAry_onSuccessSend_keyToValue[commandcode]();
//						startEditJacket();
					} else if (commandcode === 'volatileSaveEditLabel') {
						funcAry_onSuccessSend_keyToValue[commandcode]();
//						startEditLabel();
					} else if (commandcode === 'volatileSaveEditBgm') {
						funcAry_onSuccessSend_keyToValue[commandcode]();
//						startEditBgm();
					}else {
						//nop
					}
}else{
	alert(status);
}

				}
				catch( e ) {
				}
			},
			error:	function(request, status, errorThrown) {

			}
		});

	}

	//--
	//UTIL
	function getEditorInfo_stringify() {
		return JSON.stringify(edtAccesser.getEditorInfo());
	}
	
	
	function sortArray( a, getIdx, putIdx ) {
		var length = a.length;
		var target = a[getIdx];
		var fore = a.slice(0,getIdx);
		var rear = a.slice(getIdx+1);
		
		var joined = fore.concat(rear);

		if(getIdx < putIdx) {
			var ret = joined.slice(0, putIdx);
			var joined_rear = joined.slice(putIdx);
		}else {
			var ret = joined.slice(0, putIdx);
			var joined_rear = joined.slice(putIdx);
		}
		
		ret.push(target);
		ret = ret.concat(joined_rear);
		
		return ret;
	}

}

var editorcom = undefined;
function	setUpEditorCom(data) {
	editorcom = new EditorCom(data.editorInfo);
}



