﻿/*
  Copyright 2007 Paperheads, All Rights Reserved.
  http://www.paperheads.co.uk
*/

/***********************************
 *  Initialisation
 ***********************************/
function PH_previousReSize() {}
function PH_ScrollRegion_previousMouseDown() {}
function PH_ScrollRegion_previousMouseMove() {}
function PH_ScrollRegion_previousMouseUp() {}
function PH_ScrollRegion_previousSelectStart() {}

var PH_Initialised = false;
function pageLoad() {
  if(PH_Initialised) {
    return;
  }
  PH_Initialised = true;
  
  PH_previousReSize = window.onresize
  PH_ScrollRegion_previousMouseDown = document.onmousedown;
  PH_ScrollRegion_previousMouseMove = document.onmousemove;
  PH_ScrollRegion_previousMouseUp = document.onmouseup;
  PH_ScrollRegion_previousSelectStart = document.onselectstart;

  window.onresize = PH_OnResize;
  document.onmousemove = PH_ScrollRegion_OnMouseMove;
  document.onmouseup = PH_ScrollRegion_OnMouseUp;

  PH_SlideShow_oLink = document.getElementById('slideshow_link');
  PH_SlideShow_oButton = document.getElementById('slideshow_button');
  
  // Sets the opacity values to avoid invalid css.
  var oControl;
  if(oControl = document.getElementById('header_back'))  {oControl.style.MozOpacity=0.95;oControl.style.opacity=0.95;}
  if(oControl = document.getElementById('content_back')) {oControl.style.MozOpacity=0.95;oControl.style.opacity=0.95;}
  if(oControl = document.getElementById('footer'))       {oControl.style.MozOpacity=0.95;oControl.style.opacity=0.95;}

  PH_OnResize();

  document.getElementById('main').onmousedown = PH_SlideShow_BackClick;
  
  PH_SpotScroller_Initialise();

  //Start the slide show.
  if(PH_SlideShow_oImages) {
    PH_SlideShow_Toggle();
  }
}

/***********************************
 *  Slide Show
 ***********************************/

var PH_SlideShow_oImages = null;
var PH_SlideShow_iSelectedIndex = null;
var PH_SlideShow_iChangeInterval = 0;
var PH_SlideShow_oMain = null;
var PH_SlideShow_oLink = null;
var PH_SlideShow_oButton = null;

function PH_SlideShow_Clear() {
  PH_SlideShow_oImages = new Array();
  PH_SlideShow_oMain = document.getElementById('main');
  PH_SlideShow_iSelectedIndex = -1;
}

function PH_SlideShow_AddImage(Src, Title, Url) {
  var oImage = new Object();
  oImage.Src = Src;
  oImage.Url = Url;
  oImage.Title = Title;
  
  PH_SlideShow_oImages.push(oImage);
  
  //Initialise first Image
  if(PH_SlideShow_oImages && PH_SlideShow_oImages.length == 1 && !PH_Initialised) {
    PH_SlideShow_oMain.style.backgroundImage = 'url(' + oImage.Src + ')';
    PH_SlideShow_iSelectedIndex = -1;
  }
}

function PH_SlideShow_SelectImage(index) {
  if(!PH_SlideShow_oImages) {
    return;
  }
  
  if(index < 0) {
    index = PH_SlideShow_oImages.length - 1;
  } else if (index >= PH_SlideShow_oImages.length) {
    index = 0;
  }
  
  var oImage = PH_SlideShow_oImages[index];

  //Set the background image.
  PH_SlideShow_oMain.style.backgroundImage = 'url(' + oImage.Src + ')'; 
  
  //Highlight selected image.
  if(PH_SpotScroller) {
    if(PH_SlideShow_iSelectedIndex >= 0 && PH_SlideShow_iSelectedIndex < PH_SlideShow_oImages.length) {
      PH_SpotScroller.childNodes[PH_SlideShow_iSelectedIndex].className = '';
    }
    PH_SpotScroller.childNodes[index].className = 'selected';
  }
  
  //Show the project link.
  if(PH_SlideShow_oLink) {
    if(oImage.Url && oImage.Url.length > 0) {
      PH_SlideShow_oLink.innerHTML = '<a href="' + oImage.Url + '" title="' + oImage.Title.replace('"', '""') + '">' + oImage.Title + '</a>';
    } else {
      PH_SlideShow_oLink.innerHTML = oImage.Title;
    }
  }
  
  PH_SlideShow_iSelectedIndex = index;
  
  //Preload the next image.
  if(index < PH_SlideShow_oImages.length - 1) {
    var oNextImage = PH_SlideShow_oImages[index + 1];
    if(!oNextImage.PreLoad) {
      oNextImage.PreLoad = new Image(1000,680);
      oNextImage.PreLoad.src = oNextImage.Src; 
    }
  }

  //Reset auto slide.
  if(PH_SlideShow_iChangeInterval != 0) {
    clearTimeout(PH_SlideShow_iChangeInterval);
    PH_SlideShow_iChangeInterval = setTimeout(PH_SlideShow_ChangeThread, 10000);
  }
}

function PH_SlideShow_Toggle() {
  if(!PH_SlideShow_oImages) {
    return;
  }
  
  if(PH_SlideShow_iChangeInterval == 0) {
    PH_SlideShow_NextImage();
    PH_SlideShow_iChangeInterval = setTimeout(PH_SlideShow_ChangeThread, 10000);
    PH_SlideShow_oButton.className = 'pause';
    PH_SlideShow_oButton.title = 'Pause';
  } else {
    clearTimeout(PH_SlideShow_iChangeInterval);
    PH_SlideShow_iChangeInterval = 0;
    PH_SlideShow_oButton.className = 'play';
    PH_SlideShow_oButton.title = 'Play';
  }
}

function PH_SlideShow_NextImage() {
  PH_SlideShow_SelectImage(PH_SlideShow_iSelectedIndex + 1);
}

function PH_SlideShow_LastImage() {
  PH_SlideShow_SelectImage(PH_SlideShow_iSelectedIndex - 1);
}

function PH_SlideShow_ChangeThread() {
  PH_SlideShow_NextImage();
}

function PH_SlideShow_BackClick(e) {
  var oOverlay = document.getElementById('overlay');
  if(oOverlay.className == 'hide') {
    var oMouse = PH_GetMouse(e);  
    var oBounds = PH_GetBounds(document.getElementById('footer'));  
    if(oMouse.Y < oBounds.top && oOverlay.className == 'hide') {
      PH_ToggleNavigation();
    }
  }
}

/***********************************
 *  Spot Scroller
 ***********************************/

var PH_SpotScroller_MoveSpeed = 0;
var PH_SpotScroller_MoveInterval = 0;
var PH_SpotScroller = null;
var PH_SpotScroller_Left = null;
var PH_SpotScroller_Right = null;

function PH_SpotScroller_Initialise() {

  var oControl;
  if(oControl = document.getElementById('spots_back'))   {oControl.style.MozOpacity=0.95;oControl.style.opacity=0.95;}

  if(PH_SpotScroller = document.getElementById('spots')) {
    PH_SpotScroller_Left = document.getElementById('spot_move_left');
    PH_SpotScroller_Right = document.getElementById('spot_move_right');
    PH_SpotScroller.onmousemove = PH_SpotScroller_MouseMove;
    PH_SpotScroller.onmouseout = PH_SpotScroller_MouseOut;

    if(PH_SpotScroller.scrollWidth - 4 > PH_SpotScroller.offsetWidth) {
      PH_SpotScroller_Right.style.display = 'block';
    }
  } else {
    PH_SpotScroller = null;
    PH_SpotScroller_Left = null;
    PH_SpotScroller_Right = null;
  }
}

function PH_SpotScroller_MouseMove(e) {
  var oMouse  = PH_GetMouse(e);
  var oBounds = PH_GetBounds(PH_SpotScroller);
  var iMouseOffset = oMouse.X - oBounds.left - 1;

  //Determine scroll speed and direction.
  if(iMouseOffset < 100 && PH_SpotScroller.scrollLeft > 0) {
    PH_SpotScroller_MoveSpeed = ((iMouseOffset - 100) / 5);
  } else if (iMouseOffset > PH_SpotScroller.offsetWidth - 100 && PH_SpotScroller.scrollLeft < (PH_SpotScroller.scrollWidth - PH_SpotScroller.offsetWidth)) {
    PH_SpotScroller_MoveSpeed = ((PH_SpotScroller.offsetWidth - iMouseOffset - 100) / 5) * -1;
  } else {
    PH_SpotScroller_MoveSpeed = 0;
  }
  
  //Start Movement Thread if not already running.
  if(PH_SpotScroller_MoveInterval == 0 && PH_SpotScroller_MoveSpeed != 0) {
    PH_SpotScroller_MoveInterval = setTimeout(PH_SpotScroller_MoveThread, 20);
  }
}

function PH_SpotScroller_MouseOut(e) {
  var oMouse  = PH_GetMouse(e);
  var oBounds = PH_GetBounds(PH_SpotScroller);
  
  //Determine if the mouse has left the scrolling area.
  var iMouseOffsetX = oMouse.X - oBounds.left - 1;
  var iMouseOffsetY = oMouse.Y - oBounds.top;
  if(iMouseOffsetX <= 0 || iMouseOffsetX >= oBounds.width || 
     iMouseOffsetY <= 0 || iMouseOffsetY >= oBounds.height) {
    PH_SpotScroller_MoveSpeed = 0;
  }
}

function PH_SpotScroller_MoveThread() {
  if(  PH_SpotScroller_MoveSpeed == 0 || 
      (PH_SpotScroller_MoveSpeed < 0 && PH_SpotScroller.scrollLeft <= 0) ||
      (PH_SpotScroller_MoveSpeed > 0 && PH_SpotScroller.scrollLeft >= (PH_SpotScroller.scrollWidth - PH_SpotScroller.offsetWidth - 4))
    ) {
    PH_SpotScroller_MoveInterval = 0;
    return;
  }

  var iNewScroll = PH_SpotScroller.scrollLeft + Math.ceil(PH_SpotScroller_MoveSpeed);  
  if(iNewScroll > (PH_SpotScroller.scrollWidth - PH_SpotScroller.offsetWidth - 4)) {
    iNewScroll = (PH_SpotScroller.scrollWidth - PH_SpotScroller.offsetWidth - 4)
  }

  PH_SpotScroller.scrollLeft = iNewScroll;
  PH_SpotScroller_MoveInterval = setTimeout(PH_SpotScroller_MoveThread, 20);
  
  var iPercent = 0;
  if(PH_SpotScroller.scrollLeft > 0) {
    if(PH_SpotScroller.scrollLeft >= 100) {
      iPercent = 100;
    } else {
      iPercent = PH_SpotScroller.scrollLeft;
    }

    PH_SpotScroller_Left.style.filter = 'alpha(opacity=' + iPercent + ')';
    PH_SpotScroller_Left.style.MozOpacity = (iPercent/100);
    PH_SpotScroller_Left.style.opacity = (iPercent/100);
    PH_SpotScroller_Left.style.display = 'block';
  } else {
    PH_SpotScroller_Left.style.display = 'none';
  }

  var iOffset = (PH_SpotScroller.scrollWidth - PH_SpotScroller.offsetWidth - PH_SpotScroller.scrollLeft);
  if(iOffset > 0) {
    if(iOffset > 100) {
      iPercent = 100;
    } else {
      iPercent = iOffset;
    }

    PH_SpotScroller_Right.style.filter = 'alpha(opacity=' + iPercent + ')';
    PH_SpotScroller_Right.style.MozOpacity = (iPercent/100);
    PH_SpotScroller_Right.style.opacity = (iPercent/100);
    PH_SpotScroller_Right.style.display = 'block';
  } else {
    PH_SpotScroller_Right.style.display = 'none';
  }
}

/***********************************
 *  Show / Hide Navigation
 ***********************************/

function PH_ToggleNavigation() {
  var oOverlay = document.getElementById('overlay');
  var oLink = document.getElementById('nav_link');

  var oAdmin = document.getElementById('ctl00_oAdminMenu');
  
  if(oOverlay.className == 'hide') {
    var oBack = document.getElementById('content_back');
    if(oAdmin) oAdmin.style.display = oBack.style.display;
    oOverlay.className = null;
    oLink.innerHTML = 'Hide Navigation';
  } else {
    if(oAdmin) oAdmin.style.display = 'none';
    oOverlay.className = 'hide';
    oLink.innerHTML = 'Show Navigation';
  }
}

function PH_ToggleContent() {
  var oBack = document.getElementById('content_back');
  var oLink = document.getElementById('con_link');

  var oContent = document.getElementById('oScrollRegion');
  var oAdmin = document.getElementById('ctl00_oAdminMenu');
  var oAdminPanel = document.getElementById('ctl00_oPageContent_oPageLayout_oAdminPanel');

  if(oBack.style.display == 'none') {
    if(oContent) oContent.style.display = 'block';
    if(oAdmin) oAdmin.style.display = 'block';
    if(oAdminPanel) oAdminPanel.style.display = 'block';
    oBack.style.display = 'block';
    oLink.innerHTML = 'Hide Information';
  } else {
    if(oContent) oContent.style.display = 'none';
    if(oAdmin) oAdmin.style.display = 'none';
    if(oAdminPanel) oAdminPanel.style.display = 'none';
    oBack.style.display = 'none';
    oLink.innerHTML = 'Show Information';
  }
}

/***********************************
 *  Scroll Regions
 ***********************************/

var PH_ScrollRegion_oScrollRegions = null;
var PH_ScrollRegion_oSelectedScroll = null;
var PH_ScrollRegion_iMouseStartY = -1;

function PH_ScrollRegion_AddRegion(ID) {
  if(!PH_ScrollRegion_oScrollRegions) {
    PH_ScrollRegion_oScrollRegions = new Array();
  }

  var obj = document.getElementById(ID);
  if(obj){
    PH_ScrollRegion_oScrollRegions.push(new PH_ScrollRegion_ScrollArea(obj));
  }
}

/* 
 * OBJECT : ScrollArea 
 */
 
PH_ScrollRegion_ScrollArea = function(element) {
  this._element = element;
  this._mainArea = element.childNodes[0];

  this._scrollBar = null;
  this._scrollArea = null;
  
  this.CreateScrollBar();
}

PH_ScrollRegion_ScrollArea.prototype = {
  CreateScrollBar: function() {
    if(this._scrollArea != null) {
      return;
    }
    if(this._mainArea.scrollHeight <= this._mainArea.offsetHeight + 10) {
      return;
    }
    
    this._mainArea.style.overflow = 'hidden';
    this._mainArea.style.width = (this._mainArea.offsetWidth - 15) + 'px';
    this._mainArea._scrollObj = this;
    this._mainArea.onmousewheel = PH_ScrollRegion_OnMouseWheel;
    
    this._scrollArea = document.createElement('div');
    this._scrollArea.className = 'ScrollDiv';
    this._scrollArea.style.height = this._mainArea.offsetHeight + 'px';
    this._scrollArea.style.top = this._mainArea.offsetTop + 'px';
    this._scrollArea.style.left = (this._mainArea.offsetLeft + this._mainArea.offsetWidth + 10) + 'px';

    this._element.appendChild(this._scrollArea);

    this._scrollBar = document.createElement('div');
   
    this._scrollRatio = (this._mainArea.offsetHeight / this._mainArea.scrollHeight);
    var iScrollBarHeight = parseInt(this._mainArea.offsetHeight * this._scrollRatio)

    this._scrollBar.style.top = '0px';
    this._scrollBar.style.height = iScrollBarHeight + 'px';
    this._scrollArea.appendChild(this._scrollBar);
    
    this._scrollBar._scrollObj = this;
    this._scrollBar._maxTop = this._mainArea.offsetHeight - iScrollBarHeight;
    
    this._scrollBar.onmousedown = function() {
      document.onselectstart = function () { return false; }
      document.onmousedown = function () { return false; }
      
      PH_ScrollRegion_oSelectedScroll = this;
      this._startTop = parseInt(this.style.top);
    }
  },
  
  SetScrollBarPosition: function() {
    var iTop = this._mainArea.scrollTop * this._scrollRatio;
    if(iTop >= this._scrollBar._maxTop - 1) {
      iTop = this._scrollBar._maxTop;
    }
    this._scrollBar.style.top = iTop +'px';
  }
}

PH_ScrollRegion_OnMouseMove = function (e) {
  if(PH_ScrollRegion_oSelectedScroll != null) {
    var oMouse = PH_GetMouse(e);

    //Record start position
    if(PH_ScrollRegion_iMouseStartY < 0) {
      PH_ScrollRegion_iMouseStartY = oMouse.Y;
      return;
    }

    var iYmove = oMouse.Y - PH_ScrollRegion_iMouseStartY;
    var iNewTop = (PH_ScrollRegion_oSelectedScroll._startTop + iYmove);
    
    if(iNewTop < 0) {
      iNewTop = 0;
    } else if(iNewTop > PH_ScrollRegion_oSelectedScroll._maxTop) {
      iNewTop = PH_ScrollRegion_oSelectedScroll._maxTop;
    }
    
    PH_ScrollRegion_oSelectedScroll.style.top = iNewTop + 'px';
    PH_ScrollRegion_oSelectedScroll._scrollObj._mainArea.scrollTop = (iNewTop / PH_ScrollRegion_oSelectedScroll._scrollObj._scrollRatio);
  }
  
  if(PH_ScrollRegion_previousMouseMove) {
    return PH_ScrollRegion_previousMouseMove(e);
  } else {
    return true;
  }
}

PH_ScrollRegion_OnMouseUp = function (e) {
  if(PH_ScrollRegion_oSelectedScroll) {
    PH_ScrollRegion_oSelectedScroll = null;
    PH_ScrollRegion_iMouseStartY = -1;
  }

  (typeof(PH_ScrollRegion_previousSelectStart) !== 'undefined')?document.onselectstart=PH_ScrollRegion_previousSelectStart:document.onselectstart=null;
  (typeof(PH_ScrollRegion_previousSelectDown) !== 'undefined')?document.onmousedown=PH_ScrollRegion_previousSelectDown:document.onmousedown=null;
  
  if(PH_ScrollRegion_previousMouseUp) {
    return PH_ScrollRegion_previousMouseUp(e);
  } else {
    return true;
  }
}

PH_ScrollRegion_OnMouseWheel = function(e) {
  var delta = 0;
  if (!e) e = window.event;
  
  if (e.wheelDelta) {
    delta = e.wheelDelta/120;
    if (window.opera) delta = -delta;
  } else if (e.detail) {
    delta = -e.detail/3;
  }

  if (delta) {
    this.scrollTop += ((delta * -1) * 30);
    this._scrollObj.SetScrollBarPosition();
  };
  e.returnValue = false;
  return false;
}

/***********************************
 *  Screen Centering
 ***********************************/
function PH_OnResize() {
  var oCenterPanel = document.getElementById('main');
  var iHeight = oCenterPanel.offsetHeight / 2;
  var iWidth = oCenterPanel.offsetWidth / 2;
  var oBounds = PH_GetBounds(oCenterPanel.parentNode);

  if(oBounds.top - iHeight < 0) {
    oCenterPanel.style.top = (oBounds.top * -1) + "px";
  } else {
    oCenterPanel.style.top = (iHeight * -1) + "px";
  }
  
  if(oBounds.left - iWidth < 0) {
    oCenterPanel.style.left = (oBounds.left * -1) + "px";
  } else {
    oCenterPanel.style.left = (iWidth * -1) + "px";
  }
  
  //Call Previous Resize
  if(PH_previousReSize){PH_previousReSize();}
}

/***********************************
 *  Other Functions
 ***********************************/

/*
  Check if the form should be submitted when enter key pressed.
*/
function checkSubmit(e, linkButton) {
  var charCode;
  if(window.event) { 
    charCode = e.keyCode;
  } else if(e.which) {
    charCode = e.which;
  }

  if (charCode==13) {
    __doPostBack(linkButton, '');
    return false;
  } else {
    return true;
  }
}

function PH_GetBounds(obj) {
  var oBounds = new Object();
  oBounds.left = obj.offsetLeft;
  oBounds.top = obj.offsetTop;
  oBounds.width = obj.offsetWidth;
  oBounds.height = obj.offsetHeight;

  while(obj = obj.offsetParent) {
    oBounds.left += obj.offsetLeft;
    oBounds.top += obj.offsetTop;
  }
  return oBounds;
}

function PH_GetMouse(e) {
  var oPos = new Object();

  if (document.all) { // grab the x-y pos.s if browser is IE
    oPos.X = event.clientX + document.body.scrollLeft;
    oPos.Y = event.clientY + document.body.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    oPos.X = e.pageX;
    oPos.Y = e.pageY;
  }  
  
  // catch possible negative values in NS4
  if (oPos.X < 0){oPos.X = 0;}
  if (oPos.Y < 0){oPos.Y = 0;}
  
  return oPos
}

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
var PH_AJAX_Initialised = false;

if (typeof(Sys) !== 'undefined') 
{
  Sys.Application.add_load(function() {

    if (PH_AJAX_Initialised) {
      return ;
    }
    PH_AJAX_Initialised = true;

    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function(sender, args) {
      PH_SpotScroller_Initialise();
      var oPanels = args.get_panelsUpdated();
      for(var i=0;i<oPanels.length;i++) {
        PH_RunScript(oPanels[i].innerHTML);
      }
    });

    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function(sender, args) {
      var oControl = document.getElementById('ctl00_oAdminMenu');
      if(oControl) {oControl.style.MozOpacity=0.85;oControl.style.opacity=0.85;}
    });
  });

  Sys.Application.notifyScriptLoaded();
}

function PH_RunScript(text) {
  var lowerText = text.toLowerCase();
  var iScriptIndex = lowerText.indexOf('<script')
  var script;
  while(iScriptIndex >= 0) {
    iScriptIndex = lowerText.indexOf('>', iScriptIndex) + 1;
    script = text.substring(iScriptIndex, lowerText.indexOf('</script>', iScriptIndex)).trim();

    if(script.length > 5 && script.substring(0,5).trim() == '<!--') {
      script = script.substring(5);
    }
    
    try {
      eval(script);
    } catch(err) {
      alert('Error in loaded javascript:\n' + err.description);
      alert(text.substring(iScriptIndex, lowerText.indexOf('</script>', iScriptIndex)));
    }

    iScriptIndex = lowerText.indexOf('<script', iScriptIndex);
  }
}