﻿function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

function mobiChow() { }

window.mobiChow = new mobiChow();

// This code will add a WebApp iLayer if exists in the hash
// but not in the DOM so that the WA.AddEventListener load
// event will have the proper target layer
window.mobiChow.webAppAddMissingLayer = function(hash) {

	// remove the '#_' from the ash
	var layerId = hash.substring(2);

	// Check to see if there are any parameters
	// (starting w/ '/') and remove if exist
	var slashIndex = layerId.indexOf('/');
	if (slashIndex != -1) {
		layerId = layerId.substring(0, slashIndex);
	}

	// Add the 'wa' for the layerId
	layerId = 'wa' + layerId;


	// If it does not exist add it to the DOM
	if (document.getElementById(layerId) == null) {

		var element = document.createElement('div');
		element.setAttribute('id', layerId);
		element.setAttribute('class', 'iLayer');
		document.getElementById('iGroup').appendChild(element);
	}
}

window.mobiChow.getCurrentPosition = function(success_callback, error_callback) {
	if (geo_position_js && geo_position_js.init()) {
		geo_position_js.getCurrentPosition(success_callback, error_callback);
	} else {
		error_callback();
	}
}

window.mobiChow.addCss = function(cssCode) {
	var styleElement = document.createElement("style");
	styleElement.type = "text/css";
	if (styleElement.styleSheet) {
		styleElement.styleSheet.cssText = cssCode;
	} else {
		styleElement.appendChild(document.createTextNode(cssCode));
	}
	document.getElementsByTagName("head")[0].appendChild(styleElement);
}

window.mobiChow.is_geolocation_available = function() {
	return geo_position_js && geo_position_js.init();
};

if (window.mobiChow.is_geolocation_available()) {
	window.mobiChow.addCss('.geolocation { display: block; }');
}