/* tps.js
 * Collection of misc routines.  This wants to hook into document.ready so
 * on any page that includes this, be sure to call tps_document_ready at 
 * proper time.
 *
 * @requires jquery
 * ------------------------------------------------------------------------- */

function notification(title, text, image) {
  $("#notification").css("top", "-120px");
  $("#notification").css("display", "block");
  $("#notification").animate({
    top: ($(window).scrollTop()) - 50 + "px"
  },
  {
    duration: 700
  });
  $("#notification h1").html(title);
  $("#notification .text").html(text);
  $("#notification .image").html("<img src='" + image + "' alt='error' />");

  $(window).scroll(function () {
    $("#notification").stop().animate({
      top: ($(window).scrollTop()) - 50 + "px"
    },
    "slow");
  });
}


/* Handle buttonsets like the "Recent Searches"|"My Searches" pair. */
var BUTTONSET = 
{
  /* Attach events. */
  hook : function() {
    if ('v' != '\v') {
      $(".buttonset :first-child").addClass("ui-corner-five-left");
      $(".buttonset :last-child").addClass("ui-corner-five-right");
    }

    $(".buttonset button").click(
      function () {
        $(".ui-state-active").addClass("ui-state-default");
        $(".ui-state-active").removeClass("ui-state-active");
        $(this).addClass("ui-state-active");
        $(this).removeClass("ui-state-default");
      });
  },

  /* toggle selection highlight, pass in a jquery element set. */
  select : function(el) {
    $(".ui-state-active").addClass("ui-state-default");
    $(".ui-state-active").removeClass("ui-state-active");
    el.addClass("ui-state-active");
    el.removeClass("ui-state-default");
  },

  /* Get ID of first button in selected state... if any. */
  get_selected_id : function() {
    var set = $(".buttonset .ui-state-active");
    if (set.length > 0) {
      return set[0].id;
    }
    return null;
  }

};/* BUTTONSET */



/* Should be called from your document ready event handler.  Ie,
   $(document).ready(tps_document_ready) */
function tps_document_ready() {

  BUTTONSET.hook();

  $("#timechart").click(
  function () {
    //"1" forces a load of this graph on button-click.

    cloud.load_tf(1);
    $(".chartfloat").stop().animate({
      left: "-630px"
    },
    1500);
  });

  $("#authorchart").click(
  function () {
    //"1" forces a load of this graph on button-click.
    cloud.load_tt(1);
    $(".chartfloat").stop().animate({
      left: "-1240px"
    },
    1500);
  });

  $("#wordchart").click(
  function () {
    //"1" forces a load of this graph on button-click.
    cloud.load(1);
    $(".chartfloat").stop().animate({
      left: "0px"
    },
    1500);
  });

  $("#locationchart").click(
  function () {
    //"1" forces a load of this graph on button-click.
    cloud.load_tl(1);
    $(".chartfloat").stop().animate({
      left: "-1850px"
    },
    1500);
  });

  if ($('.selectbox').length < 0) {
    $('.selectbox').selectbox();
  }

  if ($('a#video').length > 0) {
    $("a#video").fancybox({
      'hideOnContentClick': false,
      'overlayShow': true,
      'frameWidth': 640,
      'frameHeight': 385
    });
  }
        
  if ($("#newsearch").length > 0) {
    $("#newsearch").validate({
      rules: {
        kw: {
          required: true
        }
      },
      messages: {
        kw: "<span>* </span>Please enter a search term"
      },
      errorPlacement: function (error, element) {
        $(element).prev().children('span.small').html(error);
      }
    });
  }

  function close_note() {
    $("#notification").css("display", "none");
  }

  $('.note-close').click(
  function () {
    $("#notification").stop().animate({
      top: "-150px"
    },
    "slow", "linear", close_note());
  });
  
  /* metrics number size */
  $.each($('.metrics-number'), function(){
   
    if ($(this).html().length > 4)
    {
      $(this).css('font-size','73px');
    } 
    
    if ($(this).html().length > 5)
    {
      $(this).css('font-size','62px');
    } 
    
    if ($(this).html().length > 6)
    {
      $(this).css('font-size','50px');
    }

    nStr = $(this).html();
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
		
	}
	$(this).html(x1 + x2);
});
 
  /* These are jquery extensions */

  $.uicornerfix = function (r) {
    DD_roundies.addRule('.ui-corner-all', r);
    DD_roundies.addRule('.ui-corner-top', r + ' ' + r + ' 0 0');
    DD_roundies.addRule('.ui-corner-bottom', '0 0 ' + r + ' ' + r);
    DD_roundies.addRule('.ui-corner-right', '0 ' + r + ' ' + r + ' 0');
    DD_roundies.addRule('.ui-corner-left', r + ' 0 0 ' + r);
    DD_roundies.addRule('.ui-corner-tl', r + ' 0 0 0');
    DD_roundies.addRule('.ui-corner-tr', '0 ' + r + ' 0 0');
    DD_roundies.addRule('.ui-corner-br', '0 0 ' + r + ' 0');
    DD_roundies.addRule('.ui-corner-bl', '0 0 0 ' + r);
  };

  $.uicornerfix5 = function (r) {
    DD_roundies.addRule('.ui-corner-five-all', r);
    DD_roundies.addRule('.ui-corner-five-top', r + ' ' + r + ' 0 0');
    DD_roundies.addRule('.ui-corner-five-bottom', '0 0 ' + r + ' ' + r);
    DD_roundies.addRule('.ui-corner-five-right', '0 ' + r + ' ' + r + ' 0');
    DD_roundies.addRule('.ui-corner-five-left', r + ' 0 0 ' + r);
    DD_roundies.addRule('.ui-corner-five-tl', r + ' 0 0 0');
    DD_roundies.addRule('.ui-corner-five-tr', '0 ' + r + ' 0 0');
    DD_roundies.addRule('.ui-corner-five-br', '0 0 ' + r + ' 0');
    DD_roundies.addRule('.ui-corner-five-bl', '0 0 0 ' + r);
  };

  $.uicornerfix3 = function (r) {
    DD_roundies.addRule('.ui-corner-three-all', r);
    DD_roundies.addRule('.ui-corner-three-top', r + ' ' + r + ' 0 0');
    DD_roundies.addRule('.ui-corner-three-bottom', '0 0 ' + r + ' ' + r);
    DD_roundies.addRule('.ui-corner-three-right', '0 ' + r + ' ' + r + ' 0');
    DD_roundies.addRule('.ui-corner-three-left', r + ' 0 0 ' + r);
    DD_roundies.addRule('.ui-corner-three-tl', r + ' 0 0 0');
    DD_roundies.addRule('.ui-corner-three-tr', '0 ' + r + ' 0 0');
    DD_roundies.addRule('.ui-corner-three-br', '0 0 ' + r + ' 0');
    DD_roundies.addRule('.ui-corner-three-bl', '0 0 0 ' + r);
  };

  $.uicornerfix('10px');
  $.uicornerfix5('5px');
  $.uicornerfix3('3px');

}/* tps_document_ready */
