/*
 * PAF_UtilityClass provides information about the PAF instance,
 * the current page and the design.
 *
 * - gets included automatically in every design
 * - requiered for AjaxRequests
 * - useful for additional interaction on the page / with the kernel
 *
 */

PAF_AJAX_REQUEST = "AJAX";
PAF_HTTP_GET_REQUEST = "HTTP_GET";
PAF_HTTP_POST_REQUEST = "HTTP_POST";

function PAF_UtilityClass() {
    this._activePageId = -1;
    this._basePath = "/PAF/";
	this._multiUploader = new Object();

    this.getActivePageId = function() {
        return this._activePageId;
    };

    this.setActivePageId = function(aActivePageId) {
        this._activePageId = aActivePageId;
    };

    this.getBasePath = function(){
    	return this._basePath;
    };

    this.isNumeric = function (value) {
		if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
		return true;
	}

	this.clearForInput = function(aInputField, aDefaultValue){
		if (aInputField.value == aDefaultValue){
			aInputField.value = "";
		}
	}

	this.fillWithDefaults = function(aInputField, aDefaultValue){
		if (aInputField.value == ""){
			aInputField.value = aDefaultValue;
		}
	}

	this.refreshPageWithoutParameters = function(aPage){
		if(aPage == undefined){
			var vUrl = document.location.href;
			vUrl = vUrl.replace(/__.*/, '').replace(/\/[^\/]*$/, '') + '/';
			document.location.href = vUrl;
		}

	}

	this.installEditor = function(aIdTextarea, aTitle, aHeight, aWidth) {
		var Dom = YAHOO.util.Dom,
			Event = YAHOO.util.Event;

		var myConfig = {
			height: aHeight,
			width: aWidth,
			dompath: true,
			focusAtStart: false,
			handleSubmit: true
		};

		var myEditor = new YAHOO.widget.Editor(aIdTextarea, myConfig);
		myEditor._defaultToolbar.buttonType = 'basic';
		myEditor._defaultToolbar.titlebar = aTitle;
		myEditor.render();

	};

	this.installMultiUploader = function(aIdentifier, aFormName, aPHPSessionId){
		this._multiUploader[aIdentifier] = new PAF_MultiUploader(aIdentifier, aFormName, aPHPSessionId);
		//console.log(this._multiUploader);
	}
	
	this.MultiUploaderAddNoFilesSelectedNotification = function(aIdentifier){
		if (this._multiUploader[aIdentifier] != undefined && this._multiUploader[aIdentifier].SWFUploader.addNoFilesSelectedNotification != undefined){
			this._multiUploader[aIdentifier].SWFUploader.addNoFilesSelectedNotification();
		}		
	}	

	this.clearMultiUploaderList = function(aIdentifier){		
		if (this._multiUploader[aIdentifier] != undefined){
		   this._multiUploader[aIdentifier].SWFUploader.clearUploadList();
		}
	}

	this.destroyMultiUploader = function(aIdentifier){
		if (this._multiUploader[aIdentifier] != undefined){
		   this._multiUploader[aIdentifier].destroy();
		}
	}

	this.toggleAdminControls = function(){
		var vActionUrl = new PAF_AjaxUrl();
		vActionUrl.addParameter("kernel__action", 'ToggleAdminControls');
		$.get(vActionUrl.getUrl());
	}

	this.toggleGuestRegistration = function(aRadioInputField){
		vRadioNameRegexResult = aRadioInputField.name.match(/^([a-zA-Z0-9]*)__aInternalEvent$/g);
		if (vRadioNameRegexResult){
			var vModletUniqueId = RegExp.$1;

			
			if(aRadioInputField.value == 1){
				$("#" + vModletUniqueId + "__wrapper_aRegistrationForGuest").hide();
			}else{
				$("#" + vModletUniqueId + "__wrapper_aRegistrationForGuest").show();
			}
		}

		
	}
}

function PAF_AjaxUrl() {
    return new PAF_Url(PAF_AJAX_REQUEST);
}

function PAF_Url(aRequestType) {
    this._projectId = PAF_ACTIVE_PROJECT_ID;
    this._websiteId = PAF_ACTIVE_WEBSITE_ID;
    this._pageId = PAF_ACTIVE_PAGE_ID;
    this._type = aRequestType;
	this._baseUrl = PAF_BASE_URL;
	this._parameterArray = new Array();

    this.getUrl = function() {
    	var vUrl = '';
    	switch(this._type){
    		case PAF_AJAX_REQUEST:
	    		vUrl = this._baseUrl + 'ajax.php';
	    		vUrl = vUrl + '?aProjectId=' + this._projectId;
    			vUrl = vUrl + '&aWebsiteId=' + this._websiteId;
    			vUrl = vUrl + '&aPageId=' + this._pageId;
    			vUrl = vUrl + '&parameters=' + this._buildParameterString();
    	}

    	return vUrl;
    };

    this._buildParameterString = function(){
    	var vParameterString = '';
		for (var i in this._parameterArray) {
			vParameterString = vParameterString + i + '/' + this._parameterArray[i] + '/';
		}
		return vParameterString;
    }

    this.setPageId = function(aActivePageId) {
        this._activePageId = aActivePageId;
    };

    this.addParameter = function(aParameter, aValue){
    	this._parameterArray[aParameter] = aValue;
    }
}

function PAF_DatePicker(aName, aShowDatePickerElement){

	this.dialog = null;
	this.calendar = null;
	this.seldate = null;
	this.test = null;
	this.context = aShowDatePickerElement;
	this.name = aName;

	this.Event = YAHOO.util.Event,
            Dom = YAHOO.util.Dom,
            this.dialog,
            this.calendar;

	this.showBtn = Dom.get(aShowDatePickerElement);

	this.init = function(){
		if (!this.dialog) {

			this.dialog = new YAHOO.widget.Dialog('caldialog_' + this.name, {
				visible:false,
				context:[this.context, "tl", "bl"],
				buttons:[ {text:"Reset", handler: PAF_DatePicker.resetHandler, isDefault:true}, {text:"Close", handler: this.closeHandler}],
				draggable:false,
				close:true
			});

			this.dialog.setHeader('Pick A Date');
			this.dialog.setBody('<div id="calcontainer_' + this.name + '"></div>');
			this.dialog.render(document.body);

			this.dialog.showEvent.subscribe(function() {
				if (YAHOO.env.ua.ie) {
					// Since we're hiding the table using yui-overlay-hidden, we
					// want to let the dialog know that the content size has changed, when
					// shown
					self.dialog.fireEvent("changeContent");
				}
			});
		}

		// Lazy Calendar Creation - Wait to create the Calendar until the first time the button is clicked.
		if (!this.calendar) {

			this.calendar = new YAHOO.widget.Calendar('calcontainer_' + this.name, {
				iframe:false,          // Turn iframe off, since container has iframe support.
				hide_blank_weeks:true  // Enable, to demonstrate how we handle changing height, using changeContent
			});

			this.calendar.render();

			this.calendar.selectEvent.subscribe(function() {
				if (self.calendar.getSelectedDates().length > 0) {

					self.seldate = self.calendar.getSelectedDates()[0];

					// Pretty Date Output, using Calendar's Locale values: Friday, 8 February 2008
//					var wStr = self.calendar.cfg.getProperty("WEEKDAYS_LONG")[self.seldate.getDay()];
					var dStr = String(self.seldate.getDate());
					dStr = (dStr.length == 1)?"0"+dStr:dStr;
					var mStr = String(self.seldate.getMonth()+1);
					mStr = (mStr.length == 1)?"0"+mStr:mStr;
					var yStr = self.seldate.getFullYear();

					Dom.get(self.name).value = dStr + "." + mStr + "." + yStr;
				} else {

					Dom.get(self.name).value = "";
				}
				self.dialog.hide();
			});

			this.calendar.renderEvent.subscribe(function() {
				// Tell Dialog it's contents have changed, which allows
				// container to redraw the underlay (for IE6/Safari2)
				self.dialog.fireEvent("changeContent");
			});
		}
	}
	this.show = function(){
		self = this;

		// Hide Calendar if we click anywhere in the document other than the calendar
        this.Event.on(document, "click", function(e) {
			var el = self.Event.getTarget(e);
			var dialogEl = self.dialog.element;
			if (el != dialogEl && !Dom.isAncestor(dialogEl, el) && el != self.showBtn && !Dom.isAncestor(self.showBtn, el)) {
				self.dialog.hide();
			}
		});

		this.seldate = this.calendar.getSelectedDates();

		if (this.seldate.length > 0) {
			// Set the pagedate to show the selected date if it exists
			this.calendar.cfg.setProperty("pagedate", this.seldate[0]);
			this.calendar.render();
		}

		this.dialog.show();
	};

}






/*JS für Permission Geschichte*/
/*
var allow_selection = true;
var mouse_x = 0;
var mouse_y = 0;

	$(window).load(function() {
			var selection_stop_Timeout;
		    var selection_stop_Timeout_is_active = false;



			$("#sel_table").mousemove(function(e){
				mouse_x = e.pageX;
				mouse_y = e.pageY;
			});

			$("#dialog input.option_1")[0].onclick = function(){// Button alle auf ja
				$('.ui-selected input.option_1').each(function(e,ele){ele.checked=true;});
				hideDialog('delete_selection');
			};

			$("#dialog input.option_2")[0].onclick = function(){// Button alle auf nein
				$('.ui-selected input.option_2').each(function(e,ele){ele.checked=true;});
				hideDialog('delete_selection');
			};

			$("#dialog input.option_3")[0].onclick = function(){// Button alle auf n/a
				$('.ui-selected input.option_3').each(function(e,ele){ele.checked=true;});
				hideDialog('delete_selection');
			};

			$("#dialog input.option_4")[0].onclick = function(){// Button keine aktion mit der auswahl
				hideDialog('keep_selection');
			};

			$('#sel_table input').each(function(e,ele){//klick auf radio-button td bgcolor ändern
										ele.onclick = function(t){
											$(ele).parent().removeClass('color_option_1');
											$(ele).parent().removeClass('color_option_2');
											$(ele).parent().removeClass('color_option_3');
											newClass = "color_option_"+this.value;
											$(ele).parent().addClass(newClass);
										}

								  });

			$('#sel_table').selectable({ filter: 'td.value_td',delay: 100  });//selectable aktivieren

		    $('#sel_table').bind('selectablestart', function(event, ui) {//selectable-event -> displayDialog
				window.clearTimeout(selection_stop_Timeout);
				selection_stop_Timeout_is_active = false;
			});


			$('#sel_table').bind('selectablestop', function(event, ui) {//selectable-event -> displayDialog
				if(selection_stop_Timeout_is_active == false){
				    selection_stop_Timeout = window.setTimeout("displayDialog();",100);
			    }
			});

			//add select-function to each group-header
			$('th.group_header_0')[0].ondblclick = function(){
				if(allow_selection == true){
					$('td.group_0').addClass('ui-selected');
				};
				displayDialog();
			}

			$('th.group_header_1')[0].ondblclick = function(){
				if(allow_selection == true){
					$('td.group_1').addClass('ui-selected');
				};
				displayDialog();
			}

			$('th.group_header_2')[0].ondblclick = function(){
				if(allow_selection == true){
					$('td.group_2').addClass('ui-selected');
				};
				displayDialog();
			}

			//add select-function to each permission-header
			$('td.permission_header').each(function(e,ele){
				ele.ondblclick = function(me){
					if(allow_selection == true){
						$(ele).nextAll().addClass('ui-selected');
					};
				displayDialog();
				}
			})

			$('.radio').hide();
			$('.radioDescr').hide();


	});


	displayDialog = function(){//nach Auswahl Dialog anzeigen und selectable deaktivieren
		if($('td.ui-selected').length > 0){
			allow_selection = false;
			$('#sel_table').selectable('disable');
			$('#dialog')[0].style.marginTop=mouse_y-50+"px";
			$('#dialog')[0].style.marginLeft=mouse_x-175+"px";
			$("#dialog").show();
		}

	}

	hideDialog = function(mode){// alles noetige um den Dialog zuschließen + nachträgliche aktionen starten und selectable wieder aktivieren
		$("#dialog").hide();
		allow_selection = true;
		$('#sel_table').selectable('enable');
		if(mode == "delete_selection"){
		  $('td.ui-selected').removeClass('ui-selected');
		};
		update_colorful();
	}

	update_colorful = function(){//alle TDs anhand von ihren checked radio-Buttons einfärben
		$('td>input:checked').each(function(e,ele){
			input = $(ele);
			td = input.parent();
			td.removeClass('color_option_1');
			td.removeClass('color_option_2');
			td.removeClass('color_option_3');
			td.addClass('color_option_'+input[0].value);
		});
	};
*/




/* globale Fuktion zum ein und ausblenden von Layer mit Iconwechsel*/
function showHideLayer(myLayer,myImageId,imageVisible,imageInVisible){
	if(document.getElementById(myLayer).style.display=='none'){
		document.getElementById(myLayer).style.display='';
		document.getElementById(myImageId).src=imageVisible;
	}else{
		document.getElementById(myLayer).style.display='none';
		document.getElementById(myImageId).src=imageInVisible;
	}
}
/* globale Fuktion zum ein und ausblenden von Layer mit Iconwechsel*/

