var ds_Base = new Class({
	
	options: {
		
		formatterSourceElm: 'title',
		formatterTargetElm: 'url_segment'
	},
	
	wizards: [],
	accordions: [],
	forms: [],
	dropdowns: [],
	imageSelectors: [],
	fileSelectors: [],
	colorSelectors: [],
	colorSelectButtons: [],
	hasmanies: [],
	regionSelectors: [],
        
	wizardInstanceCounter: 0,
        wizardInits: [],
	accordionInstanceCounter: 0,
	formInstanceCounter: 0,
	dropdownInstanceCounter: 0,
	imageSelectInstanceCounter: 0,
	fileSelectInstanceCounter: 0,
	hasManyInstanceCounter: 0,
	regionSelectInstanceCounter: 0,

	initialize: function(options) {
            this.setOptions(options);
            this.oAjax = new ds_Plugin_Ajax();
	},
	
	formatUrl: function(e) {
            var targetElm = $(e.target.form).getElement('#' + this.options.formatterTargetElm);
            $(targetElm).setProperty('value', $(e.target).getProperty('value').clean().replace(/[^a-z0-9 ]/ig,'-').replace(/ /g, '-').replace(/[-]+/g, '-'));
	},
	
	
	numerateString: function(str) {
            if ($type(str) == 'string') {
                return str.replace(/[^0-9]/ig,'');
            }
	},
	
	setInfoMessage: function(config) {
            if ($('statusbar')) {
                var statusbar = $('statusbar').getElement('div#messages').getElement('p').addClass(config.icon).setText(config.text);
            }
	},
	
	/*normalizeString: function(str) {
	       return str.toLowerCase()
	                 .replace(/�|�|�|�/,'e')
	                 .replace(/�|�|�|�|�/,'o')
	                 .replace(/�|�|�|�/,'i')
	                 .replace(/�|�|�|�|�|�|�/,'a')
	                 .replace(/�|�|�|�/,'u')
	                 .replace(/�/g, "n");
	},*/

	getHasMany: function(options) {
            if (!$defined(this.hasmanies[options.key])) {
                this.wizardInstanceCounter++;
                this.hasmanies[options.key] = new ds_Plugin_HasMany(options);

                return this.hasmanies[options.key];
            } else {
                return this.hasmanies[options.key];
            }
            return false;
	},

	getWizard: function(key) {
            if (!$defined(this.wizards[key])) {
                this.wizardInstanceCounter++;
                this.wizards[key] = new ds_Plugin_Wizard({zindex:($$('.ds_Wizard').length * 10) + 101, instanceId:this.wizardInstanceCounter, instanceKey: key});
                
                return this.wizards[key];
            } else {
                return this.wizards[key];
            }
            return false;
	},
	
	closeAllWizards: function() {
            console.log('closeallwizlle');
            $E('.ds_Wizard').remove();
	},
	
	getFileSelector: function(options) {
            if (!$defined(this.fileSelectors[options.key])) {
                this.fileSelectInstanceCounter++;
                $extend(options, {instanceId : this.fileSelectInstanceCounter});
                this.fileSelectors[options.key] = new ds_Plugin_FileSelect(options);
                return this.fileSelectors[options.key];
            } else {
                this.fileSelectors[options.key].initialize(options);
                return this.fileSelectors[options.key];
            }
            return false;
	},

	getRegionSelector: function(options) {
            if (!$defined(this.regionSelectors[options.key])) {
                this.regionSelectInstanceCounter++;
                $extend(options, {instanceId : this.regionSelectInstanceCounter});
                this.regionSelectors[options.key] = new ds_Plugin_RegionSelect(options);
                return this.regionSelectors[options.key];
            } else {
                this.regionSelectors[options.key].initialize(options);
                return this.regionSelectors[options.key];
            }
            return false;
	},

	getWizardByInstanceId: function(instance_id) {
            if (instance_id > 0) {
                $each(this.wizards, function (wizard) {
                    if (wizard.instance_id === instance_id){
                        return wizard;
                    }
                });
            }
            return false;
	},

	getDropdown: function(options) {
            var ddKey = options.key;

            if (!$defined(this.dropdowns[ddKey])) {
                this.dropdownInstanceCounter++;
                $extend(options, {zindex     : 100 - this.dropdownInstanceCounter,
                                  instanceId : this.dropdownInstanceCounter});
                this.dropdowns[ddKey] = new ds_Plugin_Dropdown(options);
            }
            return this.dropdowns[ddKey];
	},
	
	getForm: function(key) {
            if (!$defined(this.forms.key)) {
                this.formInstanceCounter++;
                this.forms.key = new ds_Plugin_Form({instanceId:this.formInstanceCounter});

                return this.forms.key;
            } else {
                return this.forms.key;
            }
            return false;
	},
	
	getAccordion: function(key) {
            if (!$defined(this.accordions.key)) {
                this.accordionInstanceCounter++;
                this.accordions.key = new ds_Plugin_Accordion({zindex:this.accordions.length + 101, instanceId:this.accordionInstanceCounter});

                return this.accordions.key;
            } else {
                return this.accordions.key;
            }
            return false;
	},
	
	getImageSelector: function(options) {
            if (!$defined(this.imageSelectors[options.key])) {
                this.imageSelectInstanceCounter++;
                $extend(options, {instanceId : this.imageSelectInstanceCounter});
                this.imageSelectors[options.key] = new ds_Plugin_ImageSelect(options);
                return this.imageSelectors[options.key];
            } else {
                this.imageSelectors[options.key].initialize(options);
                return this.imageSelectors[options.key];
            }
            return false;
	},
	
	getColorSelector: function(elmId) {
		var startColor = [58, 142, 246];
		if (!$(elmId).getParent().hasClass('colorpicker')) {
			$(elmId).getParent().addClass('colorpicker');
		}
		if ($(elmId).getProperty('type') == 'hidden') {
			$(elmId).setProperty('type', 'hidden');
		}
		$(elmId).getParent().setStyle('height', 135);
		$(elmId).setStyles({'width':'286px', 'height':'100px'});
		
		var oDiv = new Element('div').addClass('floatedDiv').injectInside($(elmId).getParent());
		if ($(elmId).getProperty('value') !== '') {
			startColor = $(elmId).getProperty('value').split(',');
			$(elmId).setStyle('background-color','rgb(' + $(elmId).getProperty('value') + ')');
			$(elmId).setStyle('color','rgb(' + $(elmId).getProperty('value') + ')');
		}
		this.colorSelectButtons[elmId] = new Element('a').setProperties({href:'javascript:void(0);', id: 'cpb_' + elmId}).addEvent('click', this.toggleAllColorpickers.bind(this)).setText('knop').injectInside(oDiv);
	
		this.colorSelectors[elmId] = new MooRainbow(this.colorSelectButtons[elmId], {
			id: 'moor_' + elmId,
			'startColor': startColor,
			'onChange': function(color) {
				$(elmId).value = color.rgb;
				$(elmId).setStyle('background-color', 'rgb(' + color.rgb + ')');
				$(elmId).setStyle('color', 'rgb(' + color.rgb + ')');
				$(elmId).fireEvent('change');
			},
			'onComplete': function(color) {
				$(elmId).value = color.rgb;
				$(elmId).setStyle('background-color', 'rgb(' + color.rgb + ')');
				$(elmId).setStyle('color', 'rgb(' + color.rgb + ')');
				$(elmId).fireEvent('change');
			}
		});
		
	},
	
	toggleAllColorpickers: function (e) {
		
		//var id = $(e.target).getProperty('id').replace(/[^0-9]/ig,'').toInt();
		
		this.colorSelectors.each(function(picker) {
			picker.hide();
		});
//		/this.colorSelectors[$(e.target).getProperty('id')].show();
	},
	
	showOverlay: function(sType) {
		
		// Create the transparent overlay
		var oOverlay = new Element('div').setProperty('id', 'transOverlay');
			
		oOverlay.addClass('loadingOverlay');
		
		var oText = new Element('p').setStyle('margin-top', (window.getHeight()/2 + 30)).setText('Een ogenblik geduld a.u.b.').inject(oOverlay);
		
		// Inject the overlay into the DOM
		oOverlay.inject(document.body);
	},
	
	hideOverlay: function() {
		if($defined($('transOverlay'))) {
			// Remove the overlay from the DOM
			$ES('#transOverlay').getLast().remove();
		}
	},
	
	showError: function (msg) {
		this.errorPopUp = this.getWizard('error');
		this.errorPopUp.setOptions({
			title: 'Systeemfout',
			width: 325,
			height: 200,
			message: msg
		});
		this.errorPopUp.addButton({icon: 'cancel', text: 'Sluiten', event: this.errorPopUp.closeWizard.bind(this.errorPopUp)});
		this.errorPopUp.showConfirm(msg);
	},
	
	getButton: function (buttonProperties) {
		if ($type(buttonProperties) == 'object') {
			if ($defined(buttonProperties.container)) {
				if ($(buttonProperties.container).getElements('a.button').length == 0) {
					if ($(buttonProperties.container).getStyle('height').toInt() < 50) {
						$(buttonProperties.container).setStyle('height', '50px');
					}
					$(buttonProperties.container).setStyle('display', 'block');
				}
				var buttonLink = new Element('a').addClass('button').inject(buttonProperties.container);
			}
			var buttonIcon = new Element('span').addClass('icon');
			if ($defined(buttonProperties.icon)) {
				buttonIcon.addClass(buttonProperties.icon);
			}
			buttonIcon.inject(buttonLink);
			if ($defined(buttonProperties.text)) {
				var buttonText = new Element('span').addClass('value').setText(buttonProperties.text).inject(buttonLink);
				buttonLink.setProperty('title', buttonProperties.text);
			}
			if ($defined(buttonProperties.event)) {
				//$(buttonLink).removeEvent('click');
				$(buttonLink).addEvent('click', buttonProperties.event);
				$(buttonLink).addEvent('dblclick', buttonProperties.event);
			}
			return buttonLink;
		} else {
			alert('Er is een fout opgetreden bij het opmaken van een button.');
		}
	},
	
	scrollToTop: function(element){
		var scroll = new Fx.Scroll(window, {
			 wait: false,
			 duration: 1000,
			 transition: Fx.Transitions.Quad.easeInOut
		 });
		 if ($defined($(element))) {
	       scroll.toElement($(element));
	   } else {
	       scroll.toTop();
	   }
	},
	toggleModuleFishEye: function() {
		if ($('moduleLinks').getStyle('display') == 'none' ) {
			$('moduleLinks').setStyle('display','block');
		} else {
			$('moduleLinks').setStyle('display','none');
		}
	},
	
	setRoundCorners: function () {
		$each($ES('.dashboard_item_header'), function (elm) {
    		var title = $(elm).getElement('h4');
    		var widthFix = $(elm).getNext().getStyle('width').toInt() + 13;
    		$(title).setStyle('width', widthFix);
   			
   			if (window.ie) {
   				//$(elm).getNext().setStyle('margin', '-8px 0 0 0');
   				$(elm).setStyle('margin', 0);
   				$(elm).setStyle('padding', '8px 0 0 8px');
   				if ($(elm).getParent().getParent().hasClass('infoCol')) {
   					$(elm).setStyle('height', 32);	
   				}
   			}
    	});
		$each($ES('.dashboard_item'), function (elm) {
			if (!$(elm).getParent().hasClass('formCol') &&
				!$(elm).getParent().hasClass('formInfoCol') &&
				!$(elm).getParent().hasClass('formFileTreeCol') &&
				!$(elm).getParent().hasClass('formColFileOverview') &&
				!$(elm).getParent().hasClass('formLoginCol')) {
					
				var roundCornerTop    = RUZEE.ShadedBorder.create({ corner:20, edges:"tlr" });
		  		roundCornerTop.render(elm.getElement('div.dashboard_item_header'));
			}
    	});
    	
    	
	}, 
	
	removeLoadingAnimation: function () {
		
		$each($ES('div.dashboard_item_content'), function (elm) {
			elm.setStyle('background','none');
		});
	},
	
	is_ie6: function () {
	     return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined));
	}
	
}); // end of class


ds_Base.implement(new Options);


