var Map = {
    map: null,
    infoWindow: null,
    img: null,
    width: null,
    height: null
};

Map.closeInfoWindow = function() {
    Map.infoWindow.close();
};

Map.openInfoWindow = function(marker, content) {
    var markerLatLng = marker.getPosition();
    Map.infoWindow.setContent(content);
    Map.infoWindow.open(Map.map, marker);
},

Map.init = function(mapType, lat, lon, width, height, zm, img) {
    Map.img = img;
    Map.width = width;
    Map.height = height;

    switch(mapType) {
        case 'HYBRID':
            mapType = google.maps.MapTypeId.HYBRID;
            break;
        case 'SATELLITE':
            mapType = google.maps.MapTypeId.SATELLITE;
            break;
        case 'TERRAIN':
            mapType = google.maps.MapTypeId.TERRAIN;
            break;
        case 'ROADMAP':
        default:
            mapType = google.maps.MapTypeId.ROADMAP;
    }

    Map.map = new google.maps.Map(document.getElementById('map-canvas'), {
        zoom: zm,
        center: new google.maps.LatLng(lat,lon),
        streetViewControl: false,
        mapTypeId: mapType
    });

    Map.infoWindow = new google.maps.InfoWindow();

    google.maps.event.addListener(Map.map, 'click', Map.closeInfoWindow);
}

var LatLngList = new Array();
var markers = new Array();
function createMarker(lat, lng, key, number) {
    var marker = new RichMarker({
        map: Map.map,
        position: new google.maps.LatLng(lat, lng),
        draggable: false,
        flat: true,
        anchor: RichMarkerPosition['TOP'],
        content: '<div class="store-marker" style="background: url(\''+Map.img+'\') no-repeat;">'+number+'</div>'
    });

    LatLngList[LatLngList.length] = new google.maps.LatLng (lat,lng);

    google.maps.event.addListener(marker, 'click', function() {
        viewStore(key);
    });

    markers[key] = marker;
}

function viewStore(key) {
    google.maps.event.clearListeners(Map.map, 'click');
    Map.openInfoWindow(markers[key], jQuery("#store-info"+key).html());
    setTimeout(function(){
        google.maps.event.addListener(Map.map, 'click', Map.closeInfoWindow);
    },50);
}

function changeStoreCountry(val, url) {
    startLoading();
    jQuery.ajax({
        type: 'GET',
        url: url,
        data: 'country_id='+val,
        success: function(datos) {
            jQuery('.storeselector .filters .region').html(datos);
            jQuery('.storeselector .filters select').attr('disabled', 'disabled');
        },
        complete: function() {
            jQuery('#region').change();
        },
        dataType: "html"
    });
}

function changeStoreRegion(val, url) {
    LatLngList = new Array();
    startLoading();
    jQuery.ajax({
        type: 'GET',
        url: url,
        data: 'country_id='+jQuery('#country').val()+'&region_id='+val,
        success: function(datos) {
            markers.each(function(i){
                if(i) {
                    i.setMap(null);
                }
            });
            jQuery('.storeselector .list-stores').html(datos);
            stopLoading();
        },
        dataType: "html"
    });
}

function startLoading() {
    jQuery('.storeselector .filters select').attr('disabled', 'disabled');
    jQuery('.storeselector .list-stores').hide();
    jQuery('#storelocator-loading').show();
}

function stopLoading() {
    jQuery('#storelocator-loading').hide();
    jQuery('.storeselector .list-stores').show();
    jQuery('.storeselector .filters select').removeAttr('disabled');
}

function centerMap() {
    var bounds = new google.maps.LatLngBounds();
    for (var i = 0, LtLgLen = LatLngList.length; i < LtLgLen; i++) {
      bounds.extend (LatLngList[i]);
    }

    Map.map.fitBounds(bounds);
    if(Map.map.getZoom()>18) {
        Map.map.setZoom(16);
    } else {
        Map.map.setZoom((Map.map.getZoom()-1));
    }
    Map.map.panBy(100, 0);
}

var Storepickup = {
    default_price: null,
    options: null
};

Storepickup.init = function() {
    jQuery('.sp-methods input[name="shipping_method"]').click(function(){
        var additional = jQuery('#additional_'+jQuery(this).val());
        if(additional.length) {
            jQuery('#additional_'+jQuery(this).val()).show();
        } else {
            jQuery('.sp-methods .additional').hide();
            jQuery('#storepickup_store').val('').change();
        }
    });
    if(jQuery('#storepickup_store').length) {
        jQuery('.sp-methods input[name="shipping_method"]:checked').click();
        jQuery('#storepickup_store').val(jQuery('#storepickup_store_selected').val()).change();
    }
}

Storepickup.changeStore = function(value) {
    if(value>0) {
        var nombre = eval('Storepickup.options.id_'+value+'.nombre');
        var direccion = eval('Storepickup.options.id_'+value+'.direccion');
        var localidad = eval('Storepickup.options.id_'+value+'.localidad');
        var city = eval('Storepickup.options.id_'+value+'.city');
        var region = eval('Storepickup.options.id_'+value+'.region');
        var country = eval('Storepickup.options.id_'+value+'.country');
        var horario = eval('Storepickup.options.id_'+value+'.horario');
        var costo_envio = eval('Storepickup.options.id_'+value+'.costo_envio');

        var html = '<strong>'+nombre+'</strong><br/>';
        html += '<div style="margin-left: 20px;">';
        if(direccion) {
            html += direccion+'<br/>';
        }
        if(localidad) {
            html += localidad+'<br/>';
        }
        if(city) {
            html += city+'<br/>';
        }
        if(region) {
            html += region+'<br/>';
        }
        if(country) {
            html += country+'<br/>';
        }
        if(horario) {
            html += horario+'<br/>';
        }

        jQuery('.sp-methods .additional .info').html(html);
        jQuery('.sp-methods .storepickup-price').html(costo_envio);
    } else {
        jQuery('.sp-methods .additional .info').html('');
        jQuery('.sp-methods .storepickup-price').html(Storepickup.default_price);
    }
}
