function init_sidebar_toggles() {
	if($('#sidebar a.toggle').length>0) {
		$('#sidebar .section:not(:eq(0)) a.toggle').parent().parent().parent().toggleClass('open').toggleClass('closed');
		$('#sidebar .section:not(:eq(0)) a.toggle').parent().parent().find('.section-content').hide();
	}
}

function hook_sidebar_toggle() {
	$('#sidebar a.toggle').bind('click', function() {
		$('#sidebar a.toggle').unbind('click');
		$('.section-content').slideUp();
		$(this).parent().parent().parent().toggleClass('open').toggleClass('closed');
		$(this).parent().parent().find('.section-content').slideToggle('slow', function() {
			hook_sidebar_toggle();
		});
		return false;
	});
}

function init_sidebar() {
	init_sidebar_toggles();
	hook_sidebar_toggle();
}

// Functionality for the slideshow
var current_image = 0;
var total_images = 0;
var timer = false;
var timer_interval = 4800;
var transition_interval = 1000;

$(document).ready(function() {
	// count total images
	total_images = parseInt( jQuery('ul#slides li').length ) - 1;

	if(total_images > 0) {
		// hide all but the first image
		jQuery('ul#slides li:gt(0)').hide();

		// setup gallery images
		jQuery('ul#slides li').each(function(index) {
			jQuery(this).attr('id','image-' + index);
		});

		// start the carosel
		start_rotation();
	}

	function swap_image(index) {
		jQuery('ul#slides li#image-' + current_image).fadeOut(transition_interval);
		jQuery('ul#slides li#image-' + index).fadeIn(transition_interval);
		current_image = index;
	}

	function start_rotation() {
		timer = setInterval(function() {
			if(current_image == total_images) {
				image_index = 0;
			} else {
				image_index = parseInt(current_image + 1);
			}
			swap_image(image_index);
		}, parseInt(timer_interval));
	}


	init_sidebar();
	
	// ===============
	// = GIFT TOGGLE =
	// ===============
	if($('#giftdetails').length!=0) {
		
		// Gift Details
		if($("input:radio[name=fm_gift]:checked").val()=='fm_gift_no') {
			$('#giftdetails').hide();
		}
		$("#field-fm_gift input[type='radio']").change( function() {
			if($("input:radio[name=fm_gift]:checked").val()=='fm_gift_no') {
				$('#fm_gift_notification_no').click();
				$('#giftdetails').slideUp();
			} else {
				$('#giftdetails').slideDown();
			}
		});
		
		// Gift Notification
		if($("input:radio[name=fm_gift_notification]:checked").val()=='notification_no') {
			$('#gift-notification').hide();
		}
		$("#field-fm_gift_notification input[type='radio']").change( function() {
			if($("input:radio[name=fm_gift_notification]:checked").val()=='notification_no') {
				$('#gift-notification').slideUp();
			} else {
				$('#gift-notification').slideDown();
			}
		});
	}
	
	if($('#pet-search-results').length!=0) {
		our_data = $('#pet-search-results table').html();
		$('#pet-search-results').empty().append('<table width="100%">' + our_data + '</table>');
	}
	
	if($('#pet-profile').length!=0) {
		pet_name = $('#lbName').text();
		our_data = $('#pet-profile #DefaultLayoutDiv table:eq(1)').html();
		$('#pet-profile').empty().append('<table width="100%">' + our_data + '</table>');
		$('#pet-profile table.detail-table').before('<h2 id="pet-name">' + pet_name + '</h2>');
		$('#pet-profile table.detail-table').after('<p class="begin-adoption"><a href="' + $('#adpot_application').attr('href') + '"><img src="' + imgpath + '/btn-adopt.gif" alt="Adopt this pet" /></a></p>');
	}
	
});
