/**
* OO js Calendar
* @author Lawrence Curtis - Lawton
*/
 
var outwardCalender, returnCalender;
 
var heroNights = Class.create({
    initialize: function(options) {
        this.options = {
            start: "fieldname",
            end:   "fieldname",
            diff:  "fieldname",
            min:   1,
            max:   0
        };

        // Setup options
        Object.extend(this.options, options || {});

        // Put calendar listeners in place
        outwardCalender = new Epoch(this.options.start, 'popup', $(this.options.start));
        returnCalender  = new Epoch(this.options.end,   'popup', $(this.options.end));

        outwardCalender.selectedDates = new Array(outwardCalender.selectedDate);
        returnCalender.selectedDates  = new Array(returnCalender.selectedDate);
 
        outwardCalender.reDraw();
        returnCalender.reDraw();
 
        outwardCalender.show = function () { 
            this.calendar.style.display = 'block';
        }
 
        returnCalender.show = function () {  
            this.calendar.style.display = 'block';      
        }
 
        // hider and updater
        outwardCalender.hide = function () {
            this.calendar.style.display = 'none';
            this.visible = false;   
            // returnCalender.tgt.value = outwardCalender.tgt.value;
            // returnCalender.selectedDates = new Array(this.selectedDates[0]);
            // returnCalender.goToMonth(this.displayYear, this.displayMonth); 
            // returnCalender.reDraw();
            
            
            start = outwardCalender.tgt.value.split("/");
            end   = new Date(start[2], start[1]-1, parseInt(start[0], 10) + 1);
            
            returnCalender.tgt.value = end.dateFormat();  
            returnCalender.selectedDates = new Array(end);
            returnCalender.goToMonth(this.displayYear, this.displayMonth); 
            returnCalender.reDraw();
            
        }
 
       this.diffInit();  
 
    },

    update: function() {
        start = $(this.options.start).value.split("/");
        end   = new Date(start[2], start[1]-1, (parseInt(start[0], 10) + parseInt($(this.options.diff+"-input").value)));

        returnCalender.tgt.value = end.dateFormat();  
 
        returnCalender.selectedDates = new Array(end);
        returnCalender.goToMonth(start[2], start[1] - 1); 
        returnCalender.reDraw();
    },
 
    diffInit: function() {
        var down = new Element('span');
        down.innerHTML = ("-");
        down.addClassName("down");
 
        var input = new Element('input');
        input.setAttribute("type", "text");
        input.setAttribute("id", this.options.diff+"-input");
        input.setAttribute("value", 1);
 
        up = new Element('span');
        up.innerHTML = ("+");
        up.addClassName("up");
 
        $(this.options.diff).appendChild(down);
        $(this.options.diff).appendChild(input);
        $(this.options.diff).appendChild(up);
 
        Event.observe(up, 'click', this.diffUp.bindAsEventListener(this));
        Event.observe(down, 'click', this.diffDown.bindAsEventListener(this));
 
        this.diffUpdate();
    },
 
    diffDown: function() {
        if ($(this.options.diff+"-input").value > this.options.min) {
            $(this.options.diff+"-input").value--;
            this.update();
        };
    },
 
    diffUp: function() {
        if (this.options.max == 0 || $(this.options.diff+"-input").value < this.options.max) {
            $(this.options.diff+"-input").value++;
            this.update();
        };
    },
 
    diffUpdate: function() {  
        
        var doCalender = false;
        var diff = returnCalender.selectedDates[0].getTime() - outwardCalender.selectedDates[0].getTime();
        if (returnCalender.selectedDates[0].getTime() <= outwardCalender.selectedDates[0].getTime()) {
            diff = 86400000; // one day
            doCalender = true;
        }
        
        if (this.options.max > 0  && diff > 86400000 * this.options.max) {
            diff = 86400000 * this.options.max; // max days
            doCalender = true;
        }
        
        if (doCalender) {
            start = outwardCalender.tgt.value.split("/");
            end   = new Date(start[2], start[1]-1, parseInt(start[0], 10) + Math.round(diff / 86400000));
        
            returnCalender.tgt.value = end.dateFormat();  
            returnCalender.selectedDates = new Array(end);
            returnCalender.goToMonth(parseInt(start[2], 10), parseInt(start[1]-1, 10)); 
            returnCalender.reDraw();
        }
        
        $(this.options.diff+"-input").value = Math.round(diff / 86400000);
        
    }

});



var heroCal = Class.create({
    initialize: function(options) {
 
        this.options = {
            container: "cal",
            field: "date",
            label: "",
            parent: false,
            min: 1,
            max: 999,
            diff: false,
            backText: " &lt; ",
            forwardText: " &gt; ",
            start: false
        };
        this.days = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];
        this.months = ['January', 'February', 'March', 'April',
        'May', 'June', 'July', 'August', 'September',
        'October', 'November', 'December'];
        this.days_in_months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
 
        Object.extend(this.options, options || {});
        
        var onComplete = this.options.onComplete;
        this.options.onComplete = (function(response, param) {
          if (Object.isFunction(onComplete)) onComplete(response, param);
        }).bind(this);
 
        if (this.options.start) {
            var dates = this.options.start.split("-");
 
            year = Math.round(dates[0]);
            month = Math.round(dates[1]);
            day = Math.round(dates[2]);
 
            if (this.options.parent) {
                day += this.options.min
 
                this.options.start = year + "-" + month + "-" + day;
            }
 
            this.startDate = new Date(year, month - 1, day);
 
            $(this.options.field).value = this.options.start;
        };
 
        if ($(this.options.field).value) {
            this.getDay($(this.options.field).value);
        };
 
    },
 
    getWeek: function(options) {
        defaults = {
            year: 2008,
            month: 10,
            day: 10
        };
        Object.extend(defaults, options || {});
 
        var firstDay = new Date(options.year, options.month - 1, options.day);
        var startingDay = firstDay.getDay();
        today = options.day - startingDay;
 
        if (!options.selected) {
            options.selected = options.day;
        };
 
        var row = '<table border="0" cellspacing="0" cellpadding="0" width="100%"><tr>';
 
        for (var day = 0; day <= 6; day++) {
            var date = new Date(options.year, options.month - 1, today + day);
            
            var className = "";
 
            className += ("day-" + date.getDay())
 
            if (date.getMonth() == options.month - 1) {
                className += (" day");
            }
            
            if (this.options.parent && date.getTime() >= this.options.parent.date.getTime() && date.getTime() < this.date.getTime()) {
                className += (" highlight");
                if (date.getTime() == this.options.parent.date.getTime()) {
                    className += (" other-today");
                }
            };
            
            if (this.options.child && date.getTime() <= this.options.child.date.getTime() && date.getTime() > this.date.getTime()) {
                className += (" highlight");
                if (date.getTime() == this.options.child.date.getTime()) {
                    className += (" other-today");
                }
            };
            
            if (this.options.start && this.startDate.getTime() > date.getTime() && date.getMonth() == options.month - 1) {
                className += (" past");
            };
            
            if (this.date && date.getTime() == this.date.getTime()) {
                className += (" today");
            }
 
            var rel = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
            row += '<td class="'+className+'"><a rel="'+rel+'">'+date.getDate()+'</a></td>';
 
        }
 
        html = row+"</tr></table>";
 
        return html;
    },
 
    click: function(e) {
        if (Event.element(e).rel) {
            this.getDay(Event.element(e).rel);
        }
    },
 
    getMonth: function(options) {
        defaults = {
            year: 2008,
            month: 10,
            day: 10
        };
        Object.extend(defaults, options || {});
 
        var output = new Element('div');
 
        output.appendChild(this.header());
 
        var container = new Element('div');
 
        for (var week = 0; week < 6; week++) {
            container.innerHTML += (this.getWeek({
                year: options.year,
                month: options.month,
                selected: options.day,
                day: week * 7
            }));
        }
 
        output.appendChild(container);
 
        return output;
    },
 
    getDay: function(date) {
        var dates = date.split("-");
 
        this.year = Math.round(dates[0]);
        this.month = Math.round(dates[1]);
        this.day = Math.round(dates[2]);
 
        this.date = new Date(this.year, this.month - 1, this.day);
 
        if (this.startDate && this.date.getTime() <= this.startDate.getTime()) {
            this.date = this.startDate;
            this.year = this.startDate.getFullYear();
            this.month = this.startDate.getMonth() + 1;
            this.day = this.startDate.getDate();
 
            $(this.options.field).value = this.options.start;
            this.update();
        };
 
        this.setDate();
 
        if (this.options.child) {
            var minDate = new Date(this.year, this.month - 1, this.day + this.options.child.options.min);
            var maxDate = new Date(this.year, this.month - 1, this.day + this.options.child.options.max);
 
            var newDate = (maxDate.getTime() < minDate.getTime()) ? maxDate: minDate;
 
            if (!this.options.child.date || (this.options.child.date && newDate.getTime() > this.options.child.date.getTime()) || (maxDate.getTime() < minDate.getTime())) {
                this.options.child.date = newDate;
 
                this.options.child.year = newDate.getFullYear();
                this.options.child.month = newDate.getMonth() + 1;
                this.options.child.day = newDate.getDate();
                this.options.child.setDate();
 
            };
            this.options.child.getDiff();
 
        } else if (this.options.parent) {
 
            var minDate = new Date(this.year, this.month - 1, this.day - this.options.min);
            var maxDate = new Date(this.year, this.month - 1, this.day - this.options.max);
 
            if (!this.options.parent.date || (this.options.parent.date && this.options.parent.date.getTime() > minDate.getTime())) {
 
                this.options.parent.year = minDate.getFullYear();
                this.options.parent.month = minDate.getMonth() + 1;
                this.options.parent.day = minDate.getDate();
                this.options.parent.date = minDate;
                this.options.parent.setDate();
 
            } else if (this.options.parent.date && this.options.parent.date.getTime() < maxDate.getTime()) {
 
                this.options.parent.year = maxDate.getFullYear();
                this.options.parent.month = maxDate.getMonth() + 1;
                this.options.parent.day = maxDate.getDate();
                this.options.parent.date = maxDate;
                this.options.parent.setDate();
 
            }
            this.getDiff();
        }
    },
 
    getDiff: function() {
        if (this.options.diff) {
            var diff = this.date.getTime() - this.options.parent.date.getTime();
            diff = Math.round(diff / 86400000)
            $(this.options.diff).down("input").value = diff;
        };
 
        if (!this.options.ajax) {
            this.options.ajax = 1;
            if (this.options.onComplete) this.options.onComplete.bind(this).defer();
        }
    },
 
    setDiff: function() {
        if (event.keyCode == Event.KEY_UP) {
            $(this.options.diff).down("input").value++;
        } else if (event.keyCode == Event.KEY_DOWN) {
            this.diffDown();
        }
 
        if (event.keyCode != Event.KEY_LEFT && event.keyCode != Event.KEY_RIGHT) {
            this.updateDiff();
        }
    },
 
    updateDiff: function() {
        if (parseInt($(this.options.diff).down("input").value)) {
            var newDate = new Date(this.options.parent.year, this.options.parent.month - 1, this.options.parent.day + parseInt($(this.options.diff).down("input").value));
            this.getDay(newDate.getFullYear() + "-" + (newDate.getMonth() + 1) + "-" + newDate.getDate());
        };
    },
 
    diffDown: function() {
        if ($(this.options.diff).down("input").value > this.options.min) {
            $(this.options.diff).down("input").value--;
            this.updateDiff();
        };
    },
 
    diffUp: function() {
        if ($(this.options.diff).down("input").value < this.options.max) {
            $(this.options.diff).down("input").value++;
            this.updateDiff();
        };
    },
 
    update: function() {
        var dates = $(this.options.field).value.split("-");
 
        $(this.options.container).innerHTML = (this.getMonth({
            day: Math.round(dates[2]),
            month: Math.round(dates[1]),
            year: Math.round(dates[0])
        }).innerHTML);
 
        if (this.options.child && $(this.options.container).down(".today")) {
            Element.clonePosition("starter", $(this.options.container).down(".today"), {
                offsetTop: -18,
                setWidth: false,
                setHeight: false,
                offsetLeft: -2
            });
            $("starter").show();
        } else if (this.options.child) {
            $('starter').hide();
        };
 
        if (this.options.parent && $(this.options.container).down(".today")) {
            Element.clonePosition("ender", $(this.options.container).down(".today"), {
                offsetTop: 20,
                setWidth: false,
                setHeight: false,
                offsetLeft: -2
            });
            $("ender").show();
        } else if (this.options.parent) {
            $("ender").hide();
        }
 
        $(this.options.container).down("span.month").innerHTML = ("<strong>" + this.months[this.month - 1] + "</strong> " + this.year.toString().substring(2, 4));
        Event.observe($(this.options.container).down(".backButton"), 'click', this.monthDown.bindAsEventListener(this));
        Event.observe($(this.options.container).down(".forwardButton"), 'click', this.monthUp.bindAsEventListener(this));
        Event.observe($(this.options.container).down("div"), 'click', this.click.bindAsEventListener(this));
 
        if (this.options.start && this.year <= this.startDate.getFullYear() &&
        this.month - 1 <= this.startDate.getMonth()) {
            var opacity = 0.5;
        } else {
            var opacity = 1;
        }
 
        $(this.options.container).down(".backButton").setStyle({
            opacity: opacity
        });
    },
 
    monthUp: function() {
        this.getDate();
 
        if (this.month <= 11) {
            this.month += 1;
        } else {
            this.year += 1;
            this.month = 1;
        }
 
        this.setDate();
    },
 
    starter: function() {
        var date = new Date(this.year, this.month - 1, this.day - 7);
        this.getDay(date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate());
    },
 
    ender: function(event) {
        var date = new Date(this.year, this.month - 1, this.day + 7);
        this.getDay(date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate());
    },
 
    monthDown: function() {
        this.getDate();
 
        if (this.options.start && this.year <= this.startDate.getFullYear() &&
        this.month - 1 <= this.startDate.getMonth()) {
            return false;
        };
 
        if (this.month > 1) {
            this.month -= 1;
        } else {
            this.year -= 1;
            this.month = 12;
        }
 
        this.setDate();
    },
 
    getDate: function() {
        var dates = $(this.options.field).value.split("-");
 
        this.year = Math.round(dates[0]);
        this.month = Math.round(dates[1]);
        this.day = Math.round(dates[2]);
    },
 
    setDate: function() {
        $(this.options.field).value = this.year + "-" + this.month + "-" + this.day;
        this.update();
 
        if (this.options.parent) {
            this.options.parent.update();
        };
 
        if (this.options.child) {
            this.options.child.update();
        };
    },
 
    header: function() {
        var table = new Element('table', {
            width: "100%"
        });
 
        table.addClassName('monthNames')
 
        // month
        var row = new Element('tr')
        var cell = new Element('td');
        cell.setAttribute('colSpan', 7);
        cell.addClassName("controls")
 
        row.appendChild(cell);
        table.appendChild(row);
 
        var back = new Element('span');
        back.addClassName("backButton");
        back.title = "Back";
        back.innerHTML = (this.options.backText)
 
        var month = new Element('span');
        month.addClassName("month");
 
        var forward = new Element('span');
        forward.addClassName("forwardButton");
        forward.title = "Forward";
        forward.innerHTML = (this.options.forwardText)
 
        var label = new Element('span');
        label.addClassName("label");
        label.innerHTML = (this.options.label);
 
        cell.appendChild(back);
        cell.appendChild(month);
        cell.appendChild(forward);
        cell.appendChild(label);
 
        // days
        var row = new Element('tr')
 
        for (var day = 0; day <= 6; day++) {
            var cell = new Element('th');
            cell.innerHTML = (this.days[day]);
            cell.addClassName("day-" + day)
            row.appendChild(cell);
        }
        table.appendChild(row);
 
        return table;
    },
 
    diff: function(element) {
        this.options.diff = element;
 
        if (this.options.parent) {
            this.options.parent.options.child = this;
            this.options.parent.update();
        };
 
        var down = new Element('span');
        down.innerHTML = ("-");
        down.addClassName("down");
 
        var input = new Element('input');
        input.setAttribute("type", "text");
        input.setAttribute("id", element+"-input");
 
        up = new Element('span');
        up.innerHTML = ("+");
        up.addClassName("up");
 
        $(this.options.diff).appendChild(down);
        $(this.options.diff).appendChild(input);
        $(this.options.diff).appendChild(up);
 
        Event.observe(this.options.diff, 'keyup', this.setDiff.bindAsEventListener(this));
        Event.observe(up, 'click', this.diffUp.bindAsEventListener(this));
        Event.observe(down, 'click', this.diffDown.bindAsEventListener(this));
 
        Event.observe($("ender"), 'click', this.ender.bindAsEventListener(this));
        Event.observe($("starter"), 'click', this.starter.bindAsEventListener(this));
 
        this.getDiff();
    }
});
 
var heroRate = Class.create({
    initialize: function(options) {
 
        this.options = {
            container: "rate",
            field: "rate-field",
            max: 5,
            min: 1
        };
 
        Object.extend(this.options, options || {});
 
        var output = new Element('div');
 
        for (var i = this.options.min; i <= this.options.max; i++) {
            var link = new Element('a')
            link.innerHTML = (i);
            link.rel = i;
            output.appendChild(link);
        };
        $(this.options.container).innerHTML = (output.innerHTML);
 
        Event.observe(this.options.container, 'mouseover', this.show.bindAsEventListener(this));
        Event.observe(this.options.container, 'mouseout', this.update.bindAsEventListener(this));
        Event.observe(this.options.container, 'click', this.set.bindAsEventListener(this));
 
        this.update();
    },
    
    show: function(obj) {
        stars = obj.element().rel;
        
        $(this.options.container).childElements().each(function(s, index) {
            s.removeClassName("active");
            if (s.rel <= stars) {
                s.addClassName("active");
            };
        });
        
    },
    
    update: function() {
        current = $(this.options.field).value.split(",");
        
        $(this.options.container).childElements().each(function(s, index) {
            s.removeClassName("active");
            if (current.contains(s.rel)) {
                s.addClassName("active");
            };
        });  
    },
    
    set: function() {
    
        var active = new Array();
    
        $(this.options.container).childElements().each(function(s, index) {
    
            if (s.hasClassName("active")) {
                active[active.length] = s.rel;
            };
    
        });
    
        $(this.options.field).value = active.join(",");
    }
});
 
 
var heroAdjustable = Class.create({
 
    initialize: function(options) {
        this.options = {
            selector: "adjustable",
            max: 20,
            min: 0,
            prices: new Array()
        };
        Object.extend(this.options, options || {});  
        this.create();
    },
    
    create: function() {
        $$("."+this.options.selector).each(function(s, index) {
            
            if (!s.value) {
                s.value = "0";
                                
                if (this.options.prices[s.id]) {
                    s.value = this.options.prices[s.id];
                };
            };
            

            // this.options.max = $(s).readAttribute('capacity');
            var newMax = $(s).readAttribute('capacity');
            
            var minus = new Element("a", { rel: s.id, className: "down" });
            minus.update("-");
            
            var plus = new Element("a", { rel: s.id, className: "up" });
            plus.update("+");
            
            s.insert({
                before: minus,
                after: plus
                });
                
            Event.observe(minus, 'click', this.down.bindAsEventListener(this));
            Event.observe(plus, 'click', this.up.bindAsEventListener(this));
            Event.observe(s, 'change', this.update.bindAsEventListener(this));
            
            this.update(s);
 
        }, this);
    },
    
    down: function(event) {
        var obj = event.element().rel;
        if ($(obj).value > this.options.min) {
            $(obj).value--;
        };
        this.update($(obj));
    },
 
    up: function(event) {
        var obj = event.element().rel;
        
        if ($(obj).value < $(obj).readAttribute('capacity')) {
        // if ($(obj).value < this.options.max) {
            $(obj).value++;
        };
        this.update($(obj));
    },
    
    update: function(obj) {
        
        if (!Object.isElement(obj)) {
            var obj = event.element();
        };
                
        this.options.prices[obj.id] = parseInt(obj.value);    
        $(obj.id+"_total").down("span").innerHTML = (parseInt(obj.value) * parseInt($(obj.id+"_price").down("span").innerHTML.replace(",", ""))).toFixed(2);
    }
    
});
 
function selectAll(obj) {
    var objects = $(obj).descendants();
    
    objects.each(function(s, index) {
        if (s.nodeName == "INPUT") {
            s.checked = true;
        };
    });
}
 
function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}
 
var heroAccordion = Class.create({
	
	toggles: null,
	contents: null,
	
    initialize: function(options) {
        this.options = {
            container: "accordion_container",
            toggle: "accordion_toggle",
            content: "accordion_content",
            load: 0,
            snap: true
        };
        Object.extend(this.options, options || {});
        
        this.toggles = $$("."+this.options.toggle);
    	this.contents = $$("."+this.options.content);
    	
        this.create();
    },
    
    create: function() {
        this.total = this.toggles.length;

        this.contents.each(function(s, index) {
            s.hide();
        });
        
        this.toggles.each(function(s, index) {
            if (this.total > 1) {
                Event.observe(s, 'click', this.up.bindAsEventListener(this));
            };
            if (this.options.load == index) {
                this.up(s);
            };
        }, this);  
    },
    
    up: function(obj) {
    	
        if (!Object.isElement(obj)) {
            var obj = obj.element();
        }
        
        if (obj.nodeName != "LI") {
            obj = obj.up("LI");
        }
        // alert(obj.nodeName);
        this.toggles.each(function(s, index) {
            if (s != obj && s.hasClassName("active")) {
                  rel = s.getAttribute("rel");
                  this.contents.each(function(ss, ii) {
                      if (ss.getAttribute("rel") == rel) {
                          ss.hide();
                      };
                  });
                  s.removeClassName("active");
            } else if(s == obj) {
              this.options.load = index;
            }
        }, this);
        
        if (!obj.hasClassName("active")) {            
            rel = obj.getAttribute("rel");
            this.contents.each(function(ss, ii) {
                if (ss.getAttribute("rel") == rel) {
                    ss.show();
                };
            });
            
            if (this.total > 1) {            
                obj.addClassName("active");
            } else {
                obj.addClassName("single");
            };
        };
    }
});
 
function array_extender (array, start, stop) {
    if (start < stop) {
        for (var i=start; i <= stop; i++) {
            array[array.length] = i;
        };
    } else {
        for (var i=start; i >= stop; i--) {
            array[array.length] = i;
        };
    };
 
    return array;
}
 
var heroTravelers = Class.create({
    initialize: function(options) {
        this.options = {
            container: "travelers",
            count: 1, // initial count
            structure: {},
            i: 0, // counter do not modify
            data: [],
            stash: []
        };
        Object.extend(this.options, options || {});
        
        if (this.options.data.length > 0) {
            this.options.count = this.options.data.length;
        };
 
        for (var i=0; i < this.options.count; i++) {
            this.create();
        };
    },
 
    // div[.travel-row]: 
    // [ title | v ] [ firstname ] [ lastname ] [ day | v ] [ month | v ] [ year | v ]
    create: function() {
        var container =  new Element("span", {
            'class': "travel-row",
            id: "traveler-"+(this.options.i)
        });
        
        var classes = new Array("title", "text", "text", "day", "month", "year");
        var j = 0;
        
        for(var obj in this.options.structure ) {
            if (isArray(this.options.structure[obj])) {
                
                var select = new Element("select", {
                    name: "data[travelers]["+this.options.i+"]["+obj+"]",
                    "class": classes[j],
                    id: classes[j] + '_' + (this.options.i)
                });
 
                for (var i=0; i < this.options.structure[obj].length; i++) {
                    var text = this.options.structure[obj][i];
 
                    var option = new Element('option', {
                        id: text
                    }).update(text);
                    
                    if (this.prefill(obj) && this.options.data[this.options.i][obj] == text) {
                        option.setAttribute('selected', "selected");
                    }
 
                    select.appendChild(option);
                };
 
                container.appendChild(select);
 
            } else {
                var input = new Element("input", {
                    value: this.options.structure[obj],
                    type: "text",
                    name: "data[travelers]["+this.options.i+"]["+obj+"]",
                    "class": classes[j],
                    id: obj + '_' + (this.options.i)
                });
                
                if (this.prefill(obj)) {
                    input.value = this.options.data[this.options.i][obj];
                }
                
                Event.observe(input, 'focus', function() {
                    if (this.value == "First Name" || this.value == "Surname") {
                        this.value = "";
                        // this.clean.bindAsEventListener(this);
                    }
                });
                Event.observe(input, 'blur', function() {
                    if (this.value == "") {
                        if (this.id.substr(0,1) == "f") {
                            this.value = "First Name";
                        } else {
                            this.value = "Surname";
                        }
                        //this.dirty.bindAsEventListener(this);
                    }
                });
 
                container.appendChild(input);
            };
            j++;   
        }
        
        if (this.options.i > 0) {
            var a = new Element("a", {
                'class': "remove-traveler-row",
                rel: this.options.i
            }).update("remove");
 
            container.appendChild(a); 
            Event.observe(a, 'click', this.destroy.bindAsEventListener(this));
        }
        
        $(this.options.container).insert({
            bottom: container
        });
 
        this.options.i++;
    }, 
    
    destroy: function(e) {
        $("traveler-"+(Event.element(e).rel)).remove();
    },
    
    prefill: function(obj) {
       return (this.options.data.length > this.options.i) ? true : false;
    },
    
    clean: function(event) {
        obj = event.element();
        this.options.stash[obj.id] = obj.value;
        obj.value = "";
    }, 
    
    dirty: function (event) {
        obj = event.element();
        if (obj.value == "") {
            obj.value = this.options.stash[obj.id];
        };
    }
    
});
