var DISABLESPI = false;
function Spi()
{
}
Spi.prototype = 
{
  editMode : false,

  checkForHash: function()
  {
    if (DISABLESPI)
      return;
    var url = document.location.href;
    var pos = url.indexOf("#");
    if (pos == -1)
      return;

    url = url.substr(pos + 1);
    document.getElementById("iframebottom").setAttribute("src", url);
  },

  checkForFrames: function(contentFrameRequired, id2, editMode)
  {       
    if (DISABLESPI)
      return;
    if (window.name.indexOf("popup") != -1)
      return;
      
    var url = document.location.href.substr(7);
    url = url.substr(url.indexOf("/") + 1).replace(/#.*$|\/$/, "");
    
    if(url.indexOf("opwegnaar/composities") != -1 && url.indexOf("&frameless=true") != -1)
    return;
    
    if (contentFrameRequired)
    {
      if (!editMode)
        this.checkForContentFrame(url);
      return;
    }      
    if (window.name != "iframebottom")
      document.location.href = "/#" + url;
    else
      this.loadFrames(url);
  },

  checkForContentFrame: function(url)
  {
    var insideContentFrame = (window.name == "iframecontent");

    // special fix for loading a concert item
    if (url.indexOf("concertId") != -1)
    {
      this.loadConcertFrame(url, insideContentFrame);
      return;
    }
    
    url = url.replace(/iframe=true/g, "");
    this.updateHash(url);
    
    if (insideContentFrame)
      return;

    document.location.href = "/#" + url;
  },

  loadFrames: function(url)
  {
    this.updateHash(url);

    var topFrame = parent.document.getElementById("iframetop");
    var middleFrame = parent.document.getElementById("iframemiddle");

    var curTop = topFrame.getAttribute("src");    
    if (curTop != top)
      topFrame.setAttribute("src", top);

    var flashColor = middle.replace(/.*flashcolor=(\d+).*/g, "$1") * 1;    
    if (!isNaN(flashColor))
    {
      if (this.changeFlashColor(flashColor))
        return;
    }

    // set middle frame
    var curMiddle = middleFrame.getAttribute("src");
    if (curMiddle != middle)
      middleFrame.setAttribute("src", middle);
  },

  changeFlashColor: function(theColor)
  {     
    var middleFrame = parent.document.getElementById("iframemiddle");
    var f = middleFrame.contentWindow;
    if (f)
    {
      if (f.setFlashColor)
      {
        f.setFlashColor(theColor);
        return true;
      }
    }
    return false;
  },

  loadConcertFrame: function(url, alreadyInsideContentFrame)
  {
    var itemId = url.replace(/.*\?id=(\d+).*/, "$1") * 1;
    var concertId = url.replace(/.*concertId=(\d+).*/i, "$1") * 1;
    var tab = url.replace(/.*tab=(\d+).*/i, "$1") * 1;
    var isNieuws = (url.indexOf("nieuws") != -1);

    if (isNieuws)    
      url = "opwegnaar/?id=" + concertId + "&tab=" + tab + "&id3=" + itemId;    
    else
      url = "opwegnaar/?id=" + concertId + "&tab=" + tab + "&id2=" + itemId;
    
    this.updateHash(url);

    if (!alreadyInsideContentFrame)
      document.location.href = "/#" + url;
  },

  updateHash: function(url)
  {
    var w = parent;
    while (w.parent != w && w.spi)
      w = w.parent;
    w.document.location.hash = "#" + url.replace(/\&+$/,"");
  },

  checkFrameSize : function(frameName, size)
  {
    if (window.name != frameName)
      return;
    var frame = parent.document.getElementById(frameName);
    if (!frame)
      return;
    var td = frame.parentNode;

    if (size == 0 && td.parentNode.style.display == "none")
      return;

    td.parentNode.style.display = "block";
    var endCode = (size <= 1)? "el.parentNode.style.display='none';" : "";

    parent.animator.start({element:td,attribute:"height",target:size,duration:1000,endCode:endCode});
  }
}
var spi = new Spi();