var maxSplash=0;
var currentSplash=0;
var splashTimer;
var splashDelayTime=9000;
var splashTransitionTime=500;
var canClickSplashNav=false;

$(document).ready(function() {
  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');
        });
      }
    }
  });
  
  if((maxSplash=$('#splash li').length)>1) {
    $('#splash li').css({top:0,left:0}).not(':eq('+currentSplash+')').fadeTo(0,0);
    splashTimer=setTimeout('nextSplash()',splashDelayTime);
  }
  
  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() {
      if(canClickSplashNav)
        $('#splash-nav').stop().fadeTo(500,1,'easeOutQuart');
    },
    function() {
      if(canClickSplashNav)
        $('#splash-nav').stop().fadeTo(500,0,'easeOutQuart');
    }
  );
  $('#splash-nav li').click(function() {
    gotoSplash($(this).attr('eq'));
  });
  canClickSplashNav=true;
  
  mapRollover();
});

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();
  
  //Redirect for Global
  if (slug == 'global') {location.href='http://www.elevationchurchglobal.org'; return false;}
  
  $('#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();
    }
  });
  
  pageTracker._trackEvent('Home Page Campus Select','Select',title);
  
  return false;
}

function nextSplash() {
  var n=currentSplash;
  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;
                         }
                        );
  
  $('#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);
  }
}

function embedVideo(video, lowresvideo) {
	if (video.indexOf('multicastmedia.com') > -1) {
		var html = '<video id="videoembed" class="jwplayer" controls autoplay autobuffer width="960" height="540">'
		if (/iphone/gi.test(navigator.userAgent) && $('<div id="iphone4bug"/>').appendTo('body').css('display') !== 'inline') {
			html += '<source src="http://' + lowresvideo + '" type="video/mp4" />';
		} else {
			html += '<source src="http://' + video + '" type="video/mp4" />';
		}
		html += '</video>';
		$('<div/>').attr('id','splash-video-player').appendTo($('#splash-container .wrapper')).html(html);
		elevationVideoEmbed.init('videoembed', '960', '540', 'http://' + video, '', true);
	} else {
		$('<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>\
		');
	}
}

function playSermon(video, lowresvideo) {
  canClickSplashNav = false;
  
  if($('body,html').scrollTop()>90)
    $('body,html').animate({scrollTop:90},500,'easeOutCirc');
    
  //var video=$('#latest-sermon').val();
  
  embedVideo(video, lowresvideo);
    
  $('#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);
  
  pageTracker._trackEvent('Home Page Video Player','Play','Sermon');
  
  return false;
}

function playVideo(type,title,link,color,video,lowresvideo) {
  canClickSplashNav=false;
  
  if($('body,html').scrollTop()>90)
    $('body,html').animate({scrollTop:90},500,'easeOutCirc');
  
  embedVideo(video, lowresvideo);
    
  $('#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);
  
  pageTracker._trackEvent('Home Page Video Player','Play','Invite');
  
  return false;
}

function closeSermon() {
  canClickSplashNav=true;
  
  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);
  
  pageTracker._trackEvent('Home Page Video Player','Stop','Sermon');
  
  return false;
}

function closeVideo() {
  canClickSplashNav=true;
  
  $('#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);
    
  pageTracker._trackEvent('Home Page Video Player','Stop','Invite');
  
  return false;
}
