var Core = new Class({

		script:	'index.php',
		useCache:	false,
		
		queue:	[],
		css:	[],
		js:		[],
		
		popups: {},
		
		
		initialize:	function()
		{
			var self = this;
			
			window.addEvent('domready', function()
			{
				self.implement();
				self.checkRequirements();
				self.countRequest({cat: 'page request'});
			});
			
		
		},
		
		
		checkRequirements:	function()
		{
			var elms = $$('div.notice');
			var notices = {};
			elms.each(function(elm)
			{
				notices[elm.getProperty('name')] = elm;
			});

			//
			// check for ie
			//
			
			// check is done by conditional comment
			
			//
			// check for flash 10
			//
			if (Browser.Plugins.Flash.version < 10)
			{
				notices.flashlt10.setStyle('display', 'block');
			}
			
			//
			// check for javascript
			//
			
			// check is done by <noscript> tag
		
		},
		
		
		countRequest:	function(o)
		{
			var post = new Hash(o);
			
			var req = new Request({
									url:		'?run=stats_frontend',
									method:		'post',
									noCache:	true
								}).send(post.toQueryString());
			
		},
		
		
		/**
		 * @brief add some custom methods to the Element class
		 */
		implement:	function()
		{
			Element.implement({
			
				nextSib: function(o)
				{
					var elm = this;
					do
					{
						elm = elm.nextSibling;
						if (! elm)
						{
							return false;
						}
					}
					while (elm.nodeName.match(/(text|comment)/));
					
					if (o)
					{
						for (var i in o)
						{
							if (elm[i] != o[i])
							{
								return false;
							}
						}
					}
					
					return elm;
				},
				
				
				isHidden:	function()
				{
					var elm = this;
					while (elm)
					{
						if (elm.clientWidth === 0 && elm.clientHeight === 0)
						{
							return true;
						}
						elm = elm.parentNode;
					}
					
					return false;
				},
				
								
				removeWrapper:	function()
				{
					var wrapper = this;
					var children = wrapper.getChildren();
					children.each(function(elm)
					{
						var clone = elm.clone(1,1).cloneEvents(elm);
						clone.inject(wrapper, 'after');
					});
					wrapper.destroy();
			
				},
				
				old_processScripts:	function()
				{
					var elm = this;	
					var scripts = elm.getElements('script');
					var exec = [];
					scripts.each(function(item)
					{
						if (! item.get('js'))
						{
							item.set('js', item.innerHTML);
						}
						exec.push(item.get('js'));
						$exec(item.get('js'));
					});
				}
				
			});
			
		},
		
		
		getCache:	function()
		{
			var self = this;
			var body = $(document.body);
			var c = body.getElementById('_c');
			if (! c) { return; }
			
			if (c.value)
			{
				body.set('html', c.value);
				body.processScripts();
				self.runQueue();
				self.addCache('domready repeat');
			}
			else
			{
				self.addCache('domready');
			}
		},
		
		addCache:	function(msg)
		{
			//alert('cache: ' + msg);
			var c = document.body.getElementById('_c');
			if (c) { c.value = document.body.innerHTML; }
		},
		
				
		/**
		 * @brief process the queue
		 *
		 * if the queue array isn't empty, run each of its commands
		 * and then reset the array
		 *
		 */
		runQueue:	function()
		{
			/*
			//
			// skip if the queue is empty
			//
			if (this.queue.length < 1) { return; }
			//
			// eval each command in the queue
			//
			for (var i = 0; i < this.queue.length; i++)
			{
				window.eval(this.queue[i]);
				//window.setTimeout(this.queue[i], 100);
			}
			//
			// reset the queue array
			//
			this.queue = [];
			*/
			queue.run();
		},
		
		
		/**
		 * @brief	jump to a URL
		 *
		 * @param object	an object that, when converted into a queryString, 
		 *					will become the current page's new 'search' part
		 *					of its URI and therefore trigger a full page refresh
		 */
		jumpTo:		function(o)
		{
			var query = new Hash(o);
			document.location.search = '?' + query.toQueryString();
		},
		
		
		/**
		 * @brief	load an external stylesheet
		 *
		 * loads an external stylesheet by adding another
		 * <link> element to the <head> part of the current page
		 *
		 * @param	string	the absolute path to the css file to be loaded
		 */			 
		loadCSS:	function(f)
		{
			var self = this;
			//
			// add all css files loaded as <link /> to the css array
			//
			if (this.css.length < 1)
			{
				var css = $(document).getElements('link[rel=stylesheet]');
				css.each(function(item)
				{
					self.css.push(item.getProperty('href'));
				});
			}
			//
			// skip if the requested css file is part of the css array
			//
			if (this.css.contains(f)) { return; }	
			//
			// otherwise load the css file
			//
			var sheet = Asset.css(f);
			//
			// and add it to the css array
			//
			this.css.push(f);
		},
		
		
		/**
		 * @brief	load an external javascript file
		 *
		 * loads an external javascript file by running a synchronous
		 * Ajax call and evaling its responseText.
		 * Since the call is synchronous the page will freeze until the call
		 * is completed
		 *
		 * @param	string	the absolute path to the javascript file to be loaded
		 */
		loadJS:	function(f)
		{
			var self = this;

			//
			// if it hasn't been done yet,
			// add all js files loaded as <script/> to the js array
			//
			if (this.js.length < 1)
			{
				var js = $(document).getElements('script');
				js.each(function(item)
				{
					var src = item.getProperty('src');
					if (src)
					{
						self.js.push(src);
					}
				});
			}
			
			//
			// skip if the file to be loaded is listed in the js array
			//
			if (this.js.contains(f)) { return; }	
			
			//
			// fire the Ajax call
			//
			var req = new Request({
										url:			f,
										method:			'get',
										async:			false,
										evalResponse:	true,
										noCache:		true,
										//onComplete:		function() { alert('js complete'); },
										onFailure:		function(xhr) { alert('failure: ' + xhr); }
									});
			
			req.send();
			
			//
			// add the file requested to the js array
			//
			this.js.push(f);
		},
		
		
		getQuery: function(name)
		{
			var GET = {};
			//
			// remove the leading ? from the search string
			//
			var query = location.search.replace(/^\?/, '');
			//
			// split the query string
			//
			query = query.split('&');
			//
			// split each value pair and add it to the GET object
			//
			query.each(function(item)
			{
				var pair = item.split('=');
				GET[(pair[0])] = pair[1];
			});
			//
			// return the desired value
			//
			return GET[name];
		
		},
		
		isEmpty:	function(val)
		{
			if (val.replace(/[\t\n\r ]/g, '').length < 1)
			{
				return true;
			}
			
			return false;
		},
		
		addDummy:	function(elm, position)
		{
			elm = $(elm);
			var dummy = new Element(	'div', 
										{
											id:	'dummy_' + Math.random()
										});
			dummy.inject(elm, position);
			
			return dummy;
		},
		
		addDummyAbove:	function(elm)
		{
			return this.addDummy(elm, 'before');
		},
		
		addDummyBelow:	function(elm)
		{
			return this.addDummy(elm, 'after');
		},
		
		addDummyUnder:	function(elm)
		{
			return this.addDummy(elm, 'after');
		},
		
		addDummyWrapper:	function(arr)
		{
			var wrapper = this.addDummyAbove(arr[0]);
			arr.each(function(member)
			{
				member.inject(wrapper, 'bottom');
			});
			return wrapper;
		},
		
		getFormData:	function(elm)
		{
			var inputs = $(elm).getElements('input');
			var selects = $(elm).getElements('select');
			var textareas = $(elm).getElements('textarea'); 
			var data = new Hash();
			
			inputs.each(function(item)
			{
				data[item.name] = item.value;	
			});
			
			selects.each(function(item)
			{
				var selected = []
				item.getSelected().each(function(elm)
				{
					selected.push(elm.value);
				});
				
				data[item.name] = selected.join(',');
				
			});
			
			
			textareas.each(function(item)
			{
				var ed = null;
				//
				// check if the textarea is a tinyMCE editor
				//
				try
				{
					ed = tinyMCE.get($(item).getProperty('id'));
				} 
				catch(e) {}
				//
				// if it is an editor, get its content
				//
				if (ed)
				{
					data[item.name] = ed.getContent();
				}
				//
				// otherwise use the textarea's value
				//
				else
				{
					data[item.name] = item.value;
				}	
			});
			
			return data;
		},
		
		showSetLink:	function(obj, onSave)
		{
			var w = this.popups.setLink;
			if (w) w.close();
			
			var win = window.open('?run=setLink', 'popup', "width=640, height=600");
			win._obj = obj;
			win.onSave = onSave;
			
			this.popups.setLink = win;
			
		},
		
		
		setHistory:	function(mid, o)
		{
			var post = new Hash({
									mid:	mid,
									data:	JSON.encode(o)
								});
			

			var req = new Request({
										url:			'?run=History.set&rand=' + Math.random(),
										method:			'post',
										async:			true,
										noCache:		true/*,
										onSuccess:		function(text) { alert('history: ' + text); }
										//onFailure:		function(xhr) { alert('failure: ' + xhr); }*/
									});
			
			req.send(post.toQueryString());
		},
		
		
		element:	{
						nextSib: function(elm, o)
						{
							do
							{
								elm = elm.nextSibling;
								if (! elm)
								{
									return false;
								}
							}
							while (elm.nodeName.match(/(text|comment)/));
							
							if (o)
							{
								for (var i in o)
								{
									if (elm[i] != o[i])
									{
										return false;
									}
								}
							}
					
							return elm;
						}
					}
});




