jQuery.ajaxSetup ({  
	cache: false  
});
var ajax_load = '<img src="images/loading.gif" alt="Loading..." />';  
var loadUrl = '/ajax.asp';


jQuery(document).ready(function() {
	// TO FIX IE JQUERY FLICKER:
	// REVEAL BODY THAT WE HID WHEN THIS FILE WAS LOADED (line 7) 
	jQuery('body').css('display','block');

	// CAPTURE AJAX BUTTONS
	//jQuery('.buttonMainDoorway').click(function() {
	//	revealDialog('mainDoorway');
	//	return false;
	//});
	jQuery('.buttonContact').click(function() {
		revealDialog('contact');
		return false;
	});
	jQuery('.buttonPrivacy').click(function() {
		revealDialog('privacy');
		return false;
	});

	//PREVENT ENTER FROM SUBMITING THE OBIT FINDER FORM, INSTEAD CALL CUSTOM FUNCTION
	jQuery('#term').keydown(function(event) {
		if ((event.keyCode == 13) ||  (event.keyCode == '13')) {
			event.preventDefault();
			sendresults();
		}
	});
	jQuery('#buttonSendResults').click(function() {
		sendresults();
		return false;
	});

	// SETUP OBIT FINDER
	jQuery('.obitFinderPopup').hide();
	jQuery('#obitFinderRecent LI').hover(function() {
		jQuery(this).addClass('hover');
		jQuery(this).find('.obitFinderPopup').css('z-index', 10000).show();
	}, function() {
		jQuery(this).removeClass('hover');
		jQuery(this).find('.obitFinderPopup').css('z-index', 0).hide();
	});
	// SETUP PERSISTENT OBIT FINDER BUTTON
	jQuery('#obitFinder #obitFinderHandle').toggle(function() {
		jQuery(this).parent().animate({ right: '0px' }, 100);
	}, function() {
		jQuery(this).parent().animate({ right: '-600px' }, 100);
	});

	// SETUP DIALOG DIVS
	jQuery('#dialog').hide();
	jQuery('#dim').hide();
	jQuery('#dim').css('height', jQuery(document).height());
	jQuery('#dim').click(function() {
		jQuery('BODY').css('overflow', 'auto');
		jQuery('#dim').toggle();
		jQuery('#dialog').hide();
	});

	// SETUP CORNERS
	jQuery('.corner').corner();
	jQuery('.bigcorner').corner("15px");
	jQuery('.LRGcorner').corner("20px");
	jQuery('.obitFinderPopup').corner("6px");
	jQuery('.cornerTLTR').corner("round tl tr 12px");
	jQuery('.cornerBLBR').corner("round bl br 12px");

	// SETUP ZEBRA
	jQuery('TABLE.zebra tr:even').addClass('zebraEven');
	jQuery('TABLE.zebra tr:odd').addClass('zebraOdd');

	// CLEAR LAST LI BORDER
	jQuery('UL LI:last-child').css('border', 'none');

	// SETUP TABS
	// CSS could do this, but not in all browsers, so we'll do it with JS
	jQuery('.tabButton').hover(function() {
		jQuery(this).css('cursor', 'pointer');
	}, function() {
		jQuery(this).css('cursor', 'auto');
	});
	// Hide all tab content
	jQuery('.tabContent LI').hide();
	// But then reveal the first one
	jQuery('.tabContent LI:eq(0)').show();
	// Attach a click to each tab button

	jQuery('.tabButton').click(function() {
		// Hide all tab content LI
		jQuery('.tabContent LI').hide();
		// Get the index of the tab button (which LI in the UL)
		var tempIndex = $('.tabs LI').index(this);
		// Show the LI in the UL with the index of the one we clicked
		jQuery('.tabContent LI:eq(' + tempIndex + ')');
		// Get the source of all the tab's images
		var tempImageSource = jQuery('.tabButton IMG').attr('src').replace('_on', '_off');
		jQuery('.tabButton IMG').attr('src', tempImageSource);


		// Get the source of the tab's image
		var myLIitem = jQuery('.tabs LI:eq(' + tempIndex + ')');
		var tempImageSource = jQuery(myLIitem).find('IMG').attr('src').replace('_off', '_on');
		alert(tempImageSource);
		// Set the source to the 'on' version
		jQuery(myLIitem).find('IMG').attr('src', tempImageSource);
	});

	// VALIDATE FORMS
	var v = jQuery('#thecontent').validate({
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				target: "#shareYourMemories",
				success: function() {
					//jQuery('#dialog').animate({borderWidth:'10px', width:'740px', height:'430px'},400);
					jQuery('#dialogWrapper').toggle();
				}
			});
		}
	});

	// FUNERAL NOTICES HOVER
	jQuery('#funeralNotices LI').hover(function() {
		jQuery(this).addClass('hover');
	}, function() {
		jQuery(this).removeClass('hover');
	});

	// FUNERAL NOTICES HOVER
	jQuery('#obituaries LI').hover(function() {
		jQuery(this).addClass('hover');
	}, function() {
		jQuery(this).removeClass('hover');
	});

	$('#grief UL LI').mouseover(function() {
		$(this).addClass('griefHover');
	});
	$('#grief UL LI').mouseout(function() {
		$(this).removeClass('griefHover');
	});
	// SHARE BOX CONTROLS
	jQuery('#thecontent').hide();
	jQuery('#shareButton').hover(function() {
		jQuery(this).addClass('hover');
	}, function() {
		jQuery(this).removeClass('hover');
	});
	jQuery('#shareButton').click(function() {
		jQuery(this).hide();
		jQuery('#thecontent').show(1000);
	});
});

// DIALOG BOX WITH AJAX
function revealDialog(element){
	jQuery('BODY').css('overflow','hidden');
	jQuery('#ajax').load(loadUrl + ' #' + element, null, function(){
		if (element == 'contact') {
			jQuery('#ajax TABLE.zebra tr:even').addClass('zebraEven');
		}
			$('#ajax TABLE.zebra tr').mouseover(function() {
				$(this).addClass('zebraHover');
			});
			$('#ajax TABLE.zebra tr').mouseout(function() {
				$(this).removeClass('zebraHover');
			});
		// VALIDATE FORM THEN AJAX SUBMIT
		if (element == 'mainDoorway'){
			revealer();
		}
		var v = jQuery('#formContact').validate({
			submitHandler: function(form){
				jQuery(form).ajaxSubmit({
					target: "#ajax",
					success: function() { 
						//jQuery('#dialog').animate({borderWidth:'10px', width:'740px', height:'430px'},400);
						jQuery('#dialogWrapper').toggle();
						}  
				});
			}
		});
	});
	jQuery('#close').click(function () {
		jQuery('BODY').css('overflow','auto');
		jQuery('#dim').hide();
		jQuery('#dialog').css({'border':'0px solid #fff', 'width':'800px', 'height':'450px'});
		jQuery('#dialog').hide();

	});
	jQuery('#dialog').css('top', (jQuery(window).scrollTop() + 50) + 'px');
	jQuery('#dim').toggle();
	jQuery('#dialog').toggle();
}

// RESIZE DIM ON WINDOW CHANGE
	jQuery(window).bind('resize', function(){
	   jQuery('#dim').css('height', jQuery(window).height());
	});

function revealer() {
	//jQuery('#revealerItemsBox').hide();
	jQuery('#revealerItemsAll').hide();
	jQuery('#revealerItemsBoxClose').click(function() {
			jQuery('#revealerItemsBox').animate({'left':'1000px'},200);
	});
	jQuery('#revealerItemsList LI').hover(function() {
			jQuery(this).addClass('hover');
		}, function() {
			jQuery(this).removeClass('hover');
		})
		.click(function() {
			var tempIndex = $('#revealerItemsList LI').index(this);
			var tempLI = $('#revealerItemsAll LI:eq('+tempIndex+')');
			jQuery('#revealerItemsBox DIV').html(tempLI.html());
			jQuery('#revealerItemsBox').animate({'left':'0px'},200);
			return false;
	});


}

