﻿/*
  Copyright 2007 Paperheads, All Rights Reserved.
  http://www.paperheads.co.uk
*/

if(typeof(Type) !== 'undefined') {
  Type.registerNamespace("PH");
}
else {
  PH = function() {}
}


PH.CheckSearch = function(e, sSearchUrl, searchID) {
  var charCode;
  if(window.event) { 
    charCode = e.keyCode;
  } else if(e.which) {
    charCode = e.which;
  }
  
  if (charCode==13) {
    PH.Search(sSearchUrl, searchID);
    return false;
  } else {
    return true;
  }
}

PH.Search = function(sSearchUrl, searchID) {
  var sTerms = "";
  var sSearchTerms = $get(searchID).value;
  
  if(sSearchTerms == 'Search the site...') {
    sSearchTerms = '';
  }
 
  for (i=0;i<sSearchTerms.length;i++) {
    if (sSearchTerms.charAt(i) == " ") {
      sTerms += "+";
     } else {
      sTerms += sSearchTerms.charAt(i);
    }
  }

  sSearchUrl += "?terms=";
  sSearchUrl += escape(sTerms);
  
  window.location.href = sSearchUrl
}

PH.Transfer = function() {}
PH.Transfer.Images = function() {}
PH.Transfer.Images.init = function() {
  this._container = document.getElementById('projectImages').childNodes[0];
  this._container.style.left = '0px';

  this._indexes = new Array();
  this._index = 0;
  this._target = 0;
  this._moveInterval = 0;
  this._width = 0;

  var iStartLeft = this._getLeftPos(this._container);

  for(var i=0;i<this._container.childNodes.length;i++) {
    var oControl = this._container.childNodes[i];
    if(oControl.tagName.toLowerCase() == "img") {
      var iLeft = this._getLeftPos(oControl) - iStartLeft;
      this._indexes.push(iLeft);
      this._width = iLeft + oControl.offsetWidth;
    }
  }
}

PH.Transfer.Images.next = function() {
  if(!this._indexes) {
    this.init();
  }

  if(this._indexes.length <= 1) return;

  this._index += 1;
  
  if(this._index >= this._container.childNodes.length) {
    this._index = 0;
  }

  var iPos = this._indexes[this._index];

  if(iPos > this._width - 865 - 1) {
    iPos = this._width - 865 - 1;
  }

  this._moveTo(iPos * -1);
}

PH.Transfer.Images._moveTo = function(left) {
  if(this._target == left) return;

  this._target = left;
  if(this._moveInterval == 0) {
    this._moveInterval = setInterval('PH.Transfer.Images._moveThread();', 40);
  }
}

PH.Transfer.Images._moveThread = function() {
  var iCurrent = parseInt(this._container.style.left);
  var iNext = Math.round(iCurrent - ((iCurrent - this._target) / 5));

  if(this._target - 4 <= iNext && iNext <= this._target + 4) {
    iNext += (this._target < iCurrent) ? -1 : 1;
    if(iNext == this._target) {
      clearInterval(this._moveInterval);
      this._moveInterval = 0;
    }
  }
  
  this._container.style.left = iNext + 'px';
}

PH.Transfer.Images._getLeftPos = function(obj) {
  var iLeft = obj.offsetLeft;
  while(obj = obj.offsetParent) {
    iLeft += obj.offsetLeft;
  }
  return iLeft
}

PH.Carpathian = function() {}
PH.Carpathian.toggleHistory = function(aNode) {
  for(var i=0;i<aNode.parentNode.childNodes.length;i++) {
    var oNode = aNode.parentNode.childNodes[i];
    if(oNode.nodeName.toLowerCase() == 'ul') {
      if(oNode.style.display == "none") {
          oNode.style.display = "block";
      } else {
          oNode.style.display = "none";
      }
    }
  }
};

PH.Carpathian.setHeader = function(value) {
  var oImage = document.getElementById('image')
  oImage.style.backgroundImage = 'url(/header/head' + value + '.gif)';
};
PH.Carpathian.showFlash = function(flashUrl) {
  if(FlashDetect.installed) {
      var oImage = document.getElementById('image');
      var sHTML = '<embed src="' + flashUrl + '" width="830" height="220" wmode="transparent" quality="high" loop="false" play="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>';
      oImage.innerHTML = sHTML;
  }
};

PH.Carpathian.setMapPosition = function(id, x, y) {
  document.getElementById(id + '_oX').value = x;
  document.getElementById(id + '_oY').value = y;
};

// Notify ScriptManager that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();