$(document).ready(function(){

  // Open external links in new tab/window
  $("a[href*='http://']:not([href*='"+location.hostname+"'])").click(function(){
    this.target = "_blank";
  });
    
  // Return inputs to default value on blur.
  $('input[alt]').focus(function(){
    if ($(this).attr("value") === $(this).attr("defaultValue")) {
      $(this).attr("value", "");
    }
  }).blur(function(){
    if ($(this).attr("value") === "") {
     $(this).attr("value", $(this).attr("defaultValue"));
    }
  });

  // Pretty select boxes.
  if ($(".selectbox").length) {
    $(".selectbox").sSelect();
  }

  // Border selection for drawing finalisation.
  $("#border-list li a").click(function(){
    details = $(this).attr("id").split("-");
    border_id = details[1];
    border_name = details[2];
    $("#drawing_details-border_id").val(border_id);
    $("#drawing").removeClass().addClass("frame-small-" + border_name);

    return false;
  });


  //hide me
  $('.hide_me').hide();
  $('.close-overlay').click(function(){
    $(this).parent().parent().hide();
    return false;
  });

  //overlays popups function
  var tooltip = function(selector, offsetX, offsetY, popup, bgimg) {
    selector.find('a').css('cursor', 'pointer');
    selector.bind({
      click: function(){
        posX = $(this).position().left + offsetX; //calculating the left value
        posY = $(this).position().top + offsetY; //calculating the top value
        popup.css({
          left: posX, //setting the left value
          top: posY, //setting the top value
          position: 'absolute',
          background: 'url(/images/core/' + bgimg + '.png) no-repeat'
        }).delay(500).show(0);
return false;
      }
    });
  };

  tooltip($('.credits-overlay'), -232, -100, $('#credits'), 'credits-overlay-bg'); //credits overlay
  tooltip($('#show_form'), -165, 15, $('#loginform'), 'login-overlay-bg'); //loginform overlay

  // Colorbox
  $("a[rel='riccardo']").colorbox(); //riccardo
  $("#nav-tvad, #watch-tvc, .tvad").colorbox(); //tv ad
  $("#watch-yogi").colorbox();

  // Hide all error/flash notices.
  setTimeout("$('#flash-notice,#flash-error').slideUp('slow')", 3500);

  // Back in history or back to gallery.
  $(".gallery-back").click(function(){
    if ( history.length > 1 ) {
      history.go(-1);
      return false;
    }
  });

});

