/* CSS background image */
function newImage(url, cssClass, title) {
  var $div = $("<div>").css("background-image", "url(" + url + ")");
  if (cssClass) $div.addClass(cssClass);
  if (title) $div.attr("title", title);
  var html = $div.wrap("<div>").parent().html();
  $div.unwrap();
  return html;
}

var flash_contentVersion = 8;
var flash_contentPathSuffix = '/flash/';
var flash_defaultImage = '/www/images/common/1x1.gif';

/*
 FlashReplace is developed by Robert Nyman, http://www.robertnyman.com. License and downloads: http://code.google.com/p/flashreplace/
 */
var FlashReplace = {
  elmToReplace : null,
  flashIsInstalled : null,
  defaultFlashVersion : 8,
  replace : function (elmToReplace, src, id, width, height, version, params) {
    this.elmToReplace = document.getElementById(elmToReplace);
    this.flashIsInstalled = this.checkForFlash(version || this.defaultFlashVersion);
    if (this.elmToReplace && this.flashIsInstalled) {
      var obj = '<object' + ((window.ActiveXObject) ? ' id="' + id + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" data="' + src + '"' : '');
      obj += ' width="' + width + '"';
      obj += ' height="' + height + '"';
      obj += '>';
      var param = '<param';
      param += ' name="movie"';
      param += ' value="' + src + '"';
      param += '>';
      param += '';
      var extraParams = '';
      var extraAttributes = '';
      for (var i in params) {
        extraParams += '<param name="' + i + '" value="' + params[i] + '">';
        extraAttributes += ' ' + i + '="' + params[i] + '"';
      }
      var embed = '<embed id="' + id + '" src="' + src + '" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '"';
      var embedEnd = extraAttributes + '></embed>';
      var objEnd = '</object>';
      this.elmToReplace.innerHTML = obj + param + extraParams + embed + embedEnd + objEnd;
      return true;
    }
    return false;
  },

  checkForFlash : function (version) {
    this.flashIsInstalled = false;
    var flash;
    if (window.ActiveXObject) {
      try {
        flash = new ActiveXObject(("ShockwaveFlash.ShockwaveFlash." + version));
        this.flashIsInstalled = true;
      }
      catch(e) {
        // Throws an error if the version isn't available
      }
    }
    else if (navigator.plugins && navigator.mimeTypes.length > 0) {
      flash = navigator.plugins["Shockwave Flash"];
      if (flash) {
        var flashVersion = navigator.plugins["Shockwave Flash"].description.replace(/.*\s(\d+\.\d+)\s.*/, "$1");
        if (flashVersion >= version) {
          this.flashIsInstalled = true;
        }
      }
    }
    return this.flashIsInstalled;
  }
};

function flash_write(siteName, fileBaseName, width, height) {
  var path = '/site/' + siteName + flash_contentPathSuffix;
  var swfFilePath = path + fileBaseName + '.swf';
  var jpgFilePath = path + fileBaseName + '.jpg';

  document.write('<div class="flash-static" id="' + fileBaseName + '"></div>');

  if (!FlashReplace.replace(fileBaseName, swfFilePath, fileBaseName + '-id', width, height, flash_contentVersion,
  {
    wmode : "transparent",
    quality: "high",
    bgcolor: "#ffffff"
  })) {
    var elem = document.getElementById(fileBaseName);
    if (elem) {
      elem.innerHTML = '<img src="' + jpgFilePath + '" alt="Flash Movie" ' +
                       'border="0" width="' + width + '" height="' + height + '">';
    }
  }
}

function flash_sfId(filepath) {
  var x = 'x' + new Date().getTime() + 'x';

  for (var i = 0; i < filepath.length; ++i) {
    var c = filepath.charAt(i).toLowerCase();
    if ((c >= 0 && c <= 9) || (c >= 'a' || c <= 'z')) {
      x = x + c;
    } else {
      x = x + '_';
    }
  }

  return x;
}

function flash_component(swfFilePath, width, height) {
  var backupImage;
  var backupImageUrl;
  var containerIdForFlash;
  var flCompId = flash_sfId(swfFilePath);
  if (arguments.length > 3) {
    backupImage = arguments[3];
  }
  if (arguments.length > 4) {
    backupImageUrl = arguments[4];
  }
  if (arguments.length > 5) {
    containerIdForFlash = arguments[5];
  }
  document.write('<div class="flash-component" id="' + flCompId + '"></div>');
  if (!containerIdForFlash) containerIdForFlash = flCompId;
  if (!FlashReplace.replace(containerIdForFlash, swfFilePath, flCompId + '-id', width, height, flash_contentVersion,
  {
    wmode : "transparent",
    quality: "high",
    bgcolor: "#ffffff"
  })) {
    var elem = document.getElementById(containerIdForFlash);
    if (elem) {
      if (!backupImage || backupImage == '' || backupImage == 'null') {
        backupImage = flash_defaultImage;
      }
      var htmlSrc = '<img src="' + backupImage + '" alt="Flash Movie" ' +
                    'border="0" width="' + width + '" height="' + height + '">';

      if (!(!backupImageUrl || backupImageUrl == '' || backupImageUrl == 'null')) {
        htmlSrc = '<a href="' + backupImageUrl + '">' + htmlSrc + '</a>';
      }

      elem.innerHTML = htmlSrc;
    }
  }
}
function toggleRoomSelectors(sufix, id) {
  var nums = getObj(sufix + id).options[ getObj(sufix + id).selectedIndex ].value;
  nums = (+nums) - 1;

  var prefix = "roomR" + sufix;
  var maxNums = 8;

  for (var i = 0; i <= nums; i++) {
    displayElement(prefix + i);
    displayElement('c' + prefix + i);
  }

  for (var j = nums + 1; j < maxNums; j++) {
    hideElement(prefix + j);
    hideElement('c' + prefix + j);
  }

  if (!checkIfAnyRoomContainsChildren(sufix)) {
    invisibleElement(sufix + "HotelSearchBirthDate");
  } else {
    visibleElement(sufix + 'HotelSearchBirthDate');
  }
}

function checkIfAnyRoomContainsChildren(sufix) {
  var retVal = false;

  var prefix = "child" + sufix;
  var maxNums = getObj(sufix + 'numRooms').selectedIndex + 1;
  for (var i = 0; i < maxNums; i++) {
    var el = getObj(prefix + i);
    var index = el.selectedIndex;
    //ibelog("index: "+index);
    if (index > 0) {
      retVal = true;
    }
  }
  return retVal;
}

function toggleChildAgeSelectors(sufix, room, nums) {
  nums = (+nums);

  var prefixChild = "hotelChildAgeR";
  var preFixChildContainer = "hotelAgesR";
  var maxNums = 3 + 1;
  if (nums == 0) {
    //ibelog(preFixChildContainer + room )
    hideElement(preFixChildContainer + room);
    if (!checkIfAnyRoomContainsChildren(sufix)) {
      invisibleElement(sufix + "HotelSearchBirthDate");
    }
    return;
  }


  displayElement(preFixChildContainer + room);
  visibleElement(sufix + 'HotelSearchBirthDate');

  for (var i = 1; i <= nums; i++) {
    displayElement(prefixChild + room + i);
  }

  for (var j = 1 + nums; j < maxNums; j++) {
    hideElement(prefixChild + room + j);
  }
}// end toggleChildAgeSelectors

function hideElement(id) {
  var el = getObj(id);
  if (el) el.style.display = "none";
}
function displayElement(id) {
  var el = getObj(id);
  if (el) el.style.display = "";
}
function disableElement(id) {
  var el = getObj(id);
  if (el) el.disabled = true;
}
function enableElement(id) {
  var el = getObj(id);
  if (el) el.disabled = false;
}
function invisibleElement(id) {
  var el = getObj(id);
  if (el) el.style.visibility = "hidden";
}
function visibleElement(id) {
  var el = getObj(id);
  if (el) el.style.visibility = "visible";
}
function setCity(text, li, name) {
  var cityCodeInput = getObj(name);
  cityCodeInput.value = li.getAttribute('code');
  cityCodeInput.searchCityText = text;
}
function clearCity(input, name) {
  var cityCodeInput = getObj(name);
  if (cityCodeInput.searchCityText != input.value) {
    cityCodeInput.value = '';
  }
}
function clearCityId(input) {
  clearCity(input, 'cityId');
}

function setCityId(text, li) {
  setCity(text, li, 'cityId');
}

