$(document).ready(function() {
 // Element for "play" container
 var play_container = $("#homepage-block-play");
 var plays = $("#homepage-block-map-pins div").size();
 var current_play = 1;
 var speed        = 2000;
 var rotate;
 var prev;

 // Setup rotation
 rotate_plays();
 rotate = setInterval(rotate_plays, speed);

 function rotate_plays() {
  if (current_play == 1) {
   prev = plays;
  } else {
   prev = current_play - 1;
  }

  $(play_container).html("");
  $("#homepage-block-map-pins div:nth-child(" + prev + ")").css("z-index", 0);
  $("#homepage-block-map-pins div:nth-child(" + prev + ")").removeClass("map-hover");
  $("#homepage-block-map-pins div:nth-child(" + current_play + ")").addClass("map-hover");
  $("#homepage-block-map-pins div:nth-child(" + current_play + ")").css("z-index", 5);
  $(play_container).html("<p>" + $("#homepage-block-map-pins div:nth-child(" + current_play + ") a").attr("title") + "</p>");

  if (current_play > plays) {
   current_play = 1;
  } else {
   current_play = ((current_play + 1) > plays) ? 1 : current_play + 1;
  }
 }

 // Pin hover function
 $("#homepage-block-map-pins a").hover(function(){
  clearInterval(rotate);
  clear_plays();

  // Add hover state
  $(this).parent().addClass("map-hover");
  $(this).parent().css("z-index", 5);

  // Add text
  $(play_container).html("<p>" + $(this).attr("title") + "</p>");
 }, function(){
  rotate = setInterval(rotate_plays, speed);

  // Remove hover state
  $(this).parent().removeClass("map-hover");
  $(this).parent().css("z-index", 0);

  // Remove text
  $(play_container).html("");
 });

 function clear_plays(){
  $("#homepage-block-map-pins div").removeClass("map-hover");
 }

 $("#ticker-up").hover(function(){
  $("#ticker-content").scrollTo($("#ticker-content div.top-corner"), 1000);
 },function(){
  $("#ticker-content").stop();
 });

 $("#ticker-down").hover(function(){
  $("#ticker-content").scrollTo($("#ticker-content div.bottom-corner"), 1000);
 },function(){
  $("#ticker-content").stop();
 });

 //rss
 // depends on date.js & truncate
 $.getFeed({
  url: '/rss.php',
  success: function(feed){
   var html = '';
   for(var i = 0; i < feed.items.length && i < 5; i++){
    var item = feed.items[i];
    var thedate = Date.parse(item.updated);
    html += '<hr /><div><p>' + thedate.toString('yyyy-MM-dd') + '</p><a href="' + item.link + '" target="_blank" class="rss-title">' + item.title + '</a></div>';
   }
   $('#ticker-content').append(html);
   $('.rss-title').truncate({width: 285, after:'...'});
  }    
 });
 
 // stock ticker
 $.get('/stocks.php', function(data){
  var stuff = '<marquee behavior="scroll" scrollamount="2" direction="left" width="317">' + data + '</maequee>';
  $("#stocks-ticker-content").html(stuff);
  $("#stocks-ticker-content marquee").marquee().mouseover(function(){
   $(this).trigger('stop');
  }).mouseout(function(){
   $(this).trigger('start');
  });
 });

 var modal_src = '/res/eagleland/vplayer.html';
 $("#playvideo").click(function(e){
  e.preventDefault();
  $.modal('<iframe src="' + modal_src + '" height="289" width="498" scrolling="no" />', {opacity: 80});
 });
});
