function objectClicked( e, object_id ) {
	if( typeof object_id == 'string' ) object_id = [object_id];
	var coord_x = 0, coord_y = 0;
	if ( typeof e != "undefined" ) {
		coord_x = e.pageX;
		coord_y = e.pageY;
	} else if (typeof window.event != "undefined"){
		coord_x = $('body').scrollLeft() + window.event.clientX;
		coord_y = $('body').scrollTop() + window.event.clientY;
	}
	var click = document.createElement('DIV');
	click.id = "document-click";
	click.style.height = '3px';
	click.style.width = '3px';
	click.style.position = 'absolute';
	var ctop = coord_y-1;
	click.style.top = ctop+'px';
	var cleft = coord_x-1;
	click.style.left = cleft+'px';
	click.style.backgroundColor = '#000000';
	//$('body').append(click);
	
	var clicked = false;
	for( var i in object_id ) {
		if( $('#'+object_id[i]).length ) {
			var position = $('#'+object_id[i]).position();
			var top = position.top;
			var left = position.left;
			var width 	= $('#'+object_id[i]).outerWidth();
			var height 	= $('#'+object_id[i]).outerHeight();
			if ( coord_x > left && coord_x < (left+width) && coord_y > top && coord_y < (top+height) ) clicked = true;;
		}
	}
	return clicked;
}

function optionSelect( value_to_select, select_tag ) {
	var opt = select_tag.options;
	for ( var index = 0; index < opt.length; index++ ) {
		if ( opt[index].value != null && opt[index].value == value_to_select ) {
			opt.selectedIndex = index;
			return true;
		}
	}
	return false;
}

function in_array( needle, haystack ) {
	for ( var x in haystack )
		if ( haystack[x] == needle ) return true;
	return false;
}

function fullIter( obj, arr ) {
	for ( i in obj ) {
		if ( typeof obj[i] == 'string' || obj[i] == 'number' ) {
			arr.push( i + ' = ' + obj[i]);
		} else if ( typeof obj[i] == 'object' ) {
			fullIter( obj[i], arr );
		}
	}
	return arr;
}

function unset( key, arr ) {
	var temp = [];
	for( var i in arr ) {
		if( i != key ) temp[i] = arr[i];
	}
	return temp;
}

var DDS = function() {
	return {
		website: 'zeisch.ch', 
		strtotime: function( time_string ) {
			var day = time_string.substring(0, 2);
			var month = time_string.substring(3, 5);
			var year = time_string.substring(6, 10);
			day = parseInt(day, 10);
			month = parseInt(month, 10) -1;
			year = parseInt(year, 10);
			thetime = new Date(year, month, day, 0, 0, 0);
			return thetime; 
		}, 
		DropDown: function( id, data, callback ) {
			var t = this;
			this.id = id;
			this.callback = this.alert;
			
			if( typeof callback == "function" ) this.callback = callback;
			
			this.alert = function( data ) {
				alert(data);
			}
			
			this.init = function() {
				if( DDS.DropDownManager.active(this.id) ) return;
				DDS.DropDownManager.add(this.id);
				
			}
			
			this.up = function() {
			
			}
			
			this.down = function() {
			
			}
			
			this.focus = function() {
			
			}
			
			this.blur = function() {
			
			}
			
			this.select = function() {
			
			}
			
			
			
			
		
		}, 
		DropDownManager: new function() {
			var t = this;
			this.arr_init = [];
			
			this.active = function( check ) {
				return in_array(check, this.arr_init)
			}
			
			this.add = function( id ) {
				this.arr_init.push(id);
			}
		}(),
		edit: function( module, id, name, func ) {
			var position = $('#dds_edit_div_'+module+'_'+id).offset();
			var height = $('#dds_edit_div_'+module+'_'+id).outerHeight();
			var width = $('#dds_edit_div_'+module+'_'+id).outerWidth();
			var top = position.top - 4;
			var left = position.left -4;
			
			
			
			
			var div = document.createElement("DIV");
			div.id = '#dds_edit_con_'+module+'_'+id;
			div.className = '_dds_edit';
			div.style.height = height+"px";
			div.style.width = width+"px";
			div.style.position = "absolute";
			div.style.top = top+"px";
			div.style.left = left+"px";
			div.style.border = "4px solid #AA1919";
			div.style.zIndex = "1";
			div.style["opacity"] = "0.8";
			div.style["filter"] = "filter:Alpha(opacity=80)";
			
			var div2 = document.createElement("DIV");
			div2.id = '#dds_edit_opt_'+module+'_'+id;
			div2.className = '_dds_edit';
			div2.style.height = "16px";
			div2.style.width = width+"px";
			div2.style.position = "absolute";
			div2.style.top = (4+top)+"px";
			div2.style.left = (4+left)+"px";
			div2.style.zIndex = "3";
			div2.style.color = "#FFFFFF";
			div2.style.fontSize = "12px";
			div2.style.fontWeight = "normal";
			div2.style.lineHeight = "12px";
			div2.style.textAlign = "right";
			div2.style.backgroundColor = "#AA1919";
			div2.style["opacity"] = "0.5";
			div2.style["filter"] = "filter:Alpha(opacity=50)";
			div2.innerHTML = "Doppelklick zum Bearbeiten.&nbsp;";
			
			
			
			
			$('#dds_edit_div_'+module+'_'+id).mouseenter(function(){DDS.editOver(module, id, name, func);$(div).stop(true, true).fadeIn("fast");$(div2).stop(true, true).fadeIn("fast");}).mouseleave(function(){$(div).stop(true, true).fadeOut("fast");$(div2).stop(true, true).fadeOut("fast");}).dblclick(function(){func();}).css({position:'relative',zIndex:'2'}).after(div).after(div2);
			$(div).fadeOut();
			$(div2).fadeOut().mouseenter(function(){$(div).stop(true).show();$(div2).stop(true).show();}).dblclick(function(){$(div).stop(true).hide();$(div2).stop(true).hide();func();});
		},
		clearEdit: function(module, id) {
			$('#dds_edit_con_'+module+'_'+id).remove();
			$('#dds_edit_opt_'+module+'_'+id).remove();
			$('._dds_edit').remove();
		}, 
		EditHighlight: new function() {
			var t = this;
			this._arr = [];
			
			this.load = function( module, id, name, func ) {
				this._arr[module+"_"+id] = new DDS.EditHighlightElement( module, id, name, func );
			}
			
		}(),
		EditHighlightElement: function( module, id, name, func ) {
			var t = this;
			
			this._module = module;
			this._id = id;
			this._name = name;
			this._func = func;

			
			this._tl;
			this._tm;
			this._tr;
			this._ml;
			this._mr;
			this._bl;
			this._bm;
			this._br;
			this._tx;
			
			this.init = function( module, id, name, func ) {
				this._tl = document.createElement("DIV");
				this._tm = document.createElement("DIV");
				this._tr = document.createElement("DIV");
				
				this._ml = document.createElement("DIV");
				this._mr = document.createElement("DIV");
				
				this._bl = document.createElement("DIV");
				this._bm = document.createElement("DIV");
				this._br = document.createElement("DIV");
				
				this._tx = document.createElement("DIV");
				$(this._tx).css({height:'16px',width:'0px',color:'#FFFFFF',fontSize:"12px",fontWeight:'normal',lineHeight:'12px',textAlign:'right'}).html("Doppelklick zum Bearbeiten.&nbsp;");
				this._tx.style["opacity"] = "0.5";
				this._tx.style["filter"] = "filter:Alpha(opacity=50)";
				
				$(this.arrEle()).css({backgroundColor:'#AA1919', position:'absolute',top:'-9999px',left:'-9999px'});
				$('body').append(this._tl).append(this._tm).append(this._tr).append(this._ml).append(this._mr).append(this._bl).append(this._bm).append(this._br).append(this._tx);
				$('#dds_edit_div_'+this._module+'_'+this._id).mouseenter(function(){t.over();}).mouseleave(function(){t.out();}).dblclick(function(){t._func();});
			}
			
			this.arrEle = function() {
				return [this._tl, this._tm, this._tr, this._ml, this._mr, this._bl, this._bm, this._br, this._tx];
			}
			
			this.reposition = function() {
				var position 	= $('#dds_edit_div_'+this._module+'_'+this._id).offset();
				var top 		= position.top;
				var left 		= position.left;
				var height 		= $('#dds_edit_div_'+this._module+'_'+this._id).outerHeight();
				var width 		= $('#dds_edit_div_'+this._module+'_'+this._id).outerWidth();
				
				$([this._tl, this._tm, this._tr, this._bl, this._bm, this._br]).css({height:'4px'});
				$([this._ml, this._mr]).css({height:(height+16)+'px'});
				
				$([this._tl, this._tr, this._ml, this._mr, this._bl, this._br]).css({width:'4px'});
				$([this._tm, this._bm, this._tx]).css({width:width+'px'});
				
				$([this._tl, this._tm, this._tr]).css({top:(top-4-16)+'px'});
				$([this._ml, this._mr, this._tx]).css({top:(top-16)+'px'});
				$([this._bl, this._bm, this._br]).css({top:(top+height)+'px'});
				
				$([this._tl, this._ml, this._bl]).css({left:(left-4)+'px'});
				$([this._tm, this._bm, this._tx]).css({left:left+'px'});
				$([this._tr, this._mr, this._br]).css({left:(left+width)+'px'});
			}
			
			this.over = function() {
				this.reposition(module, id);
				$(this.arrEle()).stop(true, true).fadeIn("fast");
			}
			
			this.out = function( module, id, name, func ) {
				$(this.arrEle()).stop(true, true).fadeOut("fast");
			}
			
			this.init();
		},
		Module: function() {
			return {
				ImagePosition: new function() {
					var t = this;
					this._ao = []; // is it open or closed...
					this.json;
					
					this.refresh = function( id ) {
						$.post("ajax.php", {c:"ImagePosition",m:"refresh",id:id}, function(html){t.write(id, html);});
					}
					
					this.write = function( id, html ) {
						$("#dds_edit_div_ImagePosition_"+id).html(html);
						this._ao[id] = 'c';
						DDS.edit('ImagePosition', id, id, function(){t.load(id, t.json)});
					}
					
					this.load = function(id) {
						if( this._ao[id] && this._ao[id] == 'o' ) return;
						else this._ao[id] = 'o';
						
						var json = arguments[1];
						this.json = json;
						var cat = typeof json.options.category != 'undefined' ? json.options.category : 'image';
						
						
						//var cat = (arguments.length > 1) ? arguments[1] : 'image';
						var width = $("#dds_edit_div_ImagePosition_"+id).width();
						var html = "<input type=\"text\" id=\"module_image_change_"+id+"\" name=\"image\" /><span style=\"font-size: 11px;\">Breite: </span><input type=\"text\" name=\"image_width\" id=\"module_image_change_"+id+"_width\" style=\"width: 35px;\" value=\""+width+"\" class=\"input\" /><button onclick=\"DDS.Module.ImagePosition.save('"+id+"');\">Neues Bild speichern</button><hr /><div style=\"clear:both;\"></div>";
						//var html = "<input type=\"text\" id=\"module_image_change_"+id+"\" name=\"image\" /><span style=\"font-size: 11px;\">Breite: </span><input type=\"text\" name=\"image_width\" id=\"module_image_change_"+id+"_width\" style=\"width: 35px;\" value=\""+width+"\" class=\"input\" /><button onclick=\"DDS.Module.ImagePosition.save('"+id+"');\">Neues Bild speichern</button><hr /><div style=\"clear:both;\"></div>"+$("#dds_edit_div_ImagePosition_"+id).html();
						$.facebox(html);
						//$("#dds_edit_div_ImagePosition_"+id).html(html);
						var ui = new CUploadImage("module_image_change_"+id, cat);
						
						DDS.clearEdit('ImagePosition', id);
					}
					
					this.save = function(id) {
						var new_id = $("#module_image_change_"+id).val();
						var width = $("#module_image_change_"+id+"_width").val();
						$.post("ajax.php", {c:"ImagePosition",m:"change",id:id,nid:new_id,width:width}, function(html){t.write(id, html);});
					}
				}(), 
				WYSIWYG: new function() {
					var thisO = this;
					this._arr_init = [];
					this._active;
					
					this.load = function( id ) {
						if( this._active != null && this._active != id  ) alert('Es kann nur ein Bereich auf einmal bearbeitet werden.\nDu musst entweder das Bearbeiten abbrechen oder speichern \num diesen Bereich bearbeiten zu koennen.');
						else if( !in_array(id, this._arr_init) ) this.start(id);
						else this.toggle(id);
					}
					
					this.start = function(id) {
						this._arr_init.push(id);
						this._active = id;
						tinyMCE.init({
							content_css : "/template/"+DDS.template+"/style.css", 
							force_br_newlines : true,
					        forced_root_block : '',
							mode : "exact",
							elements : "dds_edit_div_WYSIWYG_"+id, 
							theme : "advanced", 
							theme_advanced_buttons1: "dds_save_button,dds_save_n_close_button,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontsizeselect", 
							theme_advanced_buttons2: "bullist,numlist,|,indent,outdent,|,undo,redo,|,link,unlink,anchor,|,image,uploadImageButton,uploadDocumentButton,|,cleanup,help,code,|,pasteword", 
							theme_advanced_buttons3: "hr,removeformat,visualaid,|,sub,sup,|,tablecontrols", 
							theme_advanced_toolbar_location : "top",
							theme_advanced_toolbar_align : "left",
							theme_advanced_statusbar_location : "bottom",
							theme_advanced_resizing : true,
							plugins : "inlinepopups,table,advlink,uploadimage,uploaddocument,paste,ddssave"
						});
					}
					
					this.toggle = function( id ) {
						if (!tinyMCE.get("dds_edit_div_WYSIWYG_"+id)) { tinyMCE.execCommand('mceAddControl', false, "dds_edit_div_WYSIWYG_"+id); this._active = id; }
						else { tinyMCE.execCommand('mceRemoveControl', false, "dds_edit_div_WYSIWYG_"+id); this._active = null; }
					}
					
					this.save = function( id ) {
						var html = tinyMCE.get("dds_edit_div_WYSIWYG_"+id).getContent()
						$.post('ajax.php', {c:'WYSIWYG',m:'save',id:id, html:html}, function() {alert('Gespeichert')});
					}
					
					this.init = function( id )  {
						
					}
				}(), 
				DocumentGallery: new function() {
					var thisO = this;
					this.toggle = function(id) {
						$("#document_gallery_"+id).toggle();
					}
				}(), 
				ImageGallery: new function() {
					var thisO = this;
				}()
			}
		}(),
		Edit: new function() {
			return {
				List: new function() {
					var t = this;
					this.add = function( json ) {
						alert(json);
					}
					this.load = function() {
						
					}
				}(),
				multiSelectSend: function(json) {
					var c = json.subclass;
					if( window.opener.document.getElementById("subclass_"+c.class_id) ) {
						parent.DDS.Module.Edit.singleSelectReceive(json);
					} else alert("This class ("+c.class_id+") is not part of the class you are creating.");
					window.opener.focus();
					window.close();
				},
				multiSelectReceive: function( json ) {
					var c = json.subclass;
					if(!document.getElementById("subclass_"+c.class_id)) return;
					$("#subclass_"+c.class_id+"_input").val(c.primary);
					var a = [], h = "<div id=\"subclass_"+c.class_id+"_div\"><div class=\"text\"><img src=\"intern/graphic/icon/ui_menu_minus.gif\" alt=\"Remove\" title=\"Remove\" align=\"absmiddle\" style=\"cursor: pointer;\" onclick=\"document.getElementById('subclass_"+c.class_id+"_div').innerHTML='';\"/>&nbsp;";
					for( var i in c.identifier ) a.push(c.identifier[i]+": "+c.fields[c.identifier[i]]);
					window.opener.document.getElementById("subclass_"+c.class_id).innerHTML = h+a.join(", ")+"</div></div>";;
				},
				muliSelectRemove: function( class_id, primary ) {
					
				},
				close: function(  ) {
					window.close();
					if( !window.opener ) return;
					if( arguments.length && arguments[0] ) window.opener.location.reload();
					window.opener.focus();
				}, 
				Object: function( json ) {
					//var uri = typeof window.document.baseURI == 'undefined' ? '' : window.document.baseURI;
					var url = DDS.url+'edit.php?req='+encodeURIComponent(json.edit_file)+"&m="+encodeURIComponent(json.method)+"&p="+encodeURIComponent(json.request)+"&primary="+encodeURIComponent(json.primary);
					for( var i in json.add ) {
						url += "&"+i+"="+encodeURIComponent(json.add[i]);
					}
					for( var i in json.where ) {
						url += "&where[]="+encodeURIComponent(json.where[i]);
					}
					newwindow=window.open(url, 'window_'+json.edit_file+"_"+json.method, 'width=1000,height=800,locationbar=yes, scrollbars=yes, status=no');
					newwindow.opener = self;
					if (window.focus) {newwindow.focus()}
				},
				SingleSelect: function() {
					var json = DDS.single_select_json;
					var text = "";
					var temp = [];
					for( var i in json.identify ) temp.push(json.identify[i]);
					if( !temp.length ) text = json.primary;
					else text = temp.join(",");
					if( window.opener.document.getElementById("subclass_"+json.class_id) ) {
						window.opener.document.getElementById("subclass_"+json.class_id).innerHTML = text;
						window.opener.document.getElementById(json.field).value = json.value;
					} else alert("This class is not part of the class you are creating.");
					DDS.Edit.close();
				}, 
				editSingleSelectRemove: function() {
					
				}
			}
		},
		Quicksearch: function( str ) {
			var reg, reg1 = /(\*|\+|\?|\.|\(|\)|\[|\]|\{|\}|\\|\/|\||\^|\$)/, reg2 = /^(.){2,}$/;
			var tag = 'div';
			if( arguments.length == 2 ) tag = arguments[1];
			if ( !reg1.test(str) && reg2.test(str) ) {
				reg = eval('/' + str + '/i');
				$(tag+'[name*=quicksearch]').each(
					function() {
						if( this.id.search(reg) != -1 ) {
							this.style.display = "";
						} else {
							this.style.display = "none";
						}
					}
				);
			} else {
				$(tag+'[name*=quicksearch]').each(
					function() {
						this.style.display = "";
					}
				);
			}
		}, 
		Upload: function() {
			return {

			}
		}()
	};
}();