/*
 *  Global Javascript
 */

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// global variables
// --------------------
/* google globals */
var gcal_base_href     = 'http://www.google.com/calendar/feeds/';
var gcal_tail_href     = '/public/basic';
var gcal_print_href    = 'http://www.google.com/calendar/print_preview?hl=en&ctz=America%2FNew_York&pgsz=letter&wkst=1&src=';
var gcal_event_uid     = 'e7oot9jds2t34l1uaa2mirp7h0%40group.calendar.google.com';  // feed id for the events calendar feed
var gcal_bburg_uid     = 'ksv7vq6537i5rqiq4hmgs4rbvk%40group.calendar.google.com';  // feed id for the blacksburg schedule calendar feed
var gcal_roanoke_uid   = 'hcj0l95tehh34itvn7eg5g3vio%40group.calendar.google.com';  // feed id for the roanoke schedule calendar feed
// build the google calendar urls
var gcal_events_url    = gcal_base_href  + gcal_event_uid   + gcal_tail_href;
var gcal_events_print  = gcal_print_href + gcal_event_uid;
var gcal_bburg_url     = gcal_base_href  + gcal_bburg_uid + gcal_tail_href;
var gcal_bburg_print   = gcal_print_href + gcal_bburg_uid;
var gcal_roanoke_url   = gcal_base_href  + gcal_roanoke_uid + gcal_tail_href;
var gcal_roanoke_print = gcal_print_href + gcal_roanoke_uid;

/* flickr globals */
var flickr_api = '42359204516b7fb3785aeb9b3c8a93f2';  // flickr api key
var flickr_uid = '39926177@N02';                      // flickr user id
var flickr_gid = '1235783@N20';                       // flickr group id
var flickr_pid = '72157621333039117';                 // flickr photoset id

/* lavalamp globals */
var page_id     = 0;
var home_top    = 0;
var home_left   = 0;
var home_width  = 0;
var home_height = 0;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// time/date
// --------------------
var dayarray   = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
var montharray = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

function pad_zero(n) {
  return (n < 10 ? '0' : '') + n;
}

function num_postfix(n) {
  // add the appropriate ending to the day
  var mn = "th";
  if (n == 1) { mn = "st"; }
    else if (n == 2)  { mn = "nd"; }
    else if (n == 3)  { mn = "rd"; }
    else if (n == 21) { mn = "st"; }
    else if (n == 22) { mn = "nd"; }
    else if (n == 23) { mn = "rd"; }
    else if (n == 31) { mn = "st"; }
  return n+mn;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// window open scripts
// --------------------
var win = null;

// Advanced window open - caller(in code, not user) has full control of the window
// NewWindow(url/page,title,width,height,allow-scrollbars(yes/no),resizable(yes/no),
//           toolbar(yes/no),locationbar(yes/no),directories(yes/no),
//           statusbar(yes/no),menubar(yes/no),copyhistory(yes/no));
//   ex. <a href="URL" onclick="NewWindow(this.href,'Logon','400','400','yes','yes','yes','yes','yes','yes','yes','yes');return false">Logon</a
function NewWindow(mypage,myname,wdt,hgt,scroll,resize,tool,loc,dir,stat,menub,hist){
  LeftPosition = (screen.width) ? (screen.width-wdt)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-hgt)/2 : 0;
  settings = 'height='+hgt+',width='+wdt+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable='+resize+',toolbar='+tool+',location='+loc+',directories='+dir+',status='+stat+',menubar='+menub+',copyhistory='+hist
  win = window.open(mypage,myname,settings)
}

// Simple window open
function openWindow(myurl){
  win = window.open(myurl)
}

// get the list of photoset ids from flickr and return a random one
function get_random_photoset(){
  // retrieve the list of photoset id's for the specified user and write it to an array
  var flickr_pids = new Array();
  var furl = 'http://api.flickr.com/services/rest/?format=json&api_key='+flickr_api;
      furl += '&method=flickr.photosets.getList&user_id='+flickr_uid+'&jsoncallback=?';
  $.getJSON(furl, function (data) {
    $.each(data.photosets.photoset, function (i, set) {
      flickr_pids[i]=set.id;
    });
    var rn=Math.floor(Math.random()*flickr_pids.length);
    var rand_pid = flickr_pids[rn];
    if (rand_pid != null) {
      flickr_pid = rand_pid;
    }
  });
}

// call the random photoset function and set the photoset id to it
get_random_photoset();


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// jquery calls
// --------------------
/* tipsy top_menu tooltips */
$(document).ready(
  function() {
    $(".tt_top_menu").tipsy({
      gravity: "n",
      fade: true
    });
});
/* tipsy footer tooltips */
$(document).ready(
  function() {
    $(".tt_footer").tipsy({
      gravity: "s",
      fade: true
    });
});
/* specials fancybox */
$(document).ready(
  function (){
    $('a#special_link').fancybox({
      transitionIn        :  'elastic',
      transitionOut       :  'elastic',
      easingIn            :  'easeOutBack',
      easingOut           :  'easeInBack',
      easingChange        :  'swing',
      overlayShow         :  true,
      overlayOpacity      :  0.3,
      overlayColor        :  '#000'
    });
});
/* pikachoose+flickr image gallery */
$(document).ready(
  function (){
    $(".pikachooseCompact").flickr({
      api_key                 : flickr_api,
      //type                    : 'photoset',
      //photoset_id             : flickr_pid,
      type                    : 'search',
      user_id                 : flickr_uid,
      //group_id                : flickr_gid,
      attr                    : 'class="flickrbox"',
      page                    : 1,
      per_page                : 500,
      randomize               : true,
      cutoff                  : 5,
      show_pager              : false,
      pikachoose              : true,
      pika_id                 : 'pikame_compact',
      use_pika_dot            : true,
      pika_dot_path           : 'scripts/jquery/pikachoose/thumb-circle-active.png',
      pika_start_img          : 'images/tkd_slideshow_first.png',
      pika_start_img_caption  : "Abbott's TaeKwonDo",
      pika_end_img            : 'images/tkd_slideshow_last.png',
      pika_end_img_caption    : "TaeKwonDo America",
      callback                : pikaCallBack
    });
    function pikaCallBack(el){
      //jQuery(el).litebox(yourSettingsObject);
      $('#pikame_compact').PikaChoose({
        user_thumbs    : true,
        show_prev_next : false,
        thumb_width    : 10,
        thumb_height   : 10,
        box_width      : 605,
        box_height     : 428,
        auto_play      : true
      });
    }
});
/* lavaLamp menu */
$(document).ready(
  function (){
    $('#lavaLampVertical').lavaLamp({
      fx         : 'easeOutBack',
      speed      : 1000,
      setOnClick : true,
      returnHome : true,
      autoReturn : true,
      startItem  : page_id,
      homeTop    : home_top,
      homeLeft   : home_left,
      homeWidth  : home_width,
      homeHeight : home_height
    });
});
/* live date/time */
$(document).ready(
  function (){
    $('#date_time').jclock({
      format: '%a, %b %d, %Y | %I:%M:%S %P'
    });
});
/* simplemodal contact form */
$(document).ready(
  function (){
    $("a.cl_mnu_contact").click(function (e) {
      e.preventDefault();
      var src='contact.php';
      $.modal('<div id="osx_content"><div id="osx_title">Contact Us</div><iframe src="' + src + '" height="525" width="900" style="border:0"></div>',{
        overlayId: 'osx_overlay',
        containerId: 'osx_container',
        containerCss:{
          height:607,
          padding:0,
          width:900,
          margin:0
        },
        closeHTML: '<div class="close"><a href="#" class="simplemodal-close">x</a></div>',
        minHeight:80,
        opacity:65, 
        position:['0',],
        overlayClose:true,
        onOpen:OSX2.open,
        onClose:OSX2.close
      });
    });
    
    var OSX2 = {
      container: null,
      open: function (d) {
        var self = this;
        self.container = d.container[0];
        d.overlay.fadeIn('slow', function () {
          $("#osx_content", self.container).show();
          var title = $("#osx_title", self.container);
          title.show();
          d.container.slideDown('slow', function () {
            setTimeout(function () {
              var h = $("#simplemodal-data", self.container).height()
                + title.height()
                + 20; // padding
              d.container.animate(
                {height: h}, 
                200,
                function () {
                  $("div.close", self.container).show();
                  $("#simplemodal-data", self.container).fadeIn();
                }
              );
            },
            300
            );
          });
        })
      },
      close: function (d) {
        var self = this;
        d.container.animate(
          {top:"-" + (d.container.height() + 20)},
          700,
          function () {
            d.container.slideUp('slow', function () {
              d.overlay.fadeOut('slow', function () {
                self.close(); // or $.modal.close();
              });
            });
          }
        );
      }
    };
});
