var FlashMap = {
    _noFlashUpdate: false,
    formId: 'inner-search',
    containerId: 'map',
    lang: Lang.current,
    initialMapId: '',
    height: '280',
    width: '280',
    version: '9.0.0',
    swf: 'locator_map',

    countryId: 109,
    regionId: 0,

    defaultParams: {
        // Italy
        109: {
            url: function(request) {
                request = request || FlashMap.defaultParams[109].urlRequestVars;

                var urls = Array();
                $.each(request, function(index, value){
                    urls.push(index + '=' + (typeof value == 'function' ? value.call() : value));
                });

                return '/media/map/it/' + FlashMap.swf + '.swf?' + urls.join('&');
            },

            urlRequestVars: {
                config:   function(){return FlashMap.defaultParams[109].config();},
                xmlfile1: function(){return '/media/map/it/data/' + Lang.current + '/map.xml';}
            },

            config: function() {return '/media/map/it/config/' + Lang.current + FlashMap.initialMapId + '.xml';},

            // Region relative params
            mapfile:  function(region) {return '/media/map/it/data/' + region + '.swf';},
            xmlfile1: function(region) {return '/media/map/it/data/' + Lang.current + '/' + region + '.xml';},
            xmlfile2: function(region) {return '/media/map/it/data/' + Lang.current + '/' + region + '_data.xml';},

            regionFlashVars: function(region) {
                return {
                    xmlfile1: FlashMap.defaultParams[109].xmlfile1(region),
                    xmlfile2: FlashMap.defaultParams[109].xmlfile2(region),
                    level: 2
                };
            },

            regions: {
                '13' : 'abruzzo',
                '2'  : 'aosta',
                '16' : 'apulia',
                '17' : 'basilicata',
                '18' : 'calabria',
                '15' : 'campania',
                '8'  : 'emilia-romagna',
                '6'  : 'friuli',
                '12' : 'lazio',
                '7'  : 'liguria',
                '3'  : 'lombardy',
                '11' : 'marche',
                '14' : 'molise',
                '1'  : 'piedmont',
                '20' : 'sardinia',
                '19' : 'sicily',
                '4'  : 'trentino',
                '9'  : 'tuscany',
                '10' : 'umbria',
                '5'  : 'veneto'
            }
        },

        // Spain
        67: {
            url: function(request) {
                request = request || FlashMap.defaultParams[67].urlRequestVars;

                var urls = Array();
                $.each(request, function(index, value){
                    urls.push(index + '=' + (typeof value == 'function' ? value.call() : value));
                });

                return '/media/map/es/locator_map.swf?' + urls.join('&');
            },

            urlRequestVars: {
                config:   function(){return FlashMap.defaultParams[67].config();},
                xmlfile1: function(){return '/media/map/es/data/map.xml';}
            },

            config: function() {return '/media/map/es/config/uk' + FlashMap.initialMapId + '.xml';},

            // Region relative params
            mapfile:  function(region) {return '/media/map/es/data/es-' + region + '.swf';},
            xmlfile1: function(region) {return '/media/map/es/data/es-' + region + '.xml';},
            xmlfile2: function(region) {return '/media/map/es/data/es-' + region + '_data.xml';},

            regionFlashVars: function(region) {
                return {
                    xmlfile1: FlashMap.defaultParams[67].xmlfile1(region),
                    xmlfile2: FlashMap.defaultParams[67].xmlfile2(region),
                    level: 2
                };
            },

            regions: {
                '2593109' : 'an',
                '3336899' : 'ar',
                '3336900' : 'cl',
                '2593111' : 'cm',
                '2593110' : 'cn',
                '3336901' : 'ct',
                '2593112' : 'ex',
                '3336902' : 'ga',
                '2521383' : 'ib',
                '3336897' : 'lo',
                '3117732' : 'm',
                '2513413' : 'mu',
                '3115609' : 'na',
                '3114710' : 'o',
                '3336903' : 'pv',
                '3336898' : 's',
                '2593113' : 'vc',

                // We don't have this regions on flash map
                '2519582' : 'Ceuta',
                '6362988' : 'Melilla'
            }
        },

        // France
        74: {
            url: function(request) {
                request = request || FlashMap.defaultParams[74].urlRequestVars;

                var urls = Array();
                $.each(request, function(index, value){
                    value = (typeof value == 'function' ? value.call() : value);

                    if(value.length) {
                        urls.push(index + '=' + value);
                    }
                });

                console.log(urls);
                return '/media/map/fr/locator_map.swf?' + urls.join('&');
            },

            urlRequestVars: {
                xmlfile1: function(){ return '/media/map/fr/locator_map' + FlashMap.initialMapId + '.xml'; },
                datafile: '/media/map/fr/data.swf'
            },

            config: function() { return ""; },

            regionFlashVars: function(region) {
                var xmlfile1 = (typeof FlashMap.defaultParams[74].urlRequestVars.xmlfile1 == 'function')
                             ? FlashMap.defaultParams[74].urlRequestVars.xmlfile1.call()
                             : FlashMap.defaultParams[74].urlRequestVars.xmlfile1;

                return {
                    xmlfile1: xmlfile1,
                    datafile: FlashMap.defaultParams[74].urlRequestVars.datafile,
                    level: 2
                };
            }
        }
    },

    region: function(region) {
        this.country(this.countryId);

        setTimeout(function(){
            g = SearchForm(FlashMap.formId);

            g.form.find('#region option[value="' + region + '"]').attr('selected','selected');
            g.selectboxUpdate('region');

            FlashMap._noFlashUpdate = true;
            // Last to arguments will prevent manually changing of current region
            g.changeLevel('region', region, false, true);
        }, 600);
    },

    area: function(region,area) {
        g = SearchForm(FlashMap.formId);

        // Update region
        g.form.find('#region option[value="' + region + '"]').attr('selected','selected');
        g.selectboxUpdate('region');

        // Update province
        g.form.find('#province option[value="' + area + '"]').attr('selected','selected');
        g.selectboxUpdate('province');

        // Call procedure for donwload towns list
        g.changeLevel('province');
    },

    italy: function() {
        return this.country(109);
    },

    country: function(country) {
        g = SearchForm(FlashMap.formId);

        g.form.find('#country option[value="' + country + '"]').attr('selected','selected');
        g.selectboxUpdate('country');

        FlashMap._noFlashUpdate = true;
        FlashMap.setCountryId(country);
        FlashMap.setRegionId(0);

        g.changeLevel('country', country, false, true);
    },

    show: function(options, flashvars, url) {
        if (0 == this.countryId) {
            return ;
        }

        var options = $.extend({}, this.defaultParams[this.countryId], (options || {}));
        (function(url, options) {
            // If there are some calleble function in list,
            // we should convert them to strings

            // var opts = {};
            $.each(options, function(index, value){
                if (typeof value == 'function') {
                    options[index] = value.call();
                }
            });

            // Render SWF object with final options
            // Should use general options from FlashMap (class) object
            swfobject.embedSWF(
                url,
                FlashMap.containerId, FlashMap.height,
                FlashMap.width, FlashMap.version, "",
                options,
                {wmode: "transparent"}
            );
        })(
            (url || ((typeof options.url == 'function') ? options.url.call() : options.url)),
            $.extend({}, { config: options.config() }, (flashvars || {}))
        );
    },

    showCountry: function(country, options) {
        if (country == FlashMap.countryId) {
            return ;
        }

        this.setCountryId(country);
        if (0 == this.countryId) {
            return ;
        }

        return this.show(options, this.defaultParams[this.countryId].urlRequestVars);
    },

    showRegion: function(region, options) {
        if (0 == this.countryId) {
            return ;
        }

        if ('regions' in this.defaultParams[this.countryId]) {
            region = this.defaultParams[this.countryId].regions[region];
        }

        this.setRegionId(region);

        var flashvars = this.defaultParams[this.countryId].regionFlashVars(region);
        var options = $.extend({}, (options || {}), this.defaultParams[this.countryId]);

        var mapfile = ('mapfile' in this.defaultParams[this.countryId])
                    ? {mapfile: this.defaultParams[this.countryId].mapfile(region)} : {}

        var urlRequestVars = $.extend({}, options.urlRequestVars, flashvars, mapfile);
        return this.show(options, flashvars, options.url(urlRequestVars));
    },

    setCountryId: function(countryId) {
        this.countryId = (countryId in this.defaultParams) ? countryId : 0;
    },

    setRegionId: function(regionId) {
        this.regionId = regionId;
    },

    preventLoading: function() {
        this.setCountryId(0);
    }
};

// Incapsulate our knoledge and facilities of
// SWF objects for some country (Italy, France, Spain)
// Main target of this object - to build URL and some
// options (array) for rendering SWFObject
CountryFlashMap = function() {
    this.url = function() {

    }
}

// XXX: In future will be used for system refactoring
FlashMapProcessor = {
    // Form object will give external API for signals
    // to render swf from Search Form (selectbox changes)
    form: {
        // Will be called for country resolving
        // (when user choose country from listing of available)
        // Here we have to initialize flash container with SWFObject
        // for whole country view
        country: {

        },

        // Will be called for region resolving
        // (when user choose region from listing of available)
        // In this case we should:
        // - load region relative object of Italy
        // - load country map for France country
        region: {

        }
    },

    // Object will be pointer to all signals from flash map
    // (this signals we can set in flash object XML configurations)
    flash: {

    },

    // Renderer for SWFObject, handle main params:
    // version, size, container, wmode etc.
    swf: {
        params: {

        },

        show: function() {

        }
    }
}


