// APIを呼び出す
var Api = new Class({
	initialize:function(url,amebaId,div){
		this.url=url;
		this.amebaId=amebaId;
		this.oj=$(div);
		this.ajax();
	},
	ajax:function(){
		var query="amebaId="+encodeURIComponent(this.amebaId);
		var self = this;
		var myAjax = new Ajax(this.url, {method: 'post',postBody:query,
			onSuccess:function(res){
				self.oj.innerHTML=res;
			},
			onFailure:function(){
			//この部分はどうする？ とれなかったら エラー画面？
			//	alert(this.url);
				self.oj.innerHTML= "取得に失敗しました";
			}
		}).request();	
	}
});
// APIを呼び出す
var SlideShow = new Class({

	initialize:function(flashType,amebaId,div,action){
			
		if(flashType!=""){
			var data = swfList[nameList[flashType]];
			var previewSwf = new SWFObject(data.swf, "preview_swf", data.width, data.height, "8", "#FFFFFF");
			action=action+"?amebaId="+amebaId;
			previewSwf.addParam("allowScriptAccess", "always");
			previewSwf.addParam("wmode", "transparent");
			previewSwf.addVariable("action",action);
			previewSwf.write(div);
		}
	}
});
var Music = new Class({
	initialize:function(amebaId,div){
		$(div).innerHTML='<div id="musicArea"></div>';
		var musicSwf = new SWFObject(musicSwfPath, "music", "290", "76", "8", "#ffffff");
		musicSwf.addParam("menu", "false");
		musicSwf.addParam("allowScriptAccess", "always");
		musicSwf.addParam("wmode", "transparent");
		musicSwf.addVariable("amebaId",amebaId);
		musicSwf.write("musicArea");
	}
});
var Artist = new Class({
	initialize:function(amebaId,div){
		$(div).innerHTML='<div id="musicArea"></div>';
		var musicSwf = new SWFObject(artistSwfPath, "music", "455", "305", "8", "#ffffff");
		musicSwf.addParam("menu", "false");
		musicSwf.addParam("allowScriptAccess", "always");
		musicSwf.addParam("wmode", "transparent");
		musicSwf.addVariable("userID",amebaId);
		musicSwf.write("musicArea");
	}
});
function addFavorite(musicId) {

	var url=ADD_MUSIC+"?musicId="+musicId;
	myWin = window.open(url, "pop", "scrollbars=yes,resizable=yes,width=530 height=390");
}


var Present = new Class({
	initialize:function(api,stat,amebaId,div,login,setColors){

		var prezentSwf = new SWFObject(stat+"/room/swf/RoomMySelect.swf", "ShopMySelect", "480", "396", "9", "#ffffff");		
		if (!setColors) {
			var apiURL = "apiDomain=" + api + "&amebaId=" + amebaId + "&isLogin=" + login;
		}
		else {
			var apiURL = "apiDomain=" + api + "&amebaId=" + amebaId + "&isLogin=" + login + "&textColor=0x" + setColors.text + "&linkColor=0x" + setColors.link + "&bgColor=" + setColors.bg;
		}
		prezentSwf.addParam("FlashVars", apiURL);
		prezentSwf.addParam("pluginspage", "http://www.macromedia.com/go/getflashplayer");
		prezentSwf.addParam("align", "middle");
		prezentSwf.addParam("play", "true");
		prezentSwf.addParam("loop", "true");
		prezentSwf.addParam("scale", "showall");
		prezentSwf.addParam("devicefont", "false");
		prezentSwf.addParam("bgcolor", "#ffffff");
		prezentSwf.addParam("name", "ShopMySelect");
		prezentSwf.addParam("menu", "true");
		prezentSwf.addParam("wmode", "transparent");
		prezentSwf.addParam("allowFullScreen", "false");
		prezentSwf.addParam("allowScriptAccess","always");
		prezentSwf.useExpressInstall(stat+"/swf/playerProductInstall.swf");
		prezentSwf.write(div);
	}
});

/*
comment form
*/
var Bbs ={
	formOj:null,
	formFlag:false,
	showForm:function(){
		if(this.formFlag){
			$('bbsRegistArea').style.display="none";			
			$('bbsClose').style.display="none";
			$('bbsOpen').style.display="inline";
		}else{
			$('bbsRegistArea').style.display="inline";
			$('bbsClose').style.display="inline";
			$('bbsOpen').style.display="none";
		}
		this.formFlag=!this.formFlag;
	}
}

// コメント投稿
function saveComment( actionPath, onSuccessActionPath, amebaId, divName) {
	
	var text=document.getElementById('commentText').value;
	if(text==""){
		document.getElementById('systemMessage').innerHTML="コメントは必須入力です。";
		return;
	}
	var query = "amebaId=" + encodeURIComponent(amebaId) + "&commentText=" + encodeURIComponent(text) ;
	var myAjax = new Ajax( 
		actionPath, 
		{
			method: 'post',
			postBody:query,
			onSuccess:function(res){
				var result = Json.evaluate(res);
				var systemMessage = result.msg;
				// 投稿完了時はリフレッシュするコメントを取ってくる
				if(result.status=="OK"){
					new Api( onSuccessActionPath, amebaId, divName);
					Bbs.formFlag=!Bbs.formFlag;
					// 完了メッセージやエラーメッセージを出す
					document.getElementById('systemMessage').innerHTML="";
					document.getElementById('systemMessage').innerHTML=html_entity_decode(systemMessage);
				}
				if(result.status=="NG"){
					// 完了メッセージやエラーメッセージを出す
					document.getElementById('systemMessage').innerHTML="";
					document.getElementById('systemMessage').innerHTML=html_entity_decode(systemMessage);
				}
				// 未ログイン時はコメントフォームを表示する
				if(result.status=="NOT_LOGGED_IN"){
					document.getElementById('systemMessage').innerHTML="";
					document.getElementById('bbsRegistArea').innerHTML
						= html_entity_decode(systemMessage);
				}
			},
			onFailure:function(){
				document.getElementById('systemMessage').innerHTML="コメント投稿に失敗しました";
				//new Api( onSuccessActionPath, amebaId, divName);
			}
		}
	);
	myAjax.request();
}

// コメント前ログイン
function bbsTopLogin( actionPath, amebaId, divName) {
	
	var loginAmebaId = document.getElementById('usr_name').value;
	var query = "viewerAmebaId=" + loginAmebaId + "&password=" + document.getElementById('usr_password').value + "&amebaId=" + amebaId ;

	var myAjax = new Ajax( 
		actionPath, 
		{
			method: 'post',
			postBody:query,
			onSuccess:function(res){
				document.getElementById(divName).innerHTML=res;
			},
			onFailure:function(){
				document.getElementById(divName).innerHTML="取得に失敗しました";
			}
		}
	);
	myAjax.request();
}

// BBS 面コメント前ログイン
function bbsLogin( actionPath, amebaId, divName) {
	
	var loginAmebaId = document.getElementById('usr_name').value;
	
	var query = "amebaId=" + encodeURIComponent(amebaId) + "&viewerAmebaId=" + encodeURIComponent(loginAmebaId) + "&password=" + document.getElementById('usr_password').value ;
	
	var myAjax = new Ajax( 
		actionPath, 
		{
			method: 'post',
			postBody:query,
			onSuccess:function(res){
				document.getElementById(divName).innerHTML=res;
			},
			onFailure:function(){
				document.getElementById(divName).innerHTML="取得に失敗しました";
			}
		}
	);
	myAjax.request();
}
//画像クリック
var imgChange={
	flag:false,
	initialize:function(id,array){
		this.id=id;
		this.obj=$(id);
		this.array=array;
		this.length=this.array.length;
		this.count=1;
		if(this.length!=null && this.length>1){
			this.flag=true;
		}
	},
	change:function(){
		if(!this.flag) return;
		this.obj.innerHTML=this.array[this.count++];

		if(this.count>=this.length)
			this.count=0;
	}
}


var fraudReport={

	showForm:function(targetAmebaId){

		if(targetAmebaId=="")
			return false;
		load.startLoad();
		var query="targetId="+targetAmebaId;	
		
		new Ajax(FRAUD_REPORT_FORM_URL, {method: 'post',postBody:query,
			onSuccess:function(res){
				load.stopLoad();
				Popup.show(res);
			},
			// system error etc...
			onFailure:function(res){
				load.stopLoad();
				location.href='/errors/' + res.status + '.html'; 
			}
		}).request();
	},
	sendForm:function(name){

		if(name=="")
			return false;
		load.startLoad();
		var query=$(name).toQueryString()	

		new Ajax(FRAUD_REPORT_SEND_URL, {method: 'post',postBody:query,
			onSuccess:function(res){
				load.stopLoad();
				Popup.show(res);
			},
			// system error etc...
			onFailure:function(res){
				load.stopLoad();
				location.href='/errors/' + res.status + '.html';
			}
		}).request();
	}
}
//hipsログイン
function hipsLogin() {
	var PATH = "http://profile.ameba.jp/general/campaign/showCampaignDetail.do";
	var query =  "password=" + document.getElementById('hips_password').value ;
	var divName ="Loginform";
	var myAjax = new Ajax( 
		PATH, 
		{
			method: 'post',
			postBody:query,
			onSuccess:function(res){
				document.getElementById(divName).innerHTML=res;
				var oj = document.getElementById('loginbutton');				if(oj!=null){
				oj.onclick=hipsLogin;
				}
			},
			onFailure:function(res){
				document.getElementById(divName).innerHTML=res;
			}
		}
	);
	myAjax.request();
}


//オンマウスでBtn色を変更
function oncgbtn(el){
var changeBtn =el;
changeBtn.src = changeBtn.src.replace(".gif", "_on.gif");
}
//アウトマウスでBtn色を戻す
function outcgbtn(el){
var changeBtn =el;
changeBtn.src = changeBtn.src.replace("_on.gif", ".gif");
}




