/*** Billboard Class ***/

function Billboard(position,file,targetHeight) {

  this._position = position;
  this._file = file;
  this._posId = '#' + position;
  this._nav = this._posId + ' div.billboard_nav';
  this._next = this._nav + ' a.next';
  this._prev = this._nav + ' a.prev';
  this._header = this._posId + ' div.billboard_header';
  this._title = this._posId + ' h1.billboard_title';
  this._body = this._posId + ' div.billboard_body';
  this._container = this._posId + ' div.body_container';
  this._flashdiv = position + '_flash';

  this._panelState = 0;
  this._targetHeight = targetHeight - 36;

  this._navForwardActive = 'images/template2005/billboard/nav_forward_active.gif';
  this._navBackActive = 'images/template2005/billboard/nav_back_active.gif';
  this._navForwardInactive = 'images/template2005/billboard/nav_forward_inactive.gif';
  this._navBackInactive = 'images/template2005/billboard/nav_back_inactive.gif';
  this._loadingImage = 'images/template2005/billboard/loading.gif';

  var fi = new Image();
  var ba = new Image();

  // preload nav images
  fi.src = this._navForwardInactive;
  ba.src = this._navBackActive;

  return(this);

}

Billboard.prototype.init = function() {
  this.build();
  this.loadConfig();
}

Billboard.prototype.build = function() {
  jQuery(this._posId).empty().append(
    '<div class="billboard">' +
      '<div class="billboard_header">' +
        '<h1 class="billboard_title">&nbsp;</h1>' +
      '</div>' +
      '<div class="billboard_body">' +
        '<div class="body_container"><img src="' + this._loadingImage + '" /></div>' +
      '</div>' +
      '<div class="billboard_footer">' +
        '<div class="billboard_footer_left"></div>' +
    '</div>'
  );

}

Billboard.prototype.loadConfig = function() {
  var myUrl = this._file;
  jqLoadXml(myUrl,this);
}

Billboard.prototype.initPanel = function() {
  this._panelCount = jQuery("panel",this._xml).length;

  if (this._panelCount > 1) {
    jQuery(this._nav).show();
    this.initNav();
  }

  this.showPanel();
}

Billboard.prototype.initNav = function() {
  jQuery(this._header).append(
    '<div class="billboard_nav" dir="ltr">' +
      '<table>' +
        '<tr>' +
        //  '<td><a href="#" class="prev" rel="' + this._position + '"></a></td>' +
          '<td><a href="#" class="next" rel="' + this._position + '"></a></td>' +
        '</tr>' +
      '</table>' +
    '</div>' 
  );

  //jQuery(this._prev).append('<img src="' + this._navBackInactive + '" />');

  jQuery(this._next).append('<img src="' + this._navForwardActive + '" />');

  jQuery(this._next).bind('click',function() {
    forwardNav(this);
    return false;
  });

  jQuery(this._prev).addClass('inactive');
}

Billboard.prototype.forwardNav = function() {
  this._lastPanelIndex = jQuery("panel",this._xml).length - 1;

  // first, check to see if there are more panels to show
  if (this._panelState != this._lastPanelIndex) {
    this._panelState++;
    this.showPanel();
   // if (this._panelState == this._lastPanelIndex) { 
      // there are no more panels to show;
     // turn off the forward button
     // this.deactivateForwardNav();
	  // loop back to first panel
	  //this._panelState=0;
	  
   // } 

   // now that we've gone forward, make sure we can go back
   // this.activateBackNav();
  } 
  else if (this._panelState == this._lastPanelIndex) { 
	  this._panelState=0;
	  this.showPanel();	  
    } 
	 // this.showPanel();
}

Billboard.prototype.backNav = function() {
  // first, check to see if there are previous panels to show
  if (this._panelState != 0) {
    this._panelState--;

    if (this._panelState == 0) {
      // there are no more previous panels;
      // turn off the back button
      this.deactivateBackNav();
    } 

    // now that we've gone back, make sure we can go forward
    this.activateForwardNav();

    this.showPanel();
  }
}

Billboard.prototype.activateForwardNav = function() {
  jQuery(this._next).unbind('click');
  jQuery(this._next).bind('click',function() {
    forwardNav(this);
    return false;
  });
  jQuery(this._next).removeClass('inactive');
  jQuery(this._next + ' img').attr('src',this._navForwardActive);
}

Billboard.prototype.deactivateForwardNav = function() { 
  jQuery(this._next).addClass('inactive');
  jQuery(this._next).unbind('click');
  jQuery(this._next).bind('click',function() {
    return false;
  });
  jQuery(this._next + ' img').attr('src',this._navForwardInactive);
}

Billboard.prototype.activateBackNav = function() {
  jQuery(this._prev).unbind('click');
  jQuery(this._prev).bind('click',function() {
    backNav(this);
    return false;
  });
  jQuery(this._prev).removeClass('inactive');
  jQuery(this._prev + ' img').attr('src',this._navBackActive);
}

Billboard.prototype.deactivateBackNav = function() {
  jQuery(this._prev).addClass('inactive');
  jQuery(this._prev).unbind('click');
  jQuery(this._prev).bind('click',function() {
    return false;
  });
  jQuery(this._prev + ' img').attr('src',this._navBackInactive);
}

Billboard.prototype.showPanel = function() {

  if (! this._panels) { this._panels = jQuery("panel",this._xml); }

  var container = this._container;
  var myObject = this;
  var foundImage = false;

  var panel = this._panels.eq(this._panelState);
  var body = jQuery.trim(panel.children("body").eq(0).text());
  var title = panel.children("title").eq(0).text();
  var title_link = panel.children("title_link").eq(0).text();

  var flash = panel.children("flash").eq(0);
  var flash_file = flash.children("file").eq(0).text();

  // set the title, then get the height of the header
  jQuery(this._title).empty();

  if (title_link) {
    jQuery(this._title).append('<a href="' + title_link + '"></a>');
    jQuery(this._title + ' a').append(title);
  } else {
    jQuery(this._title).append(title);
  }

  var headerHeight = jQuery(this._header).height();
  var targetHeight = this._targetHeight;

  // alter the height of the body to keep the
  // total height of the billboard fixed
  jQuery(this._body).height(targetHeight - headerHeight);
  jQuery(container).height(targetHeight - headerHeight);

  // put the body text in the body container div,
  // then fade that div in

  jQuery(container).hide().empty();
  jQuery(container).css({overflow: 'hidden' });
  jQuery(container).css({overflow: 'auto' });

  if (flash_file) {

    var flashdiv = this._flashdiv;
    var flashdivId = '#' + flashdiv;

    var placement = flash.attr('placement');

    if (placement == 'before') {
      jQuery(container).append('<div id="'+flashdiv+'"></div>'+body);
      jQuery(flashdivId).css({ marginBottom: '5px' });
    } else {
      jQuery(container).append(body+'<div id="'+flashdiv+'"></div>');
      jQuery(flashdivId).css({ marginTop: '5px' });
    }


    var flash_width = flash.children("width").eq(0).text();
    var flash_height = flash.children("height").eq(0).text();
    var flash_variables = flash.children("variable");

    var flash_vars = new Object(); 

    jQuery.each(flash_variables, function(i,v) {
      var name = jQuery("name",v).text();
      var value = jQuery("value",v).text();
      flash_vars[name] = value;
    });


  } else {
    jQuery(container).append(body);
  }

  // change the margin on the last p to prevent unnecessary scroll bars
  jQuery(container + ' p:last').css({ margin: 0 });

  jQuery(container + ' img').each(function() { 
    if (! foundImage) {
      this.onload = function() { fadeUpBillboard(myObject); }
      foundImage = true;
    }
  });

  if (! foundImage) {
    fadeUpBillboard(myObject);
  }

  if (flash_file) {
    jQuery(flashdivId).flash(
      {
        src: flash_file,
        width: flash_width,
        height: flash_height,
        wmode: 'opaque',
        scale: 'noscale',
        flashvars: flash_vars
      }

    );
  }

}


/*** Spotlight Class ***/

function Spotlight(position,file) {

  this._position = position;
  this._file = file;
  this._posId = '#' + position;
  this._container = this._position + '_spotlight';
  this._containerId = this._posId + '_spotlight';
  this._flashdiv = position + '_flash';

  return(this);

}

Spotlight.prototype.init = function() {
  this.loadConfig();
  jQuery(this._posId).empty();
  jQuery(this._posId).append('<div id="' + this._container + '" class="spotlight"></div>');
}

Spotlight.prototype.loadConfig = function() {
  var myUrl = this._file;
  jqLoadSpotlightXml(myUrl,this);
}

Spotlight.prototype.showSpotlight = function() {

  var xml = this._xml;
  var forcePanel = jQuery('panel[@force=true]',xml);

  if (forcePanel.length > 0) {
    var panel = forcePanel;
    if (panel.length > 1) { panel = panel.eq(0); }
  } else {
    var panels = jQuery('panel',xml);
    var panelCount = panels.length;
    var randomIndex = Math.floor( Math.random() * panelCount );
    var panel = panels.eq(randomIndex);
  }

  var body = panel.children("body").eq(0).text();

  if (body) {
    jQuery(this._containerId).append(body);
    jQuery(this._containerId).css( { width: 210, paddingLeft: 10 } );
  }

  var flash = panel.children("flash").eq(0);
  var flash_file = flash.children("file").eq(0).text();

  if (flash_file) {

    var flashdiv = this._flashdiv;

    var placement = flash.attr('placement');

    if (placement == 'before') {
      jQuery(this._containerId).prepend('<div id="'+flashdiv+'"></div>');
      if (body) {
        jQuery('#' + flashdiv).css( { marginBottom: 10 } );
      } else {
        jQuery('#' + flashdiv).css( { margin: 0, padding: 0 } );
      }
    } else {
      jQuery(this._containerId).append('<div id="'+flashdiv+'"></div>');
      if (body) {
        jQuery('#' + flashdiv).css( { marginTop: 10 } );
      } else {
        jQuery('#' + flashdiv).css( { margin: 0, padding: 0 } );
      }
    }

    var flash_width = flash.children("width").eq(0).text();
    var flash_height = flash.children("height").eq(0).text();
    var flash_variables = flash.children("variable");

    var flash_vars = new Object(); 

    jQuery.each(flash_variables, function(i,v) {
      var name = jQuery("name",v).text();
      var value = jQuery("value",v).text();
      flash_vars[name] = value;
    });

    jQuery('#' + flashdiv).flash(
      {
        src: flash_file,
        width: flash_width,
        height: flash_height,
        wmode: 'opaque',
        scale: 'noscale',
        flashvars: flash_vars
      }

    );

  }

  jQuery(this._posId).css( { width: 220, height: 272, overflow: hidden } );

}


/*** utility functions for Billboard and Spotlight classes ***/

function jqLoadXml(myUrl,myObject) {
  jQuery.ajax({
    type:'GET',
    url: myUrl,
    dataType: 'xml',
    success: function(xml) { myObject._xml = xml; myObject.initPanel(); }
  });
}

function jqLoadSpotlightXml(myUrl,myObject) {
  jQuery.ajax({
    type:'GET',
    url: myUrl,
    dataType: 'xml',
    success: function(xml) { myObject._xml = xml; myObject.showSpotlight(); }
  });
}

function fadeUpBillboard(myObject) {
  jQuery(myObject._container).fadeIn(400);
}

function forwardNav(anchor) {
  var position = anchor.rel;
  billboards[position].forwardNav();
  if (autoForward) {
    clearTimeout(autoForward);
  }
}

function backNav(anchor) {
  var position = anchor.rel;
  billboards[position].backNav();
}

function autoForward(position) {
  billboards[position].forwardNav();
}

function playPodcast(div,config,createDiv,position) {

  // create the div if requested
  if (createDiv && position) { 
    jQuery('#' + position + ' div.body_container').append('<div id="'+div+'"></div>'); 
  }

  jQuery('#' + div).css({ marginTop: 5, marginBottom: 5, display: 'block' });

  var flashvars = new Object();
  flashvars = { config: config, autoplay: 'true' };

  jQuery('#' + div).flash({ 
    src: 'http://www.nortel.com/multimedia/flash/165x16/165x16_mp3_player.swf',
    width: '165',
    height: '16',
    wmode: 'opaque',
    scale: 'noscale',
    flashvars: flashvars
  });

}
