﻿//add a new photo layer above the calendar
//<div style="width:100px;height:100px"><div id="cover"><img id=""><div id="coverLayer0"><img id="coverLayer0img"></div></div><div>
function addPhoto(imgsrc){
		isChangePhoto = false;
	var arr=null;
	var winO = null;
	if(imgsrc==null&&IE){
		 arr = showModalDialog("designshare/Emotion_simple__"+arguments[1]+"_"+document.getElementById("sessioncustomerid12").value+".html",new Array(window),"dialogWidth:780px; dialogHeight:470px;dialogLeft:0px;dialogTop:0px; status:1;help:0;resizable:0;scroll:0;menubar:0");
	}else if(imgsrc==null){
		var info = getWidthAndHeight();//defined in inc.js
		var wleft = info.width/2-375;
		var url = "designshare/Emotion_simple__"+arguments[1]+"_"+document.getElementById("sessioncustomerid12").value+".html";
			winO = window.open(url,"addphoto","resizable=no,menubar=no,status=no,toolbar=no,scrollbars=no,height=417,width=780,left=0,top=0",true);
			winO.focus();
			return false;
	}else
		arr = imgsrc;
		
	if(bottomChooseDiv.length>2&&winO==null&&arr!=null){
	   changeBottomPhoto(arr);
	   return false;
	}
	
	if(arr!=null){
	var chosenDivObject = document.getElementById(chosenDiv);//example,cover
		
	var lastLayerInfo = getLastLayerInfo(chosenDivObject);
	var newLayerName = chosenDiv+"layer"+(Number(lastLayerInfo.layerNumber)+1);//example,coverlayer2
	
	var baseLayer = document.getElementById(newLayerName);
			
	if(baseLayer==null)
	baseLayer = document.createElement("div");
	baseLayer.id = newLayerName;
	baseLayer.style.zIndex = -1;
	baseLayer.style.position = "absolute";
	baseLayer.style.cursor = "move";
	baseLayer.style.left = "20px";
	baseLayer.style.top = "30px";
	baseLayer.setAttribute("beishu","1.00");
	baseLayer.onclick = function(){mouseclick(baseLayer);}
	baseLayer.ondblclick = new Function("changePhoto()"); // 設置雙擊 轉換圖片事件
	baseLayer.style.zIndex = parseInt(lastLayerInfo.zIndex) + 10;
	
	//arr=replaceall(arr,"%5C","/");
	
	arr=getImageSrc(arr);
	
	baseLayer.setAttribute("src",arr);
	
	var imageId = newLayerName+"img";//example,coverlayer2img
	var image = document.getElementById(imageId);
	if(image==null)
	image = new Image();
	image.src = arr;
	var width = parseFloat(image.width);
	var height = parseFloat(image.height);
	var imgWidth = 0;
	var imgHeight = 0;
	if(width!=0&&height!=0){
		baseLayer.setAttribute("beishu",(width/height));
		var percent = 1.00;
		if(width>80){
			percent = 80/width;
			image.width = 80;
			image.height = Math.round(percent*height);
			imgWidth = 80;
			imgHeight = Math.round(percent*height);
			//baseLayer.style.pixelWidth = 80;
			//baseLayer.style.pixelHeight = percent*height;
		}
	}
	image.onabort = function(){loadingError(image);}
	image.onerror = function(){loadingError(image);}
	if(IE){
		image.onload = function(){resizePhoto(image);}//resizePhoto() defined in scaleimage.js
		image.title = " 雙擊轉換圖片 ";
		baseLayer.appendChild(image);
	}else{
			
	var canvas = null;
	
				canvas = document.createElement("canvas");
				canvas.title = " 雙擊轉換圖片 ";	
				canvas.width = 80;
				canvas.height = percent * height;
			
	var ctx = canvas.getContext("2d");
		ctx.scale(percent,percent);
		ctx.drawImage(image,0,0);
		baseLayer.appendChild(canvas);
		baseLayer.setAttribute("width","80");
		baseLayer.setAttribute("height",Math.round(percent*height));	
	}
	
	var history = new ImageHistory();
			history.id = newLayerName;
			history.action = "add";
			history.layer = baseLayer.cloneNode(false);
			history.width = imgWidth;
			history.height = imgHeight;
			historyMr.addHistory(history);
		  historyMr.clearRedoHistory();
	
		setHandles(baseLayer,80,parseInt(percent*height));//set the handles for resizing this image
	
	chosenDivObject.appendChild(baseLayer);
	
	}
	
	window.focus();

}
//hide all the div,including the cover and the month div
function hideAllCoverAndMonths(){
	document.getElementById("cover").style.display="none";
	for(var i=1;i<13;i++)
		document.getElementById("month"+i).style.display="none";
}

//display a div,cover or month
function chooseDiv(id){
	if(isDebugButton)
		outputToConsole("enter function chooseDiv,id:"+id);
	hideAllCoverAndMonths();
	chosenDiv = id;
	setAllNoneDash();
	document.getElementById(id).style.display = "";
	var text = document.getElementById(id).style.height;
	var height = Number(text.substring(0,text.indexOf("px")));
	document.getElementById("layers").height= height;

}

var isChangePhoto = false;
var changeElement = "";// referred by openwindow
var oldPhotoSrc = "";

var imageLayerInfo = new Array();//used by setHandles()   ,waitForHandles()
//called by addPhoto()
function setHandles(baseLayer,width,height){
	document.getElementById("elText").value="";
	var obj = {imageLayer:0,width:1,height:2};
	if(baseLayer.firstChild.width!=width||baseLayer.firstChild.height != height){
		obj.imageLayer = baseLayer;
		obj.width = width;
		obj.height = height;
		imageLayerInfo.push(obj);	
		window.setTimeout("waitForHandles()",200);
	}else{
		mouseclick(baseLayer);//set the handles for resizing this image
	}
}
//called by setHandles
function waitForHandles(){
	if(imageLayerInfo.length<1)
		return;
	var obj = imageLayerInfo[imageLayerInfo.length-1];
	var imageLayer = obj.imageLayer;
	var width = obj.width;
	var height = obj.height;
	if(imageLayer.firstChild.width!=width||imageLayer.firstChild.height != height){
		window.setTimeout("waitForHandles()",200);
	}else{
		imageLayerInfo.pop();
		mouseclick(imageLayer);//set the handles for resizing this image
	}
}

//get the layer number from the given id
function getLayerNumber(id){
	return id.substring(id.indexOf("layer")+5,id.length);
}

//get the image src from a String
function getImageSrc(arr){
	var text = arr;
	if(arr.indexOf("*")!=-1){
		var end = arr.indexOf("*");
		 text = arr.substring(0,end);
	}
	return text;
}

function pixelToNumber(p){
	
	if(p==null)
		return 0;
	
	if(p.indexOf("px")!=-1){
		p = parseFloat(p.substring(0,p.indexOf("px")));
		if(isNaN(p))
			p = 0;
		return p;
	}else{
		p = parseFloat(p);
		if(isNaN(p))
		return 0;
	}
	
	return 0;
}

function getRGB(colorstr){
	
		var rgbindex=colorstr.indexOf("rgb");
		var rgbindexend=colorstr.indexOf(")");
		if(rgbindex!=-1&rgbindexend!=-1){
			colorstr=colorstr.substring(4,rgbindexend);
			var first=colorstr.indexOf(",");
			var second=colorstr.lastIndexOf(",");
			if(first!=-1&second!=-1){
			  var firststr=colorstr.substring(0,first);
			  var secondstr=colorstr.substring(first+1,second);
			  var thirdstr=colorstr.substring(second+1);
			  colorstr="#"+ToHex(Number(firststr))+ToHex(Number(secondstr))+ToHex(Number(thirdstr));
			}
		}
		
		return colorstr;
}

function ToHex(n)
{	
var h, l;
var hexch = new Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
	n = Math.round(n);
	l = n % 16;
	h = Math.floor((n / 16)) % 16;
	return (hexch[h] + hexch[l]);
}
function replaceall(strSource,strFrom,strTo){
    var pattern,re;
	var s = strSource;
	pattern = new RegExp(strFrom,"ig");
	re = s.replace(pattern,strTo);

      return re;
}
function   MyReplace(sourestr,dtag,stag)  
{  
    var strtemp="";
    var   newStr=sourestr;
    while(newStr.indexOf(dtag)>-1){
	  var index=newStr.indexOf(dtag);
	  strtemp=strtemp+newStr.substring(0,index);
	  newStr=newStr.substring(index+1);
      //newStr   =   newStr.replace(dtag,stag);
	  strtemp=strtemp+stag;
	  
	}
	strtemp=strtemp+newStr;
    return   strtemp;
}

function replace(strSource,strFrom,strTo){
    var pattern,re;
	var s = strSource;
	pattern = new RegExp(strFrom,"ig");
	re = s.replace(pattern,strTo);

      return re;
}

function isChineaseFont(font){
  if(hasChinease(font)) return true;
  if(font.indexOf("STZhongsong")!=-1){
    return true;
  }else if(font.indexOf("DFKai-SB")!=-1)
	return true;
	else if(font.indexOf("SimHei")!=-1)
	return true;
	else if(font.indexOf("FZYaoTi")!=-1)
	return true;
  else if(font.indexOf("LiSu")!=-1)
	return true;
  else if(font.indexOf("FZShuTi")!=-1)	
	return true;
  else if(font.indexOf("FZChaoCuHei-M10")!=-1)	
	return true;
  else if(font.indexOf("FZCaiYun-M09")!=-1)	
	return true;
  else if(font.indexOf("FZDaBiaoSong-B06")!=-1)	
	return true;
  else if(font.indexOf("FZFangSong-Z02")!=-1)	
	return true;
  else if(font.indexOf("FZCuHei-B03")!=-1)	
	return true;
  else if(font.indexOf("FZHuPo-M04")!=-1)	
	return true;
  else if(font.indexOf("FZHei-B01")!=-1)	
	return true;
  else if(font.indexOf("FZKai-Z03")!=-1)	
	return true;
  else if(font.indexOf("FZLiBian-S02")!=-1)	
	return true;
  else if(font.indexOf("FZPingHei-B04")!=-1)	
	return true;
  else if(font.indexOf("FZWeiBei-S03")!=-1)	
	return true;
  else if(font.indexOf("FZXiaoBiaoSong-B05")!=-1)	
	return true;
  else if(font.indexOf("FZXiHei I-Z08")!=-1)	
	return true;
  else if(font.indexOf("FZXingKai-S04")!=-1)	
	return true;
  else if(font.indexOf("FZXiuLi-Z13")!=-1)	
	return true;
  else if(font.indexOf("FZNew ShuSong-Z10")!=-1)	
	return true;
  else if(font.indexOf("FZNew XiuLi-Z11")!=-1)	
	return true;
  else if(font.indexOf("FZXiYuan-M01")!=-1)	
	return true;
  else if(font.indexOf("FZZhunYuan-M02")!=-1)	
	return true;
  else if(font.indexOf("FZCuYuan-M03")!=-1)	
	return true;
  else if(font.indexOf("FZYouXian-Z09")!=-1)	
	return true;
  else if(font.indexOf("FZZhongKai-B08")!=-1)	
	return true;
  else if(font.indexOf("FZZongYi-M05")!=-1)	
	return true;

	/*try{
		var trtable = document.getElementById("trtable");
				if(trtable==null) {
					alert("the trtable(font table) does not exist");
					return false;
				}
		var rows = trtable.rows;
				for(var i=0;i<rows;i++){
					if(rows[i].cells[0].title==font){
						  if(hasChinease(rows[i].title)) return true;	
					}
				}		
	}catch(e){alert(e.message);}
		*/
  return false;	
}


//test a string contains chinease character
function hasChinease(text){
	
	for(var i=0;i<text.length;i++){
		if(parseInt(text.charCodeAt(i))>256){
			return true;
		}
	}
	return false;
}


function makeTextImage(textDiv,value,isFirst){ // 當value缺省時表示使用原來的文字
// isStandClicked  數組在此js中定義
			if(isFirst) return ;
					if(value!=null)
						textDiv.setAttribute("content_temp",value);
					else 
						value = textDiv.getAttribute("content_temp");	
						
						
				var textImage = textDiv.firstChild;  // div下的圖片
				// 如果是IE則要重新創建一個image
				
				if(IE){
					if(textImage!=null)
					textDiv.removeChild(textImage);
					textImage = new Image();
					textDiv.appendChild(textImage);
				}
				
				if(textImage==null){
					textImage = new Image();
					textDiv.appendChild(textImage);				
				}
				
				var text = getRGB(textDiv.style.color);
					//if(!isIE()) text = getRGB(text);
					text = text.substring(text.indexOf("#")+1,text.length);
				var param = "?color="+text;
					text = textDiv.style.fontSize;
					text = text.substring(0,text.indexOf("px"));
					param = param+"&fontSize="+text;
					text = textDiv.style.letterSpacing;
					text = text.substring(0,text.indexOf("px"));
					param = param+"&letterSpacing="+text;
					text = textDiv.style.fontWeight;
					text = text.indexOf("bold")!=-1?"true":"false";
					param = param+"&bold="+text;
			var family = 	textDiv.style.fontFamily;
					if(family!=null){
						if(family=="黑体") family = "SimHei";
						else if(family=="標楷體") family = "DFKai-SB";	
					}
					
					param = param+"&family=" + family;
					
					param = param+"&align=" + textDiv.getAttribute('textAlign');
					//param = param+"&align=left";

					/*if(textDiv.style.filter!=null&&textDiv.style.filter.length>1){					
							var rotate = textDiv.style.filter;
							if(rotate.indexOf("Rotation=1")!=-1){
								rotate = "90";	
							}else{
								rotate = "0";
							}			
							param = param+"&rotate=" + rotate;
							var writeMode = textDiv.style.writingMode;
					}*/
					var rotate=textDiv.getAttribute('rotate');
					
					param = param+"&rotate=" + rotate;
			var width = textDiv.style.width;
				  width = width.substring(0,width.indexOf("px"));	
				  width = parseFloat(width);
					param = param +"&width="+width;
					if(rotate=="90"||rotate=="270")
						textDiv.style.width="0px";
					else 
						textDiv.style.height="0px";	
			var	height = textDiv.style.height;
					height = height.substring(0,height.indexOf("px"));	
					if(rotate=="0") height = "0";	
					height = parseFloat(height);
					//alert(width+":"+height);
					param = param +"&height="+height;	
					value=escape(value);
					value=MyReplace(value,"+","%2B");
					value=replaceall(value,"\@","%40");
					value=replaceall(value,"/","%2F");
					//var value1 = "%u4E2D%u65871234+abcd%5B%5D()%3C%2B%3E%2C.~%5Cgg%2F";
					//alert(value+":"+value1);
					param = param+"&text=" + value;
					param = param+"&italic=" + (textDiv.style.fontStyle.indexOf("italic")!=-1?"true":"false");
			var  updateTime = (new Date()).getTime();
					param = param + "&updateTime="+updateTime;
					//alert(param);
					param = encodeURI(param);
					//param = encodeURI(param);
// color=000000&fontSize=33&bold=false&family=FZShuTi&align=left&rotate=&text=Add%2520Some%2520Text%2520Here&italic=false					
					textImage.src = "../getFontText"+param;
					if(!IE){
							textImage.onload=function(){
								textImage.parentNode.style.width = textImage.width+"px";
								textImage.parentNode.style.height = textImage.height+"px";	
							}
					}
					settextimagewh(textDiv);
}
function settextimagewh(obj){
  var rotate=obj.getAttribute('rotate');
  var textAlign=obj.getAttribute('textAlign');
  var textImage = obj.firstChild;  // div下的圖片
  var divleft=Number(delDiv_px(obj.style.left));
  var divtop=Number(delDiv_px(obj.style.top));
  var divwidth=Number(delDiv_px(obj.style.width));
  var divheight=Number(delDiv_px(obj.style.height));
  var imgwidth=0;
  var imgheight=0;
  textImage.onload=function(){
	 imgwidth=textImage.width;
     imgheight=textImage.height;//alert(divwidth+":"+imgwidth+":"+divleft);
	 if(textAlign=="left"){
	    if(rotate!="90"&rotate!="270"){
		  //obj.style.left=(divleft-(imgwidth-divwidth))+"px";
		  obj.style.width=(divwidth+(imgwidth-divwidth))+"px";
		  obj.style.height=imgheight+"px";
		}else if(rotate=="90"){
		  //obj.style.top=(divtop-(imgheight-divheight))+"px";
		  obj.style.width=imgwidth+"px";
		  obj.style.height=(divheight+(imgheight-divheight))+"px";
		}else if(rotate=="270"){
		  obj.style.top=(divtop-(imgheight-divheight))+"px";
		  obj.style.width=imgwidth+"px";
		  obj.style.height=(divheight+(imgheight-divheight))+"px";
		}
	 }else if(textAlign=="center"){
	    if(rotate!="90"&rotate!="270"){
		  obj.style.left=(divleft-Math.round(parseFloat((imgwidth-divwidth))/2))+"px";
		  
		  obj.style.width=(divwidth+(imgwidth-divwidth))+"px";
		  obj.style.height=imgheight+"px";
		}else if(rotate=="90"){
		  obj.style.top=(divtop-Math.round(parseFloat((imgheight-divheight))/2))+"px";
		  obj.style.width=imgwidth+"px";
		  obj.style.height=(divheight+(imgheight-divheight))+"px";
		}else if(rotate=="270"){
		  obj.style.top=(divtop-Math.round(parseFloat((imgheight-divheight))/2))+"px";
		  obj.style.width=imgwidth+"px";
		  obj.style.height=(divheight+(imgheight-divheight))+"px";
		}
	 }else if(textAlign=="right"){
		if(rotate!="90"&rotate!="270"){
			//alert(obj.style.left);
		  obj.style.left=(divleft-(imgwidth-divwidth))+"px";
		  obj.style.width=(divwidth+(imgwidth-divwidth))+"px";
		  obj.style.height=imgheight+"px";//alert(obj.style.left);
		}else if(rotate=="90"){
		  obj.style.top=(divtop-(imgheight-divheight))+"px";
		  obj.style.width=imgwidth+"px";
		  obj.style.height=(divheight+(imgheight-divheight))+"px";
		}else if(rotate=="270"){
		  //obj.style.top=(divtop+(imgheight-divheight))+"px";
		  obj.style.width=imgwidth+"px";
		  obj.style.height=(divheight+(imgheight-divheight))+"px";
		}
	}
  }
}
function settextimagewh1(obj){
  var rotate=obj.getAttribute('rotate');
  var textAlign=obj.getAttribute('textAlign');
  var textImage = obj.firstChild;  // div下的圖片
  var divleft=Number(delDiv_px(obj.style.left));
  var divtop=Number(delDiv_px(obj.style.top));
  var divwidth=Number(delDiv_px(obj.style.width));
  var divheight=Number(delDiv_px(obj.style.height));
  var imgwidth=0;
  var imgheight=0;
	 imgwidth=textImage.width;
     imgheight=textImage.height;
	 if(textAlign=="left"){
	    if(rotate!="90"&rotate!="270"){
		  //obj.style.left=(divleft-(imgwidth-divwidth))+"px";
		  obj.style.width=(divwidth+(imgwidth-divwidth))+"px";
		  obj.style.height=imgheight+"px";
		}else if(rotate=="90"){
		  //obj.style.top=(divtop-(imgheight-divheight))+"px";
		  obj.style.width=imgwidth+"px";
		  obj.style.height=(divheight+(imgheight-divheight))+"px";
		}else if(rotate=="270"){
		  obj.style.top=(divtop-(imgheight-divheight))+"px";
		  obj.style.width=imgwidth+"px";
		  obj.style.height=(divheight+(imgheight-divheight))+"px";
		}
	 }else if(textAlign=="center"){
	    if(rotate!="90"&rotate!="270"){
		  obj.style.left=(divleft-Math.round(parseFloat((imgwidth-divwidth))/2))+"px";
		  
		  obj.style.width=(divwidth+(imgwidth-divwidth))+"px";
		  obj.style.height=imgheight+"px";
		}else if(rotate=="90"){
		  obj.style.top=(divtop-Math.round(parseFloat((imgheight-divheight))/2))+"px";
		  obj.style.width=imgwidth+"px";
		  obj.style.height=(divheight+(imgheight-divheight))+"px";
		}else if(rotate=="270"){
		  obj.style.top=(divtop-Math.round(parseFloat((imgheight-divheight))/2))+"px";
		  obj.style.width=imgwidth+"px";
		  obj.style.height=(divheight+(imgheight-divheight))+"px";
		}
	 }else if(textAlign=="right"){
		if(rotate!="90"&rotate!="270"){//alert(divwidth+":"+imgwidth+":"+divleft);
		  obj.style.left=(divleft-(imgwidth-divwidth))+"px";
		  obj.style.width=(divwidth+(imgwidth-divwidth))+"px";
		  obj.style.height=imgheight+"px";
		}else if(rotate=="90"){
		  obj.style.top=(divtop-(imgheight-divheight))+"px";
		  obj.style.width=imgwidth+"px";
		  obj.style.height=(divheight+(imgheight-divheight))+"px";
		}else if(rotate=="270"){
		  //obj.style.top=(divtop+(imgheight-divheight))+"px";
		  obj.style.width=imgwidth+"px";
		  obj.style.height=(divheight+(imgheight-divheight))+"px";
		}
	}

}
//  替換單引號為\'
function replaceSinglequotes(text){
	if(text==null) return null;
	var  index = 0;
	while(index<text.length){
		var ch = text.substring(index,index+1);
		if(ch=="'"){
			text = text.substring(0,index)+"\\'"+text.substring(index+1,text.length);
			index = index +1;	
		}
		index +=1;		
	}
	return text;
}

//  替換\'為單引號
function unReplaceSinglequotes(text){
	if(text==null) return null;
	var  index = 0;
	var firstHit = false;
	while(index<text.length){
		var ch = text.substring(index,index+1);
		
		if(ch=="\\"){
			firstHit = true;	
		}else	if(ch=="'" && firstHit){
			text = text.substring(0,index-1)+text.substring(index,text.length);
			index = index - 1;	
			firstHit =false;	
		}else{
			firstHit =false;	
		}
		
		index +=1;		
	}
	return text;
}

//  替換\為\\
function replaceBackslash(text){
	if(text==null) return null;
	var  index = 0;
	while(index<text.length){
		var ch = text.substring(index,index+1);
		if(ch=="\\"){
			text = text.substring(0,index)+"\\\\"+text.substring(index+1,text.length);
			index = index +1;	
		}
		index +=1;		
	}
	return text;
	
}


//  替換\\為\
function unReplaceBackslash(text){		
	if(text==null) return null;
	var  index = 0;
	var firstHit = false;
	while(index<text.length){
		var ch = text.substring(index,index+1);
		
		if(ch=="\\" && !firstHit){
			firstHit = true;	
		}else	if(ch=="\\" && firstHit){
			text = text.substring(0,index-1)+text.substring(index,text.length);
			index = index - 1;	
			firstHit =false;	
		}else{
			firstHit =false;	
		}
		
		index +=1;		
	}
	return text;
	
}


function unReplaceAtChar(text){
	var i =0;
	while(i<text.length){
		if(text.substring(i,i+1)=="&"){
			if(text.substring(i,i+5) == "&amp;"){
				text = text.substring(0,i)+"&"+text.substring(i+5,text.length);
					
			}	
		}
		
		i +=1;
	}
	return text;
}

//  將字符&nbsp;換成" "
function unReplaceSpaceChar(text){
	if(text==null) return null;
	if(text.indexOf("&nbsp;")==-1)return text;
		var i =0;
		while(i<text.length){
			if(text.substring(i,i+1)=="&"){
				if(text.substring(i,i+6) == "&nbsp;"){
					text = text.substring(0,i)+" "+text.substring(i+6,text.length);
				}	
			}
			
			i +=1;
				
		}	
		return text;
	
}
