/*global jQuery*/
/*global Gantons*/

Gantons = function () {
	
	jQuery = jQuery.noConflict();
																	 
	// CARE GUIDE
	startCareGuide = function () {
		jQuery("#paneCareGuide, form#formCare ul ul").hide();
		
		// resets all radio buttons on page load/reload
		jQuery("form#formCare input:radio").each(function() {
			jQuery(this).removeAttr("checked");
		});

		jQuery("#step1_1").click(function () {;
			jQuery("#step1_1 + h3 + ul").fadeIn('slow');
		});
		
		jQuery("#step1_2, #step1_3").click(function () {
			jQuery("#step1_1 + h3 + ul").fadeOut('slow');
			jQuery("#step1_1 + h3 + ul input:radio").each(function() {
				jQuery(this).removeAttr("checked");
			});
		});
		
		
		jQuery("form#formCare input:radio").click(function () {
			if (jQuery(this).val() != '') { // no response for blank selection			
				// load new paneCareGuide values
				jQuery("#paneCareGuide").hide();
				if (jQuery(this).val() == 'request-information') {
					location=jQuery(this).val();
				}
				else {
					jQuery.post("/facilities/", { facility_tag : jQuery(this).val() }, function(data) {
						jQuery("#paneCareGuide").html(data).fadeIn();
					});
				}
			}
		});
	}; // END CARE GUIDE
								
								
	// PHOTO GALLERY			
	startGallery = function () {
	
		jQuery(".galleryInner").css( "visibility", "hidden" );
	
		jQuery(".galleryInner p").each(function () {
			jQuery(this).prev().andSelf().wrapAll("<div class=\"photo\"></div>");
		});
		
		jQuery(".galleryInner").cycle({
			fx: 'uncover',
			next: '.galleryNext',
			prev: '.galleryPrev'
		}).cycle("pause");
		
		jQuery(".slideshowButton").unbind();
		jQuery(".slideshowButton").toggle(
			function () {
				jQuery(".galleryInner").cycle("resume");
				jQuery(this).val("PAUSE SLIDESHOW");
			},
			function () {
				jQuery(".galleryInner").cycle("pause");
				jQuery(this).val("VIEW AS SLIDESHOW");
			}
		);
	
		jQuery(".galleryInner").css( "visibility", "visible" );
		
	};
	
	var galleryTimer = null;
	
	startGalleryPager = function () {			

		var slider_inner = jQuery( ".galleryPagerInner" );
		var slider_inner_height = slider_inner.height();
		var slider_items = jQuery( ".galleryPagerInner .page" );
		var slider_items_height = 0;
		slider_items.each(function( i ) {
			if ( i < 3 ) {
				slider_items_height += jQuery( this ).height();
			} else {
				return false;
			}
		});
		slider_inner.css({
			"height" : slider_items_height + "px",
			"visibility" : "visible"
		});

		var current_position = 0;
		jQuery( ".galleryPagerNext" ).click(function() {
			return false;
		});
		jQuery( ".galleryPagerNext" ).mousedown(function() {
			galleryTimer = setInterval(function() {
				if ( parseInt( slider_inner.css( "top" ) ) >= -slider_inner_height ) {
					slider_inner.css({
						"top" : ( parseInt( slider_inner.css( "top" ) ) - 2 ) + "px"
					});
				} else {
					clearInterval( galleryTimer );
				}
			}, 10 );
			return false;
		});
		jQuery( ".galleryPagerNext" ).mouseup(function() {
			clearInterval( galleryTimer );
			return false;
		});

		jQuery( ".galleryPagerPrev" ).click(function() {
			return false;
		});
		jQuery( ".galleryPagerPrev" ).mousedown(function() {
			galleryTimer = setInterval(function() {
				if ( parseInt( slider_inner.css( "top" ) ) <= 0 ) {
					slider_inner.css({
						"top" : ( parseInt( slider_inner.css( "top" ) ) + 2 ) + "px"
					});
				} else {
					clearInterval( galleryTimer );
				}
			}, 10 );
			return false;
		});
		jQuery( ".galleryPagerPrev" ).mouseup(function() {
			clearInterval( galleryTimer );
			return false;
		});

		jQuery(".galleryPagerInner a").click(function () {
			jQuery(".galleryInner").html( "<p class='ajax_loader'>&nbsp;</p>" );
			jQuery.post(
				jQuery(this).attr("href"),
				{ ajax_call : true },
				function (data) {
					jQuery(".galleryInner").html(data);
					startGallery();
				}
			);
			return false;
		});
		
	}; // END GALLERY
	
	
	startTour = function () {
		jQuery("#buttonGo").click(function () {
			if (jQuery("#selectFacility").val() != '') { // no response for blank selection
				document.location.href = jQuery("#selectFacility").val();
			};
			return false;
		});
	};
	
	
	startLinks = function () {
		var docHost = document.location.host;
		var docHref = document.location.href;
		jQuery("a").each(function () {
			aHref = jQuery(this).attr("href");
			if (aHref[0] == '/') aHref = 'http://' + docHost + aHref;
			if (aHref == docHref) jQuery(this).addClass("on");
		});
	};
	
	return {
		init : function () {
			if (jQuery("body.home").length > 0) {
				//startFlash();
			};
			if (jQuery("#paneTour").length > 0) {
				startTour();
			};
			if (jQuery("#paneCareGuide").length > 0) {
				startCareGuide();
			};
			if (jQuery("#paneGallery").length > 0) {
				startGallery();				
				startGalleryPager();
			};
			startLinks();
		}
	};
	
}();

jQuery(document).ready(function () {
	Gantons.init();
	if ( jQuery("body").hasClass("home") ) {
		swfobject.embedSWF("/wp-content/themes/gantons/flash/gantons_flash.swf", "flash_home", "995", "350", "9.0.0", false, {
			xmlPath : "/wp-content/themes/gantons/flash/xml/gantonhome.xml"
		}, {
			wmode : "transparent"
		});
	}
});