﻿// Misc script functions.
function _CMisc() {

	this.preparePage = _preparePage;
	this.applyBackgroundImage = _applyBackgroundImage;
	this.basePath = _basePath;
	this.pagePath = _pagePath;
	
	this._basePathCached = null;
	
	function _preparePage() {
		this.applyBackgroundImage();
		Pictures.preLoad();
		Gallery.initialize();
	}
	
	function _applyBackgroundImage() {
		// Apply background image to the page
		var href = document.location.href;
		if (href == null || href.length == 0) return;

		var fileName = href.substr(href.lastIndexOf('/') + 1);
		var imageName = fileName.substr(0, fileName.lastIndexOf('.')) + '.jpg';	
		var middel = $('#middel');				
		middel.css('background-image', "url('" + this.basePath() + "Images/Backgrounds/Page-" + imageName + "')");
	}
	
	// Get the site base path
	function _basePath() {
		if (this._basePathCached == null) {
			var href = document.location.href;
			if (href == null || href.length == 0) {
				this._basePathCached = '';
				return;
			}
		
			if (href.indexOf('file://') == 0) {
				// We are running locally
				var i = href.indexOf('/WebSite/') + '/WebSite/'.length;
				this._basePathCached = href.substr(0, i);
			}
			else {
				// We are running remotly
				var i = href.indexOf('/~ottesen/') + '/~ottesen/'.length;
				this._basePathCached = href.substr(0, i);
			}
		}
		
		return this._basePathCached;
	}
	
	// Get the full path for the current page
	function _pagePath(href) {
		var href = (href ? href : document.location.href);
		return href.substr(0, href.lastIndexOf('/') + 1);
	}
}

var Misc = new _CMisc();

