/** 
 * @author chrism
 * Client: Cypress Falls Pet Resort
 * Javascript Include
 * Version: 1.0
 */

$('#appBox, .reservationClose').hide();

$(document).ready(function() { // jQuery initializer
	
// sets default height of of first .expandable to open size & displays its content; sets others to 20px and hides their content
	if ($.browser.mozilla) { 
		$('.expandable').css({ height: '20px', 'padding-bottom': '0' }).children(':not(".clicky")').css({ display: 'none' }); 
		$('.expandable').children('.clicky').css({ 'margin-bottom': '1px' });
	}
	else { $('.expandable').css({ height: '20px' }).children(':not(".clicky")').css({ display: 'none' }); }
		
	$('.expandable').eq(0).css({ height: 'auto' }).children().css({ display: 'block' });
// on click of .clicky <legend>'s, expands form <fieldset>'s and closes others
	$('.clicky').click(function() {
		$(this).parent().css({ height: 'auto', 'padding-bottom': '1.2em' });
		$(this).siblings(':not(".clicky")').css({ display: 'block' });
		
		if ($.browser.mozilla) { 
			$(this).parent().siblings(":not('.noChange')").css({ height: '0px', 'margin-bottom': '0', 'padding-bottom': '0' });
			$(this).parent().siblings('.expandable').children(':not(".clicky")').css({ display: 'none' }); 
		}
		else { $(this).parent().siblings(":not('.noChange')").css({ height: '20px' }); }
	});
// on click of a.formNextButton, close current fieldset, and open next	
	$('.formNextButton').click(function() {
		if ($(this).parents('form').attr('id') == 'temperamentTestApp') { var nextFieldset = $(this).parents('.expandable').index() - 1; }
		else { var nextFieldset = $(this).parents('.expandable').index(); }		
		
		$(this).parent().animate({ height: '20px' }, 400);
		$(this).siblings(':not(".clicky")').css({ display: 'none' });		
		$('.expandable').eq(nextFieldset).css({ height: 'auto' }).children().css({ display: 'block' });
	});
			

// when .reservation-box is clicked, expands to display links	
	$('.reservation-box').click(function() {
		$(this).stop().animate({ width: '300px', height: '145px' }, 500);
		$('#appBox').css({ position: 'relative', left: '0px', opacity: '100' }).show();
		$('.reservationClose').delay('500').css({ opacity: '100', top: '10px', right: '10px' }).show();
	});
// when .reservationClose is clicked, contracts .reservation-box to hide links		
	$('.reservationClose').live('click', function() {
		$('#appBox').css({ opacity: '0' }).hide();
		$('.reservationClose').css({ opacity: '0' }).hide();
		$('.reservation-box').stop().animate({ width: '147px', height: '58px' }, 500);
	});
	
	
// on hover of .content-block-canineCab, .hover-popup phone number overlay appears	
	$('.content-block-canineCab').hoverIntent({
	    	over: canineCabOver, 
	    	timeout: 400, 
	    	out: canineCabOut
	});      


// Sets tabs for daycare, about us and contact us sections
	$("#tabs").organicTabs();


// adds rounded corners on hover to <a> elements that require it  
	$('#footer-inner p a, #contactInfo a, #location small a, .loadBridge, .ss-controls a, .nav-controls a').live('hover', function() {
		$(this).addClass('rounded');
	}, function() {
		$(this).removeClass('rounded');
	});
	
	
// AJAX load rainbow bridge content into #rainbowBridge container div, set container height to image container height
	$('.loadBridge').live('click', function() {
		$('#rainbowBridge').load('rainbow-bridge.html .bridgePhotos', function() {
			var newHeight = $('.bridgePhotos').height();
			$('#rainbowBridge').animate({ height: newHeight }, 500);
		});
	});
	

// AJAX reload ORIGINAL rainbow bridge content into #rainbowBridge container div, set container height to match	
	$('.returnBridge').live('click', function() {
		$('#rainbowBridge').load('rainbow-bridge.html .originalContent', function() {
			var newHeight2 = $('.originalContent').height();
			$('#rainbowBridge').animate({ height: newHeight2 }, 500);
		});
	});	
	

// on click loads appropriate events gallery page	
	var thumbsArray = [];
	$('#eventGalleryPicker').submit(function() {
		var selectedEventGallery = $('#eventGalleryPicker select').val();
		
		self.location = "events-" + selectedEventGallery + ".html" ;
		
		return false;
	});	
	$('#eventGalleryPickerSubmit').click(function() {
		$(this).parents('form').submit();
	});
	
	
// SLIDESHOW | Initially set opacity on thumbs and add additional styling for hover effect on thumbs
	var onMouseOutOpacity = 0.55;
	$('.thumbed-gallery ul.thumbs li, .thumbed-gallery a.pageLink').opacityrollover({
		mouseOutOpacity:   onMouseOutOpacity,
		mouseOverOpacity:  1.0,
		fadeSpeed:         'fast',
		exemptionSelector: '.selected'
	});
// SLIDESHOW | w/ thumbnails
	$('.thumbed-gallery').each(function(i) { 
		var gallery = $('#thumbs-'+i).galleriffic({
			delay:                     3500, // in milliseconds
			numThumbs:                 4, // The number of thumbnails to show page
			preloadAhead:              5, // Set to -1 to preload all images
			enableTopPager:            false,
			enableBottomPager:         false,
			maxPagesToShow:            7,  // The maximum number of pages to display in either the top or bottom pager
			imageContainerSel:         '#slideshow-'+i, // The CSS selector for the element within which the main slideshow image should be rendered
			controlsContainerSel:      '#controls-'+i, // The CSS selector for the element within which the slideshow controls should be rendered
			captionContainerSel:       '#caption-container-'+i, // The CSS selector for the element within which the captions should be rendered
			loadingContainerSel:       '#loading-'+i, // The CSS selector for the element within which should be shown when an image is loading
			renderSSControls:          true, // Specifies whether the slideshow's Play and Pause links should be rendered
			renderNavControls:         true, // Specifies whether the slideshow's Next and Previous links should be rendered
			playLinkText:              'Play Slideshow',
			pauseLinkText:             'Pause Slideshow',
			prevLinkText:              'Previous Photo',
			nextLinkText:              'Next Photo',
			enableHistory:             false, // Specifies whether the url's hash and the browser's history cache should update when the current slideshow image changes
			enableKeyboardNavigation:  false, // Specifies whether keyboard navigation is enabled
			autoStart:                 false, // Specifies whether the slideshow should be playing or paused when the page first loads
			syncTransitions:           false, // Specifies whether the out and in transitions occur simultaneously or distinctly
			defaultTransitionDuration: 1000, // If using the default transitions, specifies the duration of the transitions
			onSlideChange:             function(prevIndex, nextIndex) {
									// 'this' refers to the gallery, which is an extension of $('#thumbs')
									this.find('ul.thumbs').children()
										.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
										.eq(nextIndex).fadeTo('fast', 1.0);
								  },
			onTransitionOut:           undefined, // accepts a delegate like such: function(slide, caption, isSync, callback) { ... }
			onTransitionIn:            undefined, // accepts a delegate like such: function(slide, caption, isSync) { ... }
			onPageTransitionOut:       function(callback) {
								  	this.fadeTo('fast', 0.0, callback);
								  },
			onPageTransitionIn:        function() {
									var prevPageLink = this.find('a.prev').css('visibility', 'hidden');
									var nextPageLink = this.find('a.next').css('visibility', 'hidden');
                        
									// Show appropriate next / prev page links
									if (this.displayedPage > 0)
										prevPageLink.css('visibility', 'visible');

									var lastPage = this.getNumPages() - 1;
									if (this.displayedPage < lastPage)
										nextPageLink.css('visibility', 'visible');

									this.fadeTo('fast', 1.0);
								  },
        		onImageAdded:              function(imageData, $li) {
								       $li.opacityrollover({
				                                mouseOutOpacity:   onMouseOutOpacity,
				                                mouseOverOpacity:  1.0,
				                                fadeSpeed:         'fast',
				                                exemptionSelector: '.selected'
				                           });
				                      },
			onImageRemoved:            undefined // accepts a delegate like such: function(imageData, $li) { ... }
		});
		// SLIDESHOW | Custom prev/next functions
		gallery.find('a.prev').click(function(e) {
			e.preventDefault();
			gallery.previousPage();
		});
		gallery.find('a.next').click(function(e) {
			e.preventDefault();
			gallery.nextPage();
		});
	});
	
}); /* END ON DOCUMENT LOAD FUNCTIONALITY */


/* -------------- PLUGINS BELOW HERE -------------- */


/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);
/* END HOVER INTENT FUNCTION */

// canineCab hoverIntent functions
function canineCabOver(){  $('div.hover-popup').animate({ 'top': '57px', opacity: 'show' }); }
function canineCabOut(){ $('div.hover-popup').animate({ 'top': '0px', opacity: 'hide' }); }


/** 
 * ORGANIC TABS FUNCTION 
 * Features: auto-scaling height (no jumping between tab items of diff. height), no hidden <div> pop-in
 * Credit: http://css-tricks.com/organic-tabs/
 */
(function($){$.organicTabs=function(el,options){var base=this;base.$el=$(el);base.$nav=base.$el.find(".tab-nav");base.init=function(){base.options=$.extend({},$.organicTabs.defaultOptions,options);$(".hide").css({position:"relative",top:0,left:0,display:"none"});base.$nav.delegate("li > a","click",function(){var curDiv=base.$el.find("a.current").attr("href").substring(1),$newDiv=$(this),divID=$newDiv.attr("href").substring(1),$allDivWrap=base.$el.find(".tabs-wrap"),curDivHeight=$allDivWrap.height();$allDivWrap.height(curDivHeight);if((divID!=curDiv)&&(base.$el.find(":animated").length==0)){base.$el.find("#"+curDiv).fadeOut(base.options.speed,function(){base.$el.find("#"+divID).fadeIn(base.options.speed);var newHeight=base.$el.find("#"+divID).height();$allDivWrap.animate({height:newHeight});base.$el.find(".tab-nav li a").removeClass("current");$newDiv.addClass("current")})}return false})};base.init()};$.organicTabs.defaultOptions={speed:500};$.fn.organicTabs=function(options){return this.each(function(){(new $.organicTabs(this,options))})}})(jQuery);
/* END ORGANIC TABS FUNCTION */


/**
 * jQuery Opacity Rollover plugin
 * Copyright (c) 2009 Trent Foley (http://trentacular.com)  |  Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
 */
;(function($){var defaults={mouseOutOpacity:0.67,mouseOverOpacity:1.0,fadeSpeed:'fast',exemptionSelector:'.selected'};$.fn.opacityrollover=function(settings){$.extend(this,defaults,settings);var config=this;function fadeTo(element,opacity){var $target=$(element);if(config.exemptionSelector)
$target=$target.not(config.exemptionSelector);$target.fadeTo(config.fadeSpeed,opacity);}
this.css('opacity',this.mouseOutOpacity).hover(function(){fadeTo(this,config.mouseOverOpacity);},function(){fadeTo(this,config.mouseOutOpacity);});return this;};})(jQuery);
