///////////////////////////////////////////////////////////////////////////////
// 
// Copyright (c) Wim Wouters, December 2007. 
// 
// This material contains unpublished, copyrighted work, which includes 
// confidential and proprietary information of Wim Wouters. 
// 
// All rights reserved. No copy of any part of this document is permitted 
// without explicit written authorization from the author! 
// 
///////////////////////////////////////////////////////////////////////////////


//-----------------------------------------------------------------------------
// Classes
//-----------------------------------------------------------------------------
function WWImagePreloader( )
{
  // Construction
	this.ma_org_images  = new Array();
	this.ma_over_images = new Array();
	this.preloadImage = preloadImage;
	
	// Member functions
	function preloadImage( i_orig_src, i_over_src )
	{
	  // Allocate a new entry inside the image holder arrays
		var new_index = this.ma_org_images.length;
	  this.ma_org_images[new_index]  = null;
	  this.ma_over_images[new_index] = null;

    // Load the images
		if (i_orig_src)
		{
		  this.ma_org_images[new_index] = new Image();
		  this.ma_org_images[new_index].src = i_orig_src;
		}
		if (i_over_src)
		{
		  this.ma_over_images[new_index] = new Image();
		  this.ma_over_images[new_index].src = i_over_src;
		}
	}
}

//-------------------------------------
// The one and only instance of the image preloader
var g_ww_preloader = new WWImagePreloader();

//-----------------------------------------------------------------------------
// Functions
//-----------------------------------------------------------------------------
/*
Writes the Flash specific embed code for the specified flash movie and the 
given parameters.
*/
function WWEmbedFlash( i_version, i_id, i_movie, i_width, i_height, i_flashvars )
{
  document.writeln('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + i_version + ',0,0,0" width="' + i_width + '" height="' + i_height + '" id="' + i_id + '" align="middle">');
  document.writeln('<param name="allowScriptAccess" value="sameDomain" />');
  document.writeln('<param name="movie" value="' + i_movie + '" />');
  document.writeln('<param name="quality" value="high" />');
  document.writeln('<param name="wmode" value="transparent" />');
  document.writeln('<param name="bgcolor" value="#FFFFFF" />');
  document.writeln('<param name="FlashVars" value="' + i_flashvars + '" />');
  document.writeln('<embed src="' + i_movie + '" flashvars="' + i_flashvars + '" quality="high" wmode="transparent" bgcolor="#FFFFFF" width="' + i_width + '" height="' + i_height + '" id="' + i_id + '" align="middle" allowscriptaccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
  document.writeln('</object>');
}

//-----------------------------------------------------------------------------
// Event Handlers
//-----------------------------------------------------------------------------
function onWindowLoad( ) 
{
	// No support for very old browsers, sorry!
	if (!document.getElementsByTagName) 
	  return;
	  
  // Find all the image tags on the current page.
	var a_image_tags = document.getElementsByTagName('img');
	if (!a_image_tags)
	  return;

	// Pre-Load all the images for these tags.
	// But only do this for the tags that have our own 'extra' attributes
	for(i=0; i<a_image_tags.length; i++)
	{
	  // Skip all tags missing our own 'extra' attributes
	  var tag = a_image_tags[i];
	  if (!tag.src || !tag.getAttribute('srcover'))
	    continue;

	  // Load the required images
	  g_ww_preloader.preloadImage(tag.getAttribute('src'), tag.getAttribute('srcover'));  

	  // Remember the original image source.
	  // We use an attribute name that most probably will not clash with another javascript lib...
	  tag.setAttribute("__ww-src", tag.src);

	  // Setup the event handlers for the image tags.
	  // When the event handlers already exist, 
	  // we setup new ones and call the old ones last
	  if( tag.onmouseover)
	  {
		  tag.old_onmouseover = tag.onmouseover;
		  tag.onmouseover = function(){ this.src = this.getAttribute("srcover"); this.old_onmouseover(); }
	  }
	  else
	  {
	    tag.onmouseover = function(){ this.src = this.getAttribute("srcover"); }
	  }
	    
	  if (tag.onmouseout)
	  {
		  tag.old_onmouseout = tag.onmouseout;
		  tag.onmouseout = function(){ this.src = this.getAttribute("__ww-src"); this.old_onmouseout(); }
	  }
	  else
	  {
	    tag.onmouseout = function(){ this.src = this.getAttribute("__ww-src"); }
	  }
	}

  // PNG QuickFix for IE 5.5 & IE 6.x
  var version_str = navigator.appVersion.split("MSIE")
  var version_num = parseFloat(version_str[1])
  if ((version_num >= 5.5) && (version_num < 7.0) && (document.body.filters)) 
  {
    for(var i=0; i<document.images.length; i++)
    {
      var img = document.images[i]
      var img_name = img.src.toLowerCase();

      // Only process PNG files
      if (img_name.substring(img_name.length-3, img_name.length) == "png")
      {
        var img_id = (img.id) ? "id='" + img.id + "' " : "";
        var img_class = (img.className) ? "class='" + img.className + "' " : "";
        var img_title = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
        var img_style = "display:inline-block;" + img.style.cssText;
        
        if (img.align == "left") 
          img_style = "float:left;" + img_style;
        else if (img.align == "right") 
          img_style = "float:right;" + img_style;
        if (img.parentElement.href) 
          img_style = "cursor:hand;" + img_style;

        var new_html = "<span " + img_id + img_class + img_title
         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + img_style + ";"
         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
         img.outerHTML = new_html
         i = i-1
      }
   }
}

}


//-----------------------------------------------------------------------------
// Global Code
//-----------------------------------------------------------------------------
// Preload the images on load of page...
// (try to support all browsers)
if (window.addEventListener)
{
  window.addEventListener("load", onWindowLoad, false);
} 
else if (window.attachEvent)
{
  window.attachEvent("onload", onWindowLoad);
}
else if(document.getElementById)
{
  window.onload = onWindowLoad;
}
