/**
 * Vive JS
 *
 * The custom JavaScript used for the added functionality.
 *
 * @author Dan Hensby <dan@betterbrief.co.uk>
 * @author Josh Holloway <josh@betterbrief.co.uk>
 * @version 1
 * @copyright 2010 Better Brief LLP
 *
 */

//Load jQuery
google.load("jquery", "1.4");

// effectively 'on document ready'
google.setOnLoadCallback(function() {

	var J = jQuery.noConflict();
	J(function($) {
		switch(document.body.className) {
			case 'ClassesPage':
				$('#Content div.yoga_class').click(function(ev) {
					if(ev.target.tagName == 'A') {
						return true;
					}
					else if($(ev.target).closest('div.info').length) {
						return false;
					}
					var $info = $(this).find('div.info'), $closeButton = $('div.close', $info[0]), animProps, initProps, animCallback = null;
					if($info.is(':visible')) {
						animProps = {
							height: 0
						};
						initProps = {};
						animCallback = function() {
							$(this).hide();
						};
						$closeButton.fadeOut();
					}
					else {
						animProps = {
							height: '202px'
						};
						initProps = {
							height: 0,
							minHeight: 0,
							display: 'block'
						},
						animCallback = function() {
							//$closeButton.fadeIn();
						};
						$closeButton.fadeIn();
					}

					$info.css(initProps).animate(animProps, 250, animCallback);
					return true;
				});
				$('#Content div.info div.close').click(function() {
					$(this).fadeOut().parent().animate({ height: 0 }, function() { $(this).hide(); });
				});
			break;
			case 'RetreatPage':
				$.getScript('mysite/javascript/prettyPhoto/js/jquery.prettyPhoto.js',
					function() {
						$('a[rel=prettyPhoto]').attr('target', '_blank').prettyPhoto();
					}
				);
			break;
			case 'HomePage':
				var $slideShow = $('#slideshow'),
					$slides = $slideShow.find('li'),
					$currentSlide = $slides.first(),
					ssWidth = $slideShow.width(),
					inSpeed = 1000,
					outSpeed = 1000,
					inParams = {
						left: 0
					},
					outParams = {
						left: ssWidth
					},
					outCallback = function() {
						//$(this).css('left', -ssWidth)
					};

				//hide all of the slides that arent the current one
				$slides.not($currentSlide).hide();

				$slides.click(function(ev) {
					var $this = $(this);
					//dont do the click effect if we are in the current item
					if (!$this.hasClass('current')){
						//get this items slide
						var $thisSlide = $this;
						$currentSlide.stop().fadeOut(outSpeed, outCallback);
						$thisSlide.stop().fadeIn(inSpeed);
						//set the new current slide and item
						$currentSlide = $thisSlide;
					}
					//supress default action if not clicking on div.slide
					return ( !!$(ev.target).closest('div.slide', ev.currentTarget).length );
				});

				//this function is called to start the automatic slide show
				function startRotator() {
					return setInterval(function() {
						var $next = $currentSlide.next();
						if (!$next.length) {
							$next = $slides.first();
						}
						$next.click();
					},7000);
				}

				//start the slideshow and record it's ID so we can disable it later
				var intervalID = startRotator();

				//when we hover on a slide we want to stop the auto rotation
				//$slides.hover(function() {
				//	//stop the auto rotation
				//	clearInterval(intervalID);
				//}, function() {
				//	//start the auto animation
				//	intervalID = startRotator();
				//	var $this = $(this);
				//});
			break;
		}
		$('a.printPage').click(function() {
			window.print();
			return false;
		});
		function createSparkle(url) {
			var $image = $('<img/>', { 'class': 'glow', 'src': url }).hide();
			function sparkle() {
				$image.fadeIn(500, function() {
					window.setTimeout(function() {
						$image.fadeOut(500, function() {
							var position = createSparkle.coordinates[createSparkle.counter];
							$image.css({
								top: position[1],
								left: position[0]
							});
							++createSparkle.counter;
							if(createSparkle.counter >= createSparkle.coordinates.length) {
								createSparkle.counter = 0;
							}
							window.setTimeout(sparkle, 1000);
						});
					}, 250);
				});
			}
			sparkle();
			return $image;
		}
		createSparkle.delay = 500;
		createSparkle.counter = 1;
		createSparkle.coordinates = [[79,13],[92,19],[98,33],[92,46],[79,51],[65,46],[59,33],[65,19]];
		$('#logo a').append(createSparkle('themes/mandala/images/sparkle.png'));
	});

});

//Google analytics tracking
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-5532417-28']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();

