
// --- Variables ------------------------------------------------------------ //

var kpz_target_x;
var kpz_target_y;

var kpz_flyer_width  = 1105;
var kpz_flyer_height =  580;

// --- Tooltips ------------------------------------------------------------- //

var kpz_tooltips = new Array();

// --- Fonctions ------------------------------------------------------------ //

function kpzFlyerStart()
{
  if ( !kpz_flyer_image.complete ) {
    setTimeout("kpzFlyerStart()", 100);
    return;
  }

  for ( i = 0 ; i < kpz_tooltips.length ; i++ )
    if ( !document.getElementById("kpz_tooltip" + i + "_image").complete ) {
      setTimeout("kpzFlyerStart()", 100);
      return;
    }

  if ( window.Event && document.captureEvents ) // Firefox
    document.captureEvents(Event.MOUSEMOVE);

  document.onmousemove = kpzFlyerOnMouseMove;
  window.onresize      = kpzFlyerOnResize;

  kpzFlyerOnResize(null);

  kpz_flyer.style.left = kpz_target_x;
  kpz_flyer.style.top  = -kpz_flyer_height * 2;

  kpz_flyer.style.display = "block";

  kpzFlyerTick();
}

// --- //

function kpzFlyerTick()
{
  if ( kpz_flyer.display == "none" )
    return;

  // Flyer

  x = parseFloat(kpz_flyer.style.left);
  y = parseFloat(kpz_flyer.style.top );

  if ( x != kpz_target_x ) kpz_flyer.style.left = x + (kpz_target_x - x) * 0.08;
  if ( y != kpz_target_y ) kpz_flyer.style.top  = y + (kpz_target_y - y) * 0.08;

  // Tooltips

  for ( i = 0 ; i < kpz_tooltips.length ; i++ )
  {
    var tt = document.getElementById("kpz_tooltip" + i);

    if ( tt.style.display == "none" )
      continue;

    x = parseFloat(tt.style.left);
    y = parseFloat(tt.style.top );

    if ( x != kpz_tooltips[i].x ) tt.style.left = x + (kpz_tooltips[i].x - x) * 0.12;
    if ( y != kpz_tooltips[i].y ) tt.style.top  = y + (kpz_tooltips[i].y - y) * 0.12;
  }

  setTimeout("kpzFlyerTick()", 10);
}

// --- //

function kpzFlyerOnMouseMove(e)
{
  if ( kpz_flyer.style.display == "none" )
    return;

  // Test des aires de tooltips et affichages correspondants

  var bounds = kpzFlyerGetObjectBounds(kpz_flyer);

  var mp = kpzFlyerGetMousePosition(e);

  var lx = mp.x - bounds.left;
  var ly = mp.y - bounds.top;

  for ( i = 0 ; i < kpz_tooltips.length ; i++ )
  {
    var tt = document.getElementById("kpz_tooltip" + i);

    if ( kpzFlyerISInBounds(lx, ly, kpz_tooltips[i]) )
      display = "block";
    else
      display = "none";

    if ( display == "block" ) {
      kpz_tooltips[i].x = mp.x - kpz_tooltips[i].width  + 24;
      kpz_tooltips[i].y = mp.y - kpz_tooltips[i].height + 24;
    }

    if ( tt.style.display == display )
      continue;

    if ( display == "block" ) {
      tt.style.left = kpz_tooltips[i].x;
      tt.style.top  = kpz_tooltips[i].y;
    }

    tt.style.display = display;
  }
}

function kpzFlyerOnResize(e)
{
  kpz_target_x = (document.body.clientWidth  - kpz_flyer_width ) / 2;
  kpz_target_y = (document.body.clientHeight - kpz_flyer_height) / 2;
}

function kpzFlyerOnClose(e)
{
  kpz_flyer.style.display='none';

  kpz_flyer_audio.innerHTML = '';
}

// --- //

function kpzFlyerGetMousePosition(e)
{
  var result = new Object();

  if ( !document.all )
  {
    if ( !e )
      e = window.event;

    if ( typeof e == "undefined" )
      e = window.Event;

    result.x = e.pageX;
    result.y = e.pageY;
  }
  else
  {
    result.x = event.clientX + document.body.scrollLeft;
    result.y = event.clientY + document.body.scrollTop;
  }

  return result;
}

function kpzFlyerISInBounds(x, y, bounds)
{
  if ( x < bounds.left || x > bounds.right )
    return false;

  if ( y < bounds.top || y > bounds.bottom )
    return false;

  return true;
}

function kpzFlyerGetObjectBounds(o)
{
  var result = new Object();

  // Left

  result.left = 0;

  if ( o.offsetParent )
  {
    var oc = o;

    while ( true )
    {
      result.left += oc.offsetLeft;

      if ( !oc.offsetParent )
        break;

      oc = oc.offsetParent;
    }
  }
  else if ( o.x )
  {
    result.left = obj.x;
  }

  // Top

  result.top = 0;

  if ( o.offsetParent )
  {
    var oc = o;

    while ( true )
    {
      result.top += oc.offsetTop;

      if ( !oc.offsetParent )
        break;

      oc = oc.offsetParent;
    }
  }
  else if ( o.x )
  {
    result.top = obj.x;
  }

  // Width / Height

  result.width  = o.offsetWidth;
  result.height = o.offsetHeight;

  // Right / Bottom

  result.right  = result.left + result.width;
  result.bottom = result.top  + result.height;

  return result;
}

// --- Audio ---------------------------------------------------------------- //

function kpzGetPlayerString(url, width, height, player_name)
{
  url = "http://www.google.com/reader/ui/3247397568-audio-player.swf?audioUrl=" + url + "&autoPlay=true";

  return   '<object classid  = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
         + '        codebase = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"'
         + '        width    = "' +  width.toString() + '"'
         + '        height   = "' + height.toString() + '"'
         + '        id       = "' + player_name       + '">'
         + '    <param name = "movie"   value = "' + url + '">'
         + '    <param name = "quality" value = "high"       >'
         + '    <param name = "bgcolor" value = "#000000"    >'
         + '  <embed href        = "' + url + '"'
         + '         quality     = "high"'
         + '         bgcolor     = "#000000"'
         + '         width       = "' +  width.toString() + '"'
         + '         height      = "' + height.toString() + '"'
         + '         name        = "' + player_name       + '"'
         + '         align       = ""'
         + '         type        = "application/x-shockwave-flash"'
         + '         pluginspace = "http://www.macromedia.com/go/getflashplayer">'
         + '  </embed>'
         + '</object>';
}

// --- Main ----------------------------------------------------------------- //

if ( document.cookie.indexOf("www_galaxiefm_com_flyer") == -1 )
{
  var now    = new Date();
  var expire = new Date(now.getTime() + 32 * 60 * 60 * 1000); // 32h
  var html;

  document.cookie = "www_galaxiefm_com_flyer=1; expires=" + expire.toGMTString();

  // Flyer

  html = '<div id="kpz_flyer" style="position:absolute; z-index:100; display:none; width:auto; height:auto; background-color:transparent; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px"">'
       + '  <table cellpadding="2" cellspacing="0">'
       + '  <tr>'
       + '    <td><img id="kpz_flyer_image" src="http://www.galaxiefm.com/flyers/20100226/flyer.png" width="1105" height="580" /></td>'
       + '    <td style="vertical-align:top; padding-top:29"><img src="http://www.galaxiefm.com/flyers/close.png" style="cursor:hand" onclick="kpzFlyerOnClose()" alt="Fermer" /></td>'
       + '  </tr>'
       + '  </table>'
       + '</div>\n';

  document.write(html);

  // Audio

  html = '<div id="kpz_flyer_audio" style="position:absolute; top:0px; left:0px; width:1px; height:1px">' +
           kpzGetPlayerString("http://www.galaxiefm.com/flyers/20100226/audio.mp3", 1, 1, "kpzAudioPlayer") +
         '</div>';

  document.write(html);

/*
   '  <object type="application/x-mplayer2" classid="6BF52A52-394A-11d3-B153-00C04F79FAA6" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/">' +
   '    <param name="autoStart"       value="true" />' +
   '    <param name="playCount"       value="1" />' +
   '    <param name="allowNetworking" value="all" />' +
   '    <param name="fileName"        value="http://www.galaxiefm.com/flyers/20100226/audio.mp3" />' +
   '    <param name="uiMode"          value="invisible" />' +
   '  </object>' +
*/

  // Tooltips

  var kpz_tooltip;

  kpz_tooltip = new Object();
  kpz_tooltip.width  = 405;
  kpz_tooltip.height = 251;
  kpz_tooltip.left   = 871;
  kpz_tooltip.right  = kpz_tooltip.left + 185;
  kpz_tooltip.top    = 270;
  kpz_tooltip.bottom = kpz_tooltip.top + 101;
  kpz_tooltips.push(kpz_tooltip);

  kpz_tooltip = new Object();
  kpz_tooltip.width  = 411;
  kpz_tooltip.height = 205;
  kpz_tooltip.left   = 697;
  kpz_tooltip.right  = kpz_tooltip.left + 163;
  kpz_tooltip.top    = 459;
  kpz_tooltip.bottom = kpz_tooltip.top + 68;
  kpz_tooltips.push(kpz_tooltip);

  kpz_tooltip = new Object();
  kpz_tooltip.width  = 551;
  kpz_tooltip.height = 231;
  kpz_tooltip.left   = 656;
  kpz_tooltip.right  = kpz_tooltip.left + 211;
  kpz_tooltip.top    = 385;
  kpz_tooltip.bottom = kpz_tooltip.top + 72;
  kpz_tooltips.push(kpz_tooltip);

  for ( i = 0 ; i < kpz_tooltips.length ; i++ )
  {
    html = '<div id="kpz_tooltip' + i + '" style="position:absolute; z-index:101; display:none; width:auto; height:auto; background-color:transparent; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px"">'
         + '  <img id="kpz_tooltip' + i + '_image" src="http://www.galaxiefm.com/flyers/20100226/tooltip' + i + '.png" width="' + kpz_tooltips[i].width + '" height="' + kpz_tooltips[i].height + '" />'
         + '</div>\n';

    document.write(html);
  }

  kpzFlyerStart();
}
