function updateClockOn(element) {
  $(element).html(currentLocalTime());
}

function currentLocalTime() {
  var curtime = new Date();
  var curhour = curtime.getHours();
  var curmin = curtime.getMinutes();
  var cursec = curtime.getSeconds();
  var time = "";

  if(curhour == 0) curhour = 12;
  time = (curhour > 12 ? curhour - 12 : curhour) + ":" +
         (curmin < 10 ? "0" : "") + curmin + ":" +
         (cursec < 10 ? "0" : "") + cursec + " " +
         (curhour > 12 ? "PM" : "AM");
  return time;
}

function showSolapita() {
  $('.solapita').animate({
   width: "46px",
   marginLeft: "-20px",
  }, 1700, 'easeOutBounce');
}

function navigateTo(link) {
  var id = $(link).attr('id');
  $('#tabmenu li a').not($(this)).removeClass("active");
  $(link).addClass("active");
  $('#content').load("content.php?content="+id, {
    beforeSend: function(){
      // $('#content').html("Loading...")
      $('#content').html("<div id='loading_content'><img src='images/loader.gif'/></div>")
   }
  })
}

function buildNavigationWith(links) {
  links.click(function(){
    navigateTo($(this));
  }); 
} 

function updateCurrentSong(){
  $('#current_song').load('currentsong.php');
  setTimeout("updateCurrentSong()", 15000);
}

$(function()  {
  // Disable player on LOCALHOST
  if (document.location.host == "websites.local.push-k.net") {
    $('#player_area #player').html("DISABLED LOCALLY").css({color: "white"});
  };
  
  // Fetches Current Song from current_song.php
  // Gracias, jQuery. 
  updateCurrentSong();
  
  // Super duper navigation
  buildNavigationWith($('#tabmenu li a, #footer ul li a'));
  navigateTo($('#tabmenu li a').first());
  $('#tabmenu li a').hover(function() {
    $(this).stop().animate({
      height: '67px',
      marginTop: 0
    })
  }, function() {
    $(this).stop().animate({
      height: '42px',
      marginTop: '25px'
    })
  })

  $("#footer a[href^='http:'], .rssRow a").livequery(function(){
    $(this).attr("target", "_blank");
  });
  // Shroom
  $('#szstudios a').hover(function() {
    $(this).stop().animate({
      backgroundPosition: '83px top'
    }, 1500, 'easeOutQuint')
  }, function() {
    $(this).stop().animate({
      backgroundPosition: 'left 78px'
    })
  })
  
  


  // Clock
  setInterval("updateClockOn('#clock')", 1000);  
  $('#footer').animate({
   bottom: 0
  }, 2000);

  // Twitter
  $(".tweet").tweet({
      username: "ciclopradio",
      join_text: "auto",
      //avatar_size: 24,
      count: 1,
      
      // auto_join_text_default: "we said,",
      // auto_join_text_ed: "we",
      // auto_join_text_ing: "we were",
      // auto_join_text_reply: "we replied to",
      // auto_join_text_url: "we were checking out",
      
      auto_join_text_default: "",
      auto_join_text_ed: "",
      auto_join_text_ing: "",
      auto_join_text_reply: "",
      auto_join_text_url: "",
      
      loading_text: "Cargando tweets..."
    });
    
    // Detecting iOS device
    if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {    
      $('head').append('<link rel="stylesheet" href="stylesheets/ios.css" type="text/css" media="screen" charset="utf-8">');
    }
});




