document.write('<div id="traildiv"></div>');
function catSort(){	
	$('.catlink a').click(function(){
    	$('#sermon-content').html('').css({height:'300px', background:'url(/images/ajax-loader.gif) no-repeat center center'});
    	var category = $(this).attr('href').split('/');
    	var cat		= category[category.length-1];
    	var key		= category;
    	window.location.hash = 'category='+cat;
    	$.get('/ajax-sermons.php', { groupby:'category', key:'category', val:cat }, function(o){
    		var response = o.split('<!--split-->');
    		$('#sermon-content').html(response[1]).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();
    		catSort();    		
    	});
    	return false;
	});
	
	
	$('.category').click(function(){
    	$('#sermon-content').html('').css({height:'300px', background:'url(/images/ajax-loader.gif) no-repeat center center'});
    	var category = this.firstChild.nodeValue;
    	var key = this.rel;
    	if(key == 'speaker') category = this.title;
    	window.location.hash = key+'='+category;
    	$.get('/ajax-sermons.php',{
    		'groupby':key,
    		'key':key,
    		'val':category
    	},
    	function(o){
    		var response = o.split('<!--split-->');
    		$('#sermon-content').html(response[1]).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();
    		catSort();
    	});
    	return false;			
    });		
	
}


function ieNav(){
	switch($('body').attr('id')){
		case 'event' : $('#nav_about-us').addClass('current'); break;
		
		case 'article':
		case 'boards':
		case 'sermon': $('#nav_tools-for-growth').addClass('current'); break;
		
	}
	
	var current = $('#nav li.current ul');	

	if(!document.getElementById('home') && !$.browser.msie){
		if(current[0]){
			var ulWidth = current[0].clientWidth;
			current.css({left:(-210 + (706 - ulWidth))+'px', marginRight:'-500px', visibility:'visible'});
		}
	}
	
	var lis = document.getElementById('nav').getElementsByTagName('LI');
	
	for(var i=0; i<lis.length; i++){
		lis[i].onmouseover = function(){			
			if(this.id == 'nav_home') return;
			
			// for IE, let's get width of the submenu
			if(!$.browser.msie) {
				// position the item to the right side
				var width = $(this).children('ul:first').width();
				var space = 706 - width;
				$(this).children('ul:first').css('left', (-210 + space)+'px');
				$(this).children('ul:first').css({
					marginRight : '-5000px',
					left		: (-210 + space)+'px'
				});
			
			} else {

				$(this).children('ul:first').css('left', '-210px');
				
			}
						
			for(var a=0; a<lis.length; a++){				
				if(lis[a] != this) { 
					lis[a].className = '';
					if(lis[a].getElementsByTagName('UL')[0]){
						lis[a].getElementsByTagName('UL')[0].style.left = '-999em';
					}
				}
			}
		}		
	}	
}

// video playing actions
function actions(){
	$('.sl_video a, #mb_video a, a.video').bind('click', playVideo);
}

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


$(document).ready(function(){
	ieNav();
	catSort();
	actions();
	
	// Video Embedding
	var vid = 1;
	$('.videoembed').each(function(){
		var vurl = $(this).attr('href').toString();
		$(this).before('<div id="video'+vid+'">'+vurl+'</div>');		
		$(this).remove();
		
		var flashvars = { 
			file: vurl,
			autostart:'true',
			frontcolor: 'ffffff',
			lightcolor: 'cc9900',
			skin: 'http://www.longtailvideo.com/jw/upload/overlay.swf',
			controlbar: 'over'
		};
		var params = {allowFullScreen:true};
		var attributes = {};
		swfobject.embedSWF("/js/jw/player.swf", "video"+vid, "640", "360", "9.0.0", "/_js/expressInstall.swf", flashvars, params, attributes);		
		$(this).remove();								
		vid++;			
	});
	
	// sorting for testimonies and worship media
	$('.sorting').change(function(){
		if(this.selectedIndex !== 0){ 		
			$('#sermon-content').css({height:'300px', background:'url(/images/ajax-loader.gif) no-repeat center'}).html('');
			
			var group 	= $(this).attr('id').split('_')[1];
			var cat		= (group == 'month') ? $(this).attr('id').split('_')[0] : $(this).val();
			$.get('/ajax-categories.php', { 
				'groupby' :group, 
				'category':cat,
				'val':$(this).val()		
			}, 
			function(o){
				$('#sermon-content').html(o).css({height:'auto', background:'none'});	
				$('.sorting').attr({selectedIndex:0});
				actions();
			});
		}
		
	});
	
	/* SERMON SORTING */		
	// see if there is something in the query string to initially load
	var sort = window.location.hash;
	
	if(sort){
		$('#sermon-content').html('').css({height:'300px', background:'url(/images/ajax-loader.gif) no-repeat center center'});
		$.get('/ajax-sermons.php', {
			'groupby':'all',
			'key':'current',
			'val':'current'
		},
		function(o){
			var response = o.split('<!--split-->');
			//$('#primaryContent > h2').html(response[0]);
			$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});			
			$('#current').hide();
			$('#all-messages').focus();
			window.location.hash = 'all';
			catSort();
			actions();
		}
		);
	}
	
	$('#all-messages').click(function(){
		window.location.hash = 'all';
		$('#sermon-content').html('').css({height:'300px', background:'url(/images/ajax-loader.gif) no-repeat center center'});
		$.get('/ajax-sermons.php', {
			'groupby':'all',
			'key':'current',
			'val':'current'
		},
		function(o){
			var response = o.split('<!--split-->');
			//$('#primaryContent > h2').html(response[0]);
			$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});			
			$('#current').hide();
			$('#all-messages').focus();
			catSort();
			actions();
		    Logos.ReferenceTagging.lbsBibleVersion = "NIV";
		    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
		    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
		    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
		    Logos.ReferenceTagging.tag();
			
		}
		);
		return false;
	});
	
	$('.sorter').change(function(){
		window.location.hash = this.id+'_'+this.value;
		$('#sermon-content').html('').css({height:'300px', background:'url(/images/ajax-loader.gif) no-repeat center center'});
		if(this.selectedIndex !== 0){
			$.get('/ajax-sermons.php', { 
				'groupby':this.id,
				'key':this.id,
				'val':this.value			
				}, 
				function(o){
					var response = o.split('<!--split-->');
					//$('#primaryContent > h2').html(response[0]);
					$('#sermon-content').html(response[1]).css({height:'auto', background:'none'});			
					$('#current').hide();					
					
					// reset sorter selects to first value
					$('.sorter').attr({ selectedIndex:0 })		
					$('#all-messages').focus().css('outline', 'none');
					catSort();
					actions();
				    Logos.ReferenceTagging.lbsBibleVersion = "NIV";
				    Logos.ReferenceTagging.lbsLinksOpenNewWindow = true;
				    Logos.ReferenceTagging.lbsLibronixLinkIcon = "dark";
				    Logos.ReferenceTagging.lbsNoSearchTagNames = [ "h1", "h2", "h3" ];
				    Logos.ReferenceTagging.tag();
					
				}
			);
		}
	});		
	/* END SERMON SORTING */
	
	$('.play').click(function(){
		var url = 'http://my.ekklesia360.com/Clients/sermonaudioplayer.php?CMSCODE=EKK&siteid=2060&sermonid='+this.id.split('-')[1]+'&useSkin=skin_plain.xml&CMS_LINK=http://my.ekklesia360.com';
		wimpyPopPlayer(url,'wimpyMP3player','width=350,height=140,title=blah');
		return false;		 
	});
	
	$('.external').attr('target','_blank');
	
	$('#articleLists').change(function(){
		if(this.selectedIndex == 1) window.location = '/mcms_page_articlelist_sort.php?groupby=series';
		if(this.selectedIndex == 2) window.location = '/mcms_page_articlelist_sort.php?groupby=category';
		if(this.selectedIndex == 3) window.location = '/mcms_page_articlelist_sort.php?groupby=month';
		if(this.selectedIndex == 4) window.location = '/mcms_page_articlelist_sort.php?groupby=author';	
	});
	
});

