var URLPREFIX = 'http://www.skyalbum.com.hk/';
var URLPREFIXIMAGE = 'http://www.skyalbum.com.hk/img/';
var URLPREFIXCART = 'https://www.skyalbum.com.hk/';

var URL= URLPREFIX+'album.sortimageorder';

var popupDisplaying = false;


/*****   Perform EDIT. REPLY , POST comment ******/
function prepareAjaxPost(commentType, parentId, username, TheForm){
	
	var postAction=URLPREFIX+albumusername+'.comment';
	var AJAXLINK = postAction;//+EXTENSION;
	
	var folderName = "";
	var txtComment = "";
	var actionType = "";
	var commentId = "";
	var encfolderId = "";
	var parentId = "";
	var intCommentFlag = "";
	var currentDisplay = "";
	var index = "";
	var type = "";
	var typeDisplay ="";
	var from = "";
	var fromId = "";
	var continueSubmit = false;
	var folderId = "";
	
	// Do CHECKING
	//----------------------------------------------------
	//EDIT, REPLY
	if(commentType == ''){	
		commentType = document.getElementById('actionType').value;
		commentId = document.getElementById('commentId').value;		
		continueSubmit = checkComment(1, commentId);		
	}else{
	//POST
		continueSubmit = checkComment(0, '')
	}
	
	if(continueSubmit){
		
		//----------------------------------------------------
		//POST
		if(commentType == 'post'){				
			txtComment = trim(document.getElementById('txtComment').value);
			actionType = commentType;
			
			document.getElementById('parentId').value = "";
			document.getElementById('txtComment').value = "";
			
			if(document.getElementById('intCommentPostFlag').checked){
				intCommentFlag = 1; //Private
			}else{
				intCommentFlag = 0; //Public
			}			
		}else{
		//EDIT, REPLY
			if(document.getElementById('actionType')){
				if(document.getElementById('actionType').value == "reply"){//REPLY
					actionType = "reply";								
				}else{//EDIT
					actionType = "edit";
				}
				
			}
			txtComment = trim(document.getElementById('txtReplyComment['+commentId+']').value);
			
			if(document.getElementById('intCommentFlag['+commentId+']').checked){
				intCommentFlag = 1; //Private
			}else{
				intCommentFlag = 0; //Public
			}
			
			parentId = document.getElementById('commentParentId').value;	
		}
		
		//----------------------------------------------------
		
		if(document.getElementById('currentDisplay')){
			fromId = document.getElementById('currentDisplay').value;	 	
			folderId = document.getElementById('folder').value;	 	
			from = "image";
		}else if(document.getElementById('encfolderId')){
	 		fromId = document.getElementById('encfolderId').value;	 	
	 		folderId = document.getElementById('encfolderId').value;	 	
	 		from = "folder";
		}
		
		index = document.getElementById('commentIndex').value;	
		
		
		txtComment = encodeURI(txtComment);		
		//------------------
		
		
		var options = { method: 'post',
						contentType: 'application/x-www-form-urlencoded',
						encoding: 'UTF-8',
						parameters:{fromId:fromId, txtComment:txtComment, parentId:parentId, intCommentFlag:intCommentFlag, 
						actionType:actionType, commentId:commentId, albumUsername:albumusername, from:from, 
						memberShowPrivate:memberShowPrivate, loginUsername:loginusername, folder: folderId},
						onSuccess: function(transport){
							response = transport.responseText || "fail";
							//alert(response);
													
							if(response != "fail"){
								var start = response.indexOf("#####");
								var totalStart = response.indexOf("_____");							
								
								var newCommentId = response.substring(0, start);							
								var total = response.substring(parseInt(start)+5, parseInt(totalStart));
								var strHtml = response.substring(parseInt(totalStart)+5, response.length);
															
								document.getElementById("totalComment").innerHTML = total;							
								
								popupDisplaying = false;
								
								
								document.getElementById('commentResult').innerHTML = strHtml;							
								document.getElementById('commentResult').style.display = "inline";
								//alert(aryResp[1]);
								switch(actionType){
									case "edit":	displayCmtMsg(newCommentId, commentDisplay[11]); //Edit Succ
													break;
									case "reply":	displayCmtMsg(newCommentId, commentDisplay[12]); //Reply Succ
													break;
									default:		displayCmtMsg(newCommentId, commentDisplay[20]);	//NEW POST
													if(document.getElementById("newPostLink")){
														document.getElementById("newPostLink").style.display = "inline";
													}
													break;
								}
								if(actionType == "edit"){								
									displayCmtMsg(aryResp[0], commentDisplay[11]);								
								}
								
								if(actionType == "reply"){
									displayCmtMsg(aryResp[0], commentDisplay[12]);								
								}
							}else{
								alert(commentDisplay[22]);
							}
							document.getElementById('txtComment').value = "";
							document.getElementById('txtReplyComment').value = "";					
							
						}, 
						onFailure: function(transport){alert("FAIL"+transport.responseText );}
					};
		
		new Ajax.Request(AJAXLINK, options); 
	}	
}

function displayCmtMsg(index, txtMsg){
	
	document.getElementById('txtCmtMsg'+index).innerHTML = txtMsg;
	document.getElementById('cmtMsgDiv'+index).style.display="inline";							

	setTimeout("Effect.SlideUp('cmtMsgDiv"+index+"', {duration:1.0});", 2000);
}

var displayCommentId = "";

function popupComment(commentType, parentId, username, TheForm, commentId, index, type, privateFlag, commentUsername){
	
	var ImageProductTableWidth = 350;
	var checkboxText = commentDisplay[15]+" - ";
	
	if(currentDelete != ""){
		document.getElementById('deleteComment'+currentDelete).style.display = "none";
		currentDelete = "";
	}
		
	
	//Dynamic change popup checkbox text
	if(document.getElementById('currentDisplay')){
		//if(parentId.length > 0){					
		
		if(commentType == "reply"){
			//checkboxText += privateMsg[4]+username+privateMsg[7]+commentUsername+privateMsg[8];
			checkboxText += privateMsg[7]+commentUsername+privateMsg[8];
		}else{		
			if(parentId.length>0){				
			//checkboxText += privateMsg[4]+username+privateMsg[7]+commentUsername+privateMsg[8];			
			checkboxText += privateMsg[7]+commentUsername+privateMsg[8];
			}else{
				checkboxText += privateMsg[6]+username+privateMsg[5];					
			}
		}
	}else{	
		
		if(commentType == "reply"){
			checkboxText += privateMsg[0]+username+privateMsg[3]+commentUsername+privateMsg[4];			
		}else{
		
			if(parentId.length>0){				
				checkboxText += privateMsg[0]+username+privateMsg[3]+commentUsername+privateMsg[4];			
			}else{
				checkboxText += privateMsg[2]+username+privateMsg[1];					
			}
		}
	}
	
	
	document.getElementById('cmtFlagLabel'+commentId).innerHTML = checkboxText;
	
	if(commentType.toLowerCase() == "edit"){	
			
			commentId = parseInt(commentId);
			
			//document.getElementById('descTR'+commentId).style.display="none";
			document.getElementById('actionType').value = "edit";		
			document.getElementById('parentId').value = parentId;				
			document.getElementById('commentIndex').value = index;		
			
			
			if(parentId.length > 0){
				document.getElementById('commentTitle['+commentId+']').innerHTML = commentDisplay[19];
			}else{
				document.getElementById('commentTitle['+commentId+']').innerHTML = commentDisplay[3]+commentDisplay[23];
			}
			
			if(document.all){
				var formatDesc = document.getElementById("desc"+commentId).innerText.replace(/<BR>/gi, '\n');			
			}else{
				var formatDesc = document.getElementById("desc"+commentId).textContent.replace(/<BR>/gi, '\n');			
			}
			//formatDesc = document.getElementById("desc"+commentId).innerHTML.replace(/<BR \/>/gi,'\n');
			//formatDesc = document.getElementById("desc"+commentId).innerHTML.replace("<br>", '\n');
			document.getElementById('txtReplyComment['+commentId+']').value = formatDesc;
			document.getElementById('commentParentId').value = document.getElementById('newParentId['+commentId+']').value;
			
			if(document.getElementById('flag['+commentId+']').value == 1){
				document.getElementById('intCommentFlag['+commentId+']').checked = true;
			}else{
				document.getElementById('intCommentFlag['+commentId+']').checked = false;
			}
			document.getElementById('newReplyBtn['+commentId+']').value = commentDisplay[3];
			
			type = 0;
			
	}else if(commentType.toLowerCase() == "reply"){
		
			type = 1;
						
			if(privateFlag == 1){
				document.getElementById('intCommentFlag['+commentId+"]").checked = true;
			}else{
				document.getElementById('intCommentFlag['+commentId+"]").checked = false;
			}
			document.getElementById('actionType').value = "reply";	
			document.getElementById('parentId').value = parentId;				
			document.getElementById('commentIndex').value = index;
			
			document.getElementById('commentTitle['+commentId+']').innerHTML = commentDisplay[2]+commentDisplay[23];;
			document.getElementById('txtReplyComment['+commentId+']').value ="";								
			document.getElementById('commentParentId').value = document.getElementById('newParentId['+commentId+']').value;
			
			document.getElementById('newReplyBtn['+commentId+']').value = commentDisplay[2];							
	}
		
		
		document.getElementById('postType').value = type;

	if(popupDisplaying){
		
		
		//document.getElementById('descTR'+commentId).style.display="none";
		
		
		//document.getElementById("commentPanel["+displayCommentId+"]").style.display="inline";
		//document.getElementById("commentPop["+displayCommentId+"]").style.display="none";
		//new Effect.toggle(document.getElementById('displayDiv').value, "slide"); 
		popupDisplaying = false;
		document.getElementById("hiddenPop["+displayCommentId+"]").style.display="none";
		//new Effect.toggle('hiddenPop['+displayCommentId+']', 'appear'); 	
		//setTimeout('displayPopdown('+displayCommentId+')', 1);
		document.getElementById('descTR'+displayCommentId).style.display="inline";
		
		
		
		
		document.getElementById("commentPanel["+displayCommentId+"]").style.display="inline";
		document.getElementById('usernamePanel'+displayCommentId).style.display="inline";
		document.getElementById('datePanel'+displayCommentId).style.display="inline";
		displayCommentId = commentId;
		
		setTimeout('displayPopdown('+commentId+', \''+commentType+'\')', 300);
		
	}else if(popupDisplaying == false){
		displayPopdown(commentId, commentType);
		displayCommentId = commentId;
	}
	
	
}

function displayPopdown(commentId, commentType){
	document.getElementById('commentId').value = commentId;
	document.getElementById("commentPanel["+commentId+"]").style.display="none";
	document.getElementById('displayDiv').value = 'commentPop['+commentId+']';			
	
	//document.getElementById('descTR'+commentId).style.display="none";
	
	
	if(commentType.toLowerCase() == "edit"){
		document.getElementById('datePanel'+commentId).style.display="none";
		document.getElementById('descTR'+commentId).style.display="none";
		document.getElementById('usernamePanel'+commentId).style.display="none";
	}else{
		document.getElementById('datePanel'+commentId).style.display="inline";
		document.getElementById('usernamePanel'+commentId).style.display="inline";
		document.getElementById('descTR'+commentId).style.display="inline";
	}
	
	popupDisplaying = true;
	new Effect.toggle('hiddenPop['+commentId+']', 'appear'); 	
	setTimeout("document.getElementById('txtReplyComment["+commentId+"]').focus();", 300);
	//setTimeout("document.getElementById('popComment["+commentId+"]').style.visibility='visible'", 300);
	//setTimeout("document.getElementById('commentPop["+commentId+"]').style.visibility='visible'", 300);
	
	//new Effect.toggle('popComment['+commentId+']', 'slide'); 		
	//new Effect.toggle('commentPop['+commentId+']', 'slide'); 	
	
}

var currentDelete = "";

function FunDeleteComment(commentId, TheForm, index){
	
	var ImageProductTableWidth = 350;
	
	/*($("deleteComment"+commentId)).innerHTML = 
		//commentDisplay[6]+"<BR /> "+document.getElementById("desc"+commentId).innerHTML+" <BR />"+
		commentDisplay[6]+"<BR /> <BR />"+
		"<input type='button' name='deleteBtn' id='deleteBtn' value='"+commentDisplay[5]+"' onClick=\"ajaxDelete("+commentId+",document.frmFolder);\" /> "+		
		"<input type='button' name='cancelBtn' id='cancelBtn' value='"+commentDisplay[8]+"' onClick=\"new Effect.Fade('deleteComment"+commentId+"');\" />";
	*/
	
	($("commentId")).value = commentId;
	($("commentIndex")).value = index;

	//alert(document.getElementById("deleteComment"+commentId).style.left);
	

		
	if(currentDelete == ""){
		//alert('here'+document.getElementById("deleteComment"+commentId));
		//document.getElementById("deleteComment"+commentId).style.display = "inline";
		currentDelete = commentId;
		new Effect.Appear('deleteComment'+commentId); 	
	}else{
		
		document.getElementById('deleteComment'+currentDelete).style.display = "none";
		currentDelete = commentId;
		//document.getElementById("deleteComment"+commentId).style.display = "inline";
		new Effect.Appear('deleteComment'+commentId);		
	}
	
	document.getElementById("deleteComment"+commentId).style.right = -180;	
	document.getElementById("deleteComment"+commentId).style.top = 10;
	//($("deleteComment"+commentId)).style.display = "inline";
	
}

function ajaxDelete(commentId, TheForm){
	
	var postAction=URLPREFIX+albumusername+'.comment.delete';
	var AJAXLINK = postAction;//+EXTENSION;
	
	var encfolderId = "";
	var actionType = "";
	var index = "";
	var fromId = "";
	var from = "";
	var folderId = "";
	
	if(document.getElementById('currentDisplay')){
		fromId = document.getElementById('currentDisplay').value;	 	
		folderId = document.getElementById('folder').value;
		from = "image";
	}else if(document.getElementById('encfolderId')){
 		fromId = document.getElementById('encfolderId').value;	 	
 		folderId = document.getElementById('encfolderId').value;
 		from = "folder";
	}
	index = document.getElementById('commentIndex').value;
		
	var options = { method: 'post',
					parameters:{fromId:fromId, commentId:commentId, from:from, albumusername:albumusername,
					 memberShowPrivate:memberShowPrivate, folder:folderId, loginusername:loginusername},
					onSuccess: function(transport){
						response = transport.responseText || "no response text";
						//alert(response);
						currentDelete = "";
						new Effect.Fade('deleteComment'+commentId); 
						document.getElementById('commentResult').innerHTML = response;
						
						setTimeout("displayCmtMsg("+commentId+", '"+commentDisplay[13]+"')", 100);
					}, 
					onFailure: function(transport){alert("FAILURE"+transport.responseText);}
				};
	
	new Ajax.Request(AJAXLINK, options); 
}

function funDisgardComment(commentId){
	
	new Effect.toggle('hiddenPop['+commentId+']', 'appear'); 	
	//document.getElementById('popComment['+commentId+']').style.visibility="hidden";
	//document.getElementById('commentPop['+commentId+']').style.visibility="hidden";
	
	//new Effect.toggle('popComment['+commentId+']', 'slide'); 	
	
	//new Effect.toggle('commentPop['+commentId+']', 'slide'); 
	
	
	popupDisplaying= false; 
	setTimeout("showCommentPanel("+commentId+")", 600); 
	return false;
}
function showCommentPanel(commentId){
	document.getElementById('descTR'+commentId).style.display="inline";	
	document.getElementById('commentPanel['+commentId+']').style.display='inline';
	document.getElementById('usernamePanel'+displayCommentId).style.display="inline";
	document.getElementById('datePanel'+displayCommentId).style.display="inline";
}

// Check that comment description is filled in
//--------------------------------------------
function checkComment(methodUsed, index){
	
	var error = 0;
	var errorMsg = "";

	//Edit or Reply	
	if(methodUsed == 1){ 
		var txtComment = trim(($("txtReplyComment["+index+"]")).value);
		if(txtComment.length == 0){			
			error++;
			errorMsg += error+". "+commentDisplay[9];
			//document.getElementById("txtReplyComment["+index+"]").value = "";
		}		
	}else{		
	//Post
	
		var txtComment = trim(document.getElementById("txtComment").value);
		
		if(txtComment.length == 0){			
			error++;
			errorMsg += error+". "+commentDisplay[9];
			//document.getElementById("txtComment").value = "";
		}
	}
	
	if(error == 0){
		return true;
	}else{
		alert(errorMsg);
		return false;
	}
	
}

function truncateSummaryDesc(trunc, len, ele){
 //var len = 50;
	 
  if (trunc.length > len) {

    /* Truncate the content of the P, then go back to the end of the
       previous word to ensure that we don't truncate in the middle of
       a word */
    trunc = trunc.substring(0, len);
    trunc = trunc.replace(/\w+$/, '');

    /* Add an ellipses to the end and make it a link that expands
       the paragraph back to its original size */
   /*trunc += '<a href="#" ' +
      'onclick="this.parentNode.innerHTML=' +
      'unescape(\''+escape(document.getElementById('commentDesc'+index).innerHTML)+'\');return false;">' +
      '...<\/a>';*/
     trunc += "...";
     
    ele.innerHTML = trunc;
  }else{	  
	  
	ele.innerHTML = trunc;
  }
}

function updateComment(encImageId){
	
	var AJAXLINK = URLPREFIX+albumusername+"/comment.switch";
	var folderId = document.getElementById('folder').value;
	
	var options = { method: 'post',
					parameters:{encImageId:encImageId, user:albumusername, strMemberList:strMemberList, folder:folderId},
					onSuccess: function(transport){
						response = transport.responseText;
						
						var start = response.indexOf("_____");
						var total = response.substring(0, start);
						var strHtml = response.substring(parseInt(start)+5, response.length-(parseInt(start)+5));
												
						if(response.length > 0){							
							document.getElementById('totalComment').innerHTML = total;
							document.getElementById('commentResult').innerHTML = strHtml;
							document.getElementById('commentResult').style.display = "inline";
						}else{							
							document.getElementById('totalComment').innerHTML = 0;
							document.getElementById('commentResult').innerHTML = "";//commentDisplay[10];
							document.getElementById('commentResult').style.display = "none";
						}
						
					}, 
					onFailure: function(transport){alert("FAILURE"+transport.responseText);}
				};
	
	new Ajax.Request(AJAXLINK, options); 
	
}

function summaryPageSwitch(pageIndex, TheForm, block){
	
	var showLast = document.getElementById('showLast').value;
	
	if(isNaN(showLast)){ 		
		showLast = "x";		
	}
	
	var displayPerPage = document.getElementById('displayPerPage').value;
	
	
	
	if(isNaN(pageIndex) || pageIndex == "undefined"){
		var optPage = 1;
	}else{
		var optPage = pageIndex;
	}
	
	
	location.href = URLPREFIX+albumusername+"/commentSummary?optPage="+optPage+"&showLast="+showLast+"&displayPerPage="+displayPerPage; //"&nextIndex=same&currentBlock="+block;
	
}

function navShowMore(actionType, startIndex, maxDisplay , TheForm, totalPage, nextIndex, currentBlock){
	
	var strHTML = "";	
	var showLast = document.getElementById('showLast').value;
		
	if(isNaN(showLast)){ 		
		showLast = "x";		
	}	
	
	var displayPerPage = document.getElementById('displayPerPage').value;
	var optPage = nextIndex;
	
	location.href = URLPREFIX+albumusername+"/commentSummary?optPage="+optPage+"&showLast="+showLast+"&displayPerPage="+displayPerPage+"&nextIndex="+actionType+"&currentBlock="+currentBlock;
	
	
}


function scrollToNewPost(){
	
	new Effect.ScrollTo('postComment', 'txtComment');
//	setTimeout("document.getElementById('txtComment').focus();", 00);	
	//new Effect.ScrollTo('postComment'); return false;
	
}

function resetParentPanel(parentId){
	
	if(document.getElementById("parentReply"+parentId)){
		document.getElementById("parentReply"+parentId).style.display="none";
	}
}

function grow(ele){
		
	var maxCols = "40";
	
	ele.wrap = "hard";
	
	//alert(ele.scrollWidth+" -- "+ele.clientWidth);
	//alert(ele.scrollHeight+" -- "+ele.clientHeight);
	if(ele.scrollHeight > ele.clientHeight){
		while(ele.scrollHeight > ele.clientHeight){
		//	alert(ele.scrollHeight+" -- "+ele.clientHeight);
			ele.rows++;	
		}
		//ele.rows--;
	}else{
	
		/*while( ele.scrollWidth == ele.clientWidth){	
		
			
			ele.cols--;
		
			//alert(ele.scrollWidth+" -- "+ele.clientWidth);	
			//alert(ele.cols);
		}
		ele.cols++;*/
	}
	
	
	
	//alert(ele.cols);
}