//      ZONE SELECTOR

var api;

function highlightZone(map, zone) {
    if (map == 'resto') {
        var coords = new Array();
        coords[11] = 348;
        coords[13] = 696;
        coords[15] = 1041;
    }else if (map == 'fuera') {
        var coords = new Array();
        coords[1] = 347;
        coords[2] = 694;
        coords[3] = 1041;
        coords[4] = 1388;
    }else if (map == 'barcelona') {
        var coords = new Array();
        coords[1] = 347;
        coords[2] = 694;
        coords[3] = 1041;
        coords[4] = 1388;
        coords[5] = 1735;
        coords[6] = 2082;
        coords[7] = 2429;
        coords[8] = 2776;
        coords[9] = 3123;
        coords[10] = 3470;
    }
    $('.zoneContainer').css({
        backgroundPosition: '0px -' + coords[zone] + 'px'
    });
}

function resetZone() {
    $('.zoneContainer').css({
        backgroundPosition: '0 0'
    });
}



function generate_zone_uri( parent ){
    var uri = '';
    $(parent+" .zones-selected .zone-selected").each(function(i,v){
        uri += $(this).children("input").val()+"|";
    });
    if ( uri )
        return uri;
    return "España";
}

//var slide = last = $("#menu-slideshow .slide").last();

function slideshow_next(){
    slide.fadeOut(1500);
    slide = slide.prev();
    if ( !slide.length ) {
        slide = last;
        slide.fadeIn(1500,function(){$("#menu-slideshow .slide").show()});
    }
}

var geocoder;
var map;

function init_google_maps( id,lat,lng,zoom ) {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(lat,lng);
    var myOptions = {
    		scrollwheel: false,
        zoom: zoom,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById(id), myOptions);
}

function set_map_center( lat,lng ){
    var center = new google.maps.LatLng(lat,lng);
    map.setCenter(center);
}

function autocenter_map( minLat,maxLat,minLng,maxLng ){
    if ( minLat == maxLat && minLng == maxLng ){
        map.setZoom(16);
        return;
    } 
    map.fitBounds(new google.maps.LatLngBounds(
        new google.maps.LatLng(minLat, minLng),
        new google.maps.LatLng(maxLat, maxLng)
    ));
}

function set_marker( location ){
	var icon = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=N|075043|FFFFFF";
    var marker = new google.maps.Marker({
        map: map, 
        position: location,
        icon: icon
    });
    return marker;
}

function set_marker_wlabel( location,label,html,link,num  ){
    // var icon = (num<=99)? "http://gmaps-samples.googlecode.com/svn/trunk/markers/red/marker"+num+".png" : null;
    // http://chart.apis.google.com/chart?chst=d_map_pin_letter_withshadow&chld=9|075043|FFFFFF
    var icon = (num<=99)? "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld="+num+"|075043|FFFFFF" : null;
    var marker = new google.maps.Marker({
        map: map,
        position: location,
        title: label,
        icon: icon
    });
    
    //Eventos
    google.maps.event.addListener(marker, 'mouseover', function() {
        infowindow.setContent(html);
        infowindow.open(map,marker);
    });
    google.maps.event.addListener(marker, 'click', function() {
        window.location = link;
    });
    return marker;
}

function add_marker_from_address( address,label,html,link ) {
    if (geocoder) {
        geocoder.geocode( {'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                //map.setCenter(results[0].geometry.location);
                if (results[0].geometry.location.lat() > maxLat) maxLat = results[0].geometry.location.lat();
                if (results[0].geometry.location.lat() < minLat) minLat = results[0].geometry.location.lat();
                if (results[0].geometry.location.lng() > maxLng) maxLng = results[0].geometry.location.lng();
                if (results[0].geometry.location.lng() < minLat) minLng = results[0].geometry.location.lng();
                
                var marker = new google.maps.Marker({
                    position: results[0].geometry.location,
                    map: map,
                    title: label
                });

                //Eventos
                google.maps.event.addListener(marker, 'mouseover', function() {
                    infowindow.setContent(html);
                    infowindow.open(map,marker);
                });
                google.maps.event.addListener(marker, 'click', function() {
                    window.location = link;
                });
            }
        });
    }
}

function actualize_favorites(){
    $.ajax({
        type: "GET",
        url: BASE_URL+"index.php/"+LANG+"/favoritos/actualizar_favoritos/",
        success: function( resp ){
            $("#favorites_count").html(resp);
        }
    });
}
