//Globals
var tweetmeme_style = 'compact';
var tweetmeme_source = 'sushinomads';

function ready_search_box() {
  $('#rquery').bind('focus', function() {
		$(this).css('color', '#000000');
		if(this.value == 'Restaurant, City or State') {
			this.value = '';
		}
	});
	$('#rquery').bind('blur', function() {
		$(this).css('color', '#666666');
		if(this.value == '') {
			this.value = 'Restaurant, City or State';
		}
	});
	$('#bquery').bind('focus', function() {
		$(this).css('color', '#000000');
		if(this.value == 'Search Blog Article') {
			this.value = '';
		}
	});
	$('#bquery').bind('blur', function() {
		$(this).css('color', '#666666');
		if(this.value == '') {
			this.value = 'Search Blog Article';
		}
	});
  $('.tab').click(function() {
    $('.search').toggle();
    return false;
  });
}
function email() {
  $('.emailthis').click(function() {
    var subject = 'Check out this page ' + top.document.title;
    var body = 'Visit the site here ' + top.location.href;
    $(this).attr("href", "mailto:?subject='"+escape(subject)+'&BODY='+escape(body));
    window.open($(this).attr("href"));
    return false;
  });
}
function hide_flash() {
  if($('.flash').length > 0) {
    setTimeout(function() {
			 $('.flash').fadeOut('slow');
		}, 3000);
  }
}
var mainmapHTML = '';
function switchMap(id) {
  $('#tooltip').remove();
  if($('#sushi-map #hot-spots').length > 0) {
    mainmapHTML = $('#sushi-map').html();
  }
  $.ajax({
    type: 'GET',
    dataType: 'json',
    url: '/maps/' + id,
    success: function(data) {
      var map = data.map;
      $('#sushi-map').fadeOut('slow', function() {
        $(this).html('');
        $(this).css('background', 'transparent url('+map.image_url+') no-repeat');
        $(this).removeClass();
        $(this).addClass(map.attributes.id.toString());
        if(map.list_points != null) {
          $(map.list_points).each(function(i,e) { 
            $('#sushi-map').append('<a title="Click to view restaurants in this area" class="map-flag" href="#" style="top:'+e.point.y+'px;left:'+e.point.x+'px;">'+e.point.id+'</a>');
          });
        }
      });
    },
    error: function() {
      alert('There was an error loading that map');
    },
    complete: function() {
       $('#sushi-map').fadeIn('slow');
    }
  });
}
function resetMap() {
  if(mainmapHTML == '') {
    mainmapHTML = $('#sushi-map').html();
  }
   $('#sushi-map').fadeOut('slow', function() {
     $(this).html(mainmapHTML);
   });
    $('#sushi-map').fadeIn('slow');
}
function enablePoints() {
  $('.map-flag').live('click', function() {
    var el = this;
    point_id = $(el).text();
    map_id = $('#sushi-map').attr('class');
    if($(el).hasClass('active')) {
      //don't do anything
    } else {
      $.ajax({
        type: 'GET',
        dataType: 'json',
        url: "/maps/"+map_id+"/points/"+point_id,
        success: function(data) {
          point = data.point;
          x = point.attributes.x + 10;
          y = point.attributes.y - 150;
          $('.bubble').remove();
          $('#sushi-map').append('<div class="bubble" style="top:'+y+'px;left:'+x+'px;"><div class="bubble-top"></div><div class="bubble-mid"></div><div class="bubble-bottom"></div></div>');
          if(point.list_restaurants != null) {
            $(point.list_restaurants).each(function(i,e) {
              restaurant = eval('('+e+')').restaurant;
              $('.bubble-mid').append('<a href="/reviews/'+restaurant.attributes.permalink+'">'+restaurant.attributes.name+' - '+restaurant.address+'</a><br />');
            });
          } else {
            $('.bubble-mid').text("Looks like there's no restaurants in this area...");
          }
        },
        error: function(request, status) {
          alert('An error occurred. Error: ' + status);
        },
        complete: function() {
          $('.map-flag').removeClass('active');
          $(el).addClass('active');
          $('.bubble').slideDown('slow');
        }
      });
    }
    return false;
  });
}
function mapTooltip() {
  $('#hot-spots area').mouseover(function(e) {
    this.t = this.title;
    this.title = '';
    $('body').append('<p id="tooltip">'+this.t+'</p>');
    $('#tooltip').css("top", (e.pageY - 20) + "px").css("left", (e.pageX + 20) + "px").fadeIn("fast");
  });
  $('#hot-spots area').mouseout(function(e) {
    this.title = this.t;
    $('#tooltip').remove();
    $(this).mousemove(function(e) {
      $('#tooltip').css("top", (e.pageY - 10) + "px").css("left", (e.pageX + 20) + "px");
    });
  });
}
function twitterCallback2(twitters) {
  var statusHTML = [];
  for (var i=0; i<twitters.length; i++){
    var username = twitters[i].user.screen_name;
    var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      return '<a href="'+url+'">'+url+'</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
    });
    statusHTML.push('<li><span>'+status+'</span> <a style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>');
  }
  document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
}
function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}
$(function (){
  if($('#sushi_map').length > 0) {
    $(".dropdown dt a").click(function() {
        $(".dropdown dd ul").toggle();
        return false;
    });
    $(".dropdown dd ul li.new-map a").click(function() {
        var text = $(this).html();
        $(".dropdown dt a span").html(text);
        $(".dropdown dd ul").hide();
        switchMap($('dt a span.value').text());
    });
     $(".dropdown dd ul li.world-map a").click(function() {
       var text = $(this).html();
       $(".dropdown dt a span").html(text);
       $(".dropdown dd ul").hide();
       resetMap();
     });
    $('#sushi-map').bind('click', function(e) {
      if(this == e.target) {
        $('.map-flag').removeClass('active');
        $('.bubble').remove();
      }
    });
    $(document).bind('click', function(e) {
        var $clicked = $(e.target);
        if(! $clicked.parents().hasClass("dropdown")) {
          $(".dropdown dd ul").hide();
        }
    });
    enablePoints();
    mapTooltip();
  }
  ready_search_box();
	hide_flash();
	if($('#disqus_thread').length > 0) {
  	window.disqus_no_style = true;
    $.getScript("http://disqus.com/forums/sushinomads/embed.js");
  }
  email();
});