plannerItem = function(xcsId){
	
	this.xcsId =   xcsId;
	this.unique = 0;
	this.tds = [];
	this.ajax  =   new joinkJAH();
	
}

plannerItem.prototype = {
	
	addToPlanner   :   function(){
	       this.ajax.setFunction(bind(this, this.addToPlannerResponse));
	       this.ajax.setParams('id='+this.xcsId);
	       this.ajax.doRequest('/obj/travelmate/addToPlanner.ajax.php', 'POST')
	},
	
	addToPlannerResponse   :   function(){
		  var o = JSON.parse(this.ajax.response);
		  if (o.succes == true){
		  	   $('tritems').innerHTML += o.li;
		  	   $('items').getElementsByTagName('b')[0].innerHTML = o.count;
		  }
	},
	
	removeFromPlanner  :   function(){
		  this.ajax.setFunction(bind(this, this.removeFromPlannerResponse));
		  this.ajax.setParams('id='+this.xcsId);
		  this.ajax.doRequest('/obj/travelmate/removeFromPlanner.ajax.php', 'POST')
	},
	
	removeFromSchedule :   function(){
	    this.ajax.setFunction(bind(this, this.removeFromScheduleResponse));
	    this.ajax.setParams('id='+this.xcsId+'&unique='+this.unique);
	    this.ajax.doRequest('/obj/travelmate/removeFromSchedule.php', 'POST');
	},

	removeFromScheduleResponse :   function(){
	    var o = JSON.parse(this.ajax.response);
	    if (o.succes == true){
    	    var item = $$('scheduleItem-' + this.xcsId + '-' + this.unique);
            for (var n = 0;n<item.length;n++){
                item[n].parentNode.removeChild(item[n]);
            }
	    }
	},
	
    removeFromPlannerResponse   :   function(){
          var o = JSON.parse(this.ajax.response);
          if (o.succes == true){
               var li = $('travelitem' + this.xcsId);
               li.parentNode.removeChild(li);
               $('items').getElementsByTagName('b')[0].innerHTML = o.count;
          }
          // remove all shizzle form the table
          for(var n = 0;n<o.schedules.length;n++){
        	  // check for div's with the id
        	  var item = $$('scheduleItem-' + this.xcsId + '-' + o.schedules[n]);
        	  for (var m = 0;m<item.length;m++){
        		  item[m].parentNode.removeChild(item[m]);
        	  }
          }
    },
		
	placeOnSchedule    :   function(type, len, from, to){
		  if (!type){
		  	type = null;
		  }
		  if (!len){
		  	len = 0;
		  }
		  //  Check if we have the schedule table, if so, calculate where to position it
		  
		  //var start = parseInt(window.prompt('startdag?'));
		  //var einde = start;
		  if (len > 0){
			 atprompt(from, to, 'Gelieve uw startdag te specifiëren', '', this);
		  }else{
			 atprompt(from, to, 'Gelieve uw startdag te specifiëren', 'Gelieve uw einddag te specifiëren', this);
		  }
    },
    
    doPlaceOnSchedule	:	function(){
    	var prm = document.getElementById('atprompt');
    	var inp = prm.getElementsByTagName('input');
    	var params = '';
    	for (var n = 0;n<inp.length;n++){
    		params += '&' + inp[n].id.split('-')[1] + '=' + inp[n].value;
    	}

          this.ajax.setFunction(bind(this, this.placeOnScheduleResponse));
          this.ajax.setParams('id='+this.xcsId+params);
          this.ajax.doRequest('/obj/travelmate/scheduleOnPlanner.ajax.php', 'POST')
	},
	
	placeOnScheduleResponse : function(){
		var o = JSON.parse(this.ajax.response);
	
		var tbl = $('travelmate');
	  	for( var n = parseInt(o.start);n<=parseInt(o.end);n++){
	  		var tds = $$(o.type.toString());
	  		var t = new RegExp('day' + n + ' ');
	  		for (var m = 0;m< tds.length;m++){
	  			if (t.test(tds[m].className)){
	  				tds[m].innerHTML += o.html;
	  			}
	  		}
	  	}

		hoverBalloon();
		atcancelprompt();
	}
	
}

var planner_ajax = null;

function initPlanner(){
	planner_ajax = new joinkJAH();
	planner_ajax.setFunction(showPlannerResponse);
    planner_ajax.doRequest('/obj/travelmate/initPlanner.ajax.php', 'GET');    
}

function showPlannerResponse(){
	var planner = JSON.parse(planner_ajax.response);
    var msg = '';
    for (var o in planner.planner){
    	for (var p in planner.planner[o]){
    		for (var n = 0;n<planner.planner[o][p]['days'].length;n++){
		  		var tds = $$(planner.planner[o][p]['type']);
		  		var t = new RegExp('day' + planner.planner[o][p]['days'][n] + ' ');
		  		for (var m = 0;m< tds.length;m++){
		  			if (t.test(tds[m].className)){
		  				tds[m].innerHTML += planner.planner[o][p]['html'];
		  			}
		  		}    			
    		}
    		//msg += p + ' ==> ' + planner.planner[o][p]['html'] + '\n';
    	}
    }
    hoverBalloon();
}

function add2planner(x){
	var t = new plannerItem(x);
	t.addToPlanner();
	return false;
}

function removeFromSchedule(xcsId, unique){
	var rem = window.confirm('Wilt u dit arrangement verwijderen van uw planner?');
	if (rem){
	    var t = new plannerItem(xcsId);
	    t.unique = unique;
        t.removeFromSchedule();
	}
	return false;
}

function removeFromPlanner(x, title){
    var c = window.confirm('Wilt u ' + title + ' verwijderen?\nAls u dit item al op uw gedetailleerd reisplan geplaatst hebt zal het ook hier verwijderd worden.');
    if (c){	
		var t = new plannerItem(x);
		t.removeFromPlanner();
		// remove the dt and stuff like that
		var items = $$('detail' + x);
		for (var n = 0;n<items.length;n++){
			items[n].parentNode.removeChild(items[n]);
		}
    }
	return false;
}

function placeOnPlanner(x, type, len, from, to){
    var t = new plannerItem(x);
    t.placeOnSchedule(type, len, from, to);
    return false;
}


function atprompt(from, to, start, einde, o){
	var div = document.createElement('div');
	div.id = 'atprompt';
	div.innerHTML += '<p><label>' + start + '</label></p>';
	var overlay = document.createElement('div');
	overlay.id = 'atoverlay';

	var body = document.getElementsByTagName('body')[0];
	
	overlay.style.height = body.offsetHeight + 'px';
	div.style.marginTop = document.documentElement.scrollTop - 50 + 'px';
	var dt = new Date();
	
	body.appendChild(overlay);
	body.appendChild(div);
	
	// create input type for start
	var inp = document.createElement('input');
	inp.id = 'ret-start-' + dt.getTime();
	inp.type = 'text';
	inp.name = 'start';
	inp.className = 'w8em format-y-m-d divider-dash highlight-days-67';
	inp.setAttribute('value', from);
	div.appendChild(inp);
	
	
	if (einde){
		div.innerHTML += '<p><label>' + einde + '</label></p>';
		var inp2 = document.createElement('input');
		inp2.type = 'text';
		inp2.id = 'ret-end-' + dt.getTime();
		inp2.name = 'end';
		inp2.setAttribute('value', to);
		inp2.className = 'w8em format-y-m-d divider-dash highlight-days-67';
		div.appendChild(inp2);
	}
	
	div.innerHTML += '<p><button type="button" id="doit">Submit</button><button type="button" id="dontdoit">Cancel</button></p>';
	document.getElementById('doit').onclick = bind(o, o.doPlaceOnSchedule);
	document.getElementById('dontdoit').onclick = atcancelprompt;
	
	datePickerController.create(inp);
	datePickerController.create(inp2);
}

function atcancelprompt(){
	var overlay = document.getElementById('atoverlay')
	var div = document.getElementById('atprompt');
	var body = document.getElementsByTagName('body')[0];
	body.removeChild(overlay);
	body.removeChild(div);
}