﻿/*
  Copyright 2007 Paperheads, All Rights Reserved.
  http://www.paperheads.co.uk
*/

if(typeof(Type) !== 'undefined') {
  Type.registerNamespace("PH.BeAt");
}
else {
  PH = function() {}
  PH.BeAt = function() {}
}

PH.BeAt.SetBackground = function(Url) {
  var oBackDiv = document.body;

  if(oBackDiv.oBackImage) {
    var oParent = oBackDiv.oBackImage.parentNode;
    oParent.parentNode.removeChild(oParent);
    oBackDiv.oBackImage.onload = null;
    oBackDiv.oBackImage = null;
  }

  if(Url != 'none') {
    var oDiv = document.createElement("div");
    oDiv.style.zIndex = 0;
    oDiv.style.position = 'fixed';
    oDiv.style.top = "0px";
    oDiv.style.left = "0px";
    oDiv.style.overflow = 'hidden';
    oBackDiv.appendChild(oDiv)

    var oImage = document.createElement("img");
    oImage.onload = PH.BeAt.BackgroundLoaded;
    oImage.src = Url;
    oImage.style.position = 'absolute';
    oImage.style.top = "0px";
    oImage.style.left = "0px";
    oDiv.appendChild(oImage)

    oBackDiv.oBackImage = oImage;
  }
};

PH.BeAt.BackgroundLoaded = function() {
  var oImage = document.body.oBackImage;
  if(!oImage) return;

  oImage.iOriginalWidth = oImage.width;
  oImage.iOriginalHeight = oImage.height;
  PH.BeAt.PositionBackground();
}

PH.BeAt.PositionBackground = function() {
  var oImage = document.body.oBackImage;
  if(!oImage) return;

  var iScreenHeight = window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.innerHeight);
  var iScreenWidth  = window.innerWidth  ? window.innerWidth  : (document.documentElement.clientWidth  ? document.documentElement.clientWidth  : document.body.innerWidth);

  oImage.parentNode.style.width = iScreenWidth + 'px';
  oImage.parentNode.style.height = iScreenHeight + 'px';

  var iScreenAspect = iScreenWidth / iScreenHeight;
  var iImageAspect = oImage.iOriginalWidth / oImage.iOriginalHeight;

  var iTop = 0;
  var iLeft = 0;
  var iWidth = iScreenWidth;
  var iHeight = iScreenHeight;

  if(iImageAspect < iScreenAspect) {
    iHeight = Math.round(iWidth / iImageAspect);
    iTop = Math.round((iScreenHeight - iHeight) / 2);
  } else {
    iWidth = Math.round(iHeight * iImageAspect);
    iLeft = Math.round((iScreenWidth - iWidth) / 2);
  }

  oImage.style.width = iWidth + 'px';
  oImage.style.height = iHeight + 'px';
  oImage.style.left = iLeft + 'px';
  oImage.style.top = iTop + 'px';
}

PH.BeAt.WindowSize = window.onresize;

window.onresize = function() {
  PH.BeAt.PositionBackground();
  if(PH.BeAt.WindowSize) {
    PH.BeAt.WindowSize();
  }
}

PH.BeAt.ShowFlash = function(ID, Path, Width, Height) {
  var oContainer = $get(ID);
  var sOutput = '';

  if(navigator.appName.indexOf("Microsoft") != -1) {
    sOutput = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+Width+'" height="'+Height+'" alowFullscreen="true"><param name="movie" value="'+Path+'" /><param name="quality" value="high" /><param name="allowFullScreen" value="true" /></object>'
    //sOutput = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+Width+'" height="'+Height+'"><param name="movie" value="'+Path+'" /><param name="quality" value="high" /><param name="allowscriptaccess" value="true" /><param name="wmode" value="transparent" /><param name="swliveconnect" value="true" /></object>'
  } else {
    sOutput = '<embed src="'+Path+'" width="'+Width+'" height="'+Height+'" quality="high" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?p1_prod_version=shockwaveflash"></embed>'
    //sOutput = '<embed src="'+Path+'" width="'+Width+'" height="'+Height+'" quality="high" allowScriptAccess="true" wmode="transparent" swLiveConnect="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?p1_prod_version=shockwaveflash"></embed>'
  }

  oContainer.innerHTML = sOutput;
}

PH.BeAt.GetFlashContent = function() {
  var oPlayer = $get('flash_player').childNodes[0];;
  var sContent = $get('flash_content').innerHTML;

  alert(oPlayer);
  alert(sContent);

  oPlayer.SetPlayerContent(sContent);
}

PH.FlashPage = function(appPath, url, query) {
    window.location.replace(appPath + '/#' + url + '/' + query);
}

PH.CheckSearch = function(e, sSearchUrl) {
  var charCode;
  if(window.event) { 
    charCode = e.keyCode;
  } else if(e.which) {
    charCode = e.which;
  }

  if (charCode==13) {
    PH.Search(sSearchUrl);
  } else {
    return true;
  }
}

PH.Search = function(sSearchUrl) {
  var sTerms = "";
  var oSearchTerms = $get('oSearchTerms');
 
  for (i=0;i<oSearchTerms.value.length;i++) {
    if (oSearchTerms.value.charAt(i) == " ") {
      sTerms += "+";
     } else {
      sTerms += oSearchTerms.value.charAt(i);
    }
  }

  sSearchUrl += "?terms=";
  sSearchUrl += escape(sTerms);
  
  window.location.href = sSearchUrl
}

// Notify ScriptManager that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
