/**
 *    Iata scripts
 *    --------------------------
 */

DOMContent.onload(function(){

    var body = document.getElementsByTagName("body")[0];
    if(body) {
        ClassName.add(body, 'js-enabled');
    }

    new ToggleController();
    new VisitedCountries();
    new NationalityImplications();
    new FormController();
    new TriggerDispatcher();

});


/**
 *    ToggleController
 *    --------------------------
 */

function ToggleController() {
    LinkListener.register(/(\s|^)toggle-/i, this.handleClick, this);
}

ToggleController.prototype = {
    handleClick:function(link, rel) {
        var type = /toggle-([^ ]+)/i.exec(rel)[1];
        var hash = /#(.*)$/i.exec(link.getAttribute('href'))[1];
        var element = document.getElementById(hash);
        var parent = link.parentNode;
        
        switch (type) {
            case 'open':
                ClassName.add(parent, 'active');
            break;
            case 'close':
                this.tryClose(parent);
            break;
            case 'faq':
                ClassName.toggle(parent, 'open');
            break;
            case 'id':
                ClassName.toggle(element, 'active');
            break;
            case 'countries':
                ClassName.toggle(parent.parentNode, 'active');
                ClassName.swap(parent, 'open', 'close');
            break;
        }

        return true;
    },

    tryClose:function(node) {
        var active = /(^|\s)active(\s|$)/i;
        while(node) {
            if(active.test(node.className)) {
                ClassName.remove(node, 'active');
                return;
            }
            node = node.parentNode;
        }
    }
}


/**
 *    VisitedCountries
 *    --------------------------
 */

function VisitedCountries() {
    this.selectable = document.getElementById("selectvisited");
    this.selected = document.getElementById("visited");

    if(this.selectable && this.selected) {
        EventListener.addEvent(this.selectable, 'click', this.add, this);
        EventListener.addEvent(this.selected, 'click', this.remove, this);
    }
}

VisitedCountries.prototype = {
    add:function(e){
        try {
            this.move(e, this.selectable, this.selected);
        } catch (e) {}
    },

    remove:function(e){
        try {    
            this.move(e, this.selected, this.selectable);
        } catch (e) {}
    },

    move:function(e, origin, target) {
        var option = EventListener.getTarget(e);
        if(/select/i.test(option.nodeName))
            option = option[option.selectedIndex];
        
        if(option && /option/i.test(option.nodeName)) {
            var option = origin.options[origin.selectedIndex];
            if (option.value == 'NL')
            {
                option.text= 'Netherlands';
            }
            if (option.value == 'UK' || option.value == 'GB')
            {
                option.text= 'United Kingdom (Great Britain)'
            }
            target.options[target.options.length] = new Option(option.text, option.value);
            for(var next,i = origin.selectedIndex; i<origin.options.length; i++) {
                next = origin.options[i + 1];
                if(next) {
                    origin.options[i] = new Option(next.text, next.value);
                }
            }
            origin.options.length -= 1;
        }
    }
}


/**
 *    NationalityRelations
 *    --------------------------
 */

function NationalityImplications() {
    var origin = document.getElementById("birthcountry");
    if(origin) {
        EventListener.addEvent(origin, 'change', this.suggest, this);
    }
}

NationalityImplications.prototype = {
    suggest:function(e) {
        var option = EventListener.getTarget(e);
        if(option) {
            this.suggestValue(document.getElementById("residencecountry"), option.value);        
            this.suggestValue(document.getElementById("nationality"), option.value)
        }
    },

    suggestValue:function(select, value) {
        if(select.selectedIndex < 1) {
            for(var i=0; i<select.options.length; i++) {
                if(select.options[i].value == value) {
                    select.selectedIndex = i;
                    return;
                }
            }
        }
    }
}


/**
 *    SubmitButtonController
 *    --------------------------
 */

function FormController() {
    var inputs = Utils.select('input.submit.button');
    inputs.forEach(this.replace, this);

    LinkListener.register(/(\s|^)form-/i, this.handleClick, this);
}

FormController.prototype = {
    replace:function(input) {
        var className = /^submit[ ](.*)$/i.exec(input.className);
        if(className[1]) {
            var link = document.createElement('a');
            link.setAttribute('href', '#');
            link.onclick =  function() { test(); } ;
            link.setAttribute('rel', 'form-submit');
            link.className = className[1];
            var span = document.createElement('span');
            span.appendChild(document.createTextNode(input.value));
            link.appendChild(span);

            input.parentNode.insertBefore(link, input);
        }
    },

    handleClick:function(link, rel) {
        var type = /form-([^ ]+)/i.exec(rel)[1];
        var form = Utils.getParent(link, 'form');

        switch (type) {
            case 'submit':
                var valid = true;
                if(form.onsubmit)
                    valid = form.onsubmit();
                if(valid)          
                    form.submit();    
            break;
            case 'reset':
                form.reset();
            break;
        }         
    }
}


/**
 *    TriggerDispatcher
 *    --------------------------
 */

function TriggerDispatcher() {
    var triggers = Utils.select('div.trigger');
    EventListener.addEvents(triggers, 'click', this.handleClick);
}

TriggerDispatcher.prototype = {
    handleClick:function(e) {
        var link = EventListener.getTarget(e, 'a');
        if(link) { 
            return; 
        } else {
            link = this.getElementsByTagName("a")[0];
            if(link)
                window.open(link.href);
        }
    }
}

function test()
{
    return false;
}