/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var stores = {};

KikShippingForm = {
    populate: function(storeId){
        if(stores[storeId]){
            var storeInfo = stores[storeId];
            $('shipping:lastname').value = storeInfo.filiale;
            $('shipping:street1').value = storeInfo.address;
            $('shipping:city').value = storeInfo.city;
            $('shipping:postcode').value = storeInfo.zip;
            
            var htmlText = '<p>'+gMapTranslations.selectedStore+'</p>'+
            '<p><strong>KiK Textilien & Non-Food GmbH</strong><br/>'+
            storeInfo.address+'<br/>'+storeInfo.zip+' '+storeInfo.city+'</p>'+
            '<p><button href="#" onclick="return loadLightboxSearch()" class="button local-link"><span><span>'+gMapTranslations.change+'</span></span></button></p>';
			
            $('kik-shipping-info-ajax').update(htmlText);
            lightbox.hide();
        }
		
        return false;
    }
}

KikGooglemapsMarker = {
    options: {
        
    },
    create : function(storeInfo, icon, translations, withLinks){
        this.options.icon = new GIcon(G_DEFAULT_ICON);
        this.options.icon.image = icon;
        this.options.icon.shadowSize = new GSize(0, 0);
		
        var point = new GLatLng(parseFloat(storeInfo.latitude), parseFloat(storeInfo.longitude));
        var marker = new GMarker(point, this.options);//, this.options);
        var html = '<div class="gmap-marker-kik"><strong>KiK Textilien & Non-Food GmbH</strong><br/>'+
        storeInfo.address+'<br/>'+storeInfo.zip+' '+storeInfo.city+'<br/>'+
        translations.distance+': '+Math.round((storeInfo.Entfernung)/1000)+' km<br/>'+
        ((withLinks==true) ?('<a href="#" id="store_id:'+storeInfo.filiale+'" class="gmap-storeselector" onclick="return KikShippingForm.populate('+storeInfo.filiale+');"><span>'+translations.select+'</span></a>'):'')+'</div>';
        GEvent.addListener(marker, 'click', function(){
            marker.openInfoWindowHtml(html);
        });
        
        return marker;
    }
};

KikStoreList = {
    create : function(storeInfo, translations, withLinks){
        var html = '<div class="store-list-single"><strong>KiK Textilien & Non-Food GmbH</strong><br/>'+
        storeInfo.address+'<br/>'+storeInfo.zip+' '+storeInfo.city+'<br/>'+
        translations.distance+': '+Math.round((storeInfo.Entfernung)/1000)+' km<br/>'+
        ((withLinks==true) ?('<a href="#" id="store_id:'+storeInfo.filiale+'" class="gmap-storeselector" onclick="return KikShippingForm.populate('+storeInfo.filiale+');"><span>'+translations.select+'</span></a>'):'')+'</div>';
        
        return html;
    }
}

KikGooglemaps = Class.create();
KikGooglemaps.prototype = {
    mapUrls : {
        de : 'http://www.kik-textilien.com/filiale/filialsuche',
        at : '',
        cz : '',
        sk : '',
        sl : ''
    },
    initialize : function(plz_place, lang, country, controller, icon, translations){
        // alert(plz_place+' '+lang+' '+country+' '+' '+controller+' '+icon+' '+translations);
        this.plz_place = plz_place;
        this.lang = lang;
        this.controller = controller;
        this.country = country;
        this.translations = translations;
        this.icon = icon;
            switch (this.country) {
                case "DE":
                    this.region = this.country.toLowerCase();
                    this.lang = this.country;
                    this.country = "Deutschland";
                   break;
                case "SI":
                    this.region = this.country.toLowerCase();
                    this.lang = this.country;                    
                    this.country = "Slovenija";
                   break;
                case "SK":
                    this.region = this.country.toLowerCase();
                    this.lang = this.country;                    
                    this.country = "Slovenská republika";
                   break; 
                case "CZ":
                    this.region = this.country.toLowerCase();
                    this.lang = this.country;                    
                    this.country = "Česká republika";
                   break; 
                case "HU":
                    this.region = this.country.toLowerCase();
                    this.lang = this.country;                    
                    this.country = "Magyarország";
                   break; 
                case "HR":
                    this.region = this.country.toLowerCase();
                    this.lang = this.country;                    
                    this.country = "Hrvatska";
                   break;  
            }
        this.region = lang.toLowerCase();        
    },
    showMapHTML : function(target, withLinks){
        var geocoder = new google.maps.Geocoder();
        var plz_place = this.plz_place;
        var country = this.country;        
        var region = this.region;
        var address = plz_place+','+country;
        var controller = this.controller;
        var lang = this.lang;
        var icon = this.icon;
        var translations = this.translations;
        plz_place = plz_place.replace(/,/g," ");
        var plz_place_split = plz_place.split (' ');
        geocoder.geocode( {
            'address': address,
            'region': region
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {	
                var latitude = (results[0].geometry.location.lat());
                var longitude = (results[0].geometry.location.lng());
                //console.log(results);
                var params = {
                    'searchlocation': plz_place_split[0], 
                    'lat': latitude, 
                    'long': longitude, 
                    'country': lang                    
                };
                new Ajax.Request(controller,
                {
                    method: 'get',
                    parameters: params,
                    onSuccess: function(transport){
                        if(transport != ''){
                            var map = new GMap2($(target));
                            map.setCenter(new GLatLng(latitude, longitude), 11);
                            map.addControl(new GLargeMapControl());
                            map.addControl(new GMapTypeControl());
                            map.enableScrollWheelZoom();
                            var json = transport.responseText.evalJSON();
                            if(json.success){
                                var jresults = json.results;
				var resultsText = '';	
                                var lastStore;
                                for(var i in jresults){
                                    if(typeof(jresults[i]) == 'object'){
                                   
                                        var storeInfo = jresults[i];
                                        stores[storeInfo.filiale] = storeInfo;
                                        if(storeInfo.toBeOpened != true){
                                            map.addOverlay(KikGooglemapsMarker.create(storeInfo, icon, translations, withLinks));
                                            resultsText += KikStoreList.create(storeInfo, translations, withLinks);
                                            if (lastStore){
                                                if (lastStore.Entfernung > storeInfo.Entfernung){
                                                    lastStore = storeInfo;
                                                }
                                            }else{
                                               lastStore = storeInfo; 
                                            }
                                            
                                        }
                                    }
                                }
                                if (lastStore){
                                    map.setCenter(new GLatLng(parseFloat(lastStore.latitude), parseFloat(lastStore.longitude)), 11);
                                }
                                
                                var divList = '<div id="storelist" class="store-list">'+resultsText+'</div>';
                                
                                if ($('storelist')) {
                                  $('storelist').update(resultsText);
                                }else{
                                    lightbox.lightboxContent.insert(divList);
                                }                               						
                            }
                        }
                    },
                    onFailure: function(){
                        alert('Failure');
                        var map = new GMap2($(target));
                        map.setCenter(new GLatLng(latitude, longitude), 11);
                        map.addControl(new GLargeMapControl());
                        map.addControl(new GMapTypeControl());
                        map.enableScrollWheelZoom();
                    },
                    onExeption: function(){
                        alert('Exception');
                        var map = new GMap2($(target));
                        map.setCenter(new GLatLng(latitude, longitude), 11);
                        map.addControl(new GLargeMapControl());
                        map.addControl(new GMapTypeControl());
                        map.enableScrollWheelZoom();
                    },
                    onCreate: function(){
                        var map = new GMap2($(target));
                        map.setCenter(new GLatLng(latitude, longitude), 11);
                        map.addControl(new GLargeMapControl());
                        map.addControl(new GMapTypeControl());
                        map.enableScrollWheelZoom();
                    }
                });
            }else{
                alert('Error: ' +status);
            }
        });
    }
};

