/*


Dynamic Application of Functions to DOM
*/
var _debugMode = false;
Event.observe(document, 'dom:loaded', init);
Event.observe(window, 'load', initAfterImages);


function init () {
  
  // REPLACE FEATURES AND SUBNAV WITH FLASH NAV
  if(hasFlash()) {
    Flash.EmbedFlash("sub_nav","sub_nav",240,260,"/flash/subnav.swf",true);
    Flash.EmbedFlash("nav_top","nav_top",760,55,"/flash/topnav.swf",true);
  }
  
  /* hide 1st slide of home page feature */
  new SlideShow('home_feature', { fadeTime: 5 });
}

function initAfterImages () {
  $$('#gallery_button a').each(function(a) {
  	a.update('<img src="/images/button_galleries.png" border="0" width="150" height="30" />').down('img').pngHack();
  });
  
  $$('h1').each(function(a) {
    if(!a.empty()) {
      a.hide();
      var title = a.innerHTML.toUpperCase();
      var flashFilePath = "/flash/title.swf?titleVal="+title;
      a.update(Flash.FlashHTML("page_title","page_title",400,50,flashFilePath,true));
      a.show();
    }
  });
}

function t(f) {
  Try.these(f);
}

/* Library of Custom Functions */
var Flash = {
  
  /*
  This function is used to embed the Flash into the page
  
  Variables:
  
    Variable Name   : Laman        : Data Type
    
    EF_object       : id of object : String
    EF_width        : width        : String
    EF_height       : height       : String
    EF_filename     : filename     : String
    EF_transparency : transparency : Boolean
  
  */
  EmbedFlash: function(EF_object, EF_name, EF_width, EF_height, EF_filename, EF_transparency){
    if($(EF_object)){
      $(EF_object).update(Flash.FlashHTML(EF_object, EF_name, EF_width, EF_height, EF_filename, EF_transparency));
    }
  },
  FlashHTML: function (EF_object, EF_name, EF_width, EF_height, EF_filename, EF_transparency){
    if (EF_transparency){
      var EF_transparency_ParamMarkup = new String('<param name="wmode" value="transparent" />');
      var EF_transparency_EmbedMarkup = new String('wmode="transparent"');
      cl(EF_transparency_ParamMarkup);
      cl(EF_transparency_EmbedMarkup);
    } else {
      var EF_transparency_ParamMarkup = new String('');
      var EF_transparency_EmbedMarkup = new String('');
    }
    var markup = [
      '<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="', EF_width, '" height="', EF_height, '" id="', EF_name, '" align="middle" style="height: ', EF_height, '; width: ', EF_width, ';">',
      '<param name="allowScriptAccess" value="always" />',
      EF_transparency_ParamMarkup,
      '<param name="movie" value="', EF_filename, '" />',
      '<param name="quality" value="high" />',
      '<embed src="', EF_filename, '" ', EF_transparency_EmbedMarkup, ' quality="high" width="', EF_width, '" height="', EF_height, '" name="', EF_name, '" align="middle" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />',
      '</object>'].join("");
    return markup;
  }
};

/*
Returns the Flash object existing on the DOM,
allowing you to use methods and functions from within the ActionScript itself
*/
function $Flash(movieName){
  if (!navigator.appName.include("Microsoft")){
    cl(movieName);
    cl(document[movieName]);
    return document[movieName];
  } else {
    return window[movieName];
  }
}

// DETECT FLASH
function hasFlash() {
  var flashinstalled = 0;
  var flashversion = 0;
  MSDetect = "false";
  if (navigator.plugins && navigator.plugins.length) {
    x = navigator.plugins["Shockwave Flash"];
    if (x) {
      flashinstalled = 2;
      if (x.description) {
        y = x.description;
        flashversion = y.charAt(y.indexOf('.')-1);
      }
    } else { 
      flashinstalled = 1; 
    }
    
    if (navigator.plugins["Shockwave Flash 2.0"]) {
      flashinstalled = 2;
      flashversion = 2;
    }
  } else if (navigator.mimeTypes && navigator.mimeTypes.length) {
    x = navigator.mimeTypes['application/x-shockwave-flash'];
    if (x && x.enabledPlugin) {
      flashinstalled = 2;
    } else {
      flashinstalled = 1;
    }
  } else {
    MSDetect = "true";
  }
  
  return MSDetect;
}

function cl (arg) {
  if (_debugMode && console) console.log(arg);
}

/*
Makes 32 bit PNG's transparency work in Internet Explorer 6
 * Dependent on Prototype 1.6
 * Works on img elements and on background images of elements
 * Image tiling will not work
 * Refer to the readme

Example Usages:
 
 $('yourPNG').pngHack();
 $$('div#fixMe', 'img#andMe', 'img.andUsTo').invoke('pngHack');
 
*/
Element.addMethods({
  pngHack: function(el){
    var el = $(el);
    var transparentGifPath = 'images/transparent.gif';
    if (Prototype.Browser.IE){
      /* if it's an img and a png */
      if ((el.match('img')) && (el.src.include('png'))){
        var alphaImgSrc = el.src;
        var sizingMethod = 'scale';
        el.src = transparentGifPath;
        /* if it's an element with a background png */
      } else if (el.getStyle('backgroundImage').include('png')){
        var bgColor = el.getStyle('backgroundColor') || '';
        var elBg = el.getStyle('backgroundImage');
        var alphaImgSrc = elBg.slice(5, elBg.length - 2);
        var sizingMethod = 'crop';
        el.setStyle({ 'background': bgColor + transparentGifPath });
      }
      if (alphaImgSrc) el.runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="#{al}",sizingMethod="#{sz}")'.interpolate({ al: alphaImgSrc, sz: sizingMethod });
    }
    return el;
  }
});