jQuery.ajaxSetup ({  
	cache: false,
	error:function(xhr,err){
		alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		alert("responseText: "+xhr.responseText);
	}
});
var ajax_load = '<img src="/images/loading.gif" alt="Loading..." />';  
var loadUrl = '/ajax.asp';  

jQuery(document).ready(function() {
/* REPLACE TEXT - CALL THIS FIRST BECAUSE 'html()' WILL DESTORY BOUND EVENTS */
	jQuery("A:contains('Faq')").each(function(){
		jQuery(this).html(jQuery(this).html().replace(/faq/ig, "FAQ"));
	});
	jQuery("BODY:contains('Plympton-wyoming')").each(function(){
		jQuery(this).html(jQuery(this).html().replace(/wyoming/g, "Wyoming"));
	});

/* 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 BUTTONS */
	jQuery('.button').corner();
	jQuery('.button A').css({'display':'block'});
	jQuery('.button').hover(function(){
		jQuery(this).addClass('hover');
	}, function() {
		jQuery(this).removeClass('hover');
	});

/* CLEAR LAST LI BORDER */
	jQuery('.lined LI:last-child').css('border','none');

/* MOVE FIRST PHOTO TO SIDENAV */
	jQuery('IMG.photo:eq(0)').addClass('floatRight');

/* SETUP SHARING */
	jQuery('#socialSharing').Sharing();

/* CAPTURE AJAX BUTTONS */
	jQuery('A[href*="ajax"]').click(function () {
		var temp = jQuery(this).attr('href');
		temp = temp.split('/');
		var page = temp.pop();
		revealDialog(page);
		return false;
	});
/* CAPTURE Anchors WITH JPGs */
	jQuery('A[href*="jpg"], A[href*="JPG"]').click(function () {
		var temp = jQuery(this).attr('href');
		revealImage(temp);
		return false;
	});
/* CAPTURE Anchors WITH PDFs
	jQuery('#article A[href*="pdf"], #article A[href*="PDF"]').each(function() {
		jQuery(this).closest('UL').css({'list-style':'none'});
		var tempLink = jQuery('<p></p>').attr('class','button pdf');
		jQuery(this).wrap(tempLink);
	});
/*
/* MOVE SECOND PHOTO UP */
//	jQuery('IMG.photoBottom').css({'vertical-align':'top'});

/* SETUP WEATHER */
	jQuery("#weather").hide();
	jQuery("#buttonWeather").click(function() {
		jQuery("#weather").slideToggle();
	});
/* SETUP SLIDESHOW */
	jQuery('#slideShowWrapper').mouseover(function() {
		jQuery('#slideShowWrapper').cycle('pause');
	}).mouseout(function() {
		jQuery('#slideShowWrapper').cycle('resume');
	});
	jQuery('#slideShowWrapper').cycle({ 
		fx: 'fade', 
		speed: 1500, 
		timeout: 6000, 
		next: '#buttonNextSlide', 
		prev: '#buttonPreviousSlide' 
	});
	jQuery('html').keypress(function(e) {
		code = e.keyCode ? e.keyCode : e.which;
		if(code.toString() == 27) {
			jQuery('#slideShowWrapper').cycle('stop');
		}
	});

});

/* DIALOG BOX WITH AJAX IMAGE */
function revealImage(element){
	jQuery('#dialog').css('height', jQuery(window).height()-150+'px');
	jQuery('#ajax').css('height', jQuery('#dialog').height()-150+'px');
	jQuery('BODY').css('overflow','hidden');
	// GET THE PATH AND SPLIT INTO FOLDERS AND FILE
	var tempFolders = element.split('/');
	var tempGalleryPath = '';
	var tempImagePath = '';
	for (i=0; i<tempFolders.length-1; i++){
		if (i != 0){
			tempGalleryPath += '/';
		}
		tempGalleryPath += tempFolders[i];
	}
	tempImagePath = tempFolders[tempFolders.length-1];
	tempSRC = '/imageresizer.asp?gallerypath='+tempGalleryPath+'&imagepath='+tempImagePath+'&aratio=w&w=694';
	jQuery("#ajax").html('');;
	var tempImg = jQuery('<IMG />').attr('src', tempSRC)
		.load(function(){
			jQuery("#ajax").append(tempImg);
		});

	jQuery('#close').click(function () {
		jQuery('BODY').css('overflow','auto');
		jQuery('#dim').hide();
		jQuery('#dialog').hide();

	});
	jQuery('#dialog').css('top', (jQuery(window).scrollTop() + 50) + 'px');
	var tempHeight = jQuery(window).height();
	jQuery('#dialog').css('height', tempHeight-150 + 'px');
	jQuery('#dim').toggle();
	jQuery('#dialog').toggle();
}

/* DIALOG BOX WITH AJAX */
function revealDialog(element){
	jQuery('#dialog').css('height', jQuery(window).height()-150+'px');
	jQuery('#ajax').css('height', jQuery('#dialog').height()-150+'px');
	jQuery('BODY').css('overflow','hidden');
	jQuery('#ajax').load(loadUrl + ' #' + element, null, function(){
		if (element == 'contact') {
			jQuery('#ajax TABLE.zebra tr:even').addClass('zebraEven');
		}
		/* VALIDATE FORM THEN AJAX SUBMIT */
		var t = jQuery('#formWithCaptcha').captcha();
		var v = jQuery('#formContact').validate({
			submitHandler: function(form){
				jQuery(form).ajaxSubmit({
					target: "#ajax",
					success: function() { 
						jQuery('#dialogWrapper').toggle();
						}  
				});
			}
		});
	});
	jQuery('#close').click(function () {
		jQuery('BODY').css('overflow','auto');
		jQuery('#dim').hide();
		jQuery('#dialog').hide();

	});
	jQuery('#dialog').css('top', (jQuery(window).scrollTop() + 50) + 'px');
	var tempHeight = jQuery(window).height();
	jQuery('#dialog').css('height', tempHeight-150 + 'px');
	jQuery('#dim').toggle();
	jQuery('#dialog').toggle();
}

/* RESIZE DIM ON WINDOW CHANGE */
jQuery(window).bind('resize', function(){
	jQuery('#dim').css('height', jQuery(window).height());
	jQuery('#dialog').css('height', jQuery(window).height()-150+'px');
	jQuery('#ajax').css('height', jQuery('#dialog').height()-150+'px');
});

