$(document).ready(function(){
	$('#forum_newpost_uploadLink').click(function(){
		if (document.getElementById('forum_newpost_docAttachment').style.display=='block') {
			$('#forum_newpost_docAttachment').toggle(400);
		}
		document.getElementById('selectedDoc').value="";
		$('#forum_newpost_uploadAttachment').toggle(400);
	});
	
	$('#forum_newpost_docLink').click(function(){
		if (document.getElementById('forum_newpost_uploadAttachment').style.display=='block') {
			$('#forum_newpost_uploadAttachment').toggle(400);
		}
		$('#forum_newpost_docAttachment').toggle(400);
	});
});

var arrUploadAjax=new Array();
var indexUploadAjax=0;
var selectedDoc=null;

function selectDoc(lid,link) {
	if (selectedDoc != null) {
		selectedDoc.style.background='#FFFFFF';
	}
	link.style.background='#BBBBFF';
	selectedDoc=link;
	document.getElementById('selectedDoc').value=lid;
}

function attachDocFromN4A() {
	var lid=document.getElementById('selectedDoc').value;

}

function startCallback() {
	return true;
}

// adds the label to the list of labels
// the response is organized like lid,filename,rightsProfile to yield [0]=>lid, [1]=>filename, [2]=>rightsProfile
function completeCallback(response) {
	if (response.indexOf('error:') != -1 || response.indexOf('Fatal error') != -1) {
		alert(response);
	}
	else {
		var arrResponse=response.split(",");
		var storageField=document.getElementById('postLabelsAttached');
		if (storageField.value > "") {
			storageField.value += "," + arrResponse[0] + ":" + arrResponse[2];
		}
		else {
			storageField.value=arrResponse[0] + ":" + arrResponse[2];
		}
		
		// displays the file listing to the user
		var containerDiv=document.createElement('div');
		var filenameDiv=document.createElement('div');
		var linkDiv=document.createElement('div');
		var deleteLink=document.createElement('a');
		
		deleteLink.setAttribute('href','javascript:;');
		deleteLink.setAttribute('onclick','removeFile(' + arrResponse[0] + ')');
		deleteLink.innerHTML="Remove";
		
		linkDiv.appendChild(deleteLink);
		linkDiv.setAttribute('class','forumNewpostAttachment_linkContainer');
		
		filenameDiv.innerHTML="Attached Document: " + arrResponse[1];
		filenameDiv.setAttribute('class','forumNewpostAttachment_nameContainer');
		
		containerDiv.setAttribute('id','attachedDocumentContainer_'+arrResponse[0]);
		containerDiv.appendChild(filenameDiv);
		containerDiv.appendChild(linkDiv);
		
		var positionReferenceNode=document.getElementById('forum_newpost_docAttachment');
		positionReferenceNode.parentNode.insertBefore( containerDiv, positionReferenceNode.nextSibling );
		
		document.getElementById('forum_newpost_docAttachment').style.display='none';
		document.getElementById('forum_newpost_uploadAttachment').style.display='none';
	}
}
/**
*
*  AJAX IFRAME METHOD (AIM)
*  http://www.webtoolkit.info/
*
**/
AIM = {
	frame : function(c) {
 
		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');
		d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
		document.body.appendChild(d);
 
		var i = document.getElementById(n);
		if (c && typeof(c.onComplete) == 'function') {
			i.onComplete = c.onComplete;
		}
 
		return n;
	},
 
	form : function(f, name) {
		f.setAttribute('target', name);
	},
 
	submit : function(f, c) {
		AIM.form(f, AIM.frame(c));
		if (c && typeof(c.onStart) == 'function') {
			return c.onStart();
		} else {
			return true;
		}
	},
 
	loaded : function(id) {
		var i = document.getElementById(id);
		if (i.contentDocument) {
			var d = i.contentDocument;
		} else if (i.contentWindow) {
			var d = i.contentWindow.document;
		} else {
			var d = window.frames[id].document;
		}
		if (d.location.href == "about:blank") {
			return;
		}
 
		if (typeof(i.onComplete) == 'function') {
			i.onComplete(d.body.innerHTML);
		}
	}
 
};


function removeFile(lid) {
	// find the lid:permissions pair in #postLabelsAttached and remove it from the list
	if (document.getElementById('postLabelsAttached').value != "") {
		arrLabelsAttached=document.getElementById('postLabelsAttached').value.split(',');
		labelsAttachedLength=arrLabelsAttached.length;
	}
	else {
		labelsAttachedLength=0;
	}
	
	var outputList="";
	for(var i=0; i<labelsAttachedLength; i++) {
		arrLabelListing=arrLabelsAttached[i].split(':');
		if (arrLabelListing[0] != lid) {
			if (outputList == "") {
				outputList=arrLabelListing[0]+":"+arrLabelListing[1];
			}
			else {
				outputList=","+arrLabelListing[0]+":"+arrLabelListing[1];
			}
		}
	}

	document.getElementById('postLabelsAttached').value=outputList;
	var attachmentRow=document.getElementById('attachedDocumentContainer_'+lid);
	attachmentRow.parentNode.removeChild(attachmentRow);
}






