/**
 * @author Alex
 */
   var InterfaceObject = Class.create(); 
   InterfaceObject.prototype = { 
     initialize: function(config) { 
     this.config = config;
           this.variables = {'tours':$A(),'hotels':$A(),'autos':$A()}; 
           this.__loadConfig(); 
           Event.observe(window, 'load', function() {cartGUI.windowOnLoad();});         
     }, 
      
     windowOnLoad: function(){ 
     this.__drawCart();
     }, 
      
     __saveConfig: function() { 
      
			Cookie.set('cart_vars', Object.toJSON(this.variables));
      order_detail = new Array();
			order_detail_table = new Array();
			
      // the template (our formatting expression)
      var orderLine = new Template('#{oid} | #{name} #{price} &euro; #{period}');
			var tableOrderLine = new Template('<tr><td class="name">#{name}</td><td class="period">#{period}</td><td class="price">&euro; #{price}</td><td class="oid">#{oid}</td></tr>');
      
      // TOURS
      if(this.variables.tours.length){
       order_detail[order_detail.length] = this.config.label_tours + ':';
       // ordered tours
       for (var i = 0; i < this.variables.tours.length; i++) {
			 	// for email
        order_detail[order_detail.length] = orderLine.evaluate({oid: this.variables.tours[i].oid, name: this.variables.tours[i].name, price: this.variables.tours[i].price, period: this.variables.tours[i].period});
				// for output
				order_detail_table[order_detail_table.length] = tableOrderLine.evaluate({oid: this.variables.tours[i].oid, name: this.variables.tours[i].name, price: this.variables.tours[i].price, period: this.variables.tours[i].period});
       }
      }
      // HOTELS
      if(this.variables.hotels.length){
       order_detail[order_detail.length] = this.config.label_hotels + ':';
       // ordered hotels
       for (var i = 0; i < this.variables.hotels.length; i++) {
			 	// for email
        order_detail[order_detail.length] = orderLine.evaluate({oid: this.variables.hotels[i].oid, name: this.variables.hotels[i].name, price: this.variables.hotels[i].price, period: this.variables.hotels[i].period});
				// for output
        order_detail_table[order_detail_table.length] = tableOrderLine.evaluate({oid: this.variables.hotels[i].oid, name: this.variables.hotels[i].name, price: this.variables.hotels[i].price, period: this.variables.hotels[i].period});
       }
      }

      // AUTO
      if(this.variables.autos.length){
       order_detail[order_detail.length] = this.config.label_autos + ':';
       // ordered tours
       for (var i = 0; i < this.variables.autos.length; i++) {
			 	// for email
        order_detail[order_detail.length] = orderLine.evaluate({oid: this.variables.autos[i].oid, name: this.variables.autos[i].name, price: this.variables.autos[i].price, period: this.variables.autos[i].period});
				// for output
        order_detail_table[order_detail_table.length] = tableOrderLine.evaluate({oid: this.variables.autos[i].oid, name: this.variables.autos[i].name, price: this.variables.autos[i].price, period: this.variables.autos[i].period});
       }
      }
      Cookie.set('order_detail', order_detail.join("\n")); 
			Cookie.set('order_detail_table', "<table id=cartData><tr><th>Produkt</th><th>Periode</th><th>Prijs</th><th>Id</th></tr>"+order_detail_table.join("\n")+'</table>'); 
     }, 
   
     __loadConfig: function() { 
         cartConfig = Cookie.get('cart_vars'); 
         if(cartConfig != null){ 
             this.variables = cartConfig.evalJSON(); 
         } 
     },

     __drawCart: function() { 
      cartHolderDIV = $(this.config.target_div);
    if(cartHolderDIV != null){ 
      // clean cart holder
     cartHolderDIV.update('');

     // EMPTY
     if(this.variables.hotels.length == 0 && this.variables.tours.length == 0 && this.variables.autos.length == 0){
      cartHolderDIV.update('<p>'+this.config.empty_text+'</p>');
      cartHolderDIV.appendChild(Builder.node('a',{'href':'#','onclick':'runOrder(); return false;'},this.config.label_order));
      return 0;       
     }
     total = 0;
     // TOURS
     if (this.variables.tours.length) {
       // add label tours 
       cartHolderDIV.appendChild(Builder.node('h1', this.config.label_tours));
       table = Builder.node('table', {
        id: 'cart_tours',
        cellpadding: '2',
        cellspacing: '0',
        border: '0'
       });
       tbody = Builder.node('tbody');
       table.appendChild(tbody);
       
       for (var i = 0; i < this.variables.tours.length; i++) {
       
        tr = Builder.node('tr', 
         [
          Builder.node('td', [
            Builder.node('text','['),
           Builder.node('a',{'href':'#','onclick':'cartGUI.delItem("tour", '+i+'); return false;'},'x'),
           Builder.node('text',']')
          ]),
          Builder.node('td', Builder.node('a',{'href':this.variables.tours[i].url, 'title':this.variables.tours[i].name},this.variables.tours[i].name_short)), 
          Builder.node('td', {'class':'price'}, this.variables.tours[i].price)
         ]);
        
        total += this.variables.tours[i].price;
        tbody.appendChild(tr);
       };
       
       cartHolderDIV.appendChild(table);
     }
     
     // HOTELS
     if (this.variables.hotels.length){
      // add label hotels 
      cartHolderDIV.appendChild(Builder.node('h1', this.config.label_hotels));
      table = Builder.node('table', {id:'cart_tours', cellpadding:'2', cellspacing:'0', border:'0'});
      tbody = Builder.node('tbody');
      table.appendChild(tbody);
      
      for (var i=0; i < this.variables.hotels.length; i++) {
       tr = Builder.node('tr',
       [
        Builder.node('td', [
          Builder.node('text','['),
         Builder.node('a',{'href':'#','onclick':'cartGUI.delItem("hotel", '+i+'); return false;'},'x'),
         Builder.node('text',']')
         ]),
          Builder.node('td', Builder.node('a',{'href':this.variables.hotels[i].url, 'title':this.variables.hotels[i].name},this.variables.hotels[i].name_short)), 
        Builder.node('td', {'class':'price'}, this.variables.hotels[i].price), 
       ]);
        
       total += this.variables.hotels[i].price;
       tbody.appendChild(tr);
      };
      
      cartHolderDIV.appendChild(table);
      
      } 

     // AUTOS
     if (this.variables.autos.length){
      // add label hotels 
      cartHolderDIV.appendChild(Builder.node('h1', this.config.label_autos));
      table = Builder.node('table', {id:'cart_autos', cellpadding:'2', cellspacing:'0', border:'0'});
      tbody = Builder.node('tbody');
      table.appendChild(tbody);
      
      for (var i=0; i < this.variables.autos.length; i++) {
       tr = Builder.node('tr',
       [
        Builder.node('td', [
          Builder.node('text','['),
         Builder.node('a',{'href':'#','onclick':'cartGUI.delItem("auto", '+i+'); return false;'},'x'),
         Builder.node('text',']')
         ]),
          Builder.node('td', Builder.node('a',{'href':this.variables.autos[i].url, 'title':this.variables.autos[i].name},this.variables.autos[i].name_short)), 
        Builder.node('td', {'class':'price'}, this.variables.autos[i].price), 
       ]);
        
       total += this.variables.autos[i].price;
       tbody.appendChild(tr);
      };
      
      cartHolderDIV.appendChild(table);
      
      }
      // TOTAL
      if (total){
       // add total
       cartHolderDIV.appendChild(Builder.node('b', this.config.label_total+':'));
       cartHolderDIV.appendChild(Builder.node('i', total));
      }
      cartHolderDIV.appendChild(Builder.node('a',{'href':'#','onclick':'runOrder(); return false;'},this.config.label_order));
     }
     },
    
     addItem: function(type, data) { 
     switch(type) {
      case 'tour':
       this.variables.tours[this.variables.tours.length] = data;
      break;
      case 'hotel':
       this.variables.hotels[this.variables.hotels.length] = data;
      break;
      case 'auto':
       this.variables.autos[this.variables.autos.length] = data;
      break;
     }
     this.__drawCart();
     this.__saveConfig();
     },
     
    delItem: function(type, id) { 
     switch(type) {
      case 'tour':
       this.variables.tours.splice(id,1);
      break;
      case 'hotel':
       this.variables.hotels.splice(id,1);
      break;
      case 'auto':
       this.variables.autos.splice(id,1);
      break;
     }
     this.__drawCart();
     this.__saveConfig();
     }
    
   };
 var cartGUI = new InterfaceObject({'target_div':'cartHolder', 'label_tours':'Tours', 'label_hotels':'Hotels', 'label_autos':'Cars', 'label_total':'Total', 'label_order':'offerte aanvragen', 'empty_text':'Om een offerte aan te vragen kunt u de betreffende producten selecteren door op het winkelmandje achter uw productkeuze te klikken. Wij zullen u voor de totale reis dan een offerte en prijsopgave sturen. Zo kunt u makkelijk zelf een reis op maat samenstellen.<br/>Of u kunt direct naar het offerte formulier gaan (via "offerte aanvragen") en uw wensen op het formulier aangeven.'});   

function runOrder(){
  window.open('/read/offerte_aanvragen', 'Window1', 'width=865,height=650,toolbar=no,toolbar=0,scrollbars=yes,location=0,statusbar=0,menubar=0,resizable=1,top='+(screen.height/2-300)+',left='+(screen.width/2-400));
  return false;
}
