var host = 'http://' + window.location.host + '/';
var ajaxLoaderimg = host + 'images/icons/small_ajax_loader.gif';




//All the addEvent actions
window.addEvent('domready', function(){
  
  //Date pickers
  $$('input[specialtype="datePicker"]').each( function(el){
    new DatePicker(el);
  });
  
  //Color pickers
  $$('input[specialtype="colorPicker"]').each( function(el) {
    var r = new MooRainbow(el.id, {
      'id' : 'mooRainbow' + el.id,
      'startColor': el.value.hexToRgb(true),
      'onChange': function(color) {
        $(el.id).value = color.hex;
        $('s' + el.id).style.backgroundColor = color.hex;
      }
    });
  });
  
  
  //Accordeon
  $$('div.accordion').each( function(el) {
    var myAccordion = new Accordion( $(el.id), 'h3.accordeon-toggler', 'div.accordeon-element', {
      opacity: false,
      onActive: function(toggler, element){
        toggler.set('class', 'accordeon-toggler-selected');
        element.set('class', 'accordeon-element-selected');
      },
      onBackground: function(toggler, element){
        toggler.set('class', 'accordeon-toggler');
        element.set('class', 'accordeon-element');
      }
    });  
  });
  
  
  //Info panels slide
  $$('div.actionStatus').each( function(el) {
    var timeout = el.get('timeout');
    if( timeout ) {
      (function() { 
        var mySlide = new Fx.Slide( this, {mode: 'vertical'} ); 
        mySlide.slideOut(); } 
      ).delay( timeout, el );
    }
  });
  
  //large Blocks clickable
  $$('div.overview-block').each( function(el) {
    var link = el.getElement('a');
    if( link && !(el.get('nolink') == 'true')) {
      el.setStyle('cursor', 'pointer');
      el.addEvent('click', function() {
        document.location.href = link.href;
      });
    }
  });
  
  
  //fix inbox js clearfix
  var addClearFix = function( el) { el.addClass('clearfix'); }
  $$('div.input-wrapper').each( addClearFix );
  $$('div.input-text-wrapper').each( addClearFix );
  $$('div.input-data-wrapper').each( addClearFix );
  
  //IE6/7 fix date overlapping
  var fixDateOverlapping = function(el) { el.getParent('div.input-wrapper').setStyle('height', '171px'); }
  $$('input#birthdate,input#date,input#startdate,input#enddate, input#deadline, input#multiple-date-input').each( fixDateOverlapping );

  
});






//************ MSG ********************//

function insertAtCursor(el, myValue) {
  el = $(el);
  
  if( el ) {
  
    //IE support
    if (document.selection) {
      el.focus();
      sel = document.selection.createRange();
      sel.text = myValue;
    }else if (el.selectionStart || el.selectionStart == '0') {//MOZILLA/NETSCAPE support
      var startPos = el.selectionStart;
      var endPos = el.selectionEnd;
      el.value = el.value.substring(0, startPos)
      + myValue
      + el.value.substring(endPos, el.value.length);
    } else {
      el.value += myValue;
    }
  }
}



function log(text, args) {
  if (window.console) console.log(text.substitute(args || {}));
}


//************ USEFULL FUNCTIONS ********************//

/**
 * Asks for confirmation and submit the given form
 */ 
function askConfirm( formID ) {
  var agree = confirm("Weet je het zeker?");
  if( agree ) {
    $( formID ).submit();
  }
}

