// JavaScript Document
//toAlbumActionFile="module/ajax_diaryUpload.php?fmb_id="+isfmb_id;
var selectImageID=new Array();
var selectImageName=new Array();
Array.prototype.removeByIndex=function(index){this.splice(index,1);}
Array.prototype.removeByValue=function(value){for(var i=0;i<this.length;i++){if(this[i]==value){this.splice(i,1);return i;}}}
Array.prototype.search=function(value){for(var i=0;i<this.length;i++){if(this[i]==value){return i+1;}}return false;}
function $(id){return document.getElementById(id);}
function $name(name){return document.getElementsByName(name);};
function $value(node,name){try{return node.getElementsByTagName(name)[0].firstChild.nodeValue;}catch(e){return "";}}
function spacialTag(txt){txt=txt.replace(/>/g,'&gt;');txt=txt.replace(/</g,'&lt;');return txt;txt=txt.replace(/"/g,"&quot;");}
function spacialChar(txt){txt=txt.replace(/\\u000a|\\u000d/g," ");txt=txt.replace(/\\u0026/g,"&");return txt;}
function textLimitCheck(id,limit,idS){
	if(!id.nodeName){id=$(id)}
	txtStr=id.value.replace(/[\n ]+/g,' ');
	txtLength=txtStr.length;
	if(idS){
	$(idS).innerHTML=(txtLength>limit)?"<font>"+txtLength+"/"+limit+"</font>":txtLength+"<font>/"+limit+"</font>";
	}
	return (txtLength>limit||txtLength<=0)?false:true;
}
function fromCom(){
	$('fromCom').style.display="block";
	$('fromAlbum').style.display="none";
}
function fromAlbum(){
	$('fromCom').style.display="none";
	$('fromAlbum').style.display="block";
}
function fromComChangeAlbum(albumID){
	if(albumID==0){
		$('newAlbumName').disabled=false;
	}else{
		$('newAlbumName').disabled=true;
	}
}
function fromAlbumChangeAlbum(albumID){
	cp.call(toAlbumActionFile,'getImage',resGetImage,albumID);
}
function resGetImage(result){
	albumId=result.getElementsByTagName('album')[0].getAttribute('id');
	imagesNode=result.getElementsByTagName('images')[0];
	imageNode=imagesNode.getElementsByTagName('image');
	html ="";
	imgRow=Math.ceil(imageNode.length/5);
	document.title=imgRow;
	$('imageThumb').style.height=((imgRow>2)?263:imgRow*111)+"px";
	for(i=0;i<imageNode.length;i++){
		imageID=imageNode[i].getAttribute('id');
		imageName=imageNode[i].firstChild.nodeValue;
		check=(!selectImageID.search(imageID))?"":"checked='checked'";
		html+="<span><img style=\"cursor:pointer;\" src=\"user_file/"+isfmb_id+"/album/thumb/"+imageName+"\" id=\"img"+imageID+"\" onclick=\"choiceImg(this)\" /><br />";
		html+="<input type=\"checkbox\" name=\"imgCheckBox[]\" style=\"cursor:pointer;\" onclick=\"choiceImg2(this,"+imageID+");\" id=\"chk"+imageID+"\"  value=\""+imageID+"\" "+check+" /></span>";
	}
	$('imageThumb').innerHTML=html;
}
function choiceImg2(chkBox,id){
	obj=$('img'+id);
	if(chkBox.checked){
		chkBox.checked=true;
		selectImageID.push(chkBox.value);
		selectImageName.push(obj.src.match(/[a-z0-9]+.[a-z]{3,4}$/)[0]);
		albumName=$('listAlbum').options[$('listAlbum').selectedIndex].firstChild.nodeValue;
		$('imgOrder').innerHTML+="<a href=\"#\" title=\""+spacialTag(albumName)+"\" onclick=\"return false;\"><img src=\""+obj.src+"\"/></a>";
		$('boxImgOrder').style.display="block";
		
	}else{
		chkBox.checked=false;
		removeIndex=selectImageID.removeByValue(chkBox.value);
		selectImageName.removeByValue(selectImageName[removeIndex]);
		$('imgOrder').removeChild($('imgOrder').childNodes[removeIndex]);
		if(!$('imgOrder').childNodes.length){$('boxImgOrder').style.display="none";}
	}
}
function choiceImg(obj){
	checkBox=obj.parentNode.getElementsByTagName('input')[0];
	if(checkBox.checked){
		checkBox.checked=false;
		removeIndex=selectImageID.removeByValue(checkBox.value);
		selectImageName.removeByValue(selectImageName[removeIndex]);
		$('imgOrder').removeChild($('imgOrder').childNodes[removeIndex]);
		if(!$('imgOrder').childNodes.length){$('boxImgOrder').style.display="none";}
	}else{
		checkBox.checked=true;
		selectImageID.push(checkBox.value);
		selectImageName.push(obj.src.match(/[a-z0-9]+.[a-z]{3,4}$/)[0]);
		
		albumName=$('listAlbum').options[$('listAlbum').selectedIndex].firstChild.nodeValue;
		$('imgOrder').innerHTML+="<a href=\"#\" title=\""+spacialTag(albumName)+"\" onclick=\"return false;\"><img src=\""+obj.src+"\"/></a>";
		$('boxImgOrder').style.display="block";
	}
}
function addImage(){
	//if($('rFromCom').checked){
		//if(!$('newAlbumName').disabled && !textLimitCheck('newAlbumName',50,'albumNameCount')){
		//	alert(txtOverLimit);$('newAlbumName').focus();return false;
		//}else if(!$('newAlbumName').disabled){
		//	fromUpload.submitFormNewAlbum($('newAlbumName').value);
		//}else{
			fromUpload.submitFormAlbumID();
		//}
	//}else{
		//opener.addImageFromAlbum(selectImageID,selectImageName);
		//window.close();
	//}
}
function uploadSuccess(albumID,imageID,imageName){
	//if(!$('newAlbumName').disabled){
//		option=document.createElement('option');
//		cloneOpt=document.createElement('option');
//		$('selectAlbum').options.add(option);
//		$('listAlbum').options.add(cloneOpt);
//		option.appendChild(document.createTextNode($('newAlbumName').value));
//		option.value=albumID;
//		cloneOpt.appendChild(document.createTextNode($('newAlbumName').value));
//		cloneOpt.value=albumID;
//		$('selectAlbum').selectedIndex=$('selectAlbum').length-1;
//		$('newAlbumName').disabled=true;
//	}
	//$('newAlbumName').value="";
	//$('albumNameCount').innerHTML="0<font>/50</font>";
	//arrImgID=new Array();
	//arrImgID.push(imageID);
	opener.addImageFromAlbum([imageID],[imageName]);
	window.close();
}

