/*
 *  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     = '9o2qacqqef412d72l86dm30f98%40group.calendar.google.com';  // feed id for the events calendar feed
var gcal_bburg_uid     = 'rtdva15q7i8p9e8ahj1gg155cg%40group.calendar.google.com';  // feed id for the blacksburg schedule calendar feed
var gcal_roanoke_uid   = 'o7r5jpp9sjetaocmv17sh9mu90%40group.calendar.google.com';  // feed id for the roanoke schedule calendar feed
var gcal_abbott_uid    = 'v8b0arjvcahq9di826055aobro%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;
var gcal_abbott_url    = gcal_base_href  + gcal_abbott_uid + gcal_tail_href;
var gcal_abbott_print  = gcal_print_href + gcal_abbott_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 scripts
// --------------------
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;
}

/* globals to calculate current date for google calendar format */
var d = new Date();
var year  = d.getFullYear();
var month = pad_zero(d.getMonth()+1);
var day1  = pad_zero(d.getDate());
var tz = pad_zero(d.getTimezoneOffset()/60);
var s_min = year+'-'+month+'-'+day1+'T00:00:00-'+tz+':00';
var s_max = year+'-'+month+'-'+day1+'T23:59:59-'+tz+':00';

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 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)
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// flickr scripts
// --------------------
// 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();

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// email scripts
// --------------------
//generate an email address link
function contact(domain, user, tld, display) {
  // build the address
  var address = user + '@' + domain + '.' + tld;
  // setup the email link
  var email_link = '';
  email_link = email_link + '<' + 'a href=\"mailto:' + address + '\">'; // open a href tag
  if (!display) {
    email_link = email_link + address; // display email address as link text
  } else {
    email_link = email_link + display; // display the desired link text
  }
  email_link = email_link + '</' + 'a>'; // close the tag
  // display the whole she-bang on the page
  return email_link;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// jquery calls to be executed when document ready
// --------------------

$(document).ready(function() {
  
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  /* reject old browsers */
  $.reject({
    imagePath:'images/',
    closeCookie: true
  });
  
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  /* fix png transparency in ie6 */
  $('body').supersleight({shim:'images/trasnparent.gif'});
  
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  /* tipsy top_menu tooltips */
  $(".tt_top_menu").tipsy({
    gravity: "n",
    fade: true
  });
  
  /* tipsy footer tooltips */
  $(".tt_footer").tipsy({
    gravity: "s",
    fade: true
  });
  
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  /* specials fancybox */
  $('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 */
  $(".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 */
  $('#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 */
  $('#date_time').jclock({
    format: '%a, %b %d, %Y | %I:%M:%S %P'
  });
  
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  /* jquery calls for the contact panel */
  // Expand Panel
  $("#open").click(function(){
    $("div.topcontent").hide();
    $("div#toppanel").slideDown("slow");
    $("div.topcontent").fadeIn("slow");
    $("#contact-form").validationEngine({
      promptPosition: "topRight"
    });
  });
  // Collapse Panel
  $("#close").click(function(){
    $("div.topcontent").fadeOut("slow");
    $("div#toppanel").slideUp("slow");
  });
  // Switch buttons from "Contact Us" to "Close Panel" on click
  $("#toggle a").click(function () {
    $("#toggle a").toggle();
  });
  
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  /* New Student Info Panel */
  // Expand Panel
  $("#open2").click(function(){
    $("div.topconten2t").hide();
    $("div#toppanel2").slideDown("slow");
    $("div.topcontent2").fadeIn("slow");
  });
  // Collapse Panel
  $("#close2").click(function(){
    $("div.topcontent2").fadeOut("slow");
    $("div#toppanel2").slideUp("slow");
  });
  // Switch buttons from "Contact Us" to "Close Panel" on click
  $("#toggle2 a").click(function () {
    $("#toggle2 a").toggle();
  });
  

  
});
