function isInside(e, p) {
  var c = e;
  while ((c != p) && (c != document.body)) c = c.parentNode;
  return (c == p);
};

function MouseoverListener() {

  this.mouseover = function (e) {
    var src = getEventSource(e); e = getEvent(e);
    if (src) {
      var oldActiveElement = document.currentActiveElement;
      if ((src.className.indexOf('active') >= 0)) {
        if (src.className.indexOf('active mouseover') < 0) {  
          src.className = src.className.replace(/active/g, 'active mouseover');
          document.currentActiveElement = src;
        };
        stopProp(e);
      };
      if ((oldActiveElement) && (!isInside(src, oldActiveElement))) {
        oldActiveElement.className = oldActiveElement.className.replace(/active mouseover/g, 'active');
        if (document.currentActiveElement != src) document.currentActiveElement = null;
      };
    };
  };

  addEvent(document.body, 'mouseover', createEventDelegate(this, this.mouseover), true);

};

function slideShow(settings) {
  this.teaser = _('teaser');
  this.backdrops = [];

  this.iev = getInternetExplorerVersion();
  this.simple = ((this.iev) && (this.iev < 9));
  for (var attr in settings) this[attr] = settings[attr];
  if (!this.wait) this.wait = 8;
  if (!this.speed) this.speed = 2;


  this.teaser.reposition = function () {
    var wi = windowInfo();
    this.style.top = (wi.height - this.clientHeight - 38) + 'px';
    this.style.left = (wi.width - this.clientWidth) / 2 + 'px';
  };

  document.reposition.add(this.teaser);

  this.add = function (bd) {
    this.backdrops[this.backdrops.length] = bd;

    addEvent(bd.button, 'click', createDelegate(this, this.flipto, this.backdrops.length-1), 'manual');

  };

  this.flipto = function(i) {
    if (!((this.loading) && (!this.backdrops[this.current].teaserLoaded))) {
      if (this.timer) { window.clearTimeout(this.timer); this.timer = null }
      if (i == this.current) {
        this.done(!this.paused);
      } else {
        this.load(i, this.current, 'manual');
      };
    };
  };

  this.load = function (i, pre, mode) {
    this.loading = true;
    this.current = i;

    setCookie('cms.front.slideshow.index', this.current);

    this.backdrops[i].load(createDelegate(this, this.done), (arguments.length > 1 ? this.backdrops[pre] : null), mode, this.simple, this.speed);
  };

  this.done = function (mode) {
    this.loading = false;
    this.paused = (mode == 'manual');

    this.backdrops[this.current].button.className = (this.paused ? 'button selected paused' : 'button selected');

    if (!this.paused) this.timer = window.setTimeout(createDelegate(this, this.next), this.wait * 1000);
  };

  this.next = function () {
    this.load((this.current < this.backdrops.length -1 ? this.current+1 : 0), this.current)
  };

  this.start = function () {
    var initial = getCookie('cms.front.slideshow.index');
    if (initial == null) {
      initial = 0;
      //initial = Math.floor(Math.random() * this.backdrops.length);
    } else {
      initial = parseInt(initial);
    };

    this.load(initial, null, 'initial');
  };
};

function Backdrop(settings) {

  this.settings = settings;

  this.button = document.createElement('div');
  this.button.className = 'button active';
  this.button.innerHTML = ' ';
  var div = document.createElement('div');
  div.innerHTML = ' ';
  this.button.appendChild(div);


  _('teaser_buttons').appendChild(this.button);


  this.kenburns = function (pos, done) {

    this.zoom = this.burnsInitial.zoom + (1.0 - this.burnsInitial.zoom) * pos[0];
    this.panx = this.burnsInitial.panx + (0.5 - this.burnsInitial.panx) * pos[0];
    this.pany = this.burnsInitial.pany + (0.5 - this.burnsInitial.pany) * pos[0];

    this.redraw();

    var pos_t = (this.mode=='initial' ? pos[0] : (pos[0] < 0.2 ? pos[0] / 0.2 : 1.0));

    setOpacity(this.container, 100.0 * pos_t);

    if ((pos[0] < 0.5) && (this.predecessor)) {
      setOpacity(_('teaser'), 100.0 * (1.0 - pos_t) * this.predecessor.teaseropacity);
    } else {
      if (!this.teaserLoaded) this.loadTeaser();
      setOpacity(_('teaser'), 100.0 * (pos[0] >= 0.8 ? (pos[0] - 0.8) / 0.2 : 0.0) * this.teaseropacity);
    };

    if (done) {
      if (this.predecessor) {
        this.predecessor.container.style.visibility = 'hidden';
        setOpacity(this.predecessor.container, 0);
      }

      if (done) this.callback(this.mode);

    };

  };

  this.teaserfadein = function (pos, done) {
    if (!this.teaserLoaded) this.loadTeaser();

    setOpacity(_('teaser'), 100.0 * pos[0] * this.teaseropacity);

    if (done) this.callback(this.mode);
  };

  this.click = function () {
    document.location.href = this.settings.href;
  };

  this.loadTeaser = function () {
    var teaser = _('teaser');

    _('teaser_caption').innerHTML = this.settings.caption;
    _('teaser_caption').href = this.settings.href;
    _('teaser_text').innerHTML = this.settings.text;

    if (_('teaser_credits')) _('teaser_credits').innerHTML = (this.settings.credits ? this.settings.credits : '&#160;');

    $('#teaser_content').click(createDelegate(this, this.click));

    setOpacity(teaser, 0);

    teaser.style.display = 'block';

    if (this.predecessor) this.predecessor.button.className = 'button active';
    this.button.className = 'button selected';

    this.teaserLoaded = true;

    teaser.reposition();
  };

  this.load = function (callback, predecessor, mode, simple, speed) {
    this.callback = callback;
    this.predecessor = predecessor;
    this.mode = mode;
    this.simple = simple;
    this.speed = speed;
    this.teaseropacity = (this.settings.brightness ? this.settings.brightness : 0.6);

    if (!this.container) {
      this.container = document.createElement('div');
      this.container.className = 'bdContainer';

      document.body.insertBefore(this.container, document.body.firstChild);

      this.img = document.createElement('img');
      this.img.className = 'bdImage';

      this.img.onload = createDelegate(this, this.ready);

      this.img.src = this.settings.image;

      this.container.appendChild(this.img);

    } else {
      this.animate();
    };
  };

  this.ready = function () {
    this.nativeWidth = this.img.width;
    this.nativeHeight = this.img.height;

    this.animate();
  };

  this.animate = function () {

    if (this.simple) {
      this.zoom = 1.0; this.panx = 0.5; this.pany = 0.5;
    } else {
      switch (this.mode) {
        case 'manual': this.zoom = 0.6; this.panx = 0.5; this.pany = 0.5; break;
        case 'initial': this.zoom = 1.0; this.panx = 0.5; this.pany = 0.5; break;
        default:
          this.zoom = 0.7 + Math.random() * 0.2;
          this.panx = 0.5 + (Math.random() > 0.5 ? 1.0 : -1.0) * (0.3 + Math.random() * 0.2);
          this.pany = 0.5 + (Math.random() > 0.5 ? 1.0 : -1.0) * (0.3 + Math.random() * 0.2);
          break;
      };
    };

    this.redraw();

    setOpacity(this.container, 0);

    this.container.style.zIndex = -2;

    if (this.predecessor) this.predecessor.container.style.zIndex = -3;

    this.container.style.visibility = 'visible';

    document.reposition.add(createDelegate(this, this.redraw));

    this.burnsInitial = { zoom: this.zoom, panx: this.panx, pany: this.pany };
    this.teaserLoaded = false;
    var speed = (this.mode == 'manual' || this.simple ? 25 : (this.mode == 'initial' ? 50 : this.speed * 100));

    StartSlider([[0, 1]], createDelegate(this, this.kenburns), ease_Sinus, 10, speed, 'burns');
  };

  this.redraw = function () {

    var win = windowInfo(), Pw = this.nativeWidth, Ph = this.nativeHeight;

    if ((Pw / Ph) >= (win.width / win.height)) {
      Pw = (Pw * win.height) / Ph;
      Ph = win.height;
    } else {
      Ph = (Ph * win.width) / Pw;
      Pw = win.width;
    };

    this.container.style.left = Math.round((win.width - Pw / this.zoom) * this.panx) + 'px';
    this.container.style.top = Math.round((win.height - Ph / this.zoom) * this.pany) + 'px';
    this.img.width = Math.round(Pw / this.zoom); this.img.height = Math.round(Ph / this.zoom);

  };

};

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = 0; // Return value assumes failure.
  //debug(navigator.userAgent);
  if (navigator.appName == 'Microsoft Internet Explorer') {
    var ua = navigator.userAgent;
    var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat(RegExp.$1);
  }
  return rv;
}


function idxLoaded(rq) {
  var items = rq.responseXML.selectNodes('//object');
  var idxArray = document.idx.idxArray;

  var i = idxArray.find(rq.mode.objectid);
  if (i < 0) {
    idxArray.push(rq.mode);
    i = idxArray.length - 1;
  };

  var strhtml = '';
  var cmd = rq.responseXML.selectSingleNode('//command'), pObjectID = parseInt(cmd.getAttribute("objectid"))
  for (var c = 0; c != items.length; c++) {
    var item = items[c];
    var active = (objectinfo.typeid == parseInt(item.getAttribute('typeid'))), small = !active;
    var thumb = item.selectSingleNode('thumbnail');

    var data = (thumb ? ' data-mediaid="' + thumb.getAttribute('mediaid') + '"' : '');
    var onclick = (active ? 'idxGet(' + parseInt(item.getAttribute('objectid')) + ');return false' : '');
    var url = (item.getAttribute('alias') ? item.getAttribute('alias') : item.getAttribute('objectid'));
    var name = item.getAttribute('shortname');

    strhtml += '<div class="banner' + (small ? ' small' : '') + ' p' + pObjectID + '"' + data + '>' +
              '<div class="cover active">&#160;</div>' +
              '<a' + (onclick ? ' onclick="' + onclick + '"' : '') + ' href="' + url + '">' + name + '</a></div>';

    idxArray.push({ objectid: parseInt(item.getAttribute('objectid')), shortname: item.getAttribute('shortname'), loaded: false });
  };

  $('#isocontainer').isotope('insert', $(strhtml));

  idxUpdateBannerMedia();
  idxSetMouseovers('.p' + pObjectID + ' A');
  
  idxArray[i].loaded = true;
  idxDisplay(rq.mode.objectid);

};

function idxBack() {
  $.bbq.removeState();
};

function idxDisplay(objectid) {
  var idxArray = document.idx.idxArray, i = idxArray.find(objectid);

  if ((i >=0 ) && (idxArray[i].loaded)) {
    $('#iheader').text(idxArray[i].shortname);
    $('#isocontainer').isotope({ filter: '.p' + objectid });
    _('idxback').style.display = (objectid != objectinfo.objectid ? 'block' : 'none'); 
    return true
  } else { return false };

};

function idxGet(rqObjectid) {
  $.bbq.pushState({ p: rqObjectid })
};

function idxFetch(rqObjectid) {

  if (!idxDisplay(rqObjectid)) {
    var request = new Request(), command = request.addCommand('object', 'relations');
    request.setAttribute('cache', 'clear');
    command.setAttribute('objectid', rqObjectid);
    command.setAttribute('siteid', objectinfo.siteid);
    command.setAttribute('subsiteid', objectinfo.subsiteid);
    document.idx.cmsws.Post(request, null, idxLoaded, null, null, { objectid: rqObjectid});
  }

};


function idxSetMouseovers(selector) {
  $(selector).mouseover(function () { $(this).parent().find('.cover').addClass('mouseover') }).mouseout(function () { $(this).parent().find('.cover').removeClass('mouseover') });
};

function idxUpdateBannerMedia() {
  $('.banner').each(function (index) {
    $(this).css('background-image', 'url(/_/media/' + $(this).data('mediaid') + '&r=c&w=' + $(this).width() + '&h=' + $(this).height() + ')');   
  });
};

function idxInit() {
  document.idx = { cmsws: new ITSWebService('cmsservice.ashx'), idxArray: [objectinfo] };
  document.idx.idxArray[0].loaded = true;
  document.idx.idxArray.find = function (id) { for (i = 0; (i < this.length) && (this[i].objectid != id); i++); return (i < this.length ? i : -1) };
  idxUpdateBannerMedia();

  $('.banner').each(function (index) {
    document.idx.idxArray.push({ objectid: parseInt($(this).data('objectid')), shortname: $.trim($(this).text()), loaded: false });
  });

  $(function () {

    $('#isocontainer').isotope({
      itemSelector: '.banner'
    });

    idxSetMouseovers('.banner A');

  });

  $(window).bind('hashchange', function (e) {
    var state = $.bbq.getState();
    if (state.p) {
      idxFetch(parseInt(state.p));
    } else {
      idxFetch(objectinfo.objectid);
    };
  });

  $(window).trigger('hashchange');


  //installShortcut(document.body,65,function() {$('#isocontainer').isotope('shuffle')});

};


/*
* jQuery BBQ: Back Button & Query Library - v1.2.1 - 2/17/2010
* http://benalman.com/projects/jquery-bbq-plugin/
* 
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function ($, p) { var i, m = Array.prototype.slice, r = decodeURIComponent, a = $.param, c, l, v, b = $.bbq = $.bbq || {}, q, u, j, e = $.event.special, d = "hashchange", A = "querystring", D = "fragment", y = "elemUrlAttr", g = "location", k = "href", t = "src", x = /^.*\?|#.*$/g, w = /^.*\#/, h, C = {}; function E(F) { return typeof F === "string" } function B(G) { var F = m.call(arguments, 1); return function () { return G.apply(this, F.concat(m.call(arguments))) } } function n(F) { return F.replace(/^[^#]*#?(.*)$/, "$1") } function o(F) { return F.replace(/(?:^[^?#]*\?([^#]*).*$)?.*/, "$1") } function f(H, M, F, I, G) { var O, L, K, N, J; if (I !== i) { K = F.match(H ? /^([^#]*)\#?(.*)$/ : /^([^#?]*)\??([^#]*)(#?.*)/); J = K[3] || ""; if (G === 2 && E(I)) { L = I.replace(H ? w : x, "") } else { N = l(K[2]); I = E(I) ? l[H ? D : A](I) : I; L = G === 2 ? I : G === 1 ? $.extend({}, I, N) : $.extend({}, N, I); L = a(L); if (H) { L = L.replace(h, r) } } O = K[1] + (H ? "#" : L || !K[1] ? "?" : "") + L + J } else { O = M(F !== i ? F : p[g][k]) } return O } a[A] = B(f, 0, o); a[D] = c = B(f, 1, n); c.noEscape = function (G) { G = G || ""; var F = $.map(G.split(""), encodeURIComponent); h = new RegExp(F.join("|"), "g") }; c.noEscape(",/"); $.deparam = l = function (I, F) { var H = {}, G = { "true": !0, "false": !1, "null": null }; $.each(I.replace(/\+/g, " ").split("&"), function (L, Q) { var K = Q.split("="), P = r(K[0]), J, O = H, M = 0, R = P.split("]["), N = R.length - 1; if (/\[/.test(R[0]) && /\]$/.test(R[N])) { R[N] = R[N].replace(/\]$/, ""); R = R.shift().split("[").concat(R); N = R.length - 1 } else { N = 0 } if (K.length === 2) { J = r(K[1]); if (F) { J = J && !isNaN(J) ? +J : J === "undefined" ? i : G[J] !== i ? G[J] : J } if (N) { for (; M <= N; M++) { P = R[M] === "" ? O.length : R[M]; O = O[P] = M < N ? O[P] || (R[M + 1] && isNaN(R[M + 1]) ? {} : []) : J } } else { if ($.isArray(H[P])) { H[P].push(J) } else { if (H[P] !== i) { H[P] = [H[P], J] } else { H[P] = J } } } } else { if (P) { H[P] = F ? i : "" } } }); return H }; function z(H, F, G) { if (F === i || typeof F === "boolean") { G = F; F = a[H ? D : A]() } else { F = E(F) ? F.replace(H ? w : x, "") : F } return l(F, G) } l[A] = B(z, 0); l[D] = v = B(z, 1); $[y] || ($[y] = function (F) { return $.extend(C, F) })({ a: k, base: k, iframe: t, img: t, input: t, form: "action", link: k, script: t }); j = $[y]; function s(I, G, H, F) { if (!E(H) && typeof H !== "object") { F = H; H = G; G = i } return this.each(function () { var L = $(this), J = G || j()[(this.nodeName || "").toLowerCase()] || "", K = J && L.attr(J) || ""; L.attr(J, a[I](K, H, F)) }) } $.fn[A] = B(s, A); $.fn[D] = B(s, D); b.pushState = q = function (I, F) { if (E(I) && /^#/.test(I) && F === i) { F = 2 } var H = I !== i, G = c(p[g][k], H ? I : {}, H ? F : 2); p[g][k] = G + (/#/.test(G) ? "" : "#") }; b.getState = u = function (F, G) { return F === i || typeof F === "boolean" ? v(F) : v(G)[F] }; b.removeState = function (F) { var G = {}; if (F !== i) { G = u(); $.each($.isArray(F) ? F : arguments, function (I, H) { delete G[H] }) } q(G, 2) }; e[d] = $.extend(e[d], { add: function (F) { var H; function G(J) { var I = J[D] = c(); J.getState = function (K, L) { return K === i || typeof K === "boolean" ? l(I, K) : l(I, L)[K] }; H.apply(this, arguments) } if ($.isFunction(F)) { H = F; return G } else { H = F.handler; F.handler = G } } }) })(jQuery, this);
/*
* jQuery hashchange event - v1.2 - 2/11/2010
* http://benalman.com/projects/jquery-hashchange-plugin/
* 
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function ($, i, b) { var j, k = $.event.special, c = "location", d = "hashchange", l = "href", f = $.browser, g = document.documentMode, h = f.msie && (g === b || g < 8), e = "on" + d in i && !h; function a(m) { m = m || i[c][l]; return m.replace(/^[^#]*#?(.*)$/, "$1") } $[d + "Delay"] = 100; k[d] = $.extend(k[d], { setup: function () { if (e) { return false } $(j.start) }, teardown: function () { if (e) { return false } $(j.stop) } }); j = (function () { var m = {}, r, n, o, q; function p() { o = q = function (s) { return s }; if (h) { n = $('<iframe src="javascript:0"/>').hide().insertAfter("body")[0].contentWindow; q = function () { return a(n.document[c][l]) }; o = function (u, s) { if (u !== s) { var t = n.document; t.open().close(); t[c].hash = "#" + u } }; o(a()) } } m.start = function () { if (r) { return } var t = a(); o || p(); (function s() { var v = a(), u = q(t); if (v !== t) { o(t = v, u); $(i).trigger(d) } else { if (u !== t) { i[c][l] = i[c][l].replace(/#.*/, "") + "#" + u } } r = setTimeout(s, $[d + "Delay"]) })() }; m.stop = function () { if (!n) { r && clearTimeout(r); r = 0 } }; return m })() })(jQuery, this);


