// Variable initialization
var maxSplash = 0;
var currentSplash = 0; // Indicates what Splash image is currently shown
var splashTimer;
var splashDelayTime = 6000; // 6000 ms = 6 seconds
var splashTransitionTime = 500; // 500 ms = 0.5 second
var canClickSplashNav = false;

// Execute all of this when the DOM is fully loaded
$(document).ready(function() {
  if ((maxSplash = $('#splash li').length) > 1) //$('#splash li").length contains the number of items on the list id="splash"
  {
  	$('#splash li').css({top:0,left:0}).not(':eq('+currentSplash+')').fadeTo(0,0);
    splashTimer = setTimeout('nextSplash()',splashDelayTime); // Show the next splash image in "splashDelayTime" milliseconds 
  }
  
  var list = $('<ol/>').attr('id','splash-nav').appendTo('#splash-container .wrapper');
  for (i = 0; i < $('#splash li').length; i++)
  	$('<li/>').attr('eq',i).appendTo(list);
  
  
  $('#splash-nav').show().stop().fadeTo(0,0);
  $('#splash-nav li').not(':first').stop().fadeTo(0,0.4);
  $('#splash-nav li:first').stop().fadeTo(0,0.8);
  $('#splash-container .wrapper').hover(
    function() {$('#splash-nav').stop().fadeTo(500,1,'easeOutQuart');},
    function() {$('#splash-nav').stop().fadeTo(500,0,'easeOutQuart');}
  );
  $('#splash-nav li').click(function() {
    gotoSplash($(this).attr('eq'));
  });
  canClickSplashNav = true;
  
//  mapRollover();
});

function nextSplash() {
  var n = currentSplash;
  if(++n >= maxSplash)
    n = 0;
  var target = $('#splash li').eq(currentSplash); // Sets to the current splash image
  var nTarget = $('#splash li').eq(n); // Set to the next splash image
  
  $('#splash li').not(target).not(nTarget).stop().fadeTo(0,0).css('z-index','88');
  target.css('z-index','89');
  nTarget.css('z-index','90');
  
  $('.tag',nTarget).css('bottom','-20px').animate({bottom:0},{duration:splashTransitionTime*2,easing:'easeOutQuart'});
  nTarget.stop().fadeTo(0,0).fadeTo(splashTransitionTime,1,
                         function() {
                           target.stop().fadeTo(0,0);
                           currentSplash = n;
                         }
                        );
  
  // Change the opacity of the little navigational buttons (bottom left corner)
  
  $('#splash-nav li').stop().fadeTo(100,0.4);
  $('#splash-nav li').eq(n).stop().fadeTo(100,0.8);
  
  splashTimer = setTimeout('nextSplash()',splashDelayTime);
}

function gotoSplash(i) {
  if((currentSplash != i) && (canClickSplashNav)) {
    clearTimeout(splashTimer);
    canClickSplashNav=false;
  
    var n = i;
    if(n >= maxSplash)
      n = 0;
    var target = $('#splash li').eq(currentSplash);
    var nTarget = $('#splash li').eq(n);
  
    $('#splash li').not(target).not(nTarget).stop().fadeTo(0,0).css('z-index','88');
    target.css('z-index','89');
    nTarget.css('z-index','90');
  
    $('.tag',nTarget).css('bottom','-20px').animate({bottom:0},{duration:splashTransitionTime*2,easing:'easeOutQuart'});
    nTarget.stop().fadeTo(0,0).fadeTo(splashTransitionTime,1,
                           function() {
                             target.stop().fadeTo(0,0);
                             currentSplash=n;
                             canClickSplashNav=true;
                           }
                          );
  
    $('#splash-nav li').stop().fadeTo(100,0.4);
    $('#splash-nav li').eq(n).stop().fadeTo(100,0.8);
  
    splashTimer = setTimeout('nextSplash()',splashDelayTime);
  }
}

/*
  var month=new Array();
  month[0]="January";
  month[1]="February";
  month[2]="March";
  month[3]="April";
  month[4]="May";
  month[5]="June";
  month[6]="July";
  month[7]="August";
  month[8]="September";
  month[9]="October";
  month[10]="November";
  month[11]="December";
  
  var weekday=new Array();
  weekday[0]="Sunday";
  weekday[1]="Monday";
  weekday[2]="Tuesday";
  weekday[3]="Wednesday";
  weekday[4]="Thursday";
  weekday[5]="Friday";
  weekday[6]="Saturday";
  
  //Grab the latest blog entry from StevenFurtick.com
  $.ajax({
    url:'feed/stevenfurtick',
    timeout:7000,
    type:"POST",
    success:function(feed) {
      if($('item:first pubDate',feed).length>0 && $('item:first title',feed).length>0) {
        var date=new Date($("item:first pubDate",feed).text());
        var dateStr=weekday[date.getDay()]+', '+month[date.getMonth()]+' '+date.getDate();
        var title=$("item:first title",feed).text();
      
        $('#quick-links .blog span').fadeOut(350,function() {
          $('#quick-links .blog span').html('<em>'+dateStr+':</em> '+title).fadeIn(350,'easeOutCirc');
        });
      }
    }
  });
*/


/*
function mapRollover() {
  var target=$('<span/>').text('Click to enlarge map').fadeTo(0,0).appendTo('.map-rollover');
  $('.map').hover(
    function() {$('.map-rollover span',this).stop().fadeTo(350,0.8);},
    function() {$('.map-rollover span',this).stop().fadeTo(350,0);}
  );
}

function campus(target) {
  var slug=$(target).attr('rel');
  var title=$(target).text();
  var link=$(target).attr('href');
  
  var previous=$('#campus-select .selected');
  var previousLink=$(previous).attr('href');
  var previousSlug=$(previous).attr('rel');
  var previousTitle=$(previous).text();
  
  $('#information-dynamic').stop().fadeTo(300,0);
  $.ajax({
    url:'home/campus/'+slug,
    success:function(data) {
      $('#information-dynamic').html(data).stop().fadeTo(200,1);
      $('span',target).text(previousTitle);
      $(target).attr({rel:previousSlug,href:previousSlug});
      $('span',previous).text(title);
      $(previous).attr({rel:slug,href:slug});
      
      mapRollover();
    }
  });
  
  return false;
}
*/

/*
function playSermon() {
  if($('body,html').scrollTop()>90)
    $('body,html').animate({scrollTop:90},500,'easeOutCirc');
    
  var video=$('#latest-sermon').val();
  
  $('<div/>').attr('id','splash-video-player').appendTo($('#splash-container .wrapper')).html('\
      <object type="application/x-shockwave-flash" width="960" height="561" data="http://elevationchurch.lightcastmedia.com/embed/'+video+'">\
        <param name="wmode" value="transparent">\
        <param name="FlashVars" value="autoStart=1"></param>\
        <param name="movie" value="http://elevationchurch.lightcastmedia.com/embed/'+video+'"></param>\
        <param name="allowFullScreen" value="true"></param>\
      </object>\
    ');
  
  $('#splash-container').animate({height:'600px'},750,'easeOutCirc',function() {
    $('<a/>').attr('id','splash-video-close').attr('href','').click(function() {
      return closeSermon();
    }).fadeTo(0,0).fadeTo(500,1).appendTo($('#splash-container .wrapper'));
  });
  
  var seriesBG=$('#series-background').val();
  var splashBG=$('#splash-bg').css('background-image');
  $('#splash-bg').css({backgroundImage:seriesBG,height:'600px'});
  $('#series-background').val(splashBG);
  
  var title=$('#quick-links .sermon span').html();
  var link=$('#quick-links .sermon').attr('href');
  $('<div/>').attr('id','video-nav').html('\
    <p>Now watching '+title+'</p>\
    <a class="plusButton" href="'+link+'"><span>Launch Media Player</span></a>\
  ').appendTo($('#splash-container .wrapper'));
  
  $('#splash,#splash-nav').hide();
  
  clearTimeout(splashTimer);
  
  return false;
}

function playVideo(type,title,link,color,video) {
  if($('body,html').scrollTop()>90)
    $('body,html').animate({scrollTop:90},500,'easeOutCirc');
  
  $('<div/>').attr('id','splash-video-player').appendTo($('#splash-container .wrapper')).html('\
      <object type="application/x-shockwave-flash" width="960" height="561" data="http://elevationchurch.lightcastmedia.com/embed/'+video+'">\
        <param name="wmode" value="transparent">\
        <param name="FlashVars" value="autoStart=1"></param>\
        <param name="movie" value="http://elevationchurch.lightcastmedia.com/embed/'+video+'"></param>\
        <param name="allowFullScreen" value="true"></param>\
      </object>\
    ');
  
  $('#splash-container').animate({height:'600px'},750,'easeOutCirc',function() {
    $('<a/>').attr('id','splash-video-close').attr('href','').click(function() {
      return closeVideo();
    }).fadeTo(0,0).fadeTo(500,1).appendTo($('#splash-container .wrapper'));
  });
  
  $('<div/>').attr('id','video-nav').html('\
    <p>Now watching '+type+' <span style="color:'+color+'">'+title+'</span>\
    <a class="plusButton float-right" href="invite"><span>Invite Your Friends</span></a></p>\
  ').appendTo($('#splash-container .wrapper'));
  
  $('#splash,#splash-nav').hide();
  
  clearTimeout(splashTimer);
  
  return false;
}

function closeSermon() {
  var splashBG=$('#series-background').val();
  var seriesBG=$('#splash-bg').css('background-image');
  $('#splash-bg').css({backgroundImage:splashBG,height:'353px'});
  $('#series-background').val(seriesBG);
  
  $('#splash-video-player,#splash-video-close,#video-nav').remove();
  $('#splash-container').animate({height:'353px'},750,'easeOutCirc');
  $('#splash,#splash-nav').fadeIn(500,'easeOutCirc');
  
  if(maxSplash>1)
    splashTimer=setTimeout('nextSplash()',splashDelayTime);
  
  return false;
}

function closeVideo() {
  $('#splash-video-player,#splash-video-close,#video-nav').remove();
  $('#splash-container').animate({height:'353px'},750,'easeOutCirc');
  $('#splash,#splash-nav').fadeIn(500,'easeOutCirc');
  
  if(maxSplash>1)
    splashTimer=setTimeout('nextSplash()',splashDelayTime);
  
  return false;
}
*/