

$(function(){


    map_initialize();
    
    
    $('.uploadlink a').click(function(){
        $('.uploadlink').animate({opacity:'.01'},'fast',function(){
            $('.uploadlink').slideUp('fast',function(){
                $('.uploadimage form').css({opacity:'.01'});
                $('.uploadimage form').slideDown('fast', function(){
                    $('.uploadimage form').animate({opacity:'1'},'fast')
                });
            })
        });
        return false;
    });
    
    $('.uploadimage form input[name=cancel]').click(function(){
        $('.uploadimage form').animate({opacity:'.01'},'fast',function(){
            $('.uploadimage form').slideUp('fast',function(){
                $('.uploadlink').css({opacity:'.01'});
                $('.uploadlink').slideDown('fast', function(){
                    $('.uploadlink').animate({opacity:'1'},'fast')
                });
            })
        });
        return false;
    });




    /*
     *  Set Favorite / been there
     */
    $('#favoriteit, #ivebeenhere').click(function(){
    
        if($(this).hasClass('disabled')) return false;
        
        var method = this.id=='favoriteit'?'setFavorite':'setBeenThere';
        var place_id = document.forms.data.place_id.value;
        var request_key = document.forms.data.request_key.value;
        var button = this
        var action = ($(this).hasClass('active'))?'off':'on';
        var rq = '/request/place/'+method+'/action,'+action+'/place_id,'+place_id+'/key,'+request_key+'/callback_func,?';


        
        status_update('Updating, please wait...','alert_positive');
        $(button).addClass('disabled').css('cursor','wait').animate({opacity:.3},400);

        $.getJSON(rq,
            function(data){
                $(button).removeClass('disabled').css('cursor','pointer').animate({opacity:1},400);
                if(data.success){
                
                    if($(button).hasClass('active')){
                        $(button).removeClass('active');
                        if(method=='setFavorite'){
                            status_update('You have unfavorited this place','alert_positive');
                        }else{
                            status_update("Ok, you've never been to this place",'alert_positive');
                        }
                    }else{
                        
                        $(button).addClass('active');
                        
                        if(method=='setFavorite'){
                            status_update('You have favorited this place','alert_positive');
                        }else{
                            status_update("Ok, you have been to this place",'alert_positive');
                        }
                    }
                }else{
                    status_update('There was an error, please try again later','alert_negative');
                }
            });
        return false;
    })   






    
});

var map;
var geocoder;


function map_initialize(){
    DV = document.createElement('DIV');
    
    $('#map img').remove();
    $('#map').html('<div id="map_canvas" style="width:282px;height:282px;" width="282" height="282"></div>');
    
    loc = $('#data input[name=location]').val();
    title = $('#data input[name=title]').val();

    geocoder = new google.maps.Geocoder();
    
    var myOptions = {
      zoom: 15,
      navigationControl: true,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
    showAddress(loc,title);
}




function showAddress(address, location_title) {

    if (geocoder) {
      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
          
          
          var image = '/_ui/images/map_marker.png';
          var marker = new google.maps.Marker({
              map: map,
              icon: image,
              title: location_title,
              position: results[0].geometry.location
          });
        } else {
          //alert("Geocode was not successful for the following reason: " + status);
        }
      });
    }
}
