/**
  *
  *  Copyright 2006-2010 1001Produkties.nl
  *  Author: Daniel Adema <info@1001produkties.nl>
  *
  **/

if (typeof HM != "undefined" && HM.LibLoader) {
  HM.LibLoader.warn("HM.LibLoader already loaded");
}

var HM = {};

HM.LibLoader = {
  Version: '1.2.3',

  DEBUG: true,
  REQUIRED_PROTOTYPE: '1.6.0',
  DEPENDENCIES: {
    'fancyzoom':              ['fancyzoom.FancyZoom','fancyzoom.FancyZoomHTML'], /* package */

    'highslide':              ['highslide.highslide','highslide.config'],  /* package */
    'highslide.fancy':        ['highslide.easing','highslide.highslide-fancy','highslide.config','highslide.config-fancy'],  /* package */
    'highslide.full':         ['highslide.highslide-full','highslide.config'],  /* package */
    'highslide.gallery':      ['highslide.highslide-gallery','highslide.config'],  /* package */
    'highslide.html':         ['highslide.highslide-html','highslide.config'],  /* package */

    'highslide4':             ['highslide4.highslide','highslide4.config'],  /* package */
    'highslide4.fancy':       ['highslide4.easing','highslide4.highslide-fancy','highslide4.config','highslide4.config-fancy'],  /* package */
    'highslide4.full':        ['highslide4.highslide-full','highslide4.config'],  /* package */
    'highslide4.gallery':     ['highslide4.highslide-gallery','highslide4.config'],  /* package */
    'highslide4.html':        ['highslide4.highslide-html','highslide4.config'],  /* package */

    'hm.accordion':           ['scriptaculous.effects'],
    'hm.box':                 ['scriptaculous.effects'],
    'hm.dhonishow':           ['scriptaculous.effects'],	
	'hm.enjoyimagetransition':['scriptaculous.scriptaculous', 'scriptaculous.all'],
    'hm.fader':               ['scriptaculous.effects'],
	'hm.fader-all-min':       ['scriptaculous.effects'],
	'hm.fader-min':           ['scriptaculous.effects'],
    'hm.flash.text':          ['hm.flash'],
    'hm.flash.media':         ['hm.flash'],
    'hm.form.calendar':       ['calendar.calendar','calendar.lang.calendar-nl','calendar.calendar-setup','calendar.config'],
    'hm.graph':               ['raphael.raphael'],
    'hm.header-fader':        ['scriptaculous.effects'],    
	'hm.historypath':         ['hm.cookie'],
    'hm.map':                 ['hm.panner'],
    'hm.newsscroller':        ['hm.position','hm.panner'],
    'hm.panner':              ['scriptaculous.effects','scriptaculous.dragdrop','hm.position'],
    'hm.panner.screens':      ['hm.scrollbar'],
    'hm.resizeframeset':      ['scriptaculous.effects'],
    'hm.scrollbar':           ['hm.mousewheel'],
    'hm.tooltip':             ['scriptaculous.effects'],
    'hm.treeview':            ['scriptaculous.effects'],
    'hm.toc':                 ['hm.resizeframeset'],
    'lightbox':               ['scriptaculous.effects'],
    'scriptaculous':          ['scriptaculous.builder','scriptaculous.effects','scriptaculous.dragdrop','scriptaculous.controls','scriptaculous.slider','scriptaculous.sound'], /* package */
    'scriptaculous.all':      ['scriptaculous.builder','scriptaculous.effects','scriptaculous.dragdrop','scriptaculous.controls','scriptaculous.slider','scriptaculous.sound'], /* package */
    'scriptaculous.dragdrop': ['scriptaculous.effects'],
    'scriptaculous.controls': ['scriptaculous.effects']
  },
  PACKAGES: ['fancyzoom', 'highslide', 'highslide.fancy', 'highslide.full', 'highslide.gallery', 'highslide.html', 'highslide4', 'highslide4.fancy', 'highslide4.full', 'highslide4.gallery', 'highslide4.html', 'scriptaculous', 'scriptaculous.all'],

  loadPlugin: function(plugin) {
    if (HM.LibLoader.loadedPlugins.include(plugin)) return;
    if (HM.LibLoader.isPackage(plugin)) return;
    var script = plugin.replace(/\./g, "/");
    script = HM.LibLoader.base+script+'.js';
    if (HM.LibLoader.DEBUG && window.console) console.info("Load plugin %s", plugin);
    document.write('<script type="text/javascript" src="'+script+'"></script>');
    HM.LibLoader.loadedPlugins.push(plugin);
  },
  loadDependencies: function(plugin) {
    if (!HM.LibLoader.DEPENDENCIES[plugin]) return;
    if (HM.LibLoader.loadedDependencies.include(plugin)) return;
    if (HM.LibLoader.DEBUG && window.console) console.info("Load dependencies for %s: ", plugin, HM.LibLoader.DEPENDENCIES[plugin]);
    $A(HM.LibLoader.DEPENDENCIES[plugin]).each(function(dependency) {
      HM.LibLoader.loadDependencies(dependency);
      HM.LibLoader.loadPlugin(dependency);
    });
    HM.LibLoader.loadedDependencies.push(plugin);
  },
  isPackage: function(plugin) {
    return HM.LibLoader.PACKAGES.include(plugin);
  },
  loadDefaultPlugins: function() {
    // Add legacy support for Event.onDOMReady()
    if (!Event.onDOMReady) Object.extend(Event, {
      onDOMReady : function(f) {
        document.observe("dom:loaded", f);
      }
    });

    // Catch missing Firebug methods
    if (!window.console || !console.firebug) {
      var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
        "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

      window.console = {};
      for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
    }

    // Add PNG support
    (function() {
      /*@cc_on @*/
      /*@if (@_win32)
      if (/MSIE [56]/.test(navigator.userAgent)) HM.LibLoader.loadPlugin("hm.iepng");
      /*@end @*/
    })();

    // Browser stats
    (function() {
      //var script = HM.LibLoader.base+'hm/stats/browser.lp?sw='+screen.width+'&sh='+screen.height+'&scd='+screen.colorDepth+'&ua='+escape(navigator.userAgent)+'&rnd='+Math.random();
      //document.write('<script type="text/javascript" src="'+script+'"></script>');
      new Element('img', { src: 'http://c.1001produkties.nl/hm/browser/'+screen.width+'x'+screen.height+'/?rnd='+Math.random() });
    })();
  },

  add: function() {
    $A(arguments).each(function(plugin) {
      HM.LibLoader.loadDependencies(plugin);
      HM.LibLoader.loadPlugin(plugin);
    });
  },
  load: function() {
    function convertVersionString(versionString){
      var r = versionString.split('.');
      return parseInt(r[0])*100000 + parseInt(r[1])*1000 + parseInt(r[2]);
    }

    if((typeof Prototype=='undefined') || 
       (typeof Element == 'undefined') || 
       (typeof Element.Methods=='undefined') ||
       (convertVersionString(Prototype.Version) < 
        convertVersionString(HM.LibLoader.REQUIRED_PROTOTYPE)))
       throw("This script requires the Prototype JavaScript framework >= " +
        HM.LibLoader.REQUIRED_PROTOTYPE);

    HM.LibLoader.loadedPlugins = $A();
    HM.LibLoader.loadedDependencies = $A();
    HM.Form = {};
    HM.Table = {};

    $A(document.getElementsByTagName("script")).findAll(function(s) {
      return (s.src && s.src.match(/libloader\d*\.js(\?.*)?$/))
    }).each(function(script) {
      HM.LibLoader.base = script.src.replace(/libloader\d*\.js(\?.*)?$/,'');

      HM.LibLoader.loadDefaultPlugins();

      var includes = script.src.match(/\?.*load=([0-9a-z\-\.,]*)/);
      var plugins = (includes ? includes[1].split(',') : []);

      plugins.each(function(plugin) {
        HM.LibLoader.add(plugin);
      });
    });
  },
  warn: function(msg) {
    if (window.console) console.warn(msg);
    var script = HM.LibLoader.base+'hm/stats/log.lp?type=warning&msg='+escape(msg)+'&host='+location.host+'&location='+location.href+'&rnd='+Math.random();
    document.write('<script type="text/javascript" src="'+script+'"></script>');
  }
};

HM.LibLoader.load();
