var nowaitsVer = "20101229";
var nowaitsDomain = "http://nowaits.com/";
var nowaitsImgSrc = nowaitsDomain + "img/app/";
var nowaitsBookNowSrc = nowaitsImgSrc + "BookNow.png";
var nowaitsBookNowMOSrc = nowaitsImgSrc + "BookNowMO.png";
var nowaitsHost = null;
var nowaitsHostType = null;
var nowaitsIsMobile = (screen.width <= 480); // confirm("Mobile?");

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-8796064-2']);
_gaq.push(['_setDomainName', '.nowaits.com']);
_gaq.push(['_trackPageview']);
(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

function nowaitsInit() {
    nowaitsLoadScriptParams();

    if (!nowaitsIsMobile) {
        var proto = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www')
        if (document.createStyleSheet) {
            document.createStyleSheet(proto + '.nowaits.com/popup/nowaits.css');
        }
        else {
            var styles = "@import url('" + proto + ".nowaits.com/popup/nowaits.css');";
            var newSS = document.createElement('link');
            newSS.rel = 'stylesheet';
            newSS.href = 'data:text/css,' + escape(styles);
            document.getElementsByTagName("head")[0].appendChild(newSS);
        }
        var js = document.createElement('script'); js.type = 'text/javascript'; js.async = true;
        js.src = proto + '.nowaits.com/popup/Silverlight.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(js, s);
    }

	var nowaitsBookNow = document.getElementsByClassName('nowaitsBookNow');
	if (nowaitsBookNow) {
		var element;
		for(var i = 0; (element = nowaitsBookNow[i]) != null; i++) {
			element.onclick = function() { nowaitsOpen(0); }
		    element.onmouseout = function() { this.src = nowaitsBookNowSrc; }
		    element.onmouseover = function() { this.src = nowaitsBookNowMOSrc; }
		    element.onmouseout();
		}
	}
}
function nowaitsError(sender, args) {
    var appSource = "";
    if (sender != null && sender != 0) {
        appSource = sender.getHost().Source;
    }

    var errorType = args.ErrorType;
    var iErrorCode = args.ErrorCode;

    if (errorType == "ImageError" || errorType == "MediaError") {
        return;
    }

    var errMsg = "Unhandled Error in Silverlight Application " + appSource + "\n";

    errMsg += "Code: " + iErrorCode + "    \n";
    errMsg += "Category: " + errorType + "       \n";
    errMsg += "Message: " + args.ErrorMessage + "     \n";

    if (errorType == "ParserError") {
        errMsg += "File: " + args.xamlFile + "     \n";
        errMsg += "Line: " + args.lineNumber + "     \n";
        errMsg += "Position: " + args.charPosition + "     \n";
    }
    else if (errorType == "RuntimeError") {
        if (args.lineNumber != 0) {
            errMsg += "Line: " + args.lineNumber + "     \n";
            errMsg += "Position: " + args.charPosition + "     \n";
        }
        errMsg += "MethodName: " + args.methodName + "     \n";
    }

    throw new Error(errMsg);
}
function nowaitsClose() {
    if (nowaitsHost!=null) {
        if (Silverlight.isInstalled('1.0') || nowaitsHostType == "p") { nowaitsHost.style.display = "none"; nowaitsHost.innerHTML = ""; }
        else if (!nowaitsHost.closed) nowaitsHost.close();
	}
}
function nowaitsOpen(op) {
    if (nowaitsHost == null && !nowaitsIsMobile) {
        // Check if div already exists, if so set nowaitsType = "P"art of Page else nowaitsType = "A"pi
        nowaitsHost = document.getElementById("nowaitsControlHost");
        if (nowaitsHost != null) nowaitsHostType = "p";
        else {
            nowaitsHostType = "a";
            if (Silverlight.isInstalled('1.0')) {
                nowaitsHost = document.createElement("div");
                nowaitsHost.id = "nowaitsControlHost";
                nowaitsHost.className = "nowaitsHostFloating";
                document.body.appendChild(nowaitsHost);
                nowaitsCenter();
            }
        }
    }

    var params = "t=" + nowaitsHostType + "&b=" + nowaitsBID + "&o=" + op;
    if (nowaitsIsMobile) location.href = "http://m.nowaits.com?" + params;
    else {
        if (Silverlight.isInstalled('1.0')) {
            nowaitsHost.innerHTML =
		    '<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%"> \
		        <param name="source" value="http://nowaits.com/popup/ClientBin/AppointmentPopUp.xap?' + nowaitsVer + '" /> \
		        <param name="onError" value="nowaitsError" /> \
		        <param name="background" value="transparent" /> \
		        <param name="windowless" value="true" /> \
		        <param name="minRuntimeVersion" value="3.0.40624.0" /> \
		        <param name="autoUpgrade" value="true" /> \
		        <param name="enableHtmlAccess" value="true" /> \
		        <param name="initParams" value="' + params.replace(/&/g,",") + '" /> \
                <iframe style="position:absolute;top:0px;left:0px;height:350px;width:400px;border:0px;margin:0px;padding:0px;overflow:hidden;background-color:Transparent;" src ="http://nowaits.com/popup/SilverlightUpdate.htm" scrolling="no" frameborder="0" allowtransparency="true"></iframe> \
		    </object> \
		    <iframe id="_sl_historyFrame" style="position:absolute;top:0px;left:0px;visibility:hidden;height:0px;width:0px;border:0px;margin:0px;padding:0px;"></iframe>';
            nowaitsHost.style.display = "block";
        } else {
            if (nowaitsHostType == 'a') {
                if (nowaitsHost != null && !nowaitsHost.closed) nowaitsHost.close();
                nowaitsHost = window.open("http://m.nowaits.com?" + params, "", "location=0,status=0,toolbar=0,menubar=0,directories=0,resizable=0,scrollbars=0,width=400,height=350,left=" +
                                                    Math.round((screen.width - 400) / 2) + ",top=" + Math.round((screen.height - 350) / 3));
            } else {
                nowaitsHost.innerHTML =
                    '<iframe style="position:relative;top:0px;left:0px;height:350px;width:400px;border:0px;margin:0px;padding:0px;overflow:hidden;background-color:Transparent;" src ="http://m.nowaits.com?' + params + '" scrolling="no" frameborder="0" allowtransparency="true"></iframe>';
                nowaitsHost.style.display = "block";
            }
        }
    }
	return false;
}
function nowaitsCenter() {
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }

    var x = (myWidth - nowaitsHost.offsetWidth) / 2;
    var y = (myHeight - nowaitsHost.offsetHeight) / 3;

    nowaitsHost.style.left = (x >= 0) ? x.toString() + "px" : "0px";
    nowaitsHost.style.top = (y >= 0) ? y.toString() + "px" : "0px";
}
function nowaitsLoadScriptParams() {
    var scriptTags = document.getElementsByTagName("script");

    // This code is assumed to be in a file so the "src" attribute
    // is guaranteed to be present...no error-checking is needed
    var urlFrags = scriptTags[scriptTags.length - 1].src.split("#");

    var urlParams = [];
    if (urlFrags.length > 1) {
        urlParamRaw = urlFrags[1].split("/");
        if (urlParamRaw.length >= 2) {
            for (var param = 0; param < urlParamRaw.length; param += 2)
                if (urlParamRaw[param].toLowerCase()=='b')
                    nowaitsBID = (urlParamRaw.length >= param + 1 ? parseInt(unescape(urlParamRaw[param + 1])) : -1);
        }
    }
}
function _Appointment_OnClose() { nowaitsClose(); }

// Initialize
if (document.getElementsByClassName == undefined) {
    document.getElementsByClassName = function(className) {
        var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
        var allElements = document.getElementsByTagName("*");
        var results = [];
        var element;
        for (var i = 0; (element = allElements[i]) != null; i++) {
            var elementClass = element.className;
            if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
                results.push(element);
        }
        return results;
    }
}
nowaitsInit();
