// JavaScript Document
/***************************************************
 * BEGIN: Global
 */


/*
 * END: Global
 **************************************************/

/***************************************************
 * BEGIN: Array
 */
Array.prototype.unique = function()
{
 var objA = Object();
 var arrNew = Array();
 
 for( i=0;i<this.length;i++ )
 {
   objA[ this[i] ] = this[i];
 }
 
 for( i in objA )
 {
   arrNew.push(objA[i]);
 }
 
 return arrNew;
};

/***************************************************
 * BEGIN: Element
 */

Element.extend(
{
 showPopup:function( elInsert )
 {
   var elInsert  = $(elInsert).clone();
   var elBody    = $$('body')[0];
   var elClose   = elInsert.getElements('.close')[0];
   
   //console.log('jo',elClose ); 
   elInsert.setStyles({
     'top':'-1000px',
     'display':'block',
     'position':'absolute'
   });
   elBody.appendChild(elInsert);
   
   var objTemp = elInsert.getSize().size;
   var intWidth = objTemp.x; 
   var intHeight = objTemp.y;  
   
   var pos = this.getPosition();
   var size = this.getSize().size;

   var elBody    = $$('body')[0];
   var elDiv   = new Element('div').setStyles({
     'left': ((pos.x+size.x)-Number(intWidth)) + 'px',
     'top': (pos.y+(size.y+10)) + 'px',
     'border': '1px dashed #000000',
     'position': 'absolute',
     'overflow':'hidden',
     'z-index': '102',
     'opacity': '0',
     'width': Number(intWidth) + 'px',
     'height': '0px'
   });
   
   elDiv.appendChild(elInsert);
   elBody.appendChild(elDiv);
   elInsert.setStyles({
     'top':'0px',
     'display':'block',
     'position':'relative'
   });


   new Fx.Styles(elDiv,{transition:Fx.Transitions.quadOut}).start(
   {
     'height':[0, Number(intHeight)],
     'opacity':[0, 1]
   });

   // elClose.arrDel = Array( elDiv, );
   elClose.elDiv = elDiv;
   elClose.addEvent('click',function()
   {
     this.elDiv.effect('height',{
       duration:500,
       transition:Fx.Transitions.quadIn,
       onComplete:function(){
         this.element.remove();
       }
     }).start(this.elDiv.getSize().size.y,0);
   });

/*   this.elDiv = elDiv;
   this.addEvent('click',function()
   {
     this.elDiv.effect('height',{
       duration:500,
       transition:Fx.Transitions.quadIn,
       onComplete:function(){
         this.element.remove();
       }
     }).start(this.elDiv.getSize().size.y,0);
   });*/

 },
/*   var elPopupDiv  = $$('div.popup_categorie')[0];
   var bodySize = elBody.getSize();
   
   
   var pos = this.getPosition();
   var size = this.getSize().size;

   // maak slider
   elPopupDiv.setStyles({
     'left': ((pos.x+size.x)-Number(argWidth)) + 'px',
     'top': (pos.y+(size.y+10)) + 'px',
     'background-color':'#FFFFFF',
     'position': 'absolute',
     'z-index': '102',
     'opacity': '1',
     'width': Number(argWidth) + 'px',
     'height': '0px'
   });
   elPopupDiv.removeClass('hide');

   new Fx.Style(elPopupDiv,'height').start(0, Number(argHeight) );
   
*/
   // uit zetten
/*   
   elButton.elBack = elDiv;
   elButton.elCategorys = elCategorys;
   elButton.addEvent('click',function()
   {
     this.elCategorys.effect('opacity',{
       duration:100,
       onComplete:function(){
         this.element.remove();
       }
     }).start(0.5,0);
     
     this.elBack._parent = this;
     this.elBack.effect('opacity',{
       duration:100,
       onComplete:function(){
         this.element._parent.remove();
         this.element.remove();
       }
     }).start(0.5,0);
   });
*/
 
 setSelectIndex:function(index)
 {
   var blnSelected = false;
   arrElement = $A($(this).getElements('option'));
   arrElement.each(function(node,i){
     if(node.value == index){
       node.selected = true;
       blnSelected = true;
     }
   });
   
   if( !blnSelected )
   {
     arrElement.each(function(node,i){
       if(node.innerHTML == index){
         node.selected = true;
         blnSelected = true;
       }       
     });
   }
 },
 
 submitTo:function( argUrl )
 {
   this.action = argUrl;
   this.submit();
 },

 cleanWhitespace: function()
 {
   $A(this.childNodes).each(function(node){
     if ($type(node) == 'textnode') this.removeChild(node);
   }, this);
   return this;
 }
});


/*
 * END: Element
 ***************************************************/

/***************************************************
 BEGIN: location related variable and functions 

 Made By: Mientjan Stelling
 Example:
   (start code)
   var firstOb = {
     'name': 'John',
     'lastName': 'Doe'
   };
   var secondOb = {
     'age': '20',
     'sex': 'male',
     'lastName': 'Dorian'
   };
   Object.extend(firstOb, secondOb);
   //firstOb will become: 
   {
     'name': 'John',
     'lastName': 'Dorian',
     'age': '20',
     'sex': 'male'
   };
   (end)
 
 Returns:
   The first object, extended.
*/
objGet = {};
var arrGet = location.search.split('?').join('').split('&');
arrGet.each(function(node,i){
 var arrSub = node.split('=');
 var key = (arrSub[0].split('[]').join(''));

 if(arrSub[0].indexOf('[]') > 0)
 {
   if($type(objGet[key]) != "array"){ objGet[key] = Array(); }
   objGet[key].push(arrSub[1]);
 }
 else
 {
   objGet[key] = arrSub[1];
 }
});

var basehref = $pick($$('base')[0].href,'');

Object.extend(location,
{
 get:objGet,
 basehref:basehref,
 requesturi:( location.href.split( location.search).join('') ),
 arrrequesturi:( location.href.split(location.search).join('').split('//').join('/').split('/') ),
 changeSwitch:function()
 {
   var objA = arguments[0];
   var objB = arguments[1];
   
   objChange = Object();
   
   for( i in objA )
   {
     if(location.get[i] == objA[i])
     {
       objChange[i] = objB[i];
     }
     else
     {
       objChange[i] = objA[i];
     }
   }

   this.change( objChange );
 },
 change:function(){    
   if( arguments.length < 2 )
   {
     var objChange = arguments[0];
     var strBaseUrl = location.requesturi;
     if( strBaseUrl.indexOf('#') > 0 )
     {
       var strBaseUrl = strBaseUrl.substr(0,strBaseUrl.lastIndexOf('#'));
     }
   }
   else
   {
     var strBaseUrl = arguments[0];
     var objChange = arguments[1];
   }

   if( objChange != undefined )
   {
     var arrUrl = Array();   
     for( i in location.get )
     {
       if( objChange[i] != undefined ){
         if( $type(objChange[i]) != "array" )
         {
           arrUrl.push(i + '=' + objChange[i] );
         }
         else
         {
           objChange[i].each(function(node){
             arrUrl.push(i + '[]=' + node );
           });
         }
         delete(objChange[i]);
         continue;
       }

       if( $type(location.get[i]) != "array" )
       {
         if( location.get[i] != undefined || i != '' ) arrUrl.push(i + '=' + location.get[i] );
       }
       else
       {
         location.get[i].each(function(node){
           arrUrl.push(i + '[]=' + node );
         });
       }
     }
 
     for( i in objChange )
     {
       if( $type(objChange[i]) != "array" )
       {
         arrUrl.push(i + '=' + objChange[i] );
       }
       else
       {
         objChange[i].each(function(node){
           arrUrl.push(i + '[]=' + node );
         });
       }
     }
     

     if( strBaseUrl.substr( (strBaseUrl.length-1) ) != '/' )
     {
       strBaseUrl = strBaseUrl + '/';
     }
     // console.log(strUrl , '?', arrUrl);
     location.href = strBaseUrl + '?' + arrUrl.join('&');
   }
 }
});
/* END: location */

/********************************************************
 * Custom New custom mouse events
 */
 
Element.extend({
    isDescendantOf: function(el) {
        var p = this;
        while (p != null) {
            if (p == el) return true;
            p = p.parentNode; 
        }
        return false;
    },
    addEvent: function(type, fn) {
        this.events = this.events || {};
        this.events[type] = this.events[type]||{};
        var _fn = fn;
        var _type = type == 'mousewheel' && window.gecko ? 'DOMMouseScroll' : type;
        if (type.test('mouse(enter|leave)') && !window.ie) {
            _type = type == 'mouseleave' ? 'mouseout' : 'mouseover';
            _fn = function(e) {
                if (!$(e.relatedTarget).isDescendantOf(this)) fn(e);
            }.bind(this);
        }
        this.events[type][fn] = {fn:_fn, type:_type};
        if (this.addEventListener){
     this.addEventListener(_type, _fn, false);
 } else {
     this.attachEvent('on'+_type, _fn);  
 }
 return this;
    },
    addEvents: function(source){
 if (source){
     for (var type in source) this.addEvent(type, source[type]);
 }
 return this;
    },
    removeEvent: function(type, fn){
        if (this.events && this.events[type]) {
            var ev = this.events[type][fn];
            if (this.removeEventListener){
                this.removeEventListener(ev.type, ev.fn, false);
            } else {
                this.detachEvent('on'+ev.type, ev.fn);
            }
        }
    },
    removeEvents: function(type){
 if (this.events){
     if (type){
   if (this.events[type]){
       for (ev in this.events[type]) {
                        this.removeEvent(type, ev);
                    }
                    this.events[type] = null;
                }
            } else {
                for (var evType in this.events) this.removeEvents(evType);
                    this.events = null;
            }
        }
        return this;
    },
    fireEvent: function(type, args){
        if (this.events && this.events[type]){
            for (ev in this.events[type]) {
                this.events[type][ev].fn.bind(this, args)();
            }
        }
    }
});
window.removeEvent = document.removeEvent = Element.prototype.removeEvent;
window.removeEvents = document.removeEvents = Element.prototype.removeEvents;
window.extend({
 addEvent: function(type, fn){
   if (type == 'domready'){
     if (this.loaded) fn();
     else if (!this.events || !this.events.domready){
       var domReady = function(){
         if (this.loaded) return;
         this.loaded = true;
         if (this.timer) this.timer = $clear(this.timer);
         Element.prototype.fireEvent.call(this, 'domready');
         this.events.domready = null;
       }.bind(this);
       if (document.readyState && this.khtml){
         this.timer = function(){
           if (['loaded','complete'].test(document.readyState)) domReady();
         }.periodical(50);
       }
       else if (document.readyState && this.ie){
         document.write("<script id=ie_ready defer src=javascript:void(0)></script>");
         $('ie_ready').onreadystatechange = function(){
           if (this.readyState == 'complete') domReady();
         };
       } else {
         this.addEvent("load", domReady);
         document.addEvent("DOMContentLoaded", domReady);
       }
     }
   }
   Element.prototype.addEvent.call(this, type, fn);
   return this;
 },
 onDomReady: function(init){
   return this.addEvent('domready', init);
 }
});
