var SearchForm = function () {
    this.thumbs = this.searchMap.getElementsByTagName('img');
    if (this.thumbs.length > 1){
        for (var i=0;i<this.thumbs.length;i++) {
            eventHandler.addEvent(this.thumbs[i], 'click', function (i) {
                return function (e) {
                    e = eventHandler.getEvent(e);
                    e.preventDefault();
                    this.districtName = this.thumbs[i].id;
                    this.thumbs[i].parentNode.href.match(/^.*([0-9]).html$/);
                    this.districtId = RegExp.$1;
                    this.thumbs[i].parentNode.blur();
                    this.setDistrictSelect(this.districtId);
                    this.setRequest(this.insertMap.bindTo(this));
                    if (typeof this.map != 'undefined' && typeof this.map.parentNode != 'undefined')
                        this.map.parentNode.removeChild(this.map);
                    this.request.load('getImageMap.html?districtId='+this.districtId);
                    var date = new Date;
                    this.zipMap = renderer.createElement('div', {'id':'zipMap'});
                    this.zipMap.map = renderer.createElement('img', {
                        'src':this.thumbs[i].src.replace(/\/thumbs/, '')+'?'+date.getMilliseconds(),
                        'className':'iepngfix'
                    })
                    this.zipMap.map.setAttribute('usemap', '#map_'+this.districtName, 0);
                    this.zipMap.map.renderIn(this.zipMap);
                    eventHandler.addEvent(this.zipMap.map, 'load', function () {
                        this.zipMap.renderIn(this.searchMap, 'fade');
                    }.bindTo(this))
                    var zipMapFade;
                    eventHandler.addEvent(this.zipMap, 'fadeready', zipMapFade = function () {
                        var off = renderer.createElement('div', {'id':'zipMapOff', 'className':'aColor'});
                        off.appendChild(document.createTextNode('zurück'));
                        off.renderIn(this.zipMap);
                        eventHandler.addEvent(off, 'click', function () {
                            eventHandler.removeEvent(this.zipMap, 'fadeready', zipMapFade)
                            this.zipMap.remove('fade')
                            this.setDistrictSelect('');
                        }.bindTo(this))
                    }.bindTo(this))
                }.bindTo(this);
            }.call(this, i))
        }
    } else {
        this.map = document.getElementById(this.thumbs[0].getAttribute('usemap').replace(/#/, ''));
        this.areas = this.searchMap.getElementsByTagName('area');
        this.addAreaAction();
    }
}

SearchForm.prototype.setDistrictSelect = function (value) {
    var options = this.form.district.options;
    for (var i=0;i<options.length;i++) {
        options[i].selected = false;
        if (options[i].value == value) {
            options[i].selected = true;
        }
    }
}

SearchForm.prototype.insertMap = function (response) {
    var temp = document.createElement('div');
    temp.innerHTML=response;
    this.map = renderer.assimilate(temp.getElementsByTagName('map')[0]);
    this.areas = this.map.getElementsByTagName('area');
    this.addAreaAction();
    this.map.renderIn(document.body);
}
SearchForm.prototype.addAreaAction = function () {
    for (var i=0;i<this.areas.length;i++) {
        eventHandler.addEvent(this.areas[i], 'click', function (i) {
            return function (e) {
                e=eventHandler.getEvent(e);
                e.preventDefault();
                this.areaAction(i);
            }.bindTo(this)
        }.call(this, i))
    }
}
SearchForm.prototype.setRequest = function (callback) {
    this.request = new HttpRequest;
    this.request.init(callback);
}
SearchForm.prototype.areaAction = function (i) {
    this.form.zip.value = this.map.getElementsByTagName('area')[i].alt;
    this.form.submit();
}

eventHandler.addLoadEvent(function () {
    if (document.getElementById('searchMap')) {
        SearchForm.prototype.searchMap = document.getElementById('searchMap');
        SearchForm.prototype.form = document.getElementById('advancedSearch');
        window.searchForm = new SearchForm;
    }
})
