$(document).ready(function(){

	$('#featured-controls li:first-child').addClass('current');	
	
	
	$('.sl_video a, #mb_video a, a.video').live('click', playVideo);	
	$('.sl_listen a, #mb_jukebox a, a.play').live('click', playAudio);
	
	$('.mc-block-head p:first-child').css('margin-bottom','0px');
	$('.mc-block-head').each( function() {
		$(this).find('p:eq(1)').css('width','760px');
	})
	
	$('.showing').live( 'click', function(e) {
	
		$('#main-content .hidden').each( function() {
			$(this).slideDown();
			$('#main-content .show').each( function() {
				$(this).removeClass('show');
				$(this).addClass('hide');
				
				var a = $('.showing');
				a.removeClass('showing');
				a.addClass('hiding');
			})
		})
	
		e.preventDefault();
	
	})
	
	$('.hiding').live( 'click', function(e) {
	
		$('#main-content .hidden').each( function() {
			$('.mc-block-main').each( function() {
				$(this).slideUp();
			})
			$('.showhide').each( function() {
				$(this).removeClass('hide');
				$(this).addClass('show');
			})
		})
		
		var a = $('.hiding');
		a.removeClass('hiding');
		a.addClass('showing');
	
		e.preventDefault();
	
	})
	
	$('.showhide').click( function(e) {
	
//		if ( $(this).hasClass('show') ) {
//		
//			var id = $(this).attr('id');
//			$('.'+id).slideDown();
//			$(this).removeClass('show');
//			$(this).addClass('hide');
			
//			var a = $('.showing');
//			a.removeClass('showing');
//			a.addClass('hiding');
//		
//		} else {
//		
//			var id = $(this).attr('id');
//			$('.'+id).slideUp();
//			$(this).removeClass('hide');
//			$(this).addClass('show');
					
//		}
		
		e.preventDefault();
		
	})
	   	
   	$('a.external').each(function(){
   		if(this.href.match(/cpmassets/)){
   			this.onclick = function(){
	   			window.open(this.href, 'videoPlayer', 'width=640,height=320,scrollbars=0,statusbar=0,address=0');
	   			return false;
	   		}
	   	} else if (this.href.match(/m4v$/) || this.href.match(/mp4$/)){
	   		this.onclick = function(){
		   		window.open('http://cpmassets.com/video.php?video='+this.href, 'videoPlayer', 'width=640,height=320,scrollbars=0,statusbar=0,address=0');
		   		return false;
		   	}
   		} else {
   			this.target = '_blank';
   		}
   	});
   	
   	$('#top-links li:first a').addClass('first');
   	$('#guide').click( function(e) {
   		if ( $('#ministry').css('display') == 'none' ) {
	   		$('#ministry').slideDown();
	   		$('#top-bar').addClass('bg');
	   	} else {
	   		$('#ministry').slideUp( 400, function() {
	   			$('#top-bar').removeClass('bg');
	   		});
	   	}
   		e.preventDefault();
   	})
   	   	
   	$('#views li a').bind('click', switchCalTabs);
	$('#months').bind('change', switchCalMonth);
	$('#events-calendar caption a').live('click', switchCalendar);
	$('#list-view li a').live('click', switchEventList);
	
	// clear and restore search input
   $('input.clickClear').focus(function() {
      startText = $(this).val();
      $(this).val('');
   });
   $('input.clickClear').blur(function() {
      blurText = $(this).val();
      if (blurText == '') {
         $(this).val(startText);
      };
   });
});

// launch audio player
function playAudio(){
	window.open('http://cpmassets.com/audio-player.php?audio='+$(this).attr('href'), 'audioPlayer', 'width=210,height=30,status=0,toolbar=0'); 
	return false;
}

// launch video player
function playVideo(){
	var $self = $(this);

	if($self.attr('nodeName') !== 'A'){
		if($self.parent().attr('nodeName') == 'A'){
			var $self = $self.parent();
		} else {
			return;
		}
	}
	
	if($self.attr('rel').length > 1){
		var place = $self.attr('href');
	} else {
		var place = 'http://cpmassets.com/video.php?video='+$self.attr('href');
	}
	window.open(place, 'videoPlayer', 'width=640,height=320,scrollbars=0,statusbar=0,address=0');
	return false;
}


function switchCalTabs(){
	var div = '#'+$(this).attr('rel');
	$('#events-container div').not('.event_details').hide();
	//$('.event_details').removeAttr('style');
	$(div).show();
	$('#views li').removeClass('current');
	$(this).parent().addClass('current');
	return false;
}

function switchCalMonth(){
	var month = $(this).val(); 
	$.get('/ajax/events/', { month:month }, function(data){ 
		$('#events-calendar').html(data);
	});
}

function switchCalendar(){
	var parts = $(this).attr('href').split('/');
	var month = parts.pop();
	var year  = parts.pop();
	$.get('/ajax/events/', { month:month, year:year }, function(data){
		$('#events-calendar').html(data);
		doCalendarFilter();
	});
	return false;
}

function switchEventList(){
	var parts = $(this).attr('href').split('/');
	var month = parts.pop();
	var year  = parts.pop();
	$.get('/ajax/events/', { month:month, year:year, view:'list'}, function(data){
		$('#list-view').html(data);
	});
	return false;
}
