var Base = function() {
	if (arguments.length) {
		if (this == window) { // cast an object to this class
			Base.prototype.extend.call(arguments[0], arguments.callee.prototype);
		} else {
			this.extend(arguments[0]);
		}
	}
};
Base.version = "1.0.2";
Base.prototype = {
	extend: function(source, value) {
		var extend = Base.prototype.extend;
		if (arguments.length == 2) {
			var ancestor = this[source];
			// overriding?
			if ((ancestor instanceof Function) && (value instanceof Function) &&
				ancestor.valueOf() != value.valueOf() && /\bbase\b/.test(value)) {
				var method = value;
			//	var _prototype = this.constructor.prototype;
			//	var fromPrototype = !Base._prototyping && _prototype[source] == ancestor;
				value = function() {
					var previous = this.base;
				//	this.base = fromPrototype ? _prototype[source] : ancestor;
					this.base = ancestor;
					var returnValue = method.apply(this, arguments);
					this.base = previous;
					return returnValue;
				};
				// point to the underlying method
				value.valueOf = function() {
					return method;
				};
				value.toString = function() {
					return String(method);
				};
			}
			return this[source] = value;
		} else if (source) {
			var _prototype = {toSource: null};
			// do the "toString" and other methods manually
			var _protected = ["toString", "valueOf"];
			// if we are prototyping then include the constructor
			if (Base._prototyping) _protected[2] = "constructor";
			for (var i = 0; (name = _protected[i]); i++) {
				if (source[name] != _prototype[name]) {
					extend.call(this, name, source[name]);
				}
			}
			// copy each of the source object's properties to this object
			for (var name in source) {
				if (!_prototype[name]) {
					extend.call(this, name, source[name]);
				}
			}
		}
		return this;
	},

	base: function() {
		// call this method from any other method to invoke that method's ancestor
	}
};

Base.extend = function(_instance, _static) {
	var extend = Base.prototype.extend;
	if (!_instance) _instance = {};
	// build the prototype
	Base._prototyping = true;
	var _prototype = new this;
	extend.call(_prototype, _instance);
	var constructor = _prototype.constructor;
	_prototype.constructor = this;
	delete Base._prototyping;
	// create the wrapper for the constructor function
	var klass = function() {
		if (!Base._prototyping) constructor.apply(this, arguments);
		this.constructor = klass;
	};
	klass.prototype = _prototype;
	// build the class interface
	klass.extend = this.extend;
	klass.implement = this.implement;
	klass.toString = function() {
		return String(constructor);
	};
	extend.call(klass, _static);
	// single instance
	var object = constructor ? klass : _prototype;
	// class initialisation
	if (object.init instanceof Function) object.init();
	return object;
};

Base.implement = function(_interface) {
	if (_interface instanceof Function) _interface = _interface.prototype;
	this.prototype.extend(_interface);
};

var Lib = {};
Lib.Error = {};
Lib.Error._errorEventsArray = 		[];
Lib.Error.callOnError = 		function(func){
							Lib.Error._errorEventsArray.push(func);
};
Lib.Error.throwError = 			function (e){
						for (var i=0; i<Lib.Error._errorEventsArray.length; i++){
						   Lib.Error._errorEventsArray[i](e);
						}
};
Lib.Error.getErrorDetails = 		function (e){
						var arr = [];
						for (params in e){
							arr.push(params+': '+e[params]);
						}
						return arr;
};
Lib.Error.Report = 			function (e){
						e.userAgent = navigator.userAgent;
						var errordetails = Lib.Error.getErrorDetails(e);
						var occdateobj= new Date()
						var theyear=occdateobj.getFullYear();
						var themonth=occdateobj.getMonth()+1;
						var thetoday=occdateobj.getDate();
						var thehours=occdateobj.getHours();
						var theminutes=occdateobj.getMinutes();
						var occdate = themonth+'/'+thetoday+'/'+theyear+' '+thehours+':'+theminutes;
						var poststr = 'siteUrl='+escape(window.location.href);
						poststr += '&errorUrl='+escape((e.fileName?e.fileName:window.location.href));
						poststr += '&errorName='+escape(e.name);
						poststr += '&errorMessage='+escape(errordetails.join(",\n"));
						poststr += '&occurenceTime='+escape(occdate);
}
Lib.Error.callOnError(Lib.Error.Report);
Lib.delegate =				function(that, thatMethod){

							var _params = [];
							for(var n = 2; n < arguments.length; n++) _params.push(arguments[n]);
							return function() {

						         var paramsToUse = [];
						         for(var n = 0; n < arguments.length; n++) {
						         	paramsToUse.push(arguments[n]);
						         }
						         for(var n = 0; n < _params.length; n++) {
						         	paramsToUse.push(_params[n]);
						         }

							 try {
							  if (paramsToUse.length > 0){
							    return thatMethod.apply(that, paramsToUse)
							  }else {
							    return thatMethod.call(that)
							  }
							 }catch(e){
							  e.func = thatMethod;
							  e.params = _params.join(",");
							  Lib.Error.throwError(e);
							 };
							}
};


Lib.Dom = {};
Lib.Dom.getEventSrc = 			function (e){

							if (typeof e == 'undefined') {
							 if (window.event){
							  var e = window.event;
							 }else {
							  return false;
							 }
							}
							if (e.cancelBubble != null){
								return (typeof e.target != 'undefined'?source = e.target:source = e.srcElement);
							}
							return false;

};

/* events */

Lib.Dom.addEvent	= 		function( obj, type, fn, uniqueid ) {
								  if ( obj.attachEvent ) {
								    if (uniqueid == null){
								     uniqueid = fn;
								    }
								    obj['e'+type+uniqueid] = fn;
								    obj[type+uniqueid] = function(){obj['e'+type+uniqueid]( window.event );}
								    obj.attachEvent( 'on'+type, obj[type+uniqueid] );
								  } else
								    obj.addEventListener( type, fn, false );
}
Lib.Dom.removeEvent = 		function ( obj, type, fn, uniqueid ) {

								  if ( obj.detachEvent ) {
								    if (uniqueid == null){
								     uniqueid = fn;
								    }
								    obj.detachEvent( 'on'+type, obj[type+uniqueid] );
								    obj[type+uniqueid] = null;
								  } else
								    obj.removeEventListener( type, fn, false );
}
Lib.Dom.addEventQuick = 		function (item, type, func){
							Lib.Dom.addEvent(item, type, Lib.delegate(this, func, item));
}

Lib.Dom.preventDefaultIE  = function(){
						      window.event.cancelBubble = true;
						      window.event.returnValue = false;
}
Lib.Dom.preventDefaultBehaviour = function(e){
							 var browser = Lib.Browser.get();
						     if (browser.isIE && (window.event!=null)){
						        Lib.Dom.preventDefaultIE();
						     }
						     else
						           e.preventDefault();
}
Lib.Dom._domLoadedFunctionList	=	[];
Lib.Dom._domLoaded =			false;
Lib.Dom.callWhenDOMLoaded = 		function (func) {

							if (Lib.Dom._domLoaded) {
								try {func();}catch(e){Lib.Error.throwError(e);}
							} else {
								Lib.Dom._domLoadedFunctionList.push(func);
							}
};
Lib.Dom._domLoadedEvent	=		function() {
							Lib.Dom._domLoaded=true;

							if (arguments.callee.done) return;

							arguments.callee.done = true;

							for (var i=0;i<Lib.Dom._domLoadedFunctionList.length;i++) {
							  try {Lib.Dom._domLoadedFunctionList[i]();}catch(e){Lib.Error.throwError(e);}
							}
};





// shortcut to call when dom loaded
callWhenDOMLoaded = function(a){return Lib.Dom.callWhenDOMLoaded(a);};
if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", Lib.Dom._domLoadedEvent, null);
}
window.onload = Lib.Dom._domLoadedEvent;
/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	document.write("<script id=__ie_onload defer src=\"//:\"><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			Lib.Dom._domLoadedEvent(); // call the onload handler
		}
	};
/*@end @*/







Lib.Dom.Elm = {};
Lib.Dom.Elm.find = 			function(elm){
							 if (typeof elm == "object"){
							  if (source = Lib.Dom.getEventSrc(elm)){
							   return source;
							  }else {
							   return elm;
							  }
							 }else {
							  var id = elm;
							  var isOpera = false;
							  var isIE = false;
							  if(typeof(window.opera) != 'undefined'){isOpera = true;}
							  var isIE = /*@cc_on!@*/false;
							  if(isOpera || isIE){
							   var elem = document.getElementById(id);
							   if(elem){
							    if(elem.attributes['id'] && elem.attributes['id'].value == id){
							     return elem;
							    } else {
							     for(var i=1;i<document.all[id].length;i++){
							      if(document.all[id][i].attributes['id'] && document.all[id][i].attributes['id'].value == id){
							       return document.all[id][i];
							      }
							     }
							    }
							   }
							   return null;
							  }else {
							   return document.getElementById(id);
							  }
							 }
};




// shortcut to Lib.Dom.Elm.find()
$ = 					function (e){
							return Lib.Dom.Elm.find(e);
};

Lib.Dom.Elm.create = 		function(type, styles){

							var elm = document.createElement(type);

							if (styles){
							  Lib.Dom.Elm.Style.add(elm, styles);
							}
							return elm;
};
Lib.Dom.Elm.Style = {};
Lib.Dom.Elm.Style.add = 		function(elm, styles){
							elm = $(elm);
							for (sty in styles){
							  elm.style[sty] = styles[sty];
							}
};

var IframeLoader = Base.extend ({
	constructor : function (linksid, contentid){
		this.linksid = linksid;
		this.contentid = contentid;
		callWhenDOMLoaded(Lib.delegate(this, this.init));
	},
	init : function (){
		if (parent != null && parent.location.href != location.href){
			parent.iframeload.onIframeLoad();
		}else {
			this.applyLinkEvents();
		}
	},
	applyLinkEvents : function (){
		this.alllinks = this.getAllLinks()
		for (var i=0; i<this.alllinks.length; i++){
			var link = this.alllinks[i];
			var newhref = link.href;
			newhref += (newhref.indexOf("?") > -1) ? "&1" : "?1";
			link.delegate = Lib.delegate(this, this.onClick, newhref);
			link.href = 'javascript:void(0);';
			Lib.Dom.addEvent(link, "click", link.delegate);
		}
	},
	removeLinkEvents : function (){
		this.alllinks = this.getAllLinks();
		for (var i=0; i<this.alllinks.length; i++){
			var link = this.alllinks[i];
			Lib.Dom.removeEvent(link, "click", link.delegate);
		}
	},
	getAllLinks : function (){
		return $(this.linksid).getElementsByTagName("a");
	},
	onClick : function (e, link){
		this.getIframe().src = link;
	},
	getIframe : function (){
		if (this.iframe == null){
			this.iframe = Lib.Dom.Elm.create("iframe", {display: 'none'} );
			document.body.appendChild(this.iframe);
		}
		return this.iframe;
	},
	onIframeLoad : function (){
		this.removeLinkEvents();
		var doc;
		var iframe = this.getIframe();
		if(iframe.contentDocument)
		   doc = iframe.contentDocument;
		else if(iframe.contentWindow)
		   doc = iframe.contentWindow.document;
		else if(iframe.document)
		   doc = iframe.document;

		$(this.contentid).innerHTML = doc.getElementById(this.contentid).innerHTML;
		this.applyLinkEvents();

	}

});
