
var AgatAttachment={window_id:"agat_attachment_menu",attachElem:"",popupElem:"",displayElem:"",uploaded:false,animationSpeed:200,show:function(evt1){var evt=evt1.originalEvent;attachElem=$(evt1.target)[0];displayElem=$(attachElem).prev().children()[0];if(!displayElem)return;if(displayElem.value=="")return;uploaded=AgatAttachment.isUploaded(displayElem.id);var x=evt1.pageX;var y=evt1.pageY;if(y+60>document.body.scrollTop+document.body.clientHeight){y=document.body.scrollTop+document.body.clientHeight-60;}
var popup=document.getElementById(AgatAttachment.window_id);if(!popup){popup=document.createElement("div");popup.id=AgatAttachment.window_id;$(popup).hide();$(popup).appendTo("body");}
popupElem=popup;$(document).bind("mousedown",AgatAttachment.mouseHandler);popup.style.position="absolute";popup.style.top=y;popup.style.left=x;popup.style.zIndex="3";popup.style.float="left";popup.style.border="solid 1px #666666 ";AgatAttachment.build(popup);$(popup).show(this.animationSpeed);popup.focus();},build:function(popup){var language=AgatEngine.getGeneralAttribute("tfsLanguage").toLowerCase()||"english";if(language=="hebrew"){popup.style.width=130;popup.style.height=60;popup.style.direction="rtl";popup.style.backgroundImage="url("+systemPictures+"/hebrewBackgroundPopup.jpg)";popup.style.backgroundPosition="right top";}
else{popup.style.width=180;popup.style.height=60;popup.style.direction="ltr";popup.style.backgroundImage="url("+systemPictures+"/englishBackgroundPopup.jpg)";popup.style.backgroundPosition="left top";}
var displayLink;if(uploaded){displayLink=document.createElement("a");$(displayLink).bind("click",AgatAttachment.display);}
else{displayLink=document.createElement("div");displayLink.style.color="gray";displayLink.style.fontWeight="bold";}
displayLink.href="javascript:void('')";displayLink.style.display="block";displayLink.style.margin="5px";popup.appendChild(displayLink);var displayImg=document.createElement("img");displayImg.border="0";displayImg.src=systemPictures+"/displayAttach.png";displayImg.align="middle";displayLink.appendChild(displayImg);var displayText=document.createElement("span");$(displayText).text(AgatEngine.getString("DISPLAY_ATTACHMENT_CONTEXT_MENU","WINDOW_TITLE"));displayText.style.padding="0px 8px";displayLink.appendChild(displayText);var enabled=true;if($(displayElem).hasAttribute("tfsdelete"))
enabled=!AgatUtils.parseBool($(displayElem).attr("tfsdelete"));var removeLink;if(enabled){removeLink=document.createElement("a");$(removeLink).bind("click",AgatAttachment.remove);}
else{removeLink=document.createElement("div");removeLink.style.color="gray";removeLink.style.fontWeight="bold";}
removeLink.style.display="block";removeLink.href="javascript:void('')";removeLink.style.margin="5px";popup.appendChild(removeLink);var removeImg=document.createElement("img");removeImg.border="0";removeImg.align="middle";removeImg.src=systemPictures+"/del_small.gif";removeLink.appendChild(removeImg);var removeText=document.createElement("span");$(removeText).text(AgatEngine.getString("DELETE_ATTACHMENT_CONTEXT_MENU","WINDOW_TITLE"));removeText.style.padding="0px 8px";removeLink.appendChild(removeText);},mouseHandler:function(evt){var popup=$("#"+AgatAttachment.window_id)[0];if(!popup||popup.style.display=="none")return;var parent=evt.target;if($(evt.target).parents("#"+popup.id).length>0||$(evt.target)[0]==popup)
return;AgatAttachment.close(popup);evt.preventDefault();evt.stopPropagation();},close:function(popup){if(!popup)popup=popupElem;$(popup).hide(this.animationSpeed);setTimeout(function(){$(popup).remove();},this.animationSpeed);$(document).unbind("mousedown",AgatAttachment.mouseHandler);},display:function(){if(!uploaded)return;window.open(userAttachmentsPath+"/"+displayElem.value);AgatAttachment.close();},remove:function(){displayElem.value="";attachElem.readOnly=false;$(attachElem).hide();var temp=$(attachElem).clone(true)[0];$(attachElem).parent()[0].replaceChild(temp,attachElem);$(temp).show();AgatAttachment.close();},handleChange:function(inputFileElem){var displayElem=AgatAttachment.getDisplayElem(inputFileElem.id);displayElem.value=AgatAttachment.getFilename(displayElem.id);inputFileElem.readOnly=true;$(displayElem).attr("tfsUploaded","false");},isUploaded:function(inputDisplayId){if(!$("#"+inputDisplayId).hasAttribute("tfsUploaded"))return false;return AgatUtils.parseBool($("#"+inputDisplayId).attr("tfsUploaded"));},getDisplayElem:function(inputFileId){try{var result=$("#"+inputFileId).parent().children(":first").children(":first");if(result.attr("tfsdatatype")=="attachment")return result[0];else return null;}
catch(error){return null;}},getAttachElem:function(inputDisplayId){try{return $("#"+inputDisplayId).parent().parent().children(":last")[0];}
catch(error){return null;}},setAttachElem:function(inputDisplay,elem){try{$(inputDisplay).parent().parent().append($(elem));}
catch(error){}},getFilename:function(inputDisplayId){var attach=this.getAttachElem(inputDisplayId);var display=$("#"+inputDisplayId)[0];if(attach.value=="")
return display.value;if(attach.value.indexOf("\\")!=-1)
return/([^\\]+)$/.exec(attach.value)[1];else
return attach.value;}}