$(function() {
  
  // use css3 background gradient for all apple mobile devices (animation performance problems)
  //if (isAppleMobileDevice()) {
  //  $("#background").css("background", "-webkit-gradient(linear, left top, right bottom, from(#b6c2c5), to(#48656d))");
  //}
  
  /* AUDIO */
  // play audio files
  if (window.location.search.length==0) {
    setTimeout(function() {
      jwplayer('mediaplayer').setup({
        'id': 'heartbeatplayer',
        'width': '200',
        'height': '24',
        //'flashplayer': 'flashs/player.swf',
        'modes': [
          {type: 'flash', src: 'flashs/player.swf'},
          {type: 'html5'}
        ],
        //'file': '/audios/heartbeat.mp3',
        'levels': [ 
          { file: "audios/heartbeat.mp3" },
          { file: "audios/heartbeat.ogg" },
          { file: "audios/heartbeat.wav" }
        ],
        'controlbar': 'bottom',
        'autostart': 'true',
        'volume': 100,
        'repeat': 'none'
      }); 
    }, 0);
  }
  /* END AUDIO */
 
});

// adjust the footer to the window bottom

window.onresize = function(){
	$("#right_line").css("width", $("#innerframe").offset().left+2)
}
window.onload = function () {
  
  var skyline_time = 12000;
  var char_time = 100;
  var logo_time = 8000;
  
  // set the width of the left line
  setLeftLine();
  
  // animate heartbeat line (with css3 or jquery) but not if any parameter given
  if (window.location.search.length>0) {
    $("#background_line").css({display: 'none', '-webkit-transition': 'none', '-moz-transition': 'none', '-o-transition': 'none', '-ms-transition': 'none', '-transition': 'none', width: 715});
    $("#right_line").css("width", $("#innerframe").offset().left+2)
    $("#aim, #logo, #background_line, .chars, #footer, #right_line").fadeIn(0);
    $('#background_line_bold').hide();
    $('#aim_overlay, #logo_overlay, #footer_overlay').hide();
  } else if (hasCSS3Compatibility()) {

    $('#background_line').css({width: 713});
    $('#background_line').css({height: 600});
    $('#background_line_bold').css({left: 713});
    $('#background_line_bold img').css({left: -713});
    
    setTimeout(function() { showChars(char_time); }, skyline_time);
    showLogos(logo_time);
  
  } else {
    
    $('#background_line_bold').animate({
      'left': '+=715'
    }, skyline_time, "linear", function() {
      // Animation complete.
    });
    
    $('#background_line_bold img').show(0).animate({
      'left': '-=715'
    }, skyline_time, "linear", function() {
      // Animation complete.
    });

    $('#background_line').animate({
      'width': '+=715',
      'height': '600'
    }, skyline_time, "linear", function() {
      showChars(char_time);
    });
    showLogos(logo_time);
    
  }
  
}

// set the width of the left line
$(window).resize(function() {
  setLeftLine();
});

function showLogos(logo_time) {
  // show aim layer
  setTimeout(function() {
    $("#aim_overlay").fadeOut(logo_time-7000, function() { 
      //$(this).addClass("pulsate");
    });
  }, 4500);
  
  // show logo layer
  setTimeout(function() {
    $("#logo_overlay").fadeOut(logo_time, function() { 
      // $(this).addClass("pulsate");
    });
  }, 10000);
  
}

function setLeftLine() {
  $("#left_line").css("width", $("#innerframe").offset().left);
}
function setRightLine() {
  $("#right_line").css("width", $("#innerframe").offset().left+2).show("slide", { direction: "left" }, 1000);
}

function showChars(char_time) {
  $('#o').fadeIn(char_time, function() { 
    $(this).addClass("pulsate");
    $('#m').fadeIn(char_time, function() { 
      $(this).addClass("pulsate");
      $('#i').fadeIn(char_time, function() { 
        $(this).addClass("pulsate");
        $('#n').fadeIn(char_time, function() { 
          $(this).addClass("pulsate");
          $('#g').fadeIn(char_time, function() { 
            $(this).addClass("pulsate");
            $('#s').fadeIn(char_time, function() { 
              $(this).addClass("pulsate");
              $('#o2').fadeIn(char_time, function() { 
                $(this).addClass("pulsate");
                $('#o3').fadeIn(char_time, function() { 
                  $(this).addClass("pulsate");
                  $('#n2').fadeIn(char_time, function() { 
                    //$(this).addClass("pulsate");
                    setRightLine();
                    $('#footer_overlay').fadeOut('slow');
                  });
                });
              });
            });
          });
        });
      });
    });
  });
}


function hasCSS3Compatibility() {
  return isAppleMobileDevice() || ($.browser.webkit && parseInt($.browser.version.substr(0, 1))>=4 ) || ($.browser.mozilla && parseInt($.browser.version.substr(0, 1))>=2);
}
function isMobileDevice() {
  return navigator.userAgent.toLowerCase().match(/(iphone|ipod|ipad|android)/);
}
function isAppleMobileDevice() {
  return /Apple.*Mobile/.test(navigator.userAgent);
}
function isIE6() {
  return $.browser.msie && $.browser.version=="6.0";
}
function isIE7() {  
  return $.browser.msie && $.browser.version=="7.0";
}
function isIE8() {  
  return $.browser.msie && $.browser.version=="8.0";
}
