/*



Dynamic Application of Functions to DOM
*/

Event.observe(window, "load", initAfterImagesAreLoaded);


function initAfterImagesAreLoaded(){
  
	/*
	
	
	
	Makes Internet Explorer render 32 bit PNGs properly
	*/
	$$("#logo").each(IEHacks.MakePNGTransparentInIE);


}
/*



This is the function that applies the functions to the DOM
*/
function init(){


  /*
   * 
   * Scriptaculous effect for the Slogan
   */
  if( $("slogan") && ($("slogan").getStyle("display") == "none") ){
    new Effect.Opacity(
      "slogan",
      {
        beforeStart: function(){$("slogan").show();},
        duration: 2,
        from: 0,
        to: 1
      }
    );
  }
	/*
	
	
	
	Embed Flash
	*/
	IEHacks.EmbedFlash("presentation", "presentation", 285, 283, "interface/flash/SlideShow.swf", true);
	/*
	
	Custom Playing of Flash Animation
	*/
  $$("a.ViewAnimation").each(function(node, i){
    node.onclick = function(){
      if(!$("player")){
        var h = this.href;
        var url = h.substr(
          h.lastIndexOf("#") + 1,
          h.length
        );
        new Ajax.Request(url, {
          method: "get",
          onComplete: function(transport){
            new Insertion.Top("shell", transport.responseText);
            if($("Close")){
              $("Close").observe(
                "click",
                function(){
                  this.blur();
                  $("player").remove();
                }
              );
            }
          }
        });
      }
      this.blur();
      return false;
    };
  });
  /*
  
  Custom Playing of Flash Video
  */
  $$("a.PlayVideo").each(function(node, i){
    node.onclick = function(){
      var OldImage = $("PlayVideoShell").getElementsByTagName("img")[0];
      if(!$("TrainShell")){
        var h = this.href;
        var url = h.substr(
          h.lastIndexOf("#") + 1,
          h.length
        );
        new Ajax.Request(
          url,
          {
            method: "get",
            onComplete: function(transport){
              new Insertion.Top("PlayVideoShell", transport.responseText);
              OldImage.hide();
              $("PlayVideoText").update("Stop Video");
            }
          }
        );
      } else {
        $("TrainShell").remove();
        OldImage.show();
        $("PlayVideoText").update("Play Video");
      }
      this.blur();
      return false;
    };
  });

	/*
	
	
	#PrimaryNav Drop Down show's the first ul child
	*/
	$$("#PrimaryNav li").each(c.addDropDown);
	/*
	
	
	
	Forms
	*/
	$$("input").each(c.addFocus);
  /*
   * 
   * Metal Prices... flash ajax request... mmmkay
   */
  $$("#FlashGraphMonths a").each(function(node, i){
    node.onclick = function(){
      var h = this.href;
      var url = h.substr(
        h.lastIndexOf("#") + 1,
        h.length
      );
      var Indicator = $("FlashGraphIndicator");
      new Ajax.Updater(
        "FlashGraph",
        url,
        {
          method: "get",
          onComplete: function(){$("FlashGraphIndicator").hide();},
          onSuccess: function(){$("FlashGraphIndicator").show();}
        }
      );
      $$("#FlashGraphMonths a.active").invoke("removeClassName", "active");
      this.addClassName("active");
      this.blur();
      return false;
    };
  });

  /*
   * 
   * Apply the "alt" CSS class to tr elements 
   */
  $$("table.generic tbody").each(function(n, i){
    n.getElementsBySelector("tr").each(function(nn, ii){
      if (ii%2 != 0) nn.addClassName("alt");
    });
    n.getElementsBySelector("td.colorme").each(function(nn, ii){
     if (nn.innerHTML.indexOf("-") != -1){
       nn.addClassName("red");
     } else if (nn.innerHTML.indexOf("+") != -1){
       nn.addClassName("green");
     }
    })
  });

}






/*



Library of Custom Functions
*/
var IEHacks = $H({

	/*
	
	This function makes 32 bit PNG's with transparency work
	*/	
	MakePNGTransparentInIE: function(node, i){
		if (navigator.userAgent.indexOf("MSIE ") == -1){
			return false;
		} else {
			if (node.src.indexOf("png") == -1){
				node.runtimeStyle.filter = "";
				return;
			}
			var oldSrc = node.src;
			node.src = "interface/images/transparent.gif";
			node.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + oldSrc + "',sizingMethod='scale')";
		}
	},
	/*
	
	
	
	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) {
		/*
			var EF_object = "FlashFeature";
			var EF_name = SomethingUnique;
			var EF_width = "100%";
			var EF_height = "100%";
			var EF_filename = "/interface/flash/FlashFeature.swf";
			var EF_filename = true;
		*/
	
		if($(EF_object)){
			
			if (EF_transparency){
				var EF_transparency_ParamMarkup = new String('<param name="wmode" value="transparent" />');
				var EF_transparency_EmbedMarkup = new String('wmode="transparent"');
			} else {
				var EF_transparency_ParamMarkup = new String('');
				var EF_transparency_EmbedMarkup = new String('');
			}
	
			var FlashHTML =   new String('<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="sameDomain" />'
							+ 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>');

			$(EF_object).update(FlashHTML);
		}
	}
});
/*


Returns the Flash object,
allowing you to use methods and functions from within the ActionScript itself
*/
function $Flash(movieName) {
  if (navigator.appName.indexOf("Microsoft") != -1) {
    return window[movieName];
  } else {
    return document[movieName];
  }
}



/*


The start of the custom functions object
*/
var c = $H({
	/*
	
	
	Drop Down show's the first ul child
	*/
	addDropDown: function(node, i){
		node.observe("mouseover", function(){node.toggleClassName("hover");});
		node.observe("mouseout", function(){node.toggleClassName("hover");});
		if(node.getElementsBySelector("ul").length > 0){
			var ul = node.getElementsBySelector("ul")[0];
			node.observe("mouseover", function(){ul.toggle();});
			node.observe("mouseout", function(){ul.toggle();});
		}
	},
	addFocus: function(node, i){
		node.observe("focus", c.toggleFocus.bindAsEventListener(node));
		node.observe("blur", c.toggleFocus.bindAsEventListener(node));
	},
	toggleFocus: function(e){
		this.toggleClassName("focus");
	},
	addHover: function(node, i){
		node.observe("mousover", c.toggleHover.bindAsEventListener(node));
		node.observe("mouseout", c.toggleHover.bindAsEventListener(node));
	},
	toggleHover: function(e){
		this.toggleClassName("hover");
	}
});

Ajax.Responders.register({
  onCreate: function() {
    if(Ajax.activeRequestCount > 0){
      var asd = setTimeout(
        "if(Ajax.activeRequestCount > 0){$('FlashGraphIndicator').show();}",
        500
      );
    }
  },
  onComplete: function() {
    if(Ajax.activeRequestCount == 0){
      $('FlashGraphIndicator').hide();
    }
  }
});

