var flashvars;
var urlStr = "http://www.myonlinereservations.com:82/Component/";
var attrs = { id: "ReservationWidget", name: "ReservationWidget", align: "middle" };
var params = { wmode: "transparent", bgcolor: "#000000", allowscriptaccess: "always", quality: "high" };

google.load("jquery", "1.3.2");
google.load("jqueryui", "1.7.2");
google.load("swfobject", "2.2");
google.setOnLoadCallback(initialize);

function SetUpMORWidget(args) {

    flashvars = { rid: args.busID, analyticsID: "args.XXXX", phone: args.phoneNum };   
    if (!args.noBtn) {
        //place the button in the current element where ever the client places the script
        document.write('<div id="divLaunch" style="vertical-align:top; " align="center">');
        document.write('<input type="button" id="btnLaunch" name="btnLaunch" value="Make a Reservation" onclick="showMORWidget(false);" />');
        document.write('</div>');
    }
}

function pageOnload() {
    //the component holder must be placed at the body level to render correctly in all browsers
    if (jQuery.browser.msie && parseFloat(jQuery.browser.version) < 8) {
        document.body.innerHTML += '<div id="compContainer" style="position: absolute; left: 0px; top: 0px; width: 100%; height: 100%;z-index:1000;"><div id="flashComp"></div><div id="htmlComp"></div></div><div id="notSupported"></div>';
    }
    else {
        var d1 = document.createElement('div');
        d1.setAttribute("id", "compContainer");
        d1.setAttribute("style", "position: absolute; left: 0px; top: 0px; width: 100%; height: 100%;z-index:1000;");
        var d2 = document.createElement('div');
        d2.setAttribute("id", "flashComp");
        var d3 = document.createElement('div');
        d3.setAttribute("id", "htmlComp");
        d1.appendChild(d3);
        d1.appendChild(d2);
        document.body.appendChild(d1);
    }
}

function initialize() {
    jQuery.noConflict();
    pageOnload();
    jQuery('#htmlComp').hide().addClass('smoothness').dialog(
        { autoOpen: false, modal: true, resizable: false, width: 766, height: 465, close:
        function(event, ui) {
            jQuery('#htmlComp').html(''); jQuery('#compContainer').hide()
        }
        });
    jQuery('#compContainer').hide();
    checkForMORAction();
}

function showMORWidget(useQueryString) {
    if (!useQueryString) {
        flashvars["action"] = null;
        flashvars["confirm"] = null;
    }
    jQuery('#compContainer').show();
    if (swfobject.hasFlashPlayerVersion("6.0.65")) {
        swfobject.embedSWF(urlStr + "ReservationWidget.swf", "flashComp", "100%", "100%", "6.0.65", "", flashvars, params, attrs);
    }
    else {
        var htmlURL = urlStr + "HTMLComponentHolder.aspx?rid=" + flashvars.rid;
        var iframe = '<iframe id="modalIframeId" width="100%" height="100%" marginWidth="0" marginHeight="0" frameBorder="0" />';
        jQuery('#htmlComp').html(iframe).dialog("open").parents(".ui-dialog:first").find(".ui-dialog-titlebar").hide();
        jQuery('#modalIframeId').attr('src', htmlURL);
    }
}

function hideComponent() {
    jQuery('#compContainer').hide();
    setTimeout(function() {
        swfobject.removeSWF("ReservationWidget");
        jQuery('#compContainer').append("<div id='flashComp' />");
    }, 100);
}

function getQuerystring(key) {
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]").toLowerCase();
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href.toLowerCase());
    if (qs == null)
        return "";
    else
        return qs[1];
}

function checkForMORAction() {
    var action = getQuerystring("action");
    var confirm = getQuerystring("confirm");
    //if no flash and an action, show alt message
    if (action.length > 0) {
        if (swfobject.hasFlashPlayerVersion("6.0.65")) {
            flashvars["action"] = action;
            flashvars["confirm"] = confirm;
            showMORWidget(true);
        }
        else {
            var notSupportedDiv = document.getElementById("notSupported");
            var btnLaunch = document.getElementById("btnLaunch");
            btnLaunch.style.display = "none";
            notSupportedDiv.style.display = "block";
        }
    }
}