// NOTES:
// 1) jQuery-SWFObject 1.1.1 breaks transparency in swfs; returned to 1.0.9
// 2) See note in printB.click function: IE bug with 3D swfs. UPDATE: Only happens when running IE locally on my machine; works ok from server.
// 3) The flashleaf = $.flash.create block was moved to the webb.click function due to inconsistent positioning of the 
//    swf within leafwrap, similar to, but not as constant as, the problem with the printbox swf above.

//console.log("homeanim.js loaded at "); // + postTime());

jQuery(document).ready(function(){
  //console.log("homeanim.js ready");

  //console.log("flash available: " + $.flash.available);
  //console.log("flash version 10: " + $.flash.hasVersion(10));
  
  if($.flash.available && $.flash.hasVersion(10)) {
  
    var flashfish = $.flash.create({
      swf:"afish.swf",
      width:320,
      height:140,
      params:{quality:'medium',play:'true',loop:'false',scale:'noscale',wmode:'transparent'} //,allowScriptAccess:'true'}
    });
    //console.log("flashfish = " + flashfish);
    
    var flashleaf = $.flash.create({
      swf:"aleaf.swf",
      width:180,
      height:120,
      params:{quality:'medium',play:'true',loop:'false',scale:'noscale',wmode:'transparent'} //,allowScriptAccess:'true'}
    });
    
    var flashprintbox = $.flash.create({
      swf:"printbox.swf",
      width:400,
      height:400,
      params:{quality:'medium',play:'true',loop:'false',scale:'noscale'} //,wmode:'transparent',allowScriptAccess:'true'}
    });
    
    var flashbird = $.flash.create({
      swf:"raven.swf",
      width:120,
      height:150,
      params:{quality:'medium',play:'true',loop:'false',scale:'noscale',wmode:'transparent'} //,allowScriptAccess:'true'}
    });
   
    animOn = false; // prevents more than one anim running at a time
    
    // flash leaf animation triggered by #flashb button
    var fishCount = 0;
    
    $("#flashb").click(function(){
      //console.log("flashb clicked at " + postTime());
      if (animOn == true)
      {
        return false;
      }
      else
      {
      
        animOn = true;
        
        fishCount += 1;
        if (fishCount == 5)
        {
        fishCount = 1;
        };
        //fishCount = 3;  // test override
        //console.log("fishCount = " + fishCount);
        
        switch (fishCount)
        {
        case 1:
          $("#fishmsg").html("<p>May the holes in your net<br />be no larger than the fish in it.<br /><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&mdash;Irish Blessing</p>");
          $("#fishmsg p")
              .css("top", "10px")
              .css("font-size", "1.1em");
          msgtime = "500ms";
          break;
            
        case 2:
          $("#fishmsg").html("<p>There's a fine line between fishing<br />and just standing on the shore<br />like an idiot.<br /><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&mdash;Steven Wright</p>");
          $("#fishmsg p")
              .css("top", "2px")
              .css("font-size", "1.1em");
          msgtime = "1000ms";
          break;
          
        case 3:
          $("#fishmsg").html("<p>One fish. Two fish. Red fish. Blue fish.<br />Black fish. Blue fish. Old fish. New fish.<br />This one has a little star.<br />This one has a little car.<br />Say! What a lot of fish there are.<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&mdash;Dr. Seuss</p>");
          $("#fishmsg p")
              .css("top", "2px")
              .css("font-size", ".9em")
              .css("font-weight", "bold");
          msgtime = "3000ms";
          break;
        
        case 4:
          $("#fishmsg").html("<p>Flash fish swim<br />in javascript seas.</p>");
          msgtime = "500ms";
          break;
        }
        
        $("#fishwrap").html(flashfish);
        $("#fishwrap").oneTime("500ms", function(){
          $("#fishcover").fadeOut(1000);
        });
        
        $("#fishwrap").oneTime("4000ms", function(){
          $("#fishwrap").animate( {left: 200}, 1000, "linear", function(){
            $("#fishmsg p").slideDown(600);
            $("#fishwrap").animate( {left: -800}, 4000, "linear", function(){
              $("#fishmsg p").oneTime(msgtime, function(){
                $("#fishmsg p").slideUp(600, function(){
                  $("#fishcover").fadeIn(600, function() {
                    $("#fishwrap")
                        .html("")
                        .css("left", "390px");
                        
                    $("#fishmsg p").css("display", "none");
                    animOn = false;
                  });
                });
              });
            });
          });
        });
        
      };
      return false;
    });
    
    // flash leaf animation triggered by #webb button
    var leafCount = 0;
    
    $("#webb").click(function(){
      //console.log("webb clicked at " + postTime());
      
      if (animOn == true)
      {
        return false;
      }
      else
      {
        animOn = true;
        
        leafCount += 1;
        if(leafCount == 4) {
          leafCount = 1;
        };
        //leafCount = 3;  // test override
        
        switch (leafCount)
        {
        case 1:
          $("#leafmsg")
              .html("<p>falling leaves<br />hide the path<br />so quietly<br /><br /><small>&mdash;John Bailey</small></p>")
              .css("left", "470px");
          dest1L = 440;
          dest1T = 130;
          dest2L = 450;
          dest2T = 180;
          dest3L = 380;
          dest3T = 600;
          msgtime = "3000ms";
          break;
          
        case 2:
          $("#leafmsg")
              .html("<p>Every leaf speaks bliss to me,<br />fluttering from the autumn tree.<br /><br /><small>&mdash;Emily Bronte</small></p>")
              .css("left", "450px");
          dest1L = 430;
          dest1T = 130;
          dest2L = 360;
          dest2T = 200;
          dest3L = 0;
          dest3T = 100;
          $("#leafmsg p")
              .css("width", "200px")
              .css("height", "180px");
          msgtime = "3000ms";
          break;
          
        case 3:
          $("#leafmsg")
              .html("<p>Frog pond &mdash;<br />A leaf falls in<br />Without a sound<br /><br /><small>&mdash;Matsu Basho</small></p>")
              //.html("<p>html, css, php, javascript<br />&mdash; the winds of the web.</p>")
              .css("left", "400px");
          dest1L = 490;
          dest1T = 130;
          dest2L = 520;
          dest2T = 150;
          dest3L = 900;
          dest3T = -100;
          
          $("#leafmsg p")
              .css("width", "280px")
              .css("height", "180px");
          msgtime = "3000ms";
          break;
        }
        
        $("#leafwrap")
            .html(flashleaf)
            .css("left", "430px")
            .css("top", "-29px");
        
        $("#leafwrap").fadeIn(400, function(){
          $("#leafwrap").oneTime("3000ms", function(){
            $("#leafwrap").animate( {left: dest1L, top: dest1T}, 1500, "linear", function(){
              $("#leafwrap").animate( {left: dest2L, top: dest2T}, 400, "linear", function(){
                $("#leafmsg p").slideDown(800);
                $("#leafwrap").animate( {left: dest3L, top: dest3T, opacity: 0}, 1500, "linear", function(){
                  $("#leafwrap")
                      .css("display", "none")
                      .oneTime(msgtime, function(){
                    $("#leafmsg p").fadeOut(1000, function(){
                      $("#leafwrap").css("opacity", "1.0");
                      if(!$.support.opacity) {  // if running on IE; keeps subsequent calls from appearing with fuzzy black outline
                        $("#leafwrap").get(0).style.removeAttribute('filter');
                      };
                      $("#leafwrap").html("");
                      $("#leafmsg p").css("display", "none");
                      //flashleaf = null;
                      animOn = false;
                    });
                  });
                });
              });
            });
          });
        });
      };
      return false;
    });
    
    // flash rotating graphics animation triggered by #printb button
    $("#printb").click(function(e){
      //console.log("printb clicked at " + postTime());
      if (animOn == true)
      {
        //return false;
      }
      else
      {
        animOn = true;
        
        e.preventDefault(); // prevent default behavior
        
        $("#printmsg")
              .html("<p>Revolve.<br/>Solve.<br/>Evolve.</p>")
              //.html("<p>Involve.<br />Evolve.<br />Revolve.<br />Solve.<br />Dissolve.</p>")
              .css("left", "470px");
          $("#printmsg p")
              .css("width", "160px")
              .css("font-size", "1.4em");
          msgtime = "2000ms";
          
        $("#printcover").css("display", "block");
        $("#printwrap")
            .css("display", "block")
            .css("left", "340px")
            .css("top", "0px")
            .html(flashprintbox);
            
        $("#printwrap").oneTime("500ms", function(){
          $("#printcover").fadeOut("1000ms", function(){
            $("#printwrap").oneTime("10000ms", function(){
              $("#printmsg p").slideDown(600, function() {
                $("#printwrap")
                    .html("")
                    .css("display", "none");
                $("#printcover").css("display", "none");
                $("#printwrap").oneTime(msgtime, function(){
                  $("#printmsg p").fadeOut("700ms", function(){
                    $("#printmsg p").css("display", "none");
                    animOn = false;
                  });
                });
              });
            });
          });
        });
      };
      //return false; // prevent default behavior
    });
    
    // flash bird animation triggered by #artb button
    var birdCount = 0;
    
    $("#artb").click(function(){
      //console.log("artb clicked at " + postTime());
      
      if (animOn == true)
      {
        return false;
      }
      else
      {
        animOn = true;
        
        birdCount += 1;
        if(birdCount == 4) {
          birdCount = 1;
        };
        //birdCount = 3;  // test override
        
        switch (birdCount)
        {
        case 1:
          $("#birdmsg")
              .html("<p>The aim of art is to represent not<br />the outward appearance of<br />things, but their inward<br />significance.<br /><br /><small>&mdash;Aristotle</small></p>")
              .css("left", "380px")
              .css("top", "50px");
          $("#birdmsg p")
              .css("width", "330px")
              .css("font-size", "1.4em");
          msgtime = "2500ms";
          break;
          
        case 2:
          $("#birdmsg")
              .html("<p>The desire to reach for the sky<br />runs deep in our human psyche.<br /><br /><small>&mdash;Cesar Pelli</small></p>")
              .css("left", "370px");
          $("#birdmsg p")
              .css("width", "320px")
              .css("font-size", "1.4em");
          msgtime = "2000ms";
          break;
          
        case 3:
          $("#birdmsg")
              .html("<p>The bluebird carries<br />the sky on his back.<br /><br /><small>&mdash;Henry David Thoreau</small></p>")
              .css("left", "370px");$
          ("#birdmsg p")
              .css("width", "300px")
              .css("font-size", "1.4em");
          msgtime = "2000ms";
          break;
        }
        
        // init position
        $("#birdwrap")  // left to right
            .css("display", "block")
            .html(flashbird)
            .css("left", "-800px")
            .css("top", "300px");
        
        /*$("#birdwrap")  // right to left
            .css("display", "block")
            .html(flashbird)
            .css("left", "1400px")
            .css("top", "300px");*/
        
        $("#birdwrap").animate( {left: 400, top:-50}, 3000, "linear", function(){ // left to right
        //$("#birdwrap").animate( {left: 300, top:50}, 2800, "linear", function(){ // right to left
          $("#birdmsg p").fadeIn(800);
          $("#birdwrap").animate( {left: 700, top:-170, opacity: 0}, 1000, "linear", function(){ // left to right
          //$("#birdwrap").animate( {left: -800, top:-200}, 2800, "linear", function(){ // right to left
            $("#birdwrap")
                .css("display", "none")
                .css("opacity", "0.9")
                .html(flashbird)
                .css("left", "-800px")  // reset to starting point
                .css("top", "300px");
            $("#birdwrap").oneTime(msgtime, function(){
              $("#birdmsg p").fadeOut(800, function (){
                $("#birdwrap").html("");
                $("#birdmsg p").css("display", "none");
                animOn = false;
              });
            });
          });
        });
        
      };
      return false;
    });
  }
  // Flash not available or less than version 10
  else
  {
    $("#birdmsg")
        .html("<p>Your browser must have<br />the Flash version 10<br />plugin to display this<br />page properly.</p>")
        .css("left", "380px");
    $("#birdmsg p")
        .css("width", "300px")
        .css("height", "100px")
        .css("border", "1px")
        .css("background-color", "#a4c085");
    $("#birdmsg p").fadeIn(800);
    
  };
  
});

/*function postTime() {
  var currentTime = new Date();
  var hours = currentTime.getHours();
  var minutes = currentTime.getMinutes();
  if (minutes < 10){
  minutes = "0" + minutes
  }
  var time = hours + ":" + minutes;
  currentTime = null;
  return time;
}*/

