/************************************************************/
/*** 
/************************************************************/


function getElement(id)
{
	if(document.getElementById) {return document.getElementById(id);}
	else if(document.all){ return document.all[id];}
	return false;

}

var js_tools =
{
	ajaxObj:function()
	{/***Creates and returns an ajax object ***/
		var obj;
		if(window.XMLHttpRequest)
		{
			obj = new XMLHttpRequest();
		}
		else//IE6
		{
			obj = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		return obj;
	},
	
	/*************************************************************************/
	/***Wrapper-function for making ajax-requests,
	/***takes an object as input,
	/***only obligatory member is object.url. a calback can be defined as 
	/***object.callback, if you want to make a post-request object.post
	/***must be true, and key-value pairs in object.data will be sent as
	/***post-data (object.data will be sent on get-requests too),
	/***turn off error-handling with object.error_message=false;
	/***continue on error with halt_on_error=false
	/*************************************************************************/
	make_ajax_request:function(object)
	{
		
		if(!object.url) return false;//return false if no url is defined
		if(!object.data)
		{
			object.data={}
		}
		else
		{
			
			for(var key in object.data)
			{
				object.data[key]=object.data[key].toString().replace(/€/gi,'&#8364;');
			}
		}

		var async=false;
		if(!object.sync)
		{
			async=true;
		}
		//set request-type
		var request_type='POST';
		if(!object.post)
		{
			request_type='GET';
		}
		/***********************************************************/
		/***if no callback defined, user our own callback
		/***********************************************************/
		if(!object.callback)
		{

			return jQuery.ajax({
				url:object.url,
				data:object.data,
				type:request_type,
				async:async,
				success:function(response)
				{
					if(object.error_message!==false)//error-reporting can be turned off with object.error_message=false
					{
						var error=js_tools.request_success(response);
						if(error!==false)
						{
							alert(error.response);//else alert out the error-message
						}//endif
					}//endif
				},
				cache:false,
				error:function()
				{
					alert('connection error!')
				}
			});//end of ajax-request
			
		}//endif
		/***********************************************************/
		/***if a callback is defined
		/***********************************************************/
		else//if a callback is defined
		{
		
			return jQuery.ajax({
				url:object.url,
				data:object.data,
				type:request_type,
				async:async,
				success:function(response)
				{
					var error=false;
					if(object.error_message!==false)//error-reporting can be turned off with object.error_message=false
					{
						error=js_tools.request_success(response);
						if(error!==false)
						{
							if(error.response=='not_logged_in') 
							{
								window.location.href=idag_admin.ROOT + "admin/login.html";
								return false;
							}

							alert(error.response);//else alert out the error-message
							if(object.halt_on_error!==false && error.code==false) return false;
						}//endif
					}//endif
					object.callback(response,error);//call the callback
				},
				cache:false,
				error:function()
				{
					alert('connection error!')
				}
			});//end of ajax-request
			
		}
	},//end of function
	/*************************************************************/
	
	unescapeHTML:function(html)
	{
		return html.split("&amp;").join("&").split("&lt;").join("<").split("&gt;").join(">");
	},

	/*************************************************************************************/
	URLarguments:function(url)
	{//returns true if the given url has arguments (ie  eg. somesite.something?=arg1=1
		exp=/\?/g;
		if(url.search(exp)<0) return false;
		return true;
	},

	/***********************************************/
	BBparse:function(instring,tag)//parses strings with bb-style tags returning an array of strings containing 
	{			//everything  between [tag] and [/tag]
		exp= new RegExp("\\[" + tag + "\\](.+?)\\[\\/" + tag + "\\]","g");

		regArr=instring.match(exp);
		if(regArr==null) return false;
		for(var i=0;i<regArr.length;i++)
		{
			exp= new RegExp("\\[" + tag + "\\]","g");
			
			temp=regArr[i].replace(exp,"");
			regArr[i]=temp;
			exp= new RegExp("\\[\\/" + tag + "\\]","g");
			temp=regArr[i].replace(exp,"");
			regArr[i]=temp;
			
			
		}//end of for
		return regArr;

	},//end of parseInput
	/********************************************/
	toggle_visible:function(id)
	{
	/**********************************************************************************************/
	/***Toggle visibility of an object, ideal for making certain parts of the page 
	/***slideable on click, by changing this function effects could be added later on  
	/**********************************************************************************************/
		var element=document.getElementById(id);
		if(element.style.visibility=='visible')
		{
			element.style.visibility='hidden';
			element.style.display='none';
			return 0;
		}
		element.style.visibility='visible';
		element.style.display='block';
	},
	/************************************************************/
	/*Remove DOM-element
	/************************************************************/
	remove_element:function(element)
	{
		element.parentNode.removeChild(element);
	},
	/************************************************************/
	/***Canvas and scroll info functions
	/************************************************************/
	getCanvasWidth:function()
	{
		de=document.documentElement;
		if(!de)
		{
			de=Object();
			de.clientWidth=false;
		}
		return window.innerWidth || self.innerWidth || de.clientWidth || document.body.clientWidth;
	},
	/*******************************************************************************/

	getCanvasHeight:function()
	{
		de=document.documentElement;
		if(!de)
		{
			de=Object();
			de.clientHeight=false;
		}
		return window.innerHeight || self.innerHeight || de.clientHeight || document.body.clientHeight;
	},

	/*******************************************************************************/

	getScrollHeight:function()
	{
		de=document.documentElement;
		if(!de)
		{
			de=Object();
			de.scrollTop=false;
		}

		if(!de.scrollTop && !document.body.scrollTop && !window.pageYOffset) return false;
		return de.scrollTop || document.body.scrollTop || window.pageYOffset;
	},
	/*******************************************************************************/

	getScrollWidth:function()
	{
		de=document.documentElement;
		if(!de)
		{
			de=Object();
			de.scrollLeft=false;
		}
		if(!de.scrollLeft && !document.body.scrollLeft && !window.pageXOffset) 
		{
			return false;
		}
		return de.scrollLeft || document.body.scrollLeft || window.pageXOffset;
	},
	/*******************************************************************************/
	request_success:function(response)
	{
		var code=response.match(/^[a-z]+;/i);
		if(code==null) return false;
		var returnval=new Object();
		returnval.response=response.replace(/^[a-z]+;/i,'');
		code=code[0].split(';').join('');
		switch(code)
		{
			case 'true': 
				returnval.code=true;
			break;
			case 'false':
				returnval.code=false;
			break;
			default:
				returnval.code=code[0];
			break;
		}//end of switch
		

		return returnval;
	},
	/**************************************************************************************/
	extract_html:function(htmlString)
	{/***Separates html-tags from text and returns both in an array ***/
		var rp=new RegExp("<[^<>]+>","g");
		var returnObject=new Object();
		returnObject.tags=htmlString.match(rp);
		returnObject.stringArray=htmlString.split(rp);
		return returnObject;
	},
	strip_html:function(content)
	{
		//Not to be used if you need to preserve text as it replaces unknown entities with spaces (to be able to get a real account for number of characters
		var rp=new RegExp("<[^<>]+>","g");
		content=content.replace(rp,'');
		//FIXME:
		//need better support for html-entities
			
		content=content.split('&nbsp;').join(' ');
		content=content.split('&amp;').join('&');
		content=content.split('&gt;').join('>');
		content=content.split("<br>").join('\n');
		content=content.split('&lt;').join('<');
		content=content.split('&copy;').join('©'); 
		content=content.split('&trade;').join('™');
		content=content.split('&auml;').join('ä');
		content=content.split('&Auml;').join('Ä');
		content=content.split('&ouml;').join('ö');
		content=content.split('&Ouml;').join('Ö');
		content=content.split('&aring;').join('å');
		content=content.split('&Aring;').join('Å');
		content=content.replace(/&#[a-zA-Z0-9]*;/g,' ');
		content=content.split("\n").join("").split("\r").join("").split("\r\n").join("");
		return content;
	},//end of strip_html
	/********************************************************************************************/
	make_ajax_clean:function(in_string)
	{
		in_string=in_string.toString();
		return encodeURIComponent(in_string.replace(/€/gi,'&#8364;'));
	},
	/********************************************************************************************/
	getUniqueElementId:function()
	{
		var id=Math.random();
		while(getElement(id) != undefined)
		{
			id=Math.random();
		}
		return id;
	},
	/********************************************************************************************/
	openUrl:function(url)
	{
		window.location=decodeURIComponent(url);
	},
	/*******************************************************************************************/
	addEvent: function(obj,type,func)
	{
		if(obj.attachEvent)
		{
			obj.attachEvent('on' + type,func);
		}
		else obj.addEventListener(type,func,false);
		
	},
	
	
	/******************************************************************************************/
	/***                           T E M P L A T E  
	/******************************************************************************************/
	/*** Very simple js templating engine, templates consist of normal html , with      
	/*** values to be replaced between '|' characters   eg "<b>|bold_value|</b>"          
	/******************************************************************************************/
	template:function()
	{
		this.original="";
		this.manipulated="";
		this.loaded=false;
		this.fromUrl=function(_url)
		{/*loads  the template from an external source*/
			var response=jQuery.ajax({url:_url,async:false}).responseText;
			var error=error_request(response);
			if(error!==false) 
			{
				alert(error.response);
				this.loaded=false;
				return false;
			}
			this.original=response;
			this.manipulated=response;
		}
		/***********/
		this.data=function(html)
		{/*The data is given as a html-string*/
			
			this.original=html;
			this.manipulated=html;
		}
		/************/
		this.set=function(search,replace)//Very simple template function
		{
			exp= new RegExp("\\|" + search + "\\|","g");
			this.manipulated = this.manipulated.replace(exp,replace);
		}
		/************/
		this.getResult=function()
		{//Returns the parsed string
			return this.manipulated;
		}
		/************/
		this.rollBack=function()
		{//Rolls back the changes
			this.manipulated=this.original;
		}
	},//template

	
	/**********************************************************************************************************/
	/*This clas turns a normal unorderd list into a drag and drop sortable equalient
	/**********************************************************************************************************/
	list:function(element_id,drag_drop)
	{
		var list_object=this;
		this.element=getElement(element_id);
		this.moving_item=false;
		if(!this.element) return false;

		if(typeof drag_drop != "undefined" && drag_drop==false)
		{
			this.drag_drop=false;
		}
		else
		{
			this.drag_drop=true;
		}
		

		//This functions adds the nescessary functionality to the li-item , to make it 
		//moveable
		this.make_item_sortable=function(item)
		{

			/**************************************************************/
			/***Element information - functions
			/**************************************************************/
			item.get_x_pos=function()
			{
				return jQuery(this).offset().left;
			}
			item.get_y_pos=function()
			{
				return jQuery(this).offset().top;
			}
			item.get_width=function()
			{
				return jQuery(this).width();
			}
			item.get_height=function()
			{
				return jQuery(this).height();
			}
			
			item.get_list_object=function()
			{

				var o=new Object();
				o.value=this.getAttribute('name');
				o.element=this;
				o.last_id=false;

				
				//remove 
				o.remove=function()
				{
					js_tools.remove_element(item);
					list_object.onchange();
				}


				o.start_timer = function()
				{
					var that=this;
					this.time_run=0;
					
					function update_timer()
					{
						if(that.time_run!==false)
						{
							that.time_run +=50;
							setTimeout(function(){update_timer()},50);
						}
					}
					
					setTimeout(function(){
						update_timer();
					},50);
				}
				o.start_drag=function(e)
				{
					if(window._JS_TOOLS_LIST_MOVING) return false;
					
					window._JS_TOOLS_LIST_MOVING=true;
					var that=this;
					
					list_object.moving_item=this;
					this.start_timer();
					/******************************************/
					/***Set up mouse-coordinates
					/******************************************/
					this.mouse_x=e.clientX + js_tools.getScrollWidth();
					this.mouse_y=e.clientY + js_tools.getScrollHeight();
					/******************************************/
					/***Get element position
					/******************************************/
					var pos_top=jQuery(this.element).offset().top;
					var pos_left=jQuery(this.element).offset().left;
					
					var dif_x=pos_left - this.mouse_x;
					var dif_y=pos_top - this.mouse_y;

					var new_element=document.createElement('div');
					new_element.className=this.element.className;

					new_element.style.width=this.element.style.width;

					new_element.innerHTML=this.element.innerHTML;
					new_element.style.position="absolute";
					new_element.style.top=pos_top + "px";
					new_element.style.left=pos_left + "px";
					this.element.style.visibility="hidden";
					//new_element.style.opacity='0.5';
					list_object.element.appendChild(new_element);
					document.body.style.cursor='move';//change cursor-style
					document.onselectstart=function(){return false;}//disable events that disturbe our drag-functionality
					document.ondragstart=function(){return false;}//...
					
					document.onmousemove=function(e)
					{//when the module is moved
						e=e || window.event;
						e.cancelBuble=true;
						document.body.focus(); //prevent text-selection (very annoying when you try to move something)
						
						if(e.preventDefault) e.preventDefault();
						
						/***************************************/
						/***Update mouse-position
						/***************************************/
						that.mouse_x=e.clientX + js_tools.getScrollWidth();//mouse-polling, that takes scrolling into account
						that.mouse_y=e.clientY + js_tools.getScrollHeight();

						pos_top=that.mouse_y + dif_y;
						pos_left=that.mouse_x + dif_x;
						var pos_right=pos_left + jQuery(new_element).width();
						var pos_bottom= pos_top + jQuery(new_element).height();
						var mid_x=that.mouse_x;
						var mid_y=that.mouse_y;
						
						new_element.style.top = pos_top + 'px';
						new_element.style.left = pos_left + 'px';
						

						
						var mod_x=0;
						var mod_y=0;
						var mod_w=0;
						var mod_h=0;

						if(that.time_run>100)
						{
							var next_item=that.element.nextSibling;
							var previous_item=that.element.previousSibling;
							while(next_item!=null || previous_item!=null)
							{
								if(next_item!=null)
								{
									if(next_item.get_list_object)
									{
										mod_x=next_item.get_x_pos();
										mod_y=next_item.get_y_pos();
										mod_w=next_item.get_width();
										mod_h=next_item.get_height();
										if(mod_x+mod_w>mid_x && mid_x>mod_x && (mod_y + mod_h)>mid_y && mid_y>mod_y)//check if we've got a match
										{
											that.element.parentNode.insertBefore(that.element,next_item.nextSibling);
											list_object.onchange();
											return;
										}
									}
									next_item=next_item.nextSibling;
								}
								if(previous_item!=null)
								{
									if(previous_item.get_list_object)
									{
										mod_x=previous_item.get_x_pos();
										mod_y=previous_item.get_y_pos();
										mod_w=previous_item.get_width();
										mod_h=previous_item.get_height();
										if(mod_x+mod_w>mid_x && mid_x>mod_x && (mod_y + mod_h)>mid_y && mid_y>mod_y)//check if we've got a match
										{
											that.element.parentNode.insertBefore(that.element,previous_item);
											list_object.onchange();
											return;
										}
									}
									previous_item=previous_item.previousSibling;
								}
							}

							that.time_run=0;
						}
					}


					document.body.onmouseup=function()
					{//when the module is dropped
						window._JS_TOOLS_LIST_MOVING=false;
						document.onmousemove=null;//stop moving
						document.body.style.cursor='default';
						that.element.style.visibility="visible";

						try
						{
							list_object.element.removeChild(new_element);//remove the drag-element
						}
						catch(e)
						{
						}

					}
				}
				
				
				return o;
			}//end of get_list_object

			if(this.drag_drop!=false)
			{
				item.onmousedown=function(e)
				{
					e=e || window.event;
				
					document.body.focus();
					if(e.preventDefault) e.preventDefault();
					this.get_list_object().start_drag(e);
				}
			}

			
		}//end of make sortable

		var items=this.element.getElementsByTagName('li');
		for(var i=0;i<items.length;i++)
		{
			//make sortable
			this.make_item_sortable(items[i]);
		}


		this.get_item=function(n)
		{
			var items=this.element.getElementsByTagName('li');
			if(typeof items[n] != "undefined" && items[n].get_list_object !=null)
			{
				
				return items[n].get_list_object();
			}
		}
		
		this.add_item=function(obj)
		{
			if(!obj || !obj.key)
			{
				return false;
			}
			
			var new_li=document.createElement('li');
			if(!obj.value)
			{
				if(!obj.element)
				{
					return false;
				}
				else
				{
					new_li.appendChild(obj.element);
				}
			}
			else
			{
				new_li.innerHTML=obj.value;
			}
		
			new_li.setAttribute('name',obj.key);
			this.element.appendChild(new_li);
			this.make_item_sortable(new_li);
			this.onchange();
		}

		this.remove_container=function(node)
		{
			if(node==document.body || !node) return false;
			if(node.get_list_object)
			{
				node.get_list_object().remove();
				return;
			}
			else
			{
				this.remove_container(node.parentNode);
			}
			
			
		}
		this.get_items=function()
		{
			var return_array=new Array();
			var items=this.element.getElementsByTagName('li');
			for(var i=0;i<items.length;i++)
			{
				var item=this.get_item(i);
				return_array.push(item);

			}
			return return_array;
		}
		
		this.return_array=function()
		{
			var return_array=new Array();
			var items=this.element.getElementsByTagName('li');
			for(var i=0;i<items.length;i++)
			{
				var item=items[i].getAttribute('name');
				if(item!=null)
				{
					return_array.push(item);
				}

			}
			return return_array;
		}

		this.onchange=function(){}
	},



	/***********************************************/
	/***Simple class for handling tabs
	/***********************************************/
	tabs:function(tabbar_el_id)
	{
		var that=this;
		this.tab_list=new Array();
		this.current=false;
		this.tab_class='tab_item';
		this.selected_class='tab_item_selected';

		this.tabbar_el=getElement(tabbar_el_id);
		if(!this.tabbar_el) return false;
		
		this.tab=function(tab_element,target_element)
		{
			var tabbar=that;
			var this_tab=this;
			this.tab_element=tab_element;
			this.target_element=target_element;
			if(!this.tab_element)
			{
				return false;
			}
			if(target_element!=false)
			{
				this.target_element.style.display="none";
			}
			
			if(typeof this.tab_element.onclick !="undefined")
			{
				var old_on_click=this.tab_element.onclick;
			}
			else var old_on_click=function(){}
			
			this.tab_element.setAttribute('href','#');
			this.tab_element.setAttribute('onclick','return false;');
			this.activate=function()
			{
				if(tabbar.current!=false)
				{
					tabbar.current.tab_element.className=tabbar.tab_class;
					if(tabbar.current.target_element!=false)
					{
						tabbar.current.target_element.style.display='none';
					}
				}
				this.tab_element.className=tabbar.selected_class;
				if(this.target_element!=false)
				{
					this.target_element.style.display="block";
				}
				tabbar.current=this;
			}
			
			this.tab_element.onclick=function(e)
			{
				e=e || window.event;
				this_tab.activate();
				old_on_click();
				return false;
			}

		}
		
		var start_index=0;
		for(var i=0;i<this.tabbar_el.childNodes.length;i++)
		{
			var cur_child=this.tabbar_el.childNodes[i];

			if(cur_child.className==this.tab_class) 
			{
				var target_element_id=cur_child.getAttribute('href',2);
				var tab_name=cur_child.getAttribute('name');
				if(tab_name!== null && tab_name=='start')
				{
					start_index=this.tab_list.length;
				}
				
				if(!target_element_id || target_element_id=="#false")
				{
					var target_el=false;
				}
				else
				{
					target_element_id=target_element_id.replace(/^#/,'');
					var target_el=getElement(target_element_id);
					if(typeof target_el == 'undefined')
					{
						target_el=false;
					}
				}
				
				var new_tab=new this.tab(cur_child,target_el);//create a tab
				this.tab_list.push(new_tab);
			}
		
		}
		if(this.tab_list.length>0)
		{
			this.tab_list[start_index].activate();
		}

	},//end of tabs

	image_cropper:function(image_element,args)
	{
		var that =this;
		this.frame=document.createElement('div');
		jQuery(this.frame).css("background-color","#6f7178");
		jQuery(this.frame).css("filter","alpha(opacity=40)"); 
		jQuery(this.frame).css("opacity","0.4");
		jQuery(this.frame).css("border","1px solid black");
		jQuery(this.frame).css("z-index","10");
		jQuery(this.frame).css("cursor","move");
		this.ratio=0.5625;
		this.frame_x=0;
		this.frame_y=0;
		this.min_frame_width=200;
		
		/*Image dimensions and data*/
		this.image_width=image_element.clientWidth;
		this.image_height=image_element.clientHeight;
		this.image_offset_top=jQuery(image_element).position().top;
		this.image_offset_left=jQuery(image_element).position().left;
		
		if(!image_element.naturalWidth)//IE fix
		{
			var new_image=new Image();
			new_image.src=image_element.src.toString();
			this.image_natural_width=new_image.width;
			this.image_natural_height=new_image.height;
			delete new_image;
		}
		else
		{
			this.image_natural_width=image_element.naturalWidth;
			this.image_natural_height=image_element.naturalHeight;
		}
		
		
		
		/*change the defaults*/
		if(typeof args != "undefined")
		{
			if(args.min_frame_width)
			{
				this.min_frame_width=Math.floor(args.min_frame_width);
			}

			if(args.ratio)
			{
				this.ratio=args.artio;
			}
		}

		
		/*Set up and check the frame-dimensions*/
		this.frame_width=this.min_frame_width;

		if(this.image_width<this.frame_width)
		{
			this.min_frame_width=this.image_width;
			this.frame_width=this.image_width;
		}

		this.frame_height=Math.floor(this.frame_width * this.ratio);
		if(this.image_height<this.frame_height)
		{
			this.frame_height=this.image_height;
			this.frame_width=Math.floor(this.frame_height/this.ratio);
			this.min_frame_width=this.frame_width;
		}

		this.frame.style.width=this.frame_width -2 + "px";
		this.frame.style.height=this.frame_height -2 + "px";
		this.frame.style.position="absolute";
		this.frame.style.top=this.image_offset_top  + 'px';
		this.frame.style.left=this.image_offset_left + 'px';
		
		/*frame events*/
		this.frame.onmousedown=function(e)
		{
			e=e || window.event;
			if(e.preventDefault) e.preventDefault();

			that.mouse_x=e.clientX;
			that.mouse_y=e.clientY;

			document.onmousemove=function(ev)
			{
				ev=ev || window.event;
				that.change_frame_position(ev);
				document.body.focus();
			}
			document.onmouseup=function()
			{
				that.stop();
			}
		}


		/*Set up the resize boxes*/

		var resize_box_styles={'position':'absolute', 'width':'20px', 'height':'20px', 'background':'black','opacity': '0.8','z-index':'11'};
		
		var nw_box=document.createElement('div');
		jQuery(nw_box).css(resize_box_styles);
		this.frame.appendChild(nw_box);
		nw_box.style.top="0px";
		nw_box.style.left="0px";
		nw_box.style.cursor="nw-resize";
		nw_box.onmousedown=function(e)
		{
			e=e || window.event;
			if(e.preventDefault) e.preventDefault();
			e.cancelBubble=true;
			if( e.stopPropagation ) e.stopPropagation();
			document.body.focus();
			that.mouse_x=e.clientX;
			that.mouse_y=e.clientY;
			that.start_resize('top_left');
		}
		
		var ne_box=document.createElement('div');
		jQuery(ne_box).css(resize_box_styles);
		this.frame.appendChild(ne_box);
		ne_box.style.top="0px";
		ne_box.style.right="0px";
		ne_box.style.cursor="ne-resize";
		ne_box.onmousedown=function(e)
		{
			e=e || window.event;
			if(e.preventDefault) e.preventDefault();
			e.cancelBubble=true;
			if( e.stopPropagation ) e.stopPropagation();
			document.body.focus();
			that.mouse_x=e.clientX;
			that.mouse_y=e.clientY;
			that.start_resize('top_right');
		}
		
		
		var sw_box=document.createElement('div');
		jQuery(sw_box).css(resize_box_styles);
		this.frame.appendChild(sw_box);
		sw_box.style.bottom="0px";
		sw_box.style.left="0px";
		sw_box.style.cursor="sw-resize";
		sw_box.onmousedown=function(e)
		{
			e=e || window.event;
			if(e.preventDefault) e.preventDefault();
			e.cancelBubble=true;
			if( e.stopPropagation ) e.stopPropagation();
			document.body.focus();
			that.mouse_x=e.clientX;
			that.mouse_y=e.clientY;
			that.start_resize('bottom_left');
		}
		
		
		var se_box=document.createElement('div');
		jQuery(se_box).css(resize_box_styles);
		this.frame.appendChild(se_box);
		se_box.style.bottom="0px";
		se_box.style.right="0px";
		se_box.style.cursor="se-resize";
		se_box.onmousedown=function(e)
		{
			e=e || window.event;
			if(e.preventDefault) e.preventDefault();
			e.cancelBubble=true;
			if( e.stopPropagation ) e.stopPropagation();
			document.body.focus();
			that.mouse_x=e.clientX;
			that.mouse_y=e.clientY;
			that.start_resize('bottom_right');
		}
		image_element.parentNode.appendChild(this.frame);//add frame

		/*******************************************/
		/*Move - functionality
		/*******************************************/
		this.change_frame_position=function(event)
		{
			var last_x=that.mouse_x;
			var last_y=that.mouse_y;
			that.mouse_x=event.clientX;
			that.mouse_y=event.clientY;

			var dif_x=last_x - that.mouse_x;
			var dif_y=last_y - that.mouse_y;

			that.frame_x= that.frame_x - dif_x;
			that.frame_y=that.frame_y - dif_y;
			if(that.frame_x<0) that.frame_x=0;
			if(that.frame_y<0) that.frame_y=0;

			if(that.frame_x + that.frame_width > that.image_width)
			{
				that.frame_x=that.image_width-that.frame_width;
			}

			if(that.frame_y + that.frame_height > that.image_height)
			{
				that.frame_y=that.image_height-that.frame_height;
			}

			
			that.update_frame();
		}

		/*Stop moving*/
		this.stop=function()
		{
			document.onmousemove=null;
		}

		/**********************************************/
		/*Resize frame
		/**********************************************/
		this.start_resize=function(corner)
		{
			var that=this;
			document.onmouseup=function()
			{
				that.stop();
			}
			this.stop();
			switch(corner)
			{
				case 'top_left':
					document.onmousemove=function(e)
					{
						e=e || window.event;//IE fix
						document.body.focus();
						var last_width =that.frame_width;
						var last_height=that.frame_height;

						var last_x=that.mouse_x;
						var last_y=that.mouse_y;
						that.mouse_x=e.clientX;
						that.mouse_y=e.clientY;

						var dif_x=last_x - that.mouse_x;
						var dif_y=last_y - that.mouse_y;
						if(dif_x<0)
						{
							var change=-(Math.abs(dif_x) + Math.abs(dif_y));
						}
						else 
						{
							var change=Math.abs(dif_x) + Math.abs(dif_y);
						}
						
						that.frame_width=that.frame_width + change;
						if(that.frame_width<that.min_frame_width) 
						{
							that.frame_width=that.min_frame_width;
						}

						that.frame_height=that.ratio*that.frame_width;//Makes sure the ratio is retained
						var change_x=that.frame_width-last_width;
						var change_y=that.frame_height-last_height;

						if(that.frame_x-change_x<0 || that.frame_y-change_y<0)
						{
							that.frame_width=last_width;
							that.frame_height=last_height;
							return;
						}
						that.frame_x=that.frame_x- change_x;//makes the frame grow towards the topleft corner by changing the topleft coordinates
						that.frame_y=that.frame_y-change_y;

						that.update_frame();
					}
				break;//Upperleft
				case 'top_right':
					document.onmousemove=function(e)
					{
						e=e || window.event;//IE fix
						document.body.focus();
						var last_width =that.frame_width;
						var last_height=that.frame_height;

						var last_x=that.mouse_x;
						var last_y=that.mouse_y;
						that.mouse_x=e.clientX;
						that.mouse_y=e.clientY;

						var dif_x=last_x - that.mouse_x;
						var dif_y=last_y - that.mouse_y;
						if(dif_x<0)
						{
							var change=-(Math.abs(dif_x) + Math.abs(dif_y));
						}
						else 
						{
							var change=Math.abs(dif_x) + Math.abs(dif_y);
						}
						
						that.frame_width=that.frame_width - change;
						if(that.frame_width<that.min_frame_width) 
						{
							that.frame_width=that.min_frame_width;
						}
						
						that.frame_height=that.ratio*that.frame_width;//Makes sure the ratio is retained
						var change_y=that.frame_height - last_height;
						that.frame_y=that.frame_y-change_y;
						
						/*Boundary check*/
						if(that.frame_x + that.frame_width>that.image_width || that.frame_y<0)
						{
							that.frame_width=last_width;
							that.frame_height=last_height;
							that.frame_y=that.frame_y + change_y;
							return;
						}
						
						that.update_frame();
					}
				break;//UpperRight, and so on
				case 'bottom_left':
					document.onmousemove=function(e)
					{
						e=e || window.event;//IE fix
						document.body.focus();
						var last_width =that.frame_width;
						var last_height=that.frame_height;

						var last_x=that.mouse_x;
						var last_y=that.mouse_y;
						that.mouse_x=e.clientX;
						that.mouse_y=e.clientY;

						var dif_x=last_x - that.mouse_x;
						var dif_y=last_y - that.mouse_y;
						if(dif_x<0)
						{
							var change=-(Math.abs(dif_x) + Math.abs(dif_y));
						}
						else 
						{
							var change=Math.abs(dif_x) + Math.abs(dif_y);
						}
						
						that.frame_width=that.frame_width + change;
						if(that.frame_width<that.min_frame_width) 
						{
							that.frame_width=that.min_frame_width;
						}
						that.frame_height=that.ratio*that.frame_width;//Makes sure the ratio is retained
						var change_x=that.frame_width - last_width;
						that.frame_x=that.frame_x-change_x;
						
						/*Boundary check*/
						if(that.frame_x <0 || that.frame_y + that.frame_height>that.image_height)
						{
							that.frame_width=last_width;
							that.frame_height=last_height;
							that.frame_x=that.frame_x + change_x;
							return;
						}
						that.update_frame();
					}
				break;
				case 'bottom_right':
					document.onmousemove=function(e)
					{
						e=e || window.event;//IE fix
						document.body.focus();
						var last_width =that.frame_width;
						var last_height=that.frame_height;

						var last_x=that.mouse_x;
						var last_y=that.mouse_y;
						that.mouse_x=e.clientX;
						that.mouse_y=e.clientY;

						var dif_x=last_x - that.mouse_x;
						var dif_y=last_y - that.mouse_y;
						if(dif_x<0)
						{
							var change=-(Math.abs(dif_x) + Math.abs(dif_y));
						}
						else 
						{
							var change=Math.abs(dif_x) + Math.abs(dif_y);
						}
						
						that.frame_width=that.frame_width - change;
						if(that.frame_width<that.min_frame_width) 
						{
							that.frame_width=that.min_frame_width;
						}

						
						that.frame_height=that.ratio*that.frame_width;//Makes sure the ratio is retained
						
						/*Boundary check*/
						if(that.frame_x + that.frame_width>that.image_width || that.frame_y + that.frame_height>that.image_height)
						{
							that.frame_width=last_width;
							that.frame_height=last_height;
							return;
						}

						that.update_frame();
					}
				break;
				default:break;
			};
		}//end of start_resize
		

		/***************************************************/
		/*Set the changes to the screen
		/***************************************************/
		this.update_frame=function()
		{
			this.frame.style.width=this.frame_width -2 + "px";
			this.frame.style.height=this.frame_height -2 + "px";
			this.frame.style.top=this.image_offset_top + this.frame_y  + 'px';
			this.frame.style.left=this.image_offset_left + this.frame_x +  'px';
		}

		this.get_natural_frame_width=function()
		{
			return Math.round((this.image_natural_width/this.image_width)*this.frame_width);
		}
		this.get_natural_frame_height=function()
		{
			return Math.round((this.image_natural_width/this.image_width)*this.frame_height);
		}
		this.get_natural_frame_x=function()
		{
			return Math.round((this.image_natural_width/this.image_width)*this.frame_x);
		}
		this.get_natural_frame_y=function()
		{
			return Math.round((this.image_natural_width/this.image_width)*this.frame_y);
		}

		this.change_ratio=function(ratio)
		{
			if(ratio<0.2 || ratio>2) return;
			
			this.ratio=ratio;
			this.frame_x=0;
			this.frame_y=0;
			this.frame_height=Math.floor(this.frame_width * this.ratio);
			if(this.image_height<this.frame_height)
			{
				this.frame_height=this.image_height;
				this.frame_width=Math.floor(this.frame_height/this.ratio);
				if(this.min_frame_width>this.frame_width)
				{
					this.min_frame_width=this.frame_width;
				}
			}
			this.update_frame();
		}
	}
}//js_tools

var cForm =
{
	send:function(formElement,callBack)//Posts the given form , takes it's data from the forms action and method attributes
	{
		var queryString=cForm.get_query_string(formElement);
		var url=jQuery(formElement).attr('action');
		if(js_tools.URLarguments(url)) queryString = url + "&" + queryString;
		else queryString=url + "?" + queryString;
		
		if(callBack!=undefined)
		{
			jQuery.get(queryString,callBack);
		}
		else 
		{
			jQuery.get(queryString,function(data)
			{
				var error=js_tools.request_success(data);
				if(error!==false)
				{
					alert(error.response);//Outouts an error-message in error
				}
			});//jQuery.get
		}//else
		
	},
	find:function(element)//finds the parent-form of the given input tag
	{
		if(!jQuery(cur).is('input') || !jQuery(cur).is('select')) return false;
		var cur=element.parentNode;
		while(!jQuery(cur).is('form') || cur===document.body)
		{
			cur=cur.parentNode;
		}
		if(jQuery(cur).is('form')) return cur;
		else return false;
	},
	get_query_string:function(formElement)
	{
		var inputs = jQuery(formElement).find('input,textarea,select');
		var queryString="";
		for(var i=0;i<inputs.length;i++) 
		{
			if(inputs.eq(i).attr('name')!="")
			{
				queryString += inputs.eq(i).attr('name') + "=";
				if(inputs.eq(i).attr('type')=='checkbox')
				{
					if(inputs.eq(i).attr('checked')==true)
					{
						queryString +="1&";
					}
					else
					{
						queryString +="0&";
					}
				}
				else
				{
					queryString += encodeURIComponent(inputs.eq(i).val().replace(/€/gi,'&#8364;')) + "&";
				}
			}
		}
		queryString=queryString.replace(/&$/,'');
		return queryString;
	}
}
/*************************************************/




clone_object = function(o)
{
	if(o instanceof Array) 
	{
		var new_obj = new Array();
	}
	else
	{
		var new_obj = new Object();
	}

	for(_index in o)
	{
		if(_index=='parentRef') continue;
		if(typeof o[_index]=='object')
		{
			new_obj[_index]=clone_object(o[_index]);
			
		}
		else
		{
			new_obj[_index]=o[_index];
		}
	}
	return new_obj;
}


