/* USER VARIABLES */

// Link to profiles page
var profileLink = "http://www.ubs.com/1/e/career_candidates/graduates_and_interns/videos.html?vid=";
// Link to event calendar
var eventLink = "http://www.ubs.com/1/e/career_candidates/graduates_and_interns/events.html";
// XML file for events
var eventXMLFile = "http://www.ubs.com/2/e/medlib/group/graduates/eventcal/xml/events.xml";
// XML file for video profiles
var profileXMLFile = "http://www.ubs.com/2/e/medlib/group/graduates/videos/profiles/profiles.xml";

/* END OF USER VARIABLES - DO NOT EDIT BELOW THIS LINE */

// Array of profile objects
var arrProfiles = []; 
// Flags when an event has been loaded
var gotEvent = -1;
// Today's date
var today = new Date();

// Video profile object
var videoProfile = function()
{
    this.get_html = function()
    { 
        var strImage = (this.img_id && this.img_id != '') ? 'http://www.ubs.com/1/ShowImage/index?contentId=' + this.img_id : 'http://www.ubs.com/2/e/medlib/group/graduates/intelligent_site/grad/profile_missing.gif';
        var txt = '<table border="0" width="100%" cellpadding="0" cellspacing="0"><tr>' +
                  '<td valign="top" width="58"><a href="' + profileLink + this.id + '"><img src="' + strImage  +
                  '" width="68" height="60" alt="" border="0"/></a></td>' +
                  '<td valign="top" width="155" class="content"><h3><a href="' + profileLink + this.id + '">Meet our people</a></h3>' +
                  this.name + '<br/>' + this.city + '<br/>' + this.working_group + '<br/></td></tr></table>';
        return txt;
    }

}

// When DOM is ready..
$(document).ready(function() { 
  // Remove event trapping
  window.onresize = null;
  document.onmousemove = '';
  $("div#promoPanel div.left").empty();
  $("div#promoPanel div.right").empty();


  // Load event
  $.ajax({
    type: "GET",
    url: eventXMLFile,
    dataType: "xml",
    success: function(xml) {
      $(xml).find('event').each(function(i) {
        var proceed = 1;
        var ec_event_num = $(this).find('event_num').text();
        var ec_event_time = $(this).find('event_time').text();
        var ec_event_strdate = $(this).find('event_strdate').text();
        var ec_event_end_pcdate = $(this).find('event_end_pcdate').text();
        var ec_event_name = $(this).find('event_name').text();
        var ec_city = $(this).find('city').text();
        var ec_country = $(this).find('country').text();

        // Check date for validity
        var validDate = today.compare(ec_event_end_pcdate);
        if(validDate == 1 && gotEvent != 1) {
          gotEvent = 1;
          $('div#promoPanel div.left').html(
              '<table border="0" width="100%" cellpadding="0" cellspacing="0"><tr>' +
              '<td valign="top" width="58"><a href="' + eventLink + '">' +
              '<img src="http://www.ubs.com/2/e/medlib/group/graduates/intelligent_site/grad/grad_img1.jpg" width="58" height="60" alt="" border="0" /></a></td>' +
              '<td valign="top" width="155" class="content"><h3><a href="' + eventLink + '">Upcoming event</a></h3>' +
              '<em>' + ec_event_strdate + '</em><br/>' +
              ec_city + ', ' + ec_country + '<br/>' +
              '</td></tr></table>');
        } 
      }); // close each 
    },
    error: function(obj, str, exc){
      alert("Error: " + str);
    } 
  }); //close $.ajax 


  // Load profile
  $.ajax({
    type: "GET",
    url: profileXMLFile,
    dataType: "xml",
    success: function(xml) {
      $(xml).find('profile').each(function(i)
      {
        // Only consider "Video" profiles
        if( $(this).attr('profile') == 'Video' )
        {
          // Populate profiles
          arrProfiles.length = i;
          arrProfiles[i] = new videoProfile(); 
          arrProfiles[i].id = $(this).attr('id');
          arrProfiles[i].name = $(this).attr('name');
          arrProfiles[i].profile = $(this).attr('profile');
          arrProfiles[i].business_group = $(this).attr('business_group');
          arrProfiles[i].city = $(this).attr('city');
          arrProfiles[i].region = $(this).attr('region'); // unused
          arrProfiles[i].studied = $(this).attr('studied'); // unused
          arrProfiles[i].working_group = $(this).attr('working_group');
          arrProfiles[i].title = $(this).attr('title');
          arrProfiles[i].url = $(this).attr('url');
          arrProfiles[i].img_id = $(this).attr('img_id');
        }
      }); // close each
      
      // Get random profile and display on home page
      var pid = get_rand( arrProfiles.length );
      var pTxt = arrProfiles[pid].get_html();
      $('div#promoPanel div.right').html( pTxt );
    },
    error: function(obj, str, exc){
      alert("Error: " + str);
    } 
  }); //close $.ajax 


}); //close $(document).ready


// Return a random number between zero and 'limit'
function get_rand(limit)
{
    var num = Math.random() * limit;
    return Math.floor(num); 
}


// Check a date to see if it's in the past. Returns 1 for valid date, -1 for past date
Date.prototype.compare = function(eDate)
{
  // Date format: 2008-06-20
  var yr = eDate.substr(0,4);
  var mo = eDate.substr(5,2);
  var da = eDate.substr(8,2);

  // Make sure we have all components
  if(!yr || !mo || !da)
      return -1;

  // Compare dates
  if(yr < this.getFullYear() || (yr == this.getFullYear() && mo < this.getMonth()+1) ||
    (yr == this.getFullYear() && mo == this.getMonth()+1 && da < this.getDate()))
    return -1;
  else
    return 1;
}

