function objectClicked( e, object_id ) {
	if( typeof object_id == 'string' ) {
		var temp = new Array(); temp.push(object_id); object_id = temp;
	}
	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;
}

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; 
		}, 
		edit: new function() {
			var thisO = this;
			this._over;
			this._timeout;
			
			this.over = function( elem ) {
				window.clearTimeout(thisO._timeout);
				if( this._over != elem ) this.hide(this._over);
				else if(this._over == elem) return;
				$(elem).find(".dds_edit_button").hide().end().find(".dds_edit_link").show();
				this._over = elem;
			}
			
			this.out = function( elem ) {
				thisO._timeout = window.setTimeout(function(){thisO.hide(elem)}, 400);
			}
			
			this.hide = function( elem ) {
				$(elem).find(".dds_edit_button").show().end().find(".dds_edit_link").hide();
				this._over = null;
			}
		}(), 
		Module: function() {
			return {
				Image: new function() {
					var thisO = this;
					this._ao = [];
					
					this.refresh = function( id ) {
						$.post("ajax.php", {c:"ImagePosition",m:"refresh",id:id}, function(html){thisO.write(id, html);});
					}
					
					this.write = function( id, html ) {
						$("#image_"+id).html(html);
						this._ao[id] = 'c';
					}
					
					this.change = function(id) {
						if( this._ao[id] && this._ao[id] == 'o' ) return;
						else this._ao[id] = 'o';
						var cat = (arguments.length > 1) ? arguments[1] : 'image';
						var width = $("#image_"+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.Image.save('"+id+"');\">Neues Bild speichern</button><hr /><div style=\"clear:both;\"></div>"+$("#image_"+id).html();
						$("#image_"+id).html(html);
						var ui = new CUploadImage("module_image_change_"+id, cat);
					}
					
					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){thisO.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 : "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("wysiwyg_"+id)) { tinyMCE.execCommand('mceAddControl', false, "wysiwyg_"+id); this._active = id; }
						else { tinyMCE.execCommand('mceRemoveControl', false, "wysiwyg_"+id); this._active = null; }
					}
					
					this.save = function( id ) {
						var html = tinyMCE.get("wysiwyg_"+id).getContent()
						$.post('ajax.php', {c:'WYSIWYG',m:'save',id:id, html:html}, function() {alert('Gespeichert')});
					}
				}(), 
				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 = uri+'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]);
					}
					newwindow=window.open(url, 'window_'+json.edit_file+"_"+json.method, 'width=1200,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;
						alert("text: "+text+"\nval: "+json.value+"\nfield: "+json.field);
					} 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 {

			}
		}()
	};
}();
