/* --------------------------------------------------------
 * HRC-JS Framework
 * --------------------------------------------------------
 * Auteur                :        Hondsrug College
 * E-mail                :        matthias@hondsrugcollege.nl
 * Copyright             :        (C) 2007 - Hondsrugcollege
 * --------------------------------------------------------
 */


var is = {
        opera: !!window.opera,
        ie: /*@cc_on!@*/false,
        khtml: navigator.userAgent.indexOf('KHTML') > -1,
        gecko: navigator.userAgent.indexOf('KHTML') == -1 && navigator.userAgent.indexOf('Gecko') > -1,
        mac: navigator.userAgent.indexOf('Macintosh') > -1
}

function system( )
{
      if ( this == window )
           return new system( );

      if ( system.instance )
          return system.instance;

      system.prototype.ajax = null;
      system.prototype.dom = null;
      system.prototype.dialog = null;

      // Hier wat declaraties  enzo....
      this.instellingen =
      {
           url  :   '',
           pad  :   ''
      }

      return ( system = this );
}

system.prototype =
{
     InternalAjax : function ( ) { return new Ajax(); },
     InternalDOM  : function ( ) { return new DOM(); },
     InternalDialog : function( ) { return new Dialoog(); },

     setUrl : function ( newUrl )
     {
           this.instellingen[ 'url' ] = newUrl;
     },
     setPad : function ( newPad )
     {
           this.instellingen[ 'pad' ] = newPad;
     },

     urlParameters : function (url, parameters)
     {
           var queryString = this.getQueryString(parameters);

           if (queryString)
                queryString = (url.indexOf('?') == -1 ? '?' : '&') + queryString;

          return queryString;
    },
    urlData : function(data, parameters)
    {
        var queryString = getQueryString(parameters);

        if (queryString)
                queryString = (data == '' ? '' : '&') + queryString;

        return queryString;
    },
    getQueryString : function(parms)
    {
        var query = [];

        for (var parm in parms)
        {
                if (parms.hasOwnProperty(parm))
                        query.push(encodeURIComponent(parm) + '=' + encodeURIComponent(parms[parm]));
        }

        return query.join('&');
    }
}

/*
 * Object extenders
 */
Object.extend = function(dest, source, allowOverwrite)
{
        for (var prop in source)
        {
                if (source.hasOwnProperty(prop) && (allowOverwrite || !dest.hasOwnProperty(prop)))
                        dest[prop] = source[prop];
        }

        return dest;
}


Object.extend(Function.prototype,
{
        bind: function()
        {
                var handler = this, args = [].slice.call(arguments, 0), object = args.shift();

                return function()
                {
                        return handler.apply(object, args.concat([].slice.call(arguments, 0)));
                }
        }
});

function Set()
{
        var set = {}, i = arguments.length;
        while (i--)
                set[arguments[i]] = 1;

        return set;
}

function addEvent(obj, evType, fn, useCapture)
{
    if (obj.addEventListener)
    {
        obj.addEventListener(evType, fn, useCapture);
        return true;
    }
    else if (obj.attachEvent)
    {
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    }
    else
    {
         alert("Handler could not be attached");
    }
}


function opacity(id, opacStart, opacEnd, millisec, both_ways, append)
{
    var speed = Math.round(millisec / 100);
    var timer = 0;

    if(opacStart > opacEnd)
    {
        for(i = opacStart; i >= opacEnd; i--)
        {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;


        }

        if ( i <= 0 && both_ways )
        {
             opacity( id, opacEnd, opacStart, millisec, false, append );
        }
    }
    else if(opacStart < opacEnd)
    {
        if ( both_ways )
        {
             $( id ).innerHTML = append;
        }

        for(i = opacStart; i <= opacEnd; i++)
        {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

function changeOpac(opacity, id)
{
     var object = document.getElementById(id).style;
     object.opacity = (opacity / 100);
     object.MozOpacity = (opacity / 100);
     object.KhtmlOpacity = (opacity / 100);
     object.filter = "alpha(opacity=" + opacity + ")";
}

function $(element) { return document.getElementById( element ); }


function transBackground( bool )
{
	if( bool )
	{
		changeOpac( 0, 'layer' );
		$( 'layer' ).style.display = 'block';

		opacity( 'layer', 0, 20, 500 );
	}
}

system = new system();