
KPage.prototype = new KWidget;
KPage.prototype.constructor = KPage;

function KPage(targetURL, dataHandlerURL) {
	KWidget.call(this);
	this.panel = document.createElement('div');
	this.targetURL = targetURL;
	this.transition = null;
	this.parentSite = null;

	this.setDataHandler(dataHandlerURL);
	if (targetURL != null) {
		this.queryString = 'page=' + escape(targetURL);		
		this.setID(targetURL.replace(/\//g, '_').replace(/#/g, '_'));
	}
	else {
		this.queryString = null;
	}
}

KPage.prototype.addTransition = function(transition) {
	this.transition = transition;
}

KPage.prototype.onData  = function(e, source) {
}

KPage.prototype.onSetup  = function(e, source) {
	this.panel.innerHTML = source.responseText;
	this.write();
	
	var anchors = this.panel.getElementsByTagName('a');
	var __parentSite = this.parentSite;
	for (var i = 0; i != anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.attachEvent) {
			anchor.attachEvent("onclick", function(event) { KPage.__loadPage(event, __parentSite);});
		} 
		else {
			anchor.addEventListener("click", function(event) { KPage.__loadPage(event, __parentSite);}, false);
		}
	}
	if (this.transition != null) {
		this.transition.goIn();
	}

	if (KBrowserDetect.browser == 2) {
		var scripts = this.panel.getElementsByTagName('script');
		for (var k = 0; k != scripts.length; k++) {
			try {
				eval(scripts[k].innerHTML.replace(/<!--/g, '').replace(/^\s*/, "").replace(/\s*$/, ""));
			}
			catch (ex) {
				//alert(ex.name + " " + e.message);
			}
			finally{
			}
		}
	}

}

KPage.prototype.go = function(queryString) {	
	this.retrieveData((this.queryString != null ? this.queryString : '') + (queryString != null ? (this.queryString != null ? '&' : '') + queryString : ''));
}

KPage.prototype.draw = function() {
	this.panel.className = 'grid_12 content_container';
}

KPage.prototype.getURL = function() {
	return this.targetURL;
}

KPage.__getHRef = function(target)  {
	while (target.tagName.toLowerCase() != 'a') {
		target = target.parentNode;
	}
	return target;
}

KPage.__loadPage = function(event, __parentSite)  {
	var target = KPage.__getHRef(KWidget.__getTarget(event));
	var index = target.href.indexOf('#');
	if (index != -1) {
		var url = target.href.substr(index);
		__parentSite.go(url);
	}
}