﻿// JScript File
//$.fn.tabbedArea = function() {
//			$('.contentTabs',this).each(function () {
//				var outerContainer = $(this);
//				var container = $('.ctPaneContainer',this);
//				var tabs = $('<ul class="ctTabsNav"/>');
//				outerContainer.prepend(tabs);
//				container.children('h2').each(function (index) {
//					var thish2 = $(this);																		
//					thish2.nextUntil('h2').wrapAll('<div class="ctPane' + index + '"/>');
//					tabs.append('<li title="tab' + index + '">' + thish2.text() + '</li>').click(
//							function () {
//								container.find('.current').removeClass("current");
//								$(this).addClass("current");
//								container.find('.ctPane').css({ 'display': 'none' });
//								$('.' + $(this).attr("title")).fadeIn();
//							}
//					);
//					$(this).addClass('print');
//				});
//				container.children('.ctTabsNav li:first,.ctPane:first').addClass('current');
//				tabs.append('<div class="clear"/>');
//			});
//		};
//$(document).ready(function () {
//	$('body').tabbedArea();
//});




$(document).ready(function () {

	addImageCaptions();
	newWindowLinks();
	clearTextFields();
	backButtons();
	commentsSetup();
	profilePageSetup();
	animalAdoptionSetup();
	surveyAgeTotalFieldSetup();
	homefinderForms();
	//bullBreedsSetup();
	donateOverlay();

});

//add captions to images
function addImageCaptions() {
	//put captions on polaroid images
	$("img.polaroidRight").load(function () {
		$(this).removeClass('polaroidRight');
		addCaption($(this), 'Right');
	});

	$("img.polaroidLeft").load(function () {
		$(this).removeClass('polaroidLeft');
		addCaption($(this), 'Left');
	});
}
function addCaption(img, side) {
	img.wrap('<div class="imageCaption' + side + '"><span class="polaroid' + side + '"></span></div>');
	img.after('<p>' + img.attr("alt") + '</p>');
}

//open doc or external links in new window
function newWindowLinks() {
	$('a[href^="http://"]').attr("target", "_blank");
	$('a[href*="class=link&link=http://"]').attr("target", "_blank");
	$('a[href$=".pdf"]').attr("target", "_blank");
	$('a[rel="external"]').attr("target", "_blank");
}

//back buttons
function backButtons() {
	$('.return.back').show();
	$('.return.back').click(function () {
		history.go(-1);
		return false;
	})
}

//clear/restore initial text from fields on focus/blur
function clearTextFields() {
	clearText($("#sField"), 'search site');
}
	
function clearText(field, defaulttext) {
	field.focus(function () {
		if ($(this).val() == defaulttext) {
			$(this).val('');
		}
	})
	field.blur(function () {
		if ($(this).val() == "") {
			$(this).val(defaulttext);
		}
	})
}

function animalAdoptionSetup() {

	//switch polaroids on thumbnail click
	$('.polaroidThumbs a').click(function () {
		var imageid = $(this).attr('data-uid');
		$('.adoptImage img,.adoptImage span').each(function () {
			if ($(this).attr('id') == 'image' + imageid) {
				$(this).show();
			}
			else {
				$(this).hide();
			}
		});
		return false;
	})

	//change featured pet
	$('.fArrowLeft, .fArrowRight').click(function () {
		$('div[id^=featureWrapper]').hide();
		$('#featureWrapper' + $(this).attr('id').replace('feat', '')).show();
		return false;
	})
}


// USER DETAILS PAGE --------------------------------------------------

var loadergif = '<img class="loadergif" alt="loading" src="/images/ajax-loader.gif"/>';
var sections = ["profile", "account", "subscriptions", "petprofile"];

// Set up edit and view link clicks, and form submit event
function profilePageSetup() {
	for (i in sections) {
		bindUserDetailsClick(sections[i], "edit");
		bindUserDetailsClick(sections[i], "view");
		setUpUserDetailsForm(sections[i]);
	}
}

// Get form submit event ready. Serializes form and posts to partial.aspx which loads resulting html
function setUpUserDetailsForm(section) {
	$('#' + section + 'form').submit(function () {
		$(this).children('.buttons').html(loadergif);
		$.ajax({
			type: 'POST',
			url: '/Partial.aspx?' + $(this).attr('action').replace(/.*\?/, ''),
			data: $(this).serialize(),
			success: function (html) {
				$('#detailsContainer').replaceWith(html);
				profilePageSetup();
			},
			dataType: 'html'
		});
		return false;
	});
}

// Get edit/view link clicks ready. These load the view/edit mode in place of the current section.
function bindUserDetailsClick(section, mode) {
	$('.' + section + mode + 'link').click(function () {

		if (mode == 'view') {
			$(this).next(".psButtons").hide();
		}
		$(this).replaceWith(loadergif);

		//if edit, hide other edit links, if view, show the edit links again
		for (i in sections) {
			if (mode == "edit") {
				$('.' + sections[i] + 'editlink').hide().unbind();
			}
			else {
				if (sections[i] != section) {
					$('.' + sections[i] + 'editlink').show()
					bindUserDetailsClick(sections[i], "edit")
				}
			}
		}
		//now load the view/edit page
		var poststr = 'formaction=' + $(this).attr('href').replace('?' + mode + '=' + section, '');
		$.ajax({
			type: 'POST',
			url: '/Partial.aspx?' + $(this).attr('href').replace(/.*\?/, ''),
			data: poststr,
			success: function (html) {
				$('#mainColumn .polaroidContent').hide();
				$('#detailsContainer').replaceWith(html);
				profilePageSetup();
			},
			dataType: 'html'
		});
		return false;
	});
}

// END OF USER DETAILS PAGE --------------------------------------------


// COMMENTS ------------------------------------------------------------

function commentsSetup() {

	// Set focus/blur
	clearText($(".commentbodyreply"), 'Comment on this comment');
	clearText($(".commentbodyadd"), 'Your comments here');

	// Clicks to show reply form and hide all others
	$(".discReply").unbind();
	$(".discReply").click(function () {
		$(".discReply").show();
		$('.commentreply').val('').hide();
		$(this).hide();
		$(this).nextAll('.commentreply').show();
		return false;
	});

	// Submit a new comment
	$('#commentForm').unbind();
	$('#commentForm').submit(function () {
		$(".addcomment").replaceWith(loadergif);
		$.ajax({
			type: 'POST',
			url: '/Partial.aspx',
			data: $(this).serialize(),
			success: function (html) {
				$('#formContainer').replaceWith(html);
				commentsSetup();
			},
			dataType: 'html'
		});
		return false;
	});

	//Submit a reply
	$('.replyForm').unbind();
	$('.replyForm').submit(function () {
		$(".replycomment").replaceWith(loadergif);
		var parent = $(this).parents("li").attr("id");
		$.ajax({
			type: 'POST',
			url: '/Partial.aspx',
			data: $(this).serialize(),
			success: function (html) {
				$('#' + parent).replaceWith(html);
				commentsSetup();
			},
			dataType: 'html'
		});
		return false;
	});
}

// END OF COMMENTS -----------------------------------------------------

function surveyAgeTotalFieldSetup() {
	function updateTotal(totalField) {
		var total = 0;
		$(totalField).closest('table').find('input').each(function () {
			if (!$(this).hasClass('agesTotal')) {
				var val = parseInt($(this).val(), 10);
				if (!isNaN(val)) {
					total += val;
				}
			}
		});
		$(totalField).closest('table').find('.agesTotal').val(total);
	}

	var totalFields = $('.nehsForm .agesTotal').closest('table').find('input');
	totalFields.change(function () { updateTotal(this); });
	totalFields.each(function () { updateTotal(this); });
}

function bullBreedsSetup() {
	$('.bullBreeds #mainFeature').wrapInner('<div id="mainFeatureBorder"></div>');
	$('.bullBreeds #sNavigation ul li.bullBreeds > strong, .bullBreeds #sNavigation ul li.bullBreeds > a').wrapInner('<span class="arrow"></span>');
}

function homefinderForms() {
	$('.tHomeFinderFormPage #mainColumn .homefinderform > fieldset:nth-child(odd)').addClass('odd');
	$('.tHomeFinderFormPage #mainColumn .homefinderform > fieldset > fieldset legend').wrapInner('<span></span>');
}

function donateOverlay () {
	$('.donateOverlay .footer').addClass('disable');
	$('#stDonate:has(".donateOverlay")').hoverIntent(function() {
		$(this).addClass('current').children('.donateOverlay').fadeIn('fast');
		return false;
	}, function() {
		$(this).removeClass('current').children('.donateOverlay').fadeOut('fast');
	});
	var donateValues = $('.donateOverlay li');
	donateValues.click(function () {
		$('.donateOverlay .footer.disable').animate({opacity:1}, 'fast', function() {
			$(this).removeClass('disable');
		});
		donateValues.removeClass('current');
		$(this).addClass('current');
		donateValues.find('input[name="Amount"]').attr('checked', false);
		$(this).find('input[name="Amount"]').attr('checked', true);
	});
}
