var PhotoController = {
	init : function() {
		this.count = $$("div.lhPhotoAttachment").length;
		var sIds = $F("lhPostStoryPhotoIds");
		this.ids = [];
		if(sIds.length > 0) {
			this.ids = sIds.split(",");
		}
		var fileTemplate = new Template(
			'<div class="lhPhotoAttachment" id="lhPhotoAttachment_#{index}">'
			+' <input type="file" class="lhPhotoFile" name="photoFile_#{index}" id="photoFile_#{index}">'
			+' <input type="text" class="lhPhotoCaption lhDefault" name="photoCaption_#{index}" id="photoCaption_#{index}" maxlength="100" value="Enter Caption">'
			+' <a href="#" class="lhPhotoRemove" title="Remove"><img src="/images/icons/action_stop.gif"></a>'
			+'</div>'
		);
		var fCaptionFocusHandler = function(evt) {
			var eInput = evt.findElement("input");
			if(!eInput || !eInput.hasClassName("lhDefault")) return;
			eInput.removeClassName("lhDefault");
			eInput.value = "";
			evt.stop();
		};
		//Setup focus for pre-loaded captions
		$$("input.lhPhotoCaption").each(function(e) {
			e.observe("focus",fCaptionFocusHandler);
		});
		$("lhPostStoryPhotoContainer").observe("click",function(evt) {
			var eClicked = evt.findElement("a");
			if(!eClicked || !eClicked.hasClassName("lhPhotoRemove")) return;
			var id = parseInt(/lhPhotoAttachment_(\d+)/.exec(eClicked.up().id)[1]);
			this.ids = this.ids.without(id);
			$("lhPostStoryPhotoIds").value = this.ids.join(",");
			eClicked.up().remove();
			if($("lhPostStoryPhotoContainer").childElements().length == 0) {
				$("lhPostStoryPhotoContainer").hide();
			}
			evt.stop();
		}.bindAsEventListener(this));
		$("lhPostStoryAttachPhoto").observe("click",function(evt) {
			var iNewIndex = ++this.count;
			this.ids.push(iNewIndex);
			$("lhPostStoryPhotoIds").value = this.ids.join(",");
			$("lhPostStoryPhotoContainer").show();
			$("lhPostStoryPhotoContainer").insert(fileTemplate.evaluate({index:iNewIndex}));
			$("photoCaption_"+iNewIndex).observe("focus",fCaptionFocusHandler);
		}.bindAsEventListener(this));			
	}
}
if($("lhPostStoryPhotoIds")) {
	PhotoController.init();	
}


var RelatedHistoryController = {
	init : function() {
		var sIds = $F("lhRelatedProfileIds");
		this.ids = [];
		if(sIds.length > 0) {
			this.ids = sIds.split(",");
		}
		var relatedHistoryTemplate = new Template(
			'<div class="lhRelatedProfile" id="lhRelatedProfile_#{id}">#{address} <a href="#" class="lhRelatedProfileRemove"><img src="/images/icons/action_stop.gif"></a></div>'
		);
		$("lhRelatedProfileContainer").observe("click",function(evt) {
			var eClicked = evt.findElement("a");
			if(!eClicked || !eClicked.hasClassName("lhRelatedProfileRemove")) return;
			var eRelProfile = eClicked.up("div.lhRelatedProfile");
			var sId = eRelProfile.id;
			var id = parseInt(/lhRelatedProfile_(\d+)/.exec(sId)[1]);
			this.ids = this.ids.without(id);
			this.updateIds();
			eRelProfile.remove();
			if($("lhRelatedProfileContainer").childElements().length == 0) {
				$("lhRelatedProfileContainer").hide();
			}
			evt.stop();
		}.bindAsEventListener(this));
		$("lhRelatedProfileQuery").observe("focus",function(evt) {
			var eQuery = $("lhRelatedProfileQuery");
			if(eQuery.hasClassName("lhDefault")) {
				eQuery.removeClassName("lhDefault");
				eQuery._defaultValue = eQuery.value;
				eQuery.value = "";
			} 
		});
		$("lhRelatedProfileQuery").observe("blur",function(evt) {
			var eQuery = $("lhRelatedProfileQuery");
			if(!eQuery.hasClassName("lhDefault") && eQuery.value.length == 0) {
				eQuery.addClassName("lhDefault");
				eQuery.value = eQuery._defaultValue;
			} 
		});
		new Ajax.Autocompleter("lhRelatedProfileQuery","lhRelatedProfileSuggestions","/lh/RelatedProfilesSuggestions.html",{
			paramName : "query"
			, parameters : {profileId:$F("profileId")}
			, updateElement : function(eSelected) {
				$("lhRelatedProfileContainer").show();
				$("lhRelatedProfileContainer").insert(relatedHistoryTemplate.evaluate({id:eSelected.id,address:eSelected.innerHTML}));
				$("lhRelatedProfileQuery").value = "";
				$("lhRelatedProfileQuery").focus();
				this.ids.push(parseInt(eSelected.id));
				this.updateIds();
			}.bind(this)
		});
	}
	, updateIds : function() {
		$("lhRelatedProfileIds").value = this.ids.join(",");
	}
}
RelatedHistoryController.init();
