var pop = {
	/* POPUPS */
	initPop: function() {
		pop.popConfig =  { 
			width:"600px", 
			visible:false, 
			constraintoviewport :true, 
			zindex:2000,
			y:300, 
			postmethod:"form"
		};
		pop.inputPop = new YAHOO.widget.Panel("pop", pop.popConfig); 
		pop.inputPop.render();

		
		pop.triggers = YAHOO.util.Dom.getElementsByClassName("poplink", "a"); 
		
		var addtiny = false; // should tiny editor be added? 
		for (el in pop.triggers){
				// pop.triggers[el].style.color="red";
				if (YAHOO.util.Dom.hasClass(pop.triggers[el], "addtiny")) {
					addtiny = true;
				} else {
					addtiny = false;
				}
				YAHOO.util.Event.addListener(pop.triggers[el], "click", pop.showPop, {formUrl: pop.triggers[el].href, tiny: addtiny});

				//YAHOO.util.Event.on(pop.triggers[el], "click", pop.triggerAction, {target: pop.triggers[el].href})
		}
		pop.inputPop.beforeHideEvent.subscribe(pop.unloadTiny);
		// url defines where to find the upload form  / where to submit form to
		//YAHOO.util.Event.addListener("add_image", "click", pop.showPop, {formUrl: "/photos/upload/", saveUrl: "/photos/upload/", formId: "photo_form"});
	}, 
	unloadTiny: function() {
		/*
		if (pop.enableTiny){ 
			tinyMCE.execCommand('mceFocus', false, pop.tinyTextareaId);                    
			tinyMCE.execCommand('mceRemoveControl', false, pop.tinyTextareaId);
		}
		*/
		
	},
	manualShowPop: function(obj){
		pop.showPop(false, obj);
	},
	caretFix: function(){
		if (YAHOO.env.ua.gecko) {
        /* 
         * Other part of workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=167801 !!?
         * I have no explanation for why the new thread (setTimeout) is needed, but it is.
         */
        	pop.inputPop.form = document.getElementById("pop_form");
			YAHOO.util.Dom.addClass(pop.inputPop.form, "caretfix");
			
        	//pop.inputPop.showEvent.subscribe(function() {
            	YAHOO.util.Dom.setStyle(pop.inputPop.form, "display", "none");
			
            	var fixDisplay = function() {
    				YAHOO.util.Dom.setStyle(pop.inputPop.form, "display", "block");
                	try {
                    	pop.inputPop.firstFormElement.focus();
                	} catch (e) {
                    	// Not related to the workaround, I just try/catch focus calls
                    	// do avoid testing for the various conditions in which they could
                    	// fail.
                	}
            	}
            	setTimeout(fixDisplay, 0);
        	//});
    	}
	},
	

	showPop: function(evt, contextVars){ // get the content of popup 
		//if (evt != false) {
			YAHOO.util.Event.preventDefault(evt);  
		//}
		
		// get form content
		a.rpc = contextVars.formUrl; 
		
		// save submit-stuff for later
		pop.saveUrl = contextVars.formUrl;
		pop.formId = "pop_form";
		pop.enableTiny = true;
//		pop.enableTiny = contextVars.tiny;
		//pop.formId = contextVars.formId;

		// run setBodyContent when the data returns
		a.reqGet(pop.setBodyContent);
	}, 
	setBodyContent: function(o){
		pop.inputPop.setBody(o.responseText);

		// ADD EDITOR
		if (pop.enableTiny){ 
//!!!	
			var txta = YAHOO.util.Dom.getElementsBy(function(){return true;}, "textarea", "pop");
			pop.tinyTextareaId = txta[0].id;
			var oFCKeditor = new FCKeditor( 'id_content' ) ;
			//var oFCKeditor = new FCKeditor(pop.tinyTextareaId) ;
			oFCKeditor.BasePath    = '/site_media/fckeditor/';
			oFCKeditor.ReplaceTextarea() ;
			
			//tinyMCE.execCommand('mceAddControl', false, pop.tinyTextareaId);
		}

		pop.inputPop.show();
		
		pop.caretFix();
		
		// submit functionality on the submit button given in the template
		pop.addSubmitButton();

	}, 	
	submitForm: function(){ // submit new entry
		// save html for tiny editor 
		pop.unloadTiny();
		a.rpc = pop.saveUrl; 
		a.fileForm(pop.formId);
		a.reqPost(pop.submitFormReturn);
	},
	submitFormReturn: function(o){ // returnfunction for saving form 
		if (o.responseText=="reload") {
			// remove tiny editor before reload... or die! 
			pop.unloadTiny();
			a.reload();
		} else {
			pop.setBodyContent(o);
		}
	}, 
	addSubmitButton: function(){
		pop.submitButton = new YAHOO.widget.Button("pop_submit"); 
		var woot = YAHOO.util.Event.addListener("pop_submit", "click", pop.submitForm);
	} 
};
YAHOO.util.Event.onDOMReady(pop.initPop);
