$(document).ready(function(){
	
	$("#search_go").click(
		function() {
			$("#searchForm").submit();
			return false;
		}
	)		
	
	sermonSort();
	
	$('.sl_video a, #mb_video a, a.video').live('click', playVideo);	
	$('.sl_listen a, #mb_jukebox a, a.play').live('click', playAudio);		
   
   	$('.external').attr('target', '_blank');
   	$('#caltabs li a').bind('click', switchCalTabs);
	$('#months').bind('change', switchCalMonth);
	$('#events-calendar caption a').live('click', switchCalendar);
	$('#mini-cal caption a').live('click', switchMiniCal);
	$('#list-view li a').live('click', switchEventList);



	// Luke added: give clickClear class to search input:
   	$('input#search_term').removeClass('clearClick').addClass('clickClear');

	// Luke added: for search and e-newsletter fields, dynamic clearing/remembering:
var month_year = getCookie('calendar');
	var body		= $('body').attr('id');
	
	if( (body == 'home' || body == 'events') && month_year){
		var parts = month_year.split('-');
		var month = parts[0];
		var year = parts[1];		
		
		if(body == 'home'){
			$.get('/ajax/events.php', { month:month, year:year, mini:true}, function(data){
				$('#calendar').html(data);
			});
		} else {
		
			$.get('/ajax/events.php', { month:month, year:year }, function(data){
				$('#events-calendar').html(data);
			});
		}
	}
	$('input.clickClear').each(function(){
	   $(this).focus(function() {
         startText = $(this).val();
         $(this).val('');
      });
	   $(this).blur(function() {
         blurText = $(this).val();
         if (blurText == '') {
            $(this).val(startText);
         };
	   });
   });
   // Luke added: remove pipe from last footer utility link
   var footLinkTextRaw=$('.cpm a:last').text();
   footLinkText = footLinkTextRaw.replace(' |', '');
   $('.cpm a:last').text(footLinkText);

});

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

// launch video player
function playVideo(){
	window.open('/file/media/video.php?video='+$(this).attr('href'), 'videoPlayer', 'width=640,height=320,scrollbars=0,statusbar=0,address=0');
	return false;
}


// sermon sorting functions
function sermonSort(){
/* SERMON SORTING */		
	// see if there is something in the query string to initially load
	var sort = window.location.hash;
	
	if(sort){
		var parts = sort.split('_');
		var key	= parts[0].replace('#', '');
		var val = parts[1];			
	
		
		$('#sermon-content').html('').css({height:'300px', background:'url(/img/ajax-loader.gif) no-repeat center center'});
		$.get('/ajax/sermons.php', {		
			key:key,
			val:val
		},
		function(o){			
			$('#sermon-content').html(o).css({height:'auto', background:'none'});			
			$('#current').hide();
			$('#all-messages').focus();
			Logos.ReferenceTagging.lbsBibleVersion = "NIV";
		    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
		    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
		    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
		    Logos.ReferenceTagging.tag();
		}
		);		
	}	
	
	$('.sorter').change(function(){
		window.location.hash = this.id+'_'+this.value;
		$('#sermon-content').html('').css({height:'300px', background:'url(/img/ajax-loader.gif) no-repeat center center'});
		if(this.selectedIndex !== 0){
			$.get('/ajax/sermons.php', { 
				'key':this.id,
				'val':this.value			
				}, 
				function(o){				

					//$('#primaryContent > h2').html(response[0]);
					$('#sermon-content').html(o).css({height:'auto', background:'none'});			
					$('#current').hide();					
					
					// reset sorter selects to first value
					$('.sorter').attr({ selectedIndex:0 })		
					$('#all-messages').focus().css('outline', 'none');
				    Logos.ReferenceTagging.lbsBibleVersion = "NIV";
				    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
				    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
				    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
				    Logos.ReferenceTagging.tag();					
				}
			);
		}
	});		
	/* END SERMON SORTING */
}

function switchCalTabs(){
	var div = '#'+$(this).attr('rel');
	$('#events-container div').hide();
	$(div).show();
	if(div.match('list')){
		$('#months').hide();
	} else {
		$('#months').show();
	}

	$('#caltabs li').removeClass('current');
	$(this).parent().addClass('current');
	return false;
}

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

function switchMiniCal(){
 
	var parts = $(this).attr('href').split('/');
	var month = parts.pop();
	var year  = parts.pop();
	$.get('/ajax/events.php', { month:month, year:year, mini:true }, function(data){
	
		$('#mini-cal').html(data);
	});
	return false;
}

function switchCalendar(){
	var parts = $(this).attr('href').split('/');
	var month = parts.pop();
	var year  = parts.pop();

	$.get('/ajax/events.php', { month:month, year:year }, function(data){
		$('#events-calendar').html(data);
	});
	return false;
}

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

document.write('<div id="traildiv"></div>');

function parseParams(url) {
   var params=url;
   if (url.match(/\?(.+)$/)) {
       // in case it is a full query string with ?, only take everything after the ?
       params = RegExp.$1;
   }
	// split the params
	var pArray = params.split("&");
	// hash to store result
	var pHash = {};
	// parse each param in the array and put it in the hash
	for(var i=0;i<pArray.length;i++) {
    	var temp = pArray[i].split("=");
	    pHash[temp[0]] = unescape(temp[1]);
	}
	return pHash;
}

function actions(){
	$('#media #back').bind('click', backAction);
	//$('.pagination a').bind('click', paginateAction);
	//$('.play, .sl_listen a').bind('click', playAudio);
	//$('.series').bind('click', sermonSeries);
}

function backAction(e){
	$('#sermon-content').html('').css({height:'300px', background:'url(/images/ajax-loader.gif) no-repeat center center'});
	$.get('/ajax-sermons.php', { back:1 }, function(o){
		$('#sermon-content').html(o).css({height:'auto', background:'none'});			
		$('#main h2 span').html('');
		actions();
	});
	return false;	
}

function paginateAction(e){
	$('#sermon-content').html('').css({background:'url(/images/ajax-loader.gif) no-repeat center center', height:'300px'});
	var url = parseParams($(e.target).attr('href'));	
	$.get('/ajax-sermons.php', url, function(o){			
		var response = o.split('<!--split-->');
		$('#main > h2').html(response[0]);
		$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});							
		//$('#sidebar a').focus();
		actions();
	});	
	return false;	
}

function sermonSeries(){
	$('#sermon-content').html('').css({height:'300px', background:'url(/images/ajax-loader.gif) no-repeat center center'});
	$.get('/ajax-sermons.php', {
		'groupby': 'series',
		'key'	 : 'series',
		'val'	 : $(this).attr('href')
		}, function(o){
				var response = o.split('<!--split-->');
				$('#main > h2').html(response[0]);
				$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});			
				$('#current').hide();
				$('.sorter').attr({selectedIndex:0});		
				//$('#sidebar a').focus();
				actions();
			}
		);
	return false;

}

$(document).ready(function(){
	actions();	

	$('#service-times a img').click(function(){
		$('#shelf').slideToggle();
		if($(this).hasClass('down')){
			$(this).removeClass('down');
			$(this).attr('src','/images/contact_btn.gif');			
		} else {
			$(this).addClass('down').attr('src','/images/contact_up_btn.gif');		
		}
		return false;
	});	
	$('#tabs a:first').addClass('active').click(function(){
		$('#tabs a.active').removeClass('active');
		$(this).addClass('active');
		$('#content-one').show();
		$('#content-two').hide();
		return false;
	});
	
	$('#tabs a:last').click(function(){
		$('#tabs a.active').removeClass('active');
		$(this).addClass('active');
		$('#content-one').hide();
		$('#content-two').show();	
		
		sIFR.replace(vhand, {
			selector:'#welcome h2',
			wmode:'transparent',
			css:'.sIFR-root { font-size:44px; color:#483726; }'
		});		
			
		return false;
	});		
	
	
	$('#footer li:first').addClass('first');

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


function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;














	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );

}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );





}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';









































}

