var secDefaults = {
	effectsDuration: {
		duration: 0.2
	}
};

function secUiInit(scope){

	// Forms init
	scope.select('form').each( function(form){
		secFormInit(form);
	});

	secAddRemoveDocButtons(scope);

}

function secFormInit(form){ /* DO ALL OF THIS USING THE SAME METHOD AS JONNY DID!!! */

	// Field tips/info
	form.select('span.tip').each( function(tip){

		tip_handle = tip.up('div.field').down('input, select, textarea');

		tip_handle.observe("focus", function(evt){
			tip.appear(secDefaults.effectsDuration);
		});

		tip_handle.observe("blur", function(evt){
			tip.fade(secDefaults.effectsDuration);
		});

	});

}

function secAddRemoveDocButtons(scope){
	scope.select('a.addremove_document').each( function(btn){

		var btn_removetablerow = false;
		if ( btn.hasClassName("addremove_tablerow") ){
			btn_removetablerow = true;
		}

		btn.observe("click", function(evt){
			evt.stop();

			var btn_parent = btn.up();
			btn_parent.hide();

			new Ajax.Updater(btn_parent,btn.href, {
				onComplete: function(response){
					if ( btn_removetablerow ){
						btn_parent.up().up().hide();
					} else {
						btn_parent.appear({duration: .4});
						secAddRemoveDocButtons(btn_parent);
					}
				}
			});
		});
	});
}

document.observe('dom:loaded',function(){

	LinkedInfo = function(linksArray, linksType){
		linksArray.each( function(currentModalLink){
			currentModalLink.observe('click', function(event){
				currentModalLink.next("ul").toggle();
				if ( currentModalLink.next("ul").getStyle('display') == "none" ){
					currentModalLink.up('li').className = "";
				} else {
					currentModalLink.up('li').className = "active";
				}
				Event.stop(event);
				/*
				$$('a.link-info-title').each(function(ModalLink){
					if(ModalLink!=currentModalLink) {
						ModalLink.next("ul").hide();
						ModalLink.up('li').className = "";
					}
				});
				*/
			});
		});
	}

	LinkedInfoChild = function(linksArray, linksType){	
		linksArray.each( function(currentModalLink){
			currentModalLink.observe('click', function(event){
				Event.stop(event);
				Lightview.show({
					href: currentModalLink.href + "?ajax=1",
					rel: 'ajax',
					title: '',
					caption: '',

					options: {
						width: 485,
						height: 500,
						topclose: true,
						ajax: {
							method: 'get',
							evalScripts: true,
							onComplete: function(){ $('name').focus(); }
						}
					}
				});
			});
		});
	}


	LinkedInfo( $$('a.link-info-title') );
	LinkedInfoChild( $$('a.modal-link') );

	$$('body').each(function(elt){
		secUiInit(elt);
	});

});
