﻿Array.prototype.find = function (searchStr) {    

    if (this.length <= 0) {
        return false;
    }

    var returnArray = false;
    var searchStrType = typeof (searchStr);

    for (i = 0; i < this.length; i++) {
        if (searchStrType == 'object'
            || searchStrType == 'function') {
            if (searchStr.test(this[i])) {
                if (!returnArray) { returnArray = [] }
                returnArray.push(i);
            }
        } else {
            if (this[i] === searchStr) {
                if (!returnArray) { returnArray = [] }
                returnArray.push(i);
            }
        }
    }
    return returnArray;
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

(function(document){

    window.mobiChow = window.mobiChow || {}; 

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();
	}
}

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);
}

//http://www.ejeliot.com/blog/109
mobiChow.loadScript = function (sScriptSrc, oCallback) {
    var oHead = document.getElementsByTagName('head')[0];
    var oScript = document.createElement('script');
    oScript.type = 'text/javascript';
    oScript.src = sScriptSrc;
    oHead.appendChild(oScript);

    // most browsers
    oScript.onload = oCallback;

    // IE 6 & 7
    oScript.onreadystatechange = function () {
        if (this.readyState == 'complete') {
            oCallback();
        }
    }

    oHead.appendChild(oScript);
}



// iPhone Scale Bug Fix, read this when using http://www.blog.highub.com/mobile-2/a-fix-for-iphone-viewport-scale-bug/
MBP.scaleFix();

})(document);


function mWebAppLoad(evt) {

    // If the current hash is '' or '#' it's possible
    // that WebApp cleared it out so check the original hash
    // we may have captured and reload the context
    if (window.location.hash.length < 1
        && window.mobiChow.originalHash
        && window.mobiChow.originalHash.length > 2
        && window.mobiChow.originalHash.indexOf('#_') == 0) {

        evt.context = window.mobiChow.originalHash.split('/');
        // WebApp replaces '#_' w/ 'wa' for the layer id prefix
        evt.context[0] = 'wa' + evt.context[0].substr(2);
    }

    // First try to search based on the old scheme that was being used
    var url = mWebAppGetLoadUrlBasedOnOldFormat(evt);

    // 20111007RBP - TODO: Need to relocate to new URL if its in hash to preserve old URLs      
}

function mGetAction(evt, layerId, baseUrl, keys) {
    var targetLayer = evt.context[0];

    if (targetLayer.toLowerCase().indexOf(layerId.toLowerCase()) == -1) {
        return null;
    }

    if ((evt.context.length - 1) < keys.length) {
        // There are not enough values for the keys passed
        return null;
    }

    var findResults = keys.find(/format/i);
    if (!findResults
        || (evt.context[findResults[0] + 1].toLowerCase().indexOf('_WebApp'.toLowerCase()) != 0)) {
        return null;
    }

    return baseUrl + WA.Postable(keys, evt.context);
}
