CategoryController = new Class({
    initialize: function() {
        var container = $(document.body).getElement('ul#categories');

        // guard
        if (! container) {
            return;
        }

        this.active = null;
        var category_arr = container.getChildren();
        for (var i = 0; i < category_arr.length; i++) {
            var category = new Category(this, category_arr[i]);

            if (category_arr[i].hasClass('sel')) {
                this.active = category;
            }
        }
    },

    setActive: function(category) {
        if (this.active == category) {
            return;
        }

        if (this.active) {
            this.active.hide();
        }

        this.active = category;
        this.active.show();
    }
});

Category = new Class({
    initialize: function(controller, container) {
        this.container = container;
        var id = this.container.get('id');
        this.anchor = container.getElement('a');
        this.form = $('form-'+ id);

        if (! this.anchor || ! this.form) {
            return;
        }

        var thisObject = this;
        this.anchor.addEvent('click', function(event) {
          this.blur();
          event.stop();
          controller.setActive(thisObject);
        });
    },

    show: function(){
        this.container.addClass('sel');
        this.form.addClass('sel');
    },

    hide: function() {
        this.container.removeClass('sel');
        this.form.removeClass('sel');
    }
});

function setupTextfieldResetter(){
  $$('.formdescription').each(function(el){
    var originalValue = el.value;

    el.addEvent('focus', function(){
      if (this.value == originalValue){
        this.value = '';
      }
    });

  });
}

function setupFooterlinks() {
  if(top.frames.length > 0) {
    $$('a').each(function(hyperlink) {
	  hyperlink.target = '_parent';
	});
  }
}

function setupDatepickerfix() {
  var quicksearch = $('content-left');
  if(quicksearch) {
    $$('a.calendar').each(function(hyperlink) {
      hyperlink.addEvent('click', function(e) {
	    quicksearch.addClass('datepicker-opened');
	    e.stop();
	  });
    });
  }
}

window.addEvent('load', setupFooterlinks);
window.addEvent('load', setupTextfieldResetter);

//if(Browser.Engine.trident) {
  window.addEvent('load', setupDatepickerfix);
//}

window.addEvent('load', function() {
  var cc = new CategoryController();
  
  if (currentdateformat){
    var dateformat = currentdateformat;
  }else {
    var dateformat = 'd/m/Y';
  }

  if (currentmonthnames){
    var monthnames = currentmonthnames;
  }else {
    var monthnames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  }

  if (currentdaynames){
    var daynames = currentdaynames;
  }else {
    var daynames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  }


  /* cars form */
  new DatePicker('.form-cars-pickup-display', {
	pickerClass: 'datepicker form-cars-pickup-datepicker',
	allowEmpty: true,
	toggleElements: '.form-cars-pickup-toggler',
  format: dateformat,
  inputOutputFormat: dateformat,
  months: monthnames,
  days: daynames
  });
  new DatePicker('.form-cars-dropoff-display', {
	pickerClass: 'datepicker form-cars-dropoff-datepicker',
	allowEmpty: true,
	toggleElements: '.form-cars-dropoff-toggler',
  format: dateformat,
  inputOutputFormat: dateformat,
  months: monthnames,
  days: daynames
  });

  /* hotel form */
  new DatePicker('.form-hotel-checkin-display', {
	pickerClass: 'datepicker form-hotel-checkin-datepicker',
	allowEmpty: true,
	toggleElements: '.form-hotel-checkin-toggler',
  format: dateformat,
  inputOutputFormat: dateformat,
  months: monthnames,
  days: daynames
  });
  new DatePicker('.form-hotel-checkout-display', {
	pickerClass: 'datepicker form-hotel-checkout-datepicker',
	allowEmpty: true,
	toggleElements: '.form-hotel-checkout-toggler',
  format: dateformat,
  inputOutputFormat: dateformat,
  months: monthnames,
  days: daynames
  });

  /* activities form */
  new DatePicker('.form-activities-startdate-display', {
	pickerClass: 'datepicker form-activities-startdate-datepicker',
	allowEmpty: true,
	toggleElements: '.form-activities-startdate-toggler',
  format: dateformat,
  inputOutputFormat: dateformat,
  months: monthnames,
  days: daynames
  });
  new DatePicker('.form-activities-enddate-display', {
	pickerClass: 'datepicker form-activities-enddate-datepicker',
	allowEmpty: true,
	toggleElements: '.form-activities-enddate-toggler',
  format: dateformat,
  inputOutputFormat: dateformat,
  months: monthnames,
  days: daynames
  });

  /* flights form */
  new DatePicker('.form-flights-leavingdate-display', {
	pickerClass: 'datepicker form-flights-leavingdate-datepicker',
	allowEmpty: true,
	toggleElements: '.form-flights-leavingdate-toggler',
  format: dateformat,
  inputOutputFormat: dateformat,
  months: monthnames,
  days: daynames
  });
  new DatePicker('.form-flights-returndate-display', {
	pickerClass: 'datepicker form-flights-returndate-datepicker',
	allowEmpty: true,
	toggleElements: '.form-flights-returndate-toggler',
  format: dateformat,
  inputOutputFormat: dateformat,
  months: monthnames,
  days: daynames
  });

  /* vacationpackages form */
  new DatePicker('.form-vacationpackages-fromdate-display', {
	pickerClass: 'datepicker form-vacationpackages-fromdate-datepicker',
	allowEmpty: true,
	toggleElements: '.form-vacationpackages-fromdate-toggler',
  format: dateformat,
  inputOutputFormat: dateformat,
  months: monthnames,
  days: daynames
  });
  new DatePicker('.form-vacationpackages-todate-display', {
	pickerClass: 'datepicker form-vacationpackages-todate-datepicker',
	allowEmpty: true,
	dateformat: '.form-vacationpackages-todate-toggler',
  format: dateformat,
  inputOutputFormat: dateformat,
  months: monthnames,
  days: daynames
  });
});

Cufon.DOM.ready(function() {
  Cufon.replace('h1')('h2')('h3');
  if (document.getElementById('nav-main')){
    Cufon.replace(document.getElementById('nav-main').getElementsByTagName('a'),{hover: true});
  }
});