//Fade Function
(function(jQuery) {
	jQuery.fn.customFadeIn = function(speed, callback) {
		jQuery(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				jQuery(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	jQuery.fn.customFadeOut = function(speed, callback) {
		jQuery(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				jQuery(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);

jQuery(document).ready(function(){
	//Modal Window
	jQuery('a[name=modal]').click(function(e) {
		e.preventDefault();
		var id = jQuery(this).attr('href');
		var maskHeight = jQuery(document).height();
		var maskWidth = jQuery(window).width();
		jQuery('#PopupBg').css({'width':maskWidth,'height':maskHeight});	
		jQuery('#PopupBg').customFadeIn(1000, function() {});
		var winH = jQuery(document).height();
		var winW = jQuery(window).width(); 
		jQuery(id).css('top',  winH/2-jQuery(id).height()/2);
		jQuery(id).css('left', winW/2-jQuery(id).width()/2);
		jQuery(id).customFadeIn(2000, function() {});
		jQuery('#PopupBg').fadeTo("slow",0.8);
	});
	jQuery('a.ClosePopup').livequery('click', function(e) {										   
		e.preventDefault();
		jQuery('#PopupBg, #EmailFriend, #PostANotice, #PostMessage').customFadeOut('fast', function() {});
	});	
	
	//Email Friend Form
	jQuery('#SendFriendButton').livequery('click', function(event) {
		var EmailTo = $("#EmailTo").val();
		var EmailFrom = $("#EmailFrom").val();
		var EmailMessage = $("#EmailMessage").val();
		var notice_url = $("#notice_url").val();
		
		jQuery.post('/includes/email_friend_process.inc.php', { EmailTo: EmailTo, EmailFrom: EmailFrom, EmailMessage: EmailMessage, notice_url: notice_url }, 
        	function(EmailFriend) {
				jQuery('#EmailFriend').html(EmailFriend);
		});
		return false;
    });
	
	//Post Notice Form
	jQuery('#PostNoticeButton').livequery('click', function(event) {
		var FullName = $("#FullName").val();
		var NoticeEmail = $("#NoticeEmail").val();
		var NoticeComments = $("#NoticeComments").val();
		var NoticeName = $("#NoticeName").val();
		var NoticeID = $("#NoticeID").val();
		
		jQuery.post('/includes/post_notice_process.inc.php', { FullName: FullName, NoticeEmail: NoticeEmail, NoticeComments: NoticeComments, NoticeName: NoticeName, NoticeID: NoticeID }, 
        	function(PostNotice) {
				jQuery('#PostANotice').html(PostNotice);
		});
		return false;
    });
	
	//Post Message Form
	jQuery('#PostMessageButton').livequery('click', function(event) {
		var FullName = $("#FullName").val();
		var NoticeEmail = $("#NoticeEmail").val();
		var NoticeComments = $("#NoticeComments").val();
		var NoticeName = $("#NoticeName").val();
		var NoticeID = $("#NoticeID").val();
		
		jQuery.post('/includes/post_message_process.inc.php', { FullName: FullName, NoticeEmail: NoticeEmail, NoticeComments: NoticeComments, NoticeName: NoticeName, NoticeID: NoticeID }, 
        	function(PostMessage) {
				jQuery('#PostMessage').html(PostMessage);
		});
		return false;
    });
	
	//Search Form
	jQuery('.InputFocus').each(function() {								
		var default_value = this.value;
		jQuery(this).focus(function() {
			if(this.value == default_value) {
				this.value = '';
			}
		});
		jQuery(this).blur(function() {
			if (this.value=='' || this.value==' ' || this.value=='  ') {
				this.value = default_value;
			}
		});
	});
	jQuery("a.AdvancedSearch").click(function(){
		jQuery(this).hide();
		jQuery('#AdvancedSearchForm').customFadeIn('slow', function() {});
		return false;
	});
	
	jQuery("a.HideAdvancedSearch").click(function(){
		jQuery('#AdvancedSearchForm').hide();
		jQuery('a.AdvancedSearch').customFadeIn('slow', function() {});
		return false;
	});
	
	
	//Recent Death Notices
	jQuery("img.carouselimg, #PhotoThumbs li img").hover(function(){
		jQuery(this).fadeTo("fast", 0.6);},function(){
		jQuery(this).fadeTo("fast", 1.0);
		return false;
	});

	
	jQuery("#PhotoThumbs a").click(function(){
		var largePath = jQuery(this).attr("href");
		jQuery("#LargePhoto img").attr({ src: largePath });
		return false;
	});
	
	//Send Wreath
	jQuery("#SendWreathButton a").click(function(){
		jQuery('#SendWreath1').hide();
		jQuery('#SendWreath2').customFadeIn('slow', function() {});
		return false;
	});
	jQuery('#SendWreath2 #Comments').attr('maxlength', 250);
	jQuery('#NoticeComments').attr('maxlength', 500);
	jQuery('#EmailMessage').attr('maxlength', 250);
	jQuery('#RememberMessage').attr('maxlength', 500);
	jQuery('textarea[maxlength]').keyup(function(){
		var max = parseInt(jQuery(this).attr('maxlength'));
		if(jQuery(this).val().length > max){
			jQuery(this).val(jQuery(this).val().substr(0, jQuery(this).attr('maxlength')));
		}
		jQuery(this).parent().find('.charsRemaining').html('You have <strong>' + (max - jQuery(this).val().length) + '</strong> characters remaining');
	});
	
	//External Link
	jQuery('a[rel="external"]').click(function(){
        window.open( jQuery(this).attr('href') );
        return false;
    });

});