/**
 * This script contains embed functions for common plugins. This scripts are complety free to use for any purpose.
 */

function writeFlash(p) {
        writeEmbed(
                'D27CDB6E-AE6D-11cf-96B8-444553540000',
                'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
                'application/x-shockwave-flash',
                p
        );
}

function writeShockWave(p) {
        writeEmbed(
        '166B1BCA-3F9C-11CF-8075-444553540000',
        'http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0',
        'application/x-director',
                p
        );
}

function writeQuickTime(p) {
        writeEmbed(
                '02BF25D5-8C17-4B23-BC80-D3488ABDDC6B',
                'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0',
                'video/quicktime',
                p
        );
}

function writeRealMedia(p) {
        writeEmbed(
                'CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA',
                'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
                'audio/x-pn-realaudio-plugin',
                p
        );
}

function writeWindowsMedia(p) {
        p.url = p.src;
        writeEmbed(
                '6BF52A52-394A-11D3-B153-00C04F79FAA6',
                'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701',
                'application/x-mplayer2',
                p
        );
}

function writeEmbed(cls, cb, mt, p) {
        var h = '', n;

        h += '<object classid="clsid:' + cls + '" codebase="' + cb + '"';
        h += typeof(p.id) != "undefined" ? 'id="' + p.id + '"' : '';
        h += typeof(p.name) != "undefined" ? 'name="' + p.name + '"' : '';
        h += typeof(p.width) != "undefined" ? 'width="' + p.width + '"' : '';
        h += typeof(p.height) != "undefined" ? 'height="' + p.height + '"' : '';
        h += typeof(p.align) != "undefined" ? 'align="' + p.align + '"' : '';
        h += '>';

        for (n in p)
                h += '<param name="' + n + '" value="' + p[n] + '">';

        h += '<embed type="' + mt + '"';

        for (n in p)
                h += n + '="' + p[n] + '" ';

        h += '></embed></object>';

        document.write(h);
}

function loadCSS(abs_path, css) {
        var oLink = document.createElement("link")
        oLink.href = abs_path + css;
        oLink.rel = "stylesheet";
        oLink.type = "text/css";
        document.body.appendChild(oLink);
}

function setCSS(abs_path){
        var new_css;
        var css = readCookie('css');
        if(css == 'large.css'){
                new_css = 'normal.css';
        } else {
                new_css = 'large.css';
        }
        loadCSS(abs_path,new_css);
        createCookie('css',new_css,31);
}

function createCookie(name,value,days) {
        if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
}

function eraseCookie(name) {
        createCookie(name,"",-1);
}

function createPopup(vid_name){
        window.open('/nl/popup.php?video='+vid_name,'vid_name','width=350,height=267,scrollbars=no,toolbar=no,location=no');
        return false;
}

function createSwfPopup(vid_name, width, height){
        window.open('/nl/swfpopup.php?video='+vid_name+'&width='+width+'&height='+height,'vid_name','width='+width+',height='+height+',scrollbars=no,toolbar=no,location=no');
        return false;
}

var JoomlaDefaultEvent = {
  registerForDetachment: function(e, ev, fn) {
    if (typeof(JoomlaDefaultEvent.registrations) == 'undefined') {
      // need to detachevents to avoid memory leaks on IE
      JoomlaDefaultEvent.registrations = [];
      (function() {
        var f = function() {
          var vals = JoomlaDefaultEvent.registrations;
          for (var i = 0; i < vals.length; i++) {
            var val = vals[i];
            vals[0].detachEvent("on" + vals[1], vals[2]);
          }
        };
        window.attachEvent('onunload', fn);
        JoomlaDefaultEvent.registrations.push([window, 'unload', fn]);
      });
    }
    JoomlaDefaultEvent.registrations.push([e, ev, fn]);
  },

  observe: function(e, ev, fn) {
    if (e.addEventListener) {
      e.addEventListener(ev, fn, false);
    } else if (e.attachEvent) {
      e.attachEvent("on" + ev, fn);
      JoomlaDefaultEvent.registerForDetachment(e, ev, fn);
    }
  }
}

JoomlaDefaultEvent.observe(window, 'load', function () {
  var children = document.getElementById('mainlevel').childNodes, zindex = 0;
  for (var i = 0; i < children.length; i++) {
    (function(e) {
      JoomlaDefaultEvent.observe(children[i], 'mouseover',function () {
        if (e.getElementsByTagName("li").length) {
          e.className += ' hover';
          e.style.zIndex = zindex;
          zindex += 1;
        }
      });

      JoomlaDefaultEvent.observe(e, 'mouseout', function () {
        setTimeout (function () {
          e.className = e.className.replace(/hover/, '');
        }, 500);
      });
    })(children[i])
  }
})
