var content_window_open = false;
var help_window_open = false;
var new_memo_help_on = false;
var memo_help_memo_created = false;

function toggle_info_window () {
  if (!content_window_open) {
     show_content_window();
  }
  else {
     hide_content_window();
  }
}

function show_content_window () {
  hide_help_window();
  if (!content_window_open) {
    $("div#content_main").show();
    $("#hide").html(hide_content_box);
    $("#map_canvas_overlay").show();
    $('#map_canvas_overlay').fadeTo('slow', 0.5);
    content_window_open = true;
  }
}

function hide_content_window () {
  hide_help_window();
  if (content_window_open) {
    $("div#content_main").hide();
    $("#hide").html(show_content_box);
    $('#map_canvas_overlay').fadeTo('slow', 0);
    setTimeout (hide_map_overlay,500);
    content_window_open = false;
  }
}

function show_help_window () {
  if (!help_window_open) {
    $("#help_window").fadeTo(200,1);
    help_window_open = true;
  }
}

function hide_help_window () {
  if (help_window_open) {
    $("#help_window").fadeTo(200,0);
    help_window_open = false;
  }
}

function hide_map_overlay() {
  $("#map_canvas_overlay").hide();
}

function open_url(url,target_div) {
  $('#ajax_loader').show();
  // Check if we have tabs, if so we'll need to take care of them after load
  if ($('#tabs').length ) {
     var selected =  $('#tabs').tabs().tabs('option', 'selected');
  }
  // For some reason $.load doesn't work well here
  $.ajax({url: url,
         success: function(data) { 
           $('#ajax_loader').hide();
           if (target_div) {
             current_sub_content_hash = url;
             $(target_div).html (data);
             // If we have target, we assume we should be at tab-1.
             // Eventually, we should be smarter here
             $('#tabs').tabs().tabs('select', 1);
           }
           else {
             current_content_hash = url;
             $('#content').html (data);
             if (selected) { 
               $('#tabs').tabs().tabs('select', selected);
             }
           }
         },
         dataType: 'html'
         });
  show_content_window();
}

function submit_form(form_url,form_id) {
  var values = $('#' + form_id).serializeArray();
  $('#ajax_loader').show();
  // Check if we have tabs, if so we'll need to take care of them after load
  if ($('#tabs').length ) {
     var selected =  $('#tabs').tabs().tabs('option', 'selected');
  }
  // For some reason $.load doesn't work well here
  $.post(form_url,values,function(data) {
     $('#ajax_loader').hide();
     // This is a very dirty hack. Has to be cleaned up somewhow.
     // Basically, we check if the submit came from the 2nd tab.
     // If so, we only update the sub_content. Check behavior 
     // of threaded comments in Memory and Photo tabs to see why
     // this became necessary
     if (selected == 1) {
        $('#sub_content').html(data);
     } 
     else {
       $('#content').html(data);
       if (selected) { 
         $('#tabs').tabs().tabs('select', selected);
       }
     }
  },"html");
}

function update_content_from_hash () {
  if (location.hash.substr (1,1) == "!") {
    var url = location.hash.substr(2);
  }
  else {
    var url = location.hash.substr(1);
  }
  if (url == "") {
    url = "/welcome/";
  }
  // Check if we have anything for sub_content
  var idx = url.indexOf("#");
  if (idx != -1) {
    sub_url = url.substr(idx + 1,url.length - idx);
    url = url.substr (0, idx);
    if (url != current_content_hash) {
      open_url (url);
    }
    open_url (sub_url,'#sub_content');
  }
  else {
    open_url(url);
  }
}

function set_map_center (lat, lng, zoom) {
   map.panTo(new google.maps.LatLng(lat, lng));
   if (zoom) {
     map.setZoom(zoom);
   }
}

// This function is to update the location has, hence trigger
// onhashchange event. Needs to take care of '?' as it is the sole
// reason it was written. Check the template for pagination to see
// why this became necessary 
function add_to_hash(hash) {
  var current_hash = location.hash;
  // Check if we have ? in it
  var idx = current_hash.indexOf("?") ;
  if (idx == -1) {
     location.hash = current_hash + hash;
  }
  else {
     var current_base_hash = current_hash.substr(0,idx);
     location.hash = current_base_hash + hash;
  }
}

function show_subnav(subnav) {
  $("#messages_subnav").hide();
  $("#account_subnav").hide();
  $("#memories_subnav").hide();
  $("#friends_subnav").hide();
  $("#groups_subnav").hide();
  $('#'+ subnav).show();
}

function searchAddress(location) {
  //$.get ('/api/search_poi/', { location: location }, function(data) {
  //  if (data.poi[0]) { 
  //    var poi_id=data.poi[0].id;
  //    var location_txt = data.poi[0].location;
  //    var loc = $.parseJSON(location_txt);        
  //    var lat = loc.coordinates[1];
  //    var lng = loc.coordinates[0];
  //    set_map_center (lat,lng,16);
  //    hide_content_window();
  //  }
  //  else {
      searchAddress_on_Google (location);
  //  }
  //},"json");
}

function searchAddress_on_Google (location) {
  var geocoder = new google.maps.Geocoder();
  geocoder.geocode( { 'address': location}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      map.setZoom (17);
    } else {
      alert("Böyle bir nokta bulunamadı.");
    }
  });
}

