var maxSplash=0;
var currentSplash=0;
var splashTimer;
var splashDelayTime=6000;
var splashTransitionTime=500;

$(document).ready(function() {
  if((maxSplash=$('#splash li').length)>1) {
    $('#splash li').css({top:0,left:0}).not(':eq('+currentSplash+')').hide();
    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 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});
    }
  });
  
  return false;
}

function nextSplash() {
  var n=currentSplash+1;
  if(n>=maxSplash)
    n=0;
  var target=$('#splash li').eq(currentSplash);
  var nTarget=$('#splash li').eq(n);
  
  target.css('z-index','98');
  nTarget.css('z-index','99');
  
  $('.tag',nTarget).css('bottom','-20px').animate({bottom:0},{duration:splashTransitionTime*2,easing:'easeOutQuart'});
  nTarget.stop().fadeIn({duration:splashTransitionTime,
                         easing:'easeOutQuart',
                         complete:function() {
                           target.stop().hide();
                           currentSplash=n;
                         }
                        });
  
  splashTimer=setTimeout('nextSplash()',splashDelayTime);
}

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').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').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').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').fadeIn(500,'easeOutCirc');
  
  if(maxSplash>1)
    splashTimer=setTimeout('nextSplash()',splashDelayTime);
  
  return false;
}