if (document.all && navigator.userAgent.indexOf("Macintosh")>=0)
	var undefined;

if (!(!document.getElementsByTagName || !document.getElementById)) {
  document.write(spvStyles());

  var _onload = window.onload;
  if (typeof _onload != 'function') 
    window.onload = spvInsert;
  else {
    window.onload = function() {
      _onload();
      spvInsert();
    }
  }
}

function spvStyles() {
  var cssText = "";
  cssText += "<style type=\"text/css\">\n";
  cssText += ".panoramaInStyle .nojs { display:none }\n";
  cssText += "</style>\n";
  return cssText;
}

/**
 * Inserts SPi-V content in divs tagged with 'spv' class.
 */
function spvInsert() {
  var divs = document.getElementsByTagName('div');
  for (i = 0; div = divs[i]; i++) {
    if (div && hasClass(div, 'panoramaInStyle')) {
      // get/make name of object to insert for scripting
      var name = (div.id!=undefined && div.id!="")?"sw_"+div.id:"SPi-V";

      // get/make background color
      var bgObject;
      var bgColor;
      while(bgObject!=document.body && (bgColor=="transparent" || bgColor=="rgba(0, 0, 0, 0)" || bgColor=="" || bgColor==undefined)) {
        if (bgObject==undefined)
          bgObject = div;
        else
          bgObject = bgObject.parentNode;
        bgColor = getStyle(bgObject,"backgroundColor")
      }

      if(bgColor=="transparent" || bgColor=="rgba(0, 0, 0, 0)" || bgColor=="" || bgColor==undefined) 
        bgColor="#ffffff";
      else
        bgColor=hexColor(bgColor);

      // get/make url to display with SPi-V
      var urlclass = div.className.match(/spv\(([^\)]*)\)/);
      var swURL = (urlclass!=null)?urlclass[1]:undefined;

      var dcrclass = div.className.match(/spvdcr\(([^\)]*)\)/);
      var dcr = (dcrclass!=null)?dcrclass[1]:undefined;

      if (detectShockwave())
        div.innerHTML=spvHTML(dcr,swURL,"100%","100%",bgColor,name);
      else
        div.innerHTML="<h2>To view this panorama, you have to install or upgrade Shockwave. This browser plugin can be downloaded free of charge from <a href='http://www.macromedia.com/shockwave/download'>www.macromedia.com/shockwave/download</a>.</h2>"

    }
  }
}

/**
 * Detects Shockwave 8.5 and newer.
 */
function detectShockwave()
{
  // no detection in IE/windows or Firefox/windows/osx
  // use browser based autoinstall instead

  if ( (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 && navigator.userAgent.indexOf("Windows")>=0) ||
       (navigator.userAgent && navigator.userAgent.indexOf("Firefox")>=0 && (navigator.userAgent.indexOf("Windows")>=0 || navigator.userAgent.indexOf("Mac OS X")>=0) )) {
    return true;
  } else {
    if (navigator.mimeTypes && navigator.mimeTypes["application/x-director"] && navigator.mimeTypes["application/x-director"].enabledPlugin) {
      if (navigator.plugins && navigator.plugins["Shockwave for Director"] && (versionIndex = navigator.plugins["Shockwave for Director"].description.indexOf(".")) != - 1) {
        if (parseFloat(navigator.plugins["Shockwave for Director"].description.split('version ')[1]) >= 8.5)
          return true;
      }
    }

  }
}

function spvHTML(dcr,swURL,width,height,color,name)
{
  dcr   =(dcr   !=undefined)?dcr:"SPi-V.dcr";
  swURL =(swURL !=undefined)?swURL:"";
  width =(width !=undefined)?width:"100%";
  height=(height!=undefined)?height:"100%";
  color =(color !=undefined)?color:"#000000";
  name  =(name  !=undefined)?name:"SPi-V";

  var html = "";
  html+= '<object id="'+name+'" width="'+width+'" height="'+height+'"\n';

  html+= 'classid="clsid:166B1BCA-3F9C-11CF-8075-444553540000"\n'
  html+= 'codebase="http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0">\n';

  html+= '<param name="src" value="'+dcr+'">\n';
  html+= '<param name="swURL" value="'+swURL+'">\n';
  html+= '<param name="swStretchStyle" value="stage">\n';
  html+= '<param name="progress" value="true">\n';
  html+= '<param name="logo" value="false">\n';
  html+= '<param name="bgColor" value="'+color+'">\n';
  html+= '<param name="swRemote" value="swContextMenu=\'false\'">\n';

  html+= '<embed name="'+name+'"\n';
  html+= 'width="'+width+'" height="'+height+'" type="application/x-director"\n';
  html+= 'pluginspage="http://www.macromedia.com/shockwave/download/" src="'+dcr+'"\n';
  html+= 'swURL="'+swURL+'"\n';
  html+= 'swStretchStyle="stage" swRemote="swContextMenu=\'false\'" bgColor="'+color+'"\n';
  html+= 'swLiveconnect="true" progress="true" logo="false">\n';
  html+= '</embed>\n';
  
  html+= '</object>\n';

  return html;
}



/**
 * Returns true if an element has a specified class name
 */
function hasClass(node, className) {
  if (node.className == className) 
    return true;

  var reg = new RegExp('(^| )'+ className +'($| )')
  if (reg.test(node.className)) 
    return true;

  return false;
}

/**
 * Returns a cascaded style property
 */
function getStyle(node, prop) {
  if (node.style[prop]) {
    // inline style property
    return node.style[prop];
  } else if (node.currentStyle) {
    // external stylesheet for Explorer
    return node.currentStyle[prop];
  } else if (document.defaultView && document.defaultView.getComputedStyle) {
    // external stylesheet for Mozilla and Safari 1.3+
    prop = prop.replace(/([A-Z])/g, "-$1");
    prop = prop.toLowerCase();

    return document.defaultView.getComputedStyle(node,"").getPropertyValue(prop);
  } else {
    return null;
  }
}

/**
 * Converts rgb(rrr,ggg,bbb) to #RRGGBB style values
 */
function hexColor(color)
{
  if(color.substring(0,1)=="#")
    return color

  var rgbColor = color.match(/rgb\( *([0-9]*) *, *([0-9]*) *, *([0-9]*) *\)/i);
  if(rgbColor==null)
    return color

  var r = parseInt(rgbColor[1]).toString(16);
  var g = parseInt(rgbColor[2]).toString(16);
  var b = parseInt(rgbColor[3]).toString(16);
  if (r.length == 1) r = '0' + r;
  if (g.length == 1) g = '0' + g;
  if (b.length == 1) b = '0' + b;
  return '#' + r + g + b;
}
