
var jquery_url                    = "http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js";
var xdhs_utils_url                = "http://asset1.hotelsearch.com/javascripts/xdhs_utils.js?1327969418";
var xdhs_dialog_url               = "http://asset0.hotelsearch.com/javascripts/xdhs_dialog.js?1327969418";
var xdhs_jcal_url                 = "http://asset3.hotelsearch.com/javascripts/jCal.js?1327969418";
var xdhs_offers_calendar_url      = "http://asset1.hotelsearch.com/javascripts/jquery.offers_mashup_calendar.js?1327969418";
var xdhs_jquery_animate_clip_url  = "http://asset3.hotelsearch.com/javascripts/jquery/plugins/jquery.animate.clip.js?1327969418";
var xdhs_dd_belatedPNG_url        = "http://asset1.hotelsearch.com/javascripts/DD_belatedPNG.js?1327969418";
var xdhs_json_parse_url           = "http://asset3.hotelsearch.com/javascripts/json_parse.js?1327969418";
var offers_mashup_url             = "http://ws.hotelsearch.com/offers_mashup.js";
// services Url's
var reviews_summary_url           = "http://ws.hotelsearch.com/reviews_summary.js";
var map_mashup_url                = "http://ws.hotelsearch.com/map_mashup.js";
var xdhs_id                       = "XdHs";
var xdhs_pie_renderer_url         = "http://asset2.hotelsearch.com/javascripts/jqplot.pieRenderer.js?1327969418";
var xdhs_pie_jqplot_url           = "http://asset0.hotelsearch.com/javascripts/jquery.jqplot.js?1327969418";
var xdhs_pie_chart_url            = "http://asset0.hotelsearch.com/javascripts/pieChart.js?1327969418";
var xdhs_excanvas_url             = "http://asset0.hotelsearch.com/javascripts/excanvas.min.js?1327969418";

function removeHost(url) {
    return url.replace(/https*:\/\/[^\/]+/i,"")
}

if (window.location.protocol == "https:") {
    xdhs_utils_url      = removeHost(xdhs_utils_url);
    xdhs_dialog_url     = removeHost(xdhs_dialog_url);
    reviews_summary_url = removeHost(reviews_summary_url);
    jquery_url          = jquery_url.replace(/http:\/\//i, "https://");
}

function xdhsJqueryTag() {
    return xdhsScriptTag(jquery_url);
}

function xdhsScriptTag(tagSrc, random) {
    if (isLoaded(tagSrc)) {
      return '';
    }
    random = random || false;
    if (random) {
        tagSrc += "?" + Math.random();
    }
    return '<' + 'script src="' + tagSrc + '"></' + 'script>';
}

function isLoaded(src) {
    scripts = document.getElementsByTagName("script");
    for (var i = 0; i < scripts.length; i++) {
      if (scripts[i].getAttribute("src") != null && scripts[i].getAttribute("src").indexOf(src) != -1) {
        return true;
      }
    }
    return false;
}

function xdhsWrapperForIE6(jsCode){
    return '<!--[if IE 6]>' + jsCode + '<![endif]-->';
}

if (!services) {
    var services = {
        "reviews" : [],
        "offers"  : [],
        "maps"    : []
    }
}

// We use vanilla JavaScript because jQuery is not loaded at this point.
var scriptObj = document.getElementById(xdhs_id);
if (scriptObj) {
    var params = scriptObj.getAttribute("name");
    if (params) {
        var arrParams = params.split("|");
        if (arrParams.length) {
            var service = arrParams[0];
            var queryString = arrParams.length == 2 ? arrParams[1] : arrParams[2];
            var languageChorizo = queryString.match(new RegExp('language=(..).*'));
            var language = 'en';
            if (languageChorizo && languageChorizo.length == 2)
              language = languageChorizo[1];
            switch (service) {
              case "xdreviews": case "xdsurveys":
                review = {};
                review.service = service;
                review.queryString = queryString;
                review.language = language;
                review.xdhs_id = xdhs_id;
                services.reviews.push(review);
                break;
              case "map_mashup":
                map = {};
                map.service = service;
                map.queryString = queryString;
                map.language = language;
                map.xdhs_id = xdhs_id;
                services.maps.push(map);
                break;
              case "xdoffers":
                offer = {};
                offer.service = service;
                offer.queryString = queryString;
                offer.language = language;
                offer.xdhs_id = xdhs_id;
                services.offers.push(offer);
                break;
              default:
                alert("unkown service: " + service);
            }
            document.write(xdhsJqueryTag());
            document.write(xdhsScriptTag('http://asset1.hotelsearch.com/javascripts/xdhs.init.js?1327969418'));
            document.write(xdhsWrapperForIE6(xdhsScriptTag(xdhs_dd_belatedPNG_url)));
        }
    }
}

