var canvas;
var ctx;
var fade_timer,slide_timer;
var flasher_running = false;
var flasher_increments = 50;
var flasher_ticks = 15;
var flasher_target_alpha = 100;
var flasher_current_alpha = 0;
var sequence_count = -1;
var BannerArray = Array();

function loadFlasher(url)
{
  canvas = document.getElementById('flasher');
  if (canvas.getContext)
  {
    ctx = canvas.getContext('2d');
    ctx.globalAlpha = 1;
  } else {
    //console.log("browser does not support the html5 canvas element.");
    return false;
  }
  //console.log("loading banners from '"+url+"'");
  var xmlhttp;
  var txt,xx,x,i;
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else {
    // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() 
  {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      x=xmlhttp.responseXML.documentElement.getElementsByTagName("item");
      var img="", h1="", h2="", text="", link="", duration=0;
      BannerArray = Array();
      for (i=0;i<x.length;i++) {
        xx=x[i].getElementsByTagName("image");
        try {
          img = xx[0].firstChild.nodeValue + "&rand=" + Math.random();
        } catch (er) {
          img = "";
        }
        
        xx=x[i].getElementsByTagName("title1");
        try {
          h1 = xx[0].firstChild.nodeValue;
        } catch (er) {
          h1 = "";
        }
        
        xx=x[i].getElementsByTagName("title2");
        try {
          h2 = xx[0].firstChild.nodeValue;
        } catch (er) {
          h2 = "";
        }

        xx=x[i].getElementsByTagName("bread");
        try {
          text = xx[0].firstChild.nodeValue;
        } catch (er) {
          text = "";
        }

        xx=x[i].getElementsByTagName("address");
        try {
          link = xx[0].firstChild.nodeValue;
        } catch (er) {
          link = "";
        }

        xx=x[i].getElementsByTagName("dur");
        try {
          duration = xx[0].firstChild.nodeValue;
        } catch (er) {
          duration = 10;
        }
        BannerArray[i] = new Array(img, h1, h2, text, link, duration);
      }
      //console.log(BannerArray.length + " banners loaded.");
      start_flasher();
    }
  }
  xmlhttp.open("GET",url,true);
  xmlhttp.send();
}

function start_flasher() {
  canvas = document.getElementById('flasher');
  if (canvas.getContext)
  {
    ctx = canvas.getContext('2d');
    ctx.globalAlpha = 1;
  } else {
    alert('Your browser does not support the html5 canvas element. Sorry');
    return false;
  }
  canvas.addEventListener('mousedown', flasher_mousedown, false);
  flasher_next();
}

function flasher_next()
{
  sequence_count++;
  if(sequence_count >= BannerArray.length) {
    sequence_count = 0;
  }
  flasher_flip();
}

function flasher_mousedown(ev)
{
  var mx,my;
  
  if (ev.layerX || ev.layerX == 0) { // Firefox
    ev._x = ev.layerX;
    ev._y = ev.layerY;
  } else if (ev.offsetX || ev.offsetX == 0) { // Opera
    ev._x = ev.offsetX;
    ev._y = ev.offsetY;
  }
  mx = ev._x;
  my = ev._y;
  if(!flasher_running){
    var x=940-(BannerArray.length*30), y=190, radius = 5, height=20, width=21;
    var hitbox = false;
    //console.log("mouseclick("+mx+":"+my+")");
    for(var i = 0; i < BannerArray.length; i++)
    {
      //console.log("rect("+x+"-"+(x+width)+":"+y+"-"+(y+height)+")");
      if((x < mx && mx < (x+width)) && (y < my && my < (y+height)))
      {
        //console.log("hit #"+i);
        if(i != sequence_count) {
          sequence_count=i;
          hitbox = true;
          flasher_flip();
        }
      }
      x=x+30;
    }
    if(!hitbox) {
      window.location.href = BannerArray[sequence_count][4];
    }
  }
  //alert("x:"+x+" y:"+y);
}

function flasher_flip(){
  clearTimeout(fade_timer);
  clearTimeout(slide_timer);
  //console.log("fade out start");
  flasher_target_alpha = 0;
  flasher_fade_run();
}

function load_flasher_image(imgurl){
  //console.log("loading: " + imgurl);
  var img     = new Image();
  img.onload  = function() {
      ctx.drawImage(img,0,0);
      flasher_image_loaded();
    }
  img.src = imgurl;
}

function flasher_image_loaded(){
  h1 = BannerArray[sequence_count][1];
  h2 = BannerArray[sequence_count][2];
  text = BannerArray[sequence_count][3];
  link = BannerArray[sequence_count][4];
  //console.log("h1: '"+h1+"'");
  //console.log("h2: '"+h2+"'");
  //console.log("text: '"+text+"'");
  //console.log("link: '"+link+"'");
  //console.log("fade in start");
  var x=25,y=150;
  
  flasher_drawtext(h1,x,y);
  flasher_drawtext(h2,x,y+35);
  
  flasher_drawboxes(sequence_count,BannerArray.length);
  
  flasher_target_alpha = 100;
  flasher_fade_run();
}

function flasher_drawboxes(current,total){
  var x=940-(total*30), y=190, radius = 5, height=20, width=21;
  ctx.shadowOffsetX = 0;
  ctx.shadowOffsetY = 0;
  ctx.shadowBlur = 0;
  ctx.shadowColor = "";
  for(var i = 0; i < total; i++)
  {
    if(i==current) {
      ctx.fillStyle = "#E90029"
    } else {
      ctx.fillStyle = "#999999"
    }
    ctx.beginPath();
    ctx.moveTo(x + radius, y);
    ctx.lineTo(x + width - radius, y);
    ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
    ctx.lineTo(x + width, y + height - radius);
    ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
    ctx.lineTo(x + radius, y + height);
    ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
    ctx.lineTo(x, y + radius);
    ctx.quadraticCurveTo(x, y, x + radius, y);
    ctx.closePath();
    ctx.fill();
    
    ctx.font = "bold 10px Verdana";
    ctx.fillStyle = "rgba(255, 255, 255, 1)";
    ctx.fillText(""+(i+1),x+7,y+14);
    x=x+30;
  }
}

function flasher_drawtext(text, x, y){
  ctx.font = "bold 25px Verdana";
  //ctx.font = "bold 26px Arial Black";
  
  //shadow
  ctx.shadowOffsetX = 0;
  ctx.shadowOffsetY = 0;
  ctx.shadowBlur = 12;
  ctx.shadowColor = "rgba(0, 0, 0, 0.9)";
  
  /*
  ctx.lineWidth = 2;
  ctx.strokeStyle = "rgba(0, 0, 0, 0.25)"; // stroke color
  ctx.strokeText(text, x+1, y+1);
  */
  // antialiasing
  ctx.lineWidth = 1;
  ctx.strokeStyle = "rgba(255, 255, 255, 0.3)"; // stroke color
  ctx.strokeText(text, x, y);
  // text
  ctx.fillStyle = "rgba(255, 255, 255, 1)";
  ctx.fillText(text,x,y);
}

function flasher_fade_out_done(){
  //console.log("fade out done");
  load_flasher_image(BannerArray[sequence_count][0]);
}

function flasher_fade_in_done(){
  //console.log("fade in done. sleeping for "+BannerArray[sequence_count][5]+" seconds.");
  fade_timer = setTimeout("flasher_next()",BannerArray[sequence_count][5]*1000);
}

function flasher_fade_run(){
    flasher_running = true;
    //console.log("Alpha = " + flasher_current_alpha);
    if(flasher_current_alpha == flasher_target_alpha) {
      flasher_running = false;
      if(flasher_current_alpha == 0) {
        flasher_fade_out_done();
      } else {
        flasher_fade_in_done();
      }
    } else {
      if(flasher_current_alpha < flasher_target_alpha){
        flasher_current_alpha = Math.ceil(flasher_current_alpha + 100/flasher_increments);
      } else {
        flasher_current_alpha = Math.ceil(flasher_current_alpha - 100/(flasher_increments/2));
      }
      canvas.style.opacity = flasher_current_alpha/100;
      //canvas.style.filter = 'alpha(opacity=' + flasher_current_alpha/10 + ')'; only IE
      slide_timer = setTimeout("flasher_fade_run()",flasher_ticks);
    }
}


