//globals ----------------------------------------------------------------------
var playID = 0;

//loadStreamDesc----------------------------------------------------------------
function pdLink(feed){
  if ( $('#'+feed).is(':visible') ){
    $('#'+feed).slideUp('normal');  
  }else{
    $('.pdcUrl:visible').slideUp('normal');  
    $('#'+feed).slideDown('normal');
   }    
}

function loadStreamDesc(id, or){
  $('#dynamic_box').removeClass('backgroundLive');
  $('#dynamic_box').removeClass('podCastBg');              
  $('#dynamic_box').css('display','none').html('');
  $.get('index.php?action=streamdescription&id='+id+'&or='+or,
  	  function(res){
  	  	  $('#dynamic_box').html(res);
  	  	  $('.onDemandAudio').click(
  	  	  	  function(evt){
  	  	  	  	  evt.preventDefault();
  	  	  	  	  playStream($(this).attr('href'));
  	  	  	  }
  	  	  );
  	  	  $('.onDemandVideo').click(
  	  	  	  function(evt){
  	  	  	  	  evt.preventDefault();
  	  	  	  	  playStream($(this).attr('href'));  	  	  	  	  
  	  	  	  }
  	  	  );
  	  	  $("#dynamic_box").fadeIn(200);
  	  }
  );
}

function showPodcastPanel(id){
  if ( id == 'itunesFeed'){
    if ( $('#itunesFeed').is(':visible') ){
      $('#itunesFeed').slideUp('normal');
    }else{
      $('#alternativeFeed:visible').slideUp('normal');
      $('#itunesFeed').slideDown('normal');
    }      
  }else{
    if ( $('#alternativeFeed').is(':visible') ){
      $('#alternativeFeed').slideUp('normal');    
    }else{
      $('#itunesFeed:visible').slideUp('normal');
      $('#alternativeFeed').slideDown('normal');
    }      
  }
}

function playStream(_id){
	sname = _id.toLowerCase();
	if ( sname.indexOf('.flv')>0 ){
		_id = _id.substring(0, _id.lastIndexOf("."));
		playID = {
					provider: 'rtmp',
					autoplay: true,
					autoBuffering: true,
					url: _id 
				};
	}else if ( sname.indexOf('.f4v')>0 ){
		playID = {
					provider: 'rtmp',
					autoplay: true,
					autoBuffering: true,
					url: _id 
				};
	}else{
		playID = {
					autoplay: true,
					autoBuffering: true,
					url: mediaRoot+_id 
				};
	}
	$("#dynamic_box").html('<div id="playerInner"></div>');
	$("#dynamic_box").fadeOut(400, 
		function(){
			flowplayer("playerInner", 
				{
					src:"fp_3_2_7/flowplayer.commercial-3.2.7.swf",
					cachebusting: true,
					width: '484', 
					height: '370'
				},
				{					
					key: '#@86a4bd2a4cc5d33ccf2',
					plugins:{
						audio:{ 
							url: 'fp_3_2_7/flowplayer.audio-3.2.2.swf' 
						}, 
						controls: {
							autoHide: 'always'
						},
						rtmp: {
							url: 'fp_3_2_7/flowplayer.rtmp-3.2.3.swf',
							netConnectionUrl: 'rtmp://cp132511.edgefcs.net/ondemand'
						}
					},
					clip: playID 
				}
			);			
			$("#dynamic_box").fadeIn(200);
		}
	);
}

function playLiveStream(){
	$('#dynamic_box').addClass('backgroundLive');
	$("#dynamic_box").html("<div id=\"mediaSelect\"><a href=\"#\" class=\"selectAction audio\" title=\"Audio Only\"></a><a href=\"#\" class=\"selectAction video\" title=\"Video\"></a><br /><a href=\"#\" class=\"selectAction iphone\" title=\"IPhone\"></a></div><div class=\"liveText\"><b>Live Service: Sundays at 9:00 am &amp; 11:15 am</b><br /><span>if you are experiencing problems with video stream playing smoothly,<br />please try the &quot;audio only&quot; stream.</span></div>");
	$(".selectAction").click(
		function(evt){
			evt.preventDefault();
			$("#dynamic_box").html('<div id="playerInner"></div>');
			if ( $(this).hasClass('audio') ){
				$("#dynamic_box").fadeOut(200,
					function(){
						playLiveSelection('audio');
					}
				);
			}else if ($(this).hasClass('video')){
				$("#dynamic_box").fadeOut(200,
					function(){
						playLiveSelection('video');
					}
				);
			}else{
				window.location.href = "http://sf-crossroads.ios.gdlcdn.com/stream/playlist.m3u8";
			}
		}
	)
}

function playLiveSelection(mType){
	if ( mType == 'audio' ){
		flowplayer("playerInner", 
			{
				src:"fp_3_2_7/flowplayer.commercial-3.2.7.swf",
				cachebusting: true,
				width: '484', 
				height: '370'
			},
			{
				key: '#@86a4bd2a4cc5d33ccf2',
				plugins:{
					audio:{ 
						url: 'fp_3_2_7/flowplayer.audio-3.2.2.swf' 
					}, 
					controls: {
						autoHide: 'always'
					},
					rtmp: {
						url: 'fp_3_2_7/flowplayer.rtmp-3.2.3.swf',
						netConnectionUrl: 'rtmp://cp132511.live.edgefcs.net/live'													
					}
				},
				clip: {
					provider: 'rtmp',
					autoplay: true,
					live: true,
					url: 'crcc-audio@62188'
				}
			}
		);			
	}else{
		flowplayer("playerInner", 
			{
				src:"fp_3_2_7/flowplayer.commercial-3.2.7.swf",
				cachebusting: true,
				width: '484', 
				height: '370'
			},
			{
				key: '#@86a4bd2a4cc5d33ccf2',
				plugins:{
					controls: {
						autoHide: 'always'
					},
					rtmp: {
						url: 'fp_3_2_7/flowplayer.rtmp-3.2.3.swf',
						netConnectionUrl: 'rtmp://cp132511.live.edgefcs.net/live'													
					}
				},
				clip: {
					provider: 'rtmp',
					autoplay: true,
					live: true,
					url: 'crcc_high@62187'
				}
			}
		);			
	}
	$("#dynamic_box").fadeIn(200);
}
//accordion menu----------------------------------------------------------------
      function initAccordion() {
        $('.secList').hide();
        $('.terList').hide();
                
        $('.topHeader').click(
          function() {
            $('.isActive').removeClass('isActive');
            $(this).addClass('isActive');            
            
            if ( $(this).html() == 'Live Service' ){
              $("#dynamic_box").removeClass('podCastBg');          
              $('.secList:visible').slideUp('fast');
              if ( liveStream == 'on' ){
                window.setTimeout("playLiveStream()", 400);
              }else{            
                $('#dynamic_box').load('index.php?action=livesettings');
              }                
            }else if( $(this).html() == 'Podcast' ){          
              $("#dynamic_box").addClass('podCastBg');              
              $('.secList:visible').slideUp('fast');  
              $('#dynamic_box').load('index.php?action=podcastsettings');          
            }else{
              var checkElement = $(this).next();
              if((checkElement.is('.secList')) && (checkElement.is(':visible'))) {
                return false;
              }
              
              if((checkElement.is('.secList')) && (!checkElement.is(':visible'))) {
                $('.secList:visible').slideUp('fast');
                checkElement.slideDown('fast');
                return false;
              }
            }              
          });
          
          $('.secHeader').click(
            function() { 
              var checkElement = $(this).next();
              if( checkElement.is('.terList') ){
                if ( !checkElement.is(':visible') ){
                  checkElement.slideDown('normal');            
                }else{
                  checkElement.slideUp('normal');              
                }            
              }
              return false;
            }
          );
      }
//run on load-------------------------------------------------------------------
$(document).ready(function (){
  initAccordion();
  
  $('#giveButton').hover(
  	  function(){
  	  	  $(this).fadeOut(250, 
  	  	  	  function(){
  	  	  	  	  $('#giveButton').css('float', 'right');
  	  	  		  $('#giveButton').fadeIn(250);
  	  	  	  }
  	  	  );
  	  },
  	  
  	  function(){
  	  	  $(this).fadeOut(250, 
  	  	  	  function(){
  	  	  	  	  $('#giveButton').css('float', 'left');
  	  	  		  $('#giveButton').fadeIn(250);
  	  	  	  }
  	  	  );
  	  }  	  
  );
  
  if ( streamID ){
  	 loadStreamDesc(streamID, '');
  }
});

