// require(MochiKit);

function GuideSelector(country_sel, city_sel) {
    this.country_select = country_sel;
    this.city_select = city_sel;
    this.rss_req = doSimpleXMLHttpRequest('/guides.rss.xml');
    this.preselect_by_name = null;
    this.preselect = 2001;
    this.by_id = {};
    this.by_country = {};
};

GuideSelector.prototype.syncSelects = function() {	
        
    var country = this.country_select.options[this.country_select.selectedIndex].value;
	this.countryname=country;
    var selected = null;
    var lowest_id = 99999;
    var lowest_index = 0;
    this.city_select.options.length = 0;
	var countryLen =  this.by_country[country].length;
    for(var c = 0; c < countryLen; c++) {
        var guide = this.by_country[country][c];
        var option = new Option(guide.title, guide.id);
        this.city_select.options[this.city_select.options.length] = option;
        //alert('comparing ' + this.by_id[option.value].country + ' with ' + country);
        if(option.value == this.preselect) {
            selected = option;
            this.city_select.selectedIndex = c;
        } else if(guide.id < lowest_id) {
            lowest_id = guide.id;
            lowest_index = c;
        }
    }
	 
    if(selected === null)
        this.city_select.selectedIndex = lowest_index;

	
	
	//signal(this, 'country changed'); 
    //signal(this, 'citychanged'); 
	//callLater(0.2, err_logging(onChangeCityName));	
	
	this.cityChanged();
	
};

GuideSelector.prototype.setup = function(data) {    
    var rss_guides = data.responseXML.getElementsByTagName('item');
    var selected = null;
    var guideLen = rss_guides.length
    for(var i = 0; i < guideLen; i++) {
        var item = rss_guides[i];
        var guide = {};
        var itemLen = item.childNodes.length
        for(var c = 0; c < itemLen; c++) {
            var attr = item.childNodes[c];
            if(attr.nodeType != 1) continue;
            if((attr.tagName == 'category') && !(guide.category === undefined))
                var name = 'country';
            else
                var name = attr.tagName;
            text = scrapeText(attr);
            if(name == 'guid') {
                if(text.substring(0, 7) != 'guides/')
                    continue;
                guide.id = text.substring(7);
                if(guide.id == this.preselect)
                    selected = guide;
            } else
                guide[name] = text;
        }
        if(guide.country) {
            this.by_id[guide.id] = guide;
            if(this.by_country[guide.country] === undefined)
                this.by_country[guide.country] = [];
            this.by_country[guide.country].push(guide);
            if(this.preselect_by_name &&
               (this.preselect === null) &&
               (this.preselect_by_name == guide.title)) {
                this.preselect_by_name = null;
                this.preselect = guide.id;
                selected = guide;
            }
        }
    }
    var countries = keys(this.by_country);
    countries.sort();
    var country;
    var countriesLen = countries.length;
    for(var i=0;i<countriesLen;i++){
        //country = countries[country];		
		country=countries[i];
        this.by_country[country].sort(keyComparator('title'));		
        this.country_select.options[this.country_select.options.length] =
            new Option(country, country);
        if(selected && (country == selected.country))
            this.country_select.selectedIndex = this.country_select.options.length - 1;
    }	
    this.syncSelects();	
	
    connect(this.country_select, 'onchange', this, 'syncSelects');
    connect(this.city_select, 'onchange', this, 'cityChanged');
    signal(this, 'ready');
};

GuideSelector.prototype.ready = function(errback) {
    this.country_select = $(this.country_select);
    this.city_select = $(this.city_select);
    this.rss_req.addCallbacks(bind(this.setup, this), errback||null);
    log('guide selector ready to rock');
};

GuideSelector.prototype.rebind = function(country_sel, city_sel) {
    this.country_select = $(country_sel);
    this.city_select = $(city_sel);
    connect(this.country_select, 'onchange', this, 'syncSelects');
    connect(this.city_select, 'onchange', this, 'cityChanged');
};

GuideSelector.prototype.get_guide = function() {
    return this.by_id[this.city_select.options[this.city_select.selectedIndex].value];
};

GuideSelector.prototype.select = function(id) {
    log('requested to select', repr(id));
    if(isNaN(id)) {
        for(g_id in this.by_id) {
            guide = this.by_id[g_id];
            if(guide.title == id) {
                id = g_id;
                break;
            }
        }
    }
    log('resolved it to', repr(id));
    if(isNaN(id)) {
        this.preselect = null;
        this.preselect_by_name = id;
    } else {
        this.preselect = id;
    }
    if(this.city_select.options !== undefined) {
        var guide = this.by_id[id];
        var countryLen = this.country_select.options.length ;
        for(var c = 0; c < countryLen; c++)
        var myOption = this.country_select.options[c];
            if(myOption.value == guide.country)
                this.country_select.selectedIndex = c;
        this.syncSelects();
        var cityLen = this.city_select.options.length ;
        for(var c = 0; c < cityLen; c++)
            if(this.city_select.options[c].value == this.preselect)
                this.city_select.selectedIndex = c;
    }
};

GuideSelector.prototype.cityChanged = function() {		
    signal(this, 'citychanged');	
};
