/*
 * Email Defuscator - jQuery plugin 1.0-beta2
 *
 * Copyright (c) 2007 Joakim Stai
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: $Id$
 *
 */

/**
 * Converts obfuscated email addresses into normal, working email addresses.
 *
 * @name defuscate
 * @param Boolean link If true, all defuscated email addresses will be turned into links, defaults to true (optional)
 * @param String find The regular expression used to search for obfuscated email addresses (optional)
 * @param String replace Replacement text for defuscating email addresses (optional)
 * @descr Converts obfuscated email addresses into normal, working email addresses
 */

jQuery.fn.defuscate = function( settings ) {
    settings = jQuery.extend({
        link: true,
        find: /\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})\b/gi,
        replace: '$1@$2'
    }, settings);
    return this.each(function() {
        if ( $(this).is('a[@href]') ) {
            $(this).attr('href', $(this).attr('href').replace(settings.find, settings.replace));
            var is_link = true;
        }
        $(this).html($(this).html().replace(settings.find, (settings.link && !is_link ? '<a href="mailto:' + settings.replace + '">' + settings.replace + '</a>' : settings.replace)));
    });
};


$(document).ready(function(){
	$('#signup').attr({name:'ccoptin', target:'_blank'});
	
	//document.getElementById('signup').name = 'ccoptin';
	//document.getElementById('signup').target = '_blank';
	$('a.play').click(function(){
		var id = this.id.split('-')[1];
		var url = 'http://my.ekklesia360.com/Clients/sermonaudioplayer.php?CMSCODE=EKK&siteid=1510&sermonid='+id+'&useSkin=skin_plain.xml&CMS_LINK=http://my.ekklesia360.com';
		window.open(url, 'wimpyMP3player','width=350,height=140,title=blah');
		return false;		
	});
	
	$('#all-messages').click(function(){
		$('#sermon-content')
			.html('')
			.css({
				background:'url(/images/ajax-loader.gif) no-repeat center center',
				minHeight:'300px'
			});
		$.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({background:'none'});			
			$('#current').hide();
			$('.sorter').attr({selectedIndex:0});
			$('#all-messages').focus();
		}
		);
		return false;
	});
	
	$('.sorter').change(function(){
		if(this.selectedIndex != 0){
			$('#sermon-content')
				.html('')
				.css({
					background:'url(/images/ajax-loader.gif) no-repeat center center',
					minHeight:'300px'
				});
			
			$.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({background:'none'});			
					$('#current').hide();
					$('.sorter').attr({selectedIndex:0});
					$('#all-messages').focus();
				}
			);
		}
	});	
	
	$('.external').click(function(){
		window.open(this.href);
		return false;
	});
	
	$('#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=preacher';
		
	});		
	
	// resource link background images
	if(document.getElementById('resources')){
	var lis = document.getElementById('resources').getElementsByTagName('li');
	lis[0].firstChild.style.background = 'url(/images/blog_icon.gif) no-repeat 6px 6px';
	lis[1].firstChild.style.background = 'url(/images/enews_icon.gif) no-repeat 6px 6px';	
	lis[2].firstChild.style.background = 'url(/images/podcast_icon.gif) no-repeat 6px 4px';
	lis[3].firstChild.style.background = 'url(/images/email_icon.gif) no-repeat 6px 4px';
	lis[0].style.marginLeft = lis[2].style.marginLeft = '0';
	}
	
	// search box hovers
	$('#footer input').focus(function(){
		this.value = '';
	});
	$('#footer input').blur(function(){
		if(this.value == '') this.value = 'search the site'; 
	});
	
	// create sliding doors for button class	
	$('a.button').each(function(){
		var html = $(this).html();
		$(this).html('<span>'+html+'</span>');
	});
	
	// defuscate sidebar email
	
	$('a.obfx').defuscate();
});