
KSite.prototype = new KWidget;
KSite.prototype.constructor = KSite;

function KSite(parent) {
	KWidget.call(this);
	this.setParent(parent);
	this.panel = document.createElement('div');

	this.headerPanel = this.panel.appendChild(document.createElement('div'));
	this.menuPanel = this.panel.appendChild(document.createElement('div'));
	this.pagePanel = this.panel.appendChild(document.createElement('div'));
	this.footerPanel = this.panel.appendChild(document.createElement('div'));

	this.pages = new Array();
	this.menu = null;
	this.header = null;
	this.footer = null;
	this.currentPage = null;
	this.currentURL = null;
	this.transition = null;
	this.draw();	
}

KSite.prototype.addTransition = function(transition) {
	this.transition = transition;
	this.transition.init(this.pagePanel);
	var __thisSite = this;
	this.transition.onStop = function (e, target) {
		if (this.state == 'out') {
			var queryString = __thisSite.normalizeURL(__thisSite.currentURL);
			if (__thisSite.currentPage != null) {
				__thisSite.pagePanel.removeChild(__thisSite.currentPage.panel)
			}
			__thisSite.currentPage = __thisSite.pages[__thisSite.currentURL];
			if (__thisSite.currentPage != null) {
				__thisSite.currentPage.go(queryString);
			}
		}
	}
}

KSite.prototype.addPage = function(page) {
	page.setParent(this.pagePanel);
	page.addTransition(this.transition);
	page.parentSite = this;
	this.pages[page.getURL()] = page;
}

KSite.prototype.setMenu = function(menu) {
	menu.setParent(this.menuPanel);
	menu.parentSite = this;
	this.menu = menu;
	this.menu.go();
}

KSite.prototype.setHeader = function(header) {
	header.setParent(this.headerPanel);
	header.parentSite = this;
	this.header = header;
	this.header.go();
}

KSite.prototype.setFooter = function(footer) {
	footer.setParent(this.footerPanel);
	footer.parentSite = this;
	this.footer = footer;
	this.footer.go();
}

KSite.prototype.setSiteBG = function(url, storeCookie) {
	this.panel.style.backgroundImage = 'url(\'' + url + '\')';
	if (storeCookie) {
		KWidget.setCookie('__ksite_bg', url);
	}
}

KSite.prototype.setPageBG = function(url, storeCookie) {
	this.pagePanel.style.backgroundImage = 'url(\'' + url + '\')';
	if (storeCookie) {
		KWidget.setCookie('__kpage_bg_', url);
	}
}

KSite.prototype.setMenuBG = function(url, storeCookie) {
	this.menuPanel.style.backgroundImage = 'url(\'' + url + '\')';
	if (storeCookie) {
		KWidget.setCookie('__kmenu_bg', url);
	}
}

KSite.prototype.setHeaderBG = function(url, storeCookie) {
	this.headerPanel.style.backgroundImage = 'url(\'' + url + '\')';
	if (storeCookie) {
		KWidget.setCookie('__ksite_bg', url);
	}
}

KSite.prototype.setFooterBG = function(url, storeCookie) {
	this.footerPanel.style.backgroundImage = 'url(\'' + url + '\')';
	if (storeCookie) {
		KWidget.setCookie('__ksite_bg', url);
	}
}

KSite.prototype.go = function(url) {
	this.currentURL = url;
	if (this.transition != null) {
		this.transition.goOut();
	}
	else {
		var queryString = this.normalizeURL(url);
		if (this.currentPage != null) {
			this.pagePanel.removeChild(this.currentPage.panel)
		}
		this.currentPage = this.pages[this.currentURL];
		if (this.currentPage != null) {
			this.currentPage.go(queryString);
		}
	}
}

KSite.prototype.normalizeURL = function(url) {
	var parts = url.split('?');
	if (parts.length != 1) {
		this.currentURL = parts[0];
		return parts[1];
	}
	else {
		this.currentURL = url;
		return null;
	}
}

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

KSite.prototype.onSetup  = function(e, source) {
}

KSite.prototype.draw = function() {
	this.panel.className = 'container_12 site_container';

	this.headerPanel.className = 'grid_12 menu_container';
	this.headerPanel.setAttribute('id', 'header');
	this.menuPanel.className = 'grid_12 menu_container';
	this.menuPanel.setAttribute('id', 'menu');
	this.pagePanel.className = 'grid_12 main_container';
	this.pagePanel.setAttribute('id', 'content');
	this.footerPanel.className = 'grid_12 menu_container';
	this.footerPanel.setAttribute('id', 'footer');
	
	var siteBG = KWidget.getCookie('__ksite_bg');
	if (siteBG != '') {
		this.setSiteBG(siteBG);
	}
}

