﻿var geoLocation = new Class({
    //mode : 'state','country','continent','worldwide'
    Implements: [Options, Events],
    options: {
    mode:'state',
    state:'0',
    stateText:'Unspecified State',
    country:'0',
    countryText:'Unspecified Country',
    continent:'0',
    continentText:'Unspecified Continent',
    onChange: $empty
    },
    initialize: function(element,fileName,options){
        this.setOptions(options);
        if(options.mode){
            this.mode=options.mode;
        }
        if(options.state){
            this.state=options.state;
        }
        else{
            this.state='0';
        }
        if(options.stateText){
            this.stateText=options.stateText;
        }else{
            this.stateText='Unspecified State';
        }
        
        if(options.country){
            this.country=options.country;
        }else{
            this.country='0';
        }

        if(options.countryText){
            this.countryText=options.countryText;
        }else{
            this.countryText='Unspecified Country';
        }
        
        if(options.continent){
            this.continent=options.continent;
        }else{
            this.continent='0';
        }
        
        if(options.continentText){
            this.continentText=options.continentText;
        }else{
            this.continentText='Unspecified Continent';
        }
        this.element = $(element);
        this.fileName = fileName;
        
        if(this.element.getElement("a")){
            this.displayElement = this.element.getElement("a");
        }else{
            this.displayElement = new Element("a");
            this.displayElement.inject(this.element);
        }
        var oThis =this;
        this.items = [];
        this.displayElement.addEvent('click',function(){
            oThis.showSelect();
        });

        this.showText();
    },
    fire: function (){
        this.showText();
        this.fireEvent('onChange');
    },
    setMode: function(newMode){
        if(this.request){
            this.request.cancel();
        }
        
        this.mode = newMode;
        this.showText();
    },
    showSelect:function(){
        this.element.getElements("select").each(function(el){
            el.style.display="none";
        });
        var lst;

        switch(this.mode){
            case 'state':
                lst = this.getSelectControl();
                if(lst.length==0){
                    this.getJSON(this.fileName+"?mode=state&value="+this.state+'&country=' + this.country);
                }else{
                    var prevCountry = lst.get('country');
                    if(prevCountry==this.country){
                        //lst.value = this.state;
                        lst.style.display="block";
                    }else{
                        this.getJSON(this.fileName+"?mode=state&value="+this.state+'&country=' + this.country);
                    }
                }
                this.displayElement.style.display="none";
                break;
            case 'country':
                lst = this.getSelectControl();
                if(lst.length==0){
                    this.getJSON(this.fileName+"?mode=country&value="+this.country+'&continent='+ this.continent);
                }else{
                    //lst.value = this.country;
                    lst.style.display="block";
                }
                this.displayElement.style.display="none";
                break;
            case 'continent':
                lst = this.getSelectControl();
                if(lst.length==0){
                    this.getJSON(this.fileName+"?mode=continent&value="+this.continent);
                }else{
                    //lst.value = this.continent;
                    lst.style.display="block";
                }
                this.displayElement.style.display="none";
                break;
            case 'wordwide':
                return;
                break;
            default:
        }
        //this.displayElement.setStyle('display','none');
    },
    getSelectControl:function(){
        var lst;
        switch(this.mode){
            case 'state':
                lst = this.element.getElement(".state");
                if(!lst){
                    lst = new Element("select");
                    lst.addClass("state");
                    lst.set('country',this.country);
                    var oThis = this;
                    lst.addEvent("change",function(){
                        var value = this.value;
                        var text = this.options[this.selectedIndex].text;
                        oThis.state = value;
                        oThis.stateText = text;
                        oThis.showText();
                        oThis.fireEvent('onChange');
                    })
                    lst.inject(this.element);
                }
                lst.style.display="none";
                break;
            case 'country':
                lst = this.element.getElement(".country");
                if(!lst){
                    lst = new Element("select");
                    lst.addClass("country");
                    var oThis = this;
                    lst.addEvent("change",function(){
                        var value = this.value;
                        var text = this.options[this.selectedIndex].text;
                        oThis.country = value;
                        oThis.countryText = text;
                        oThis.showText();
                        oThis.fireEvent('onChange');
                    })
                    lst.inject(this.element);
                }
                lst.style.display="none";
                break;
            case 'continent':
                lst = this.element.getElement(".continent");
                if(!lst){
                    lst = new Element("select");
                    lst.addClass("continent");
                    var oThis = this;
                    lst.addEvent("change",function(){
                        var value = this.value;
                        var text = this.options[this.selectedIndex].text;
                        oThis.continent = value;
                        oThis.continentText = text;
                        oThis.showText();
                        oThis.fireEvent('onChange');
                    })
                    lst.inject(this.element);
                }
                lst.style.display="none";
                break;
            case 'wordwide':
                break;
            default:
                break;
        }
        return lst;
    },
    showText:function(){
        this.element.getElements("select").each(function(el){
            el.style.display="none";
        });
        var text = '';
        
        switch(this.mode){
            case 'state':
                text = this.stateText;
                break;
            case 'country':
                text = this.countryText;
                break;
            case 'continent':
                text = this.continentText;
                break;
            case 'wordwide':
                text = 'Wordwide';break;
            default:
                text = 'Unspecified';break;        
        }
        this.displayElement.set('text',text);
        this.displayElement.style.display="block";
    },
    getJSON: function(url){
        var loading = this.element.getElement("#loading");
        if(!loading){
            loading = new Element("div");
            loading.addClass("loading");
            loading.id="loading";
            loading.inject(this.element);
            loading.set('text','loading...');
        }else{
            loading.style.display="block";
        }
		this.request = new Request.JSON({
			'url': url,
			'link': 'cancel'
		}, this.options.ajaxOptions).addEvent('onComplete', this.queryResponse.bind(this));
		this.request.post();
    },
	queryResponse: function(jsonObj) {
	    var lst = null;
	    
	    lst = this.getSelectControl();
	    lst.length = 0;
	    jsonObj.geoLocation.each(function(geo){
	        lst[lst.length] = new Option(geo.textValue,geo.value);
	    })
        switch(this.mode){
            case 'state':
                lst.value = this.state;
                lst.set('country',this.country);
                break;
            case 'country':
                lst.value = this.country;
                break;
            case 'continent':
                lst.value = this.contitent;
                break;
            case 'wordwide':
                break;
            default:
                break;
        }
        var loading = this.element.getElement("#loading");
        if(loading){
            loading.style.display="none";
        }
        this.showSelect();
	}
})
