/**************************************************
  include.js
  
  Basisfunktionen.
  include()
  
  version: 3.0
  11:00 19.09.2008
  
  
**************************************************/

( function() {

	var JS_PATH = './';
	var JS_INCLUDE = {};
	var JS_SELF = 'include.js';
	
	this.include = function(src) {
		if( !is_string(src) ) return;
		
		/**
			für die Schreibweise:  Pfad::Datei
		*/
		if(src.indexOf('::') > -1) {
			var tmp = src.split('::');
			var part;
			src = '';
			while(part = tmp.shift()){
				src += '/' + part;
			}
			src += '.js';
		}
		
		if(src.lastIndexOf('.') == -1) src += '.js';
		
		//  mit Punkt wird der JS_PATH Pfad nicht verwendet
		if(src.indexOf('.') != 0) {
			src = JS_PATH +  src
		}
		// Wurde Datei schon eingebunden
		if( JS_INCLUDE[src] ) return;

		document.write( '<script defer type="text/javascript" src="' + src + '"><\/script>' );

		JS_INCLUDE[src] = true;
	};
	
	/**
	* Den Basispfad zu den Bibliotheken finden.
	*/
	if(document.all && !window.opera)	{
		// IE
		var o = document.all.tags('script');
		for(var i = 0; i < o.length; i++) {
			if( o[i].src.lastIndexOf( JS_SELF ) != -1) {
				var src = o[i].src;
				var idx = src.lastIndexOf('/');
				var base = '';
				if(src.indexOf('http://') == -1) {
					base = self.location.href;
					base = base.substring(0, base.lastIndexOf('/') + 1) ;
				}
				if(idx == -1) idx = 0;
				JS_PATH = base + src.substring(0, idx) +'/';
				break;
			}
		}
	} else if(document.getElementsByTagName)	{
		// DOM
		var o = window.document.getElementsByTagName('script');
		for(var i = 0; i < o.length; i++) {
			var src = o[i].src;
			var idx = src.lastIndexOf(JS_SELF);
			if(src && idx > -1) {
				JS_PATH = src.substring(0, idx - 1) ;
				break;
			}
		}
		JS_PATH += '/';
	}
})() // anonym call

function addOnLoad (f) {
	var oldOnLoad = window.onload;
	window.onload = function() {
		if(oldOnLoad) oldOnLoad();
		f()
	}
}
addOnLoad ( function() { if(typeof main == 'function') main();});

if(typeof undefined == 'undefined') undefined = typeof undefined;
function is_number(w){ return (typeof w == 'number') && !isNaN(w); };
function is_string(w){ return typeof w == 'string'; };
function is_object(w){ return typeof w == 'object'; };
function is_function(a){ return typeof a == 'function'; };
function is_equal(a, b){ a && b && a.constructor == b.constructor; };
function defined(w){ return w != undefined && w != null; }	
function is_a_word(val) { return !(/[^a-zöäü]/i).test(val);}
function is_a_number(val) { return !/[\D]/.test( val );} 

/**
Eine Funktion an ein Objekt koppeln
*/
Function.prototype.bind = function(obj) {
	var self = this;
	var args = [];
	for(var i = 0; i < arguments.length; i++) args.push( arguments[i] );
	return function(evt) { self.call(obj, args); };
}

Function.prototype.bindEvent = function(obj) {
	var self = this;
	return function(evt) { self.call(obj, evt || window.event); };
}

function extendObj (dest, src) {
	for (var property in src) dest[property] = src[property];
	return dest;
}

// Für alte Browser
if(!document.getElementById) include('getelementbyid.js');
if(!Array.prototype.forEach) include('array.js');


// Noch in Arbeit

//include('Std::Liste');
//include('String::Trim');



function findPath(link){
	if(!link) return './';
	var idx = link.lastIndexOf('/');
	var start = 0; 
	return link.substring(start + 1, idx + 1);
}

function height(){ return window.innerHeight || this.Body.offsetHeight;}
function width () { return window.innerWidth || this.Body.offsetWidth; }


this.Body = window.document.documentElement || window.document.body;

function docSize() {
	var width, height;
	
	if( window.document.height ) {
		
		width = window.document.width;
		height = window.document.height;
	
	} else if(Body) {
	
		var test1, test2;
			
		test1 = Body.scrollHeight;
		test2 = Body.offsetHeight || Body.clientHeight;
		height = test1 > test2 ?  test1 : test2;
			
		test1 = Body.scrollWidth;
		test2 = Body.offsetWidth || Body.clientWidth;
		width = (test1 > test2) ? test1 : test2;
	}
			
	return [ height, width ];
}



function domReady(callback) {
	var ie = /*@cc_on!@*/false;
	
	if (document.addEventListener) {
		document.addEventListener('DOMContentLoaded', callback, false);
	} else if (ie) {
		// http://www.hedgerwow.com/360/dhtml/ie-dom-ondocumentready.html
		(function () {
			if (document.loaded) return;
			
			var tempNode = document.createElement('document:ready'); 
			try {
				tempNode.doScroll('left');
				if (!document.body) throw new Error();
				
				document.loaded = true;
				callback();
				tempNode = null; 
			} catch(e) {
				window.setTimeout(arguments.callee, 0); 
			}
		})();
	} else {
		var timeout = window.setTimeout( function() {
			if (document.readyState == 'loaded' || document.readyState == 'complete' ) {
				callback();
			} else {
				window.setTimeout(arguments.callee, 1);
			}
      }, 1); 
	}
}
// Standardmodule
include('add_event');
include('string');

function _(id){
	return window.document.getElementById(id);
}

function __(name){
	var filter = function() {return true;};
	if(name.indexOf('.') > -1) {
		var reg = new RegExp('\\b' + name.split('.')[1] + '\\b', 'i');
		name = name.split('.')[0];
		filter = function(el) {
			var className = el.className;
			if(!className) return false;
			return reg.test(className);
		}
	}
	 var r = window.document.getElementsByTagName(name || '*');
	 var ret = [];
	 for(var i = 0; i < r.length; i++) if(filter(r[i])) ret.push(r[i]);
	 return ret;
}
