﻿function getInstallId() {
	try {
		if (typeof (yontooAPI) == "undefined") {
			try {
				if (window.ActiveXObject) {
					yontooAPI = new ActiveXObject("YontooIEClient.Api");
				}
			}
			catch (ex) {
			}
		}

		if (typeof (yontooAPI) != "undefined") {
			var id = yontooAPI.getInstallId();
			return id;
        } else if (typeof (getInstalledClientId) != "undefined") {    //chrome
            return getInstalledClientId();
		} else {
			var idElement = findObj("YontooInstallID");
			return idElement.innerHTML;
		}
	}
	catch (ex) {
	}
	return "";
}


/**
* Attempts to detect the user's Yontoo Layers plugin GUID the specified number
* of times (or for the specified maximum amount of time). Calls one of the
* two specified callback functions depending on whether it succeeds or fails in
* detecting the GUID. 
*/
function checkInstallId(maxChecks, successFunction, failFunction, maxTime) {
    try {
        var clientInstallId = $.cookie('y2InstallId');                                              // Check to see if we already have the client GUID in a cookie...
        if (clientInstallId && successFunction) {
            eval(successFunction + "('" + clientInstallId + "');");                                 // If so then we're done so trigger the success callback with the GUID
            return;
        }

        if (!maxTime) maxTime = 10000;                                                              // Default the maximum amount of time we spend checking to 10 seconds

        if (typeof (checkInstallId.timedOut) === 'undefined') {
            checkInstallId.timedOut = false;                                                        // Initialize the timeout flag if this is our first time...
            checkInstallId.timeoutTimer = setTimeout("checkInstallId.timedOut = true", maxTime);    //      ... and start the timeout timer
        }
        else if (maxChecks <= 1 || checkInstallId.timedOut) {                                       // If we've spent enough time trying...
            if (!checkInstallId.timedOut) clearTimeout(checkInstallId.timeoutTimer);                //      ... stop the timeout timer if necessary...
            if (failFunction) eval(failFunction + "()");                                            //      ... and trigger the fail callback
            return;
        }
        
        clientInstallId = getInstallId(); 													        // Otherwise attempt to get the GUID
        if (clientInstallId) {                                                                      // If we got something...
            $.cookie('y2InstallId', clientInstallId, { expires: 1 }); 					            //      ... remember it for the next 24 hours														
            if (successFunction) eval(successFunction + "('" + clientInstallId + "');"); 	        //		... and return success
            return;
        }
        
        setTimeout(function () {                                                                    // Otherwise, in another tenth of a second, try again.
            checkInstallId(maxChecks--, successFunction, failFunction, maxTime);
        }, 100);
    }
    catch (ex) {
        //alert(ex);																		        //	Finally, if something went wrong in the above code...
        if (failFunction) eval(failFunction + "()");										        //	... again, trigger the callback naked (without a param)
    }
};

