// Brian Tetrault JavaScript

jQuery.noConflict();

jQuery(document).ready(function($) {

// Quicktips duration slider. Requires jQuery UI.
// Reads data out of attributes on a <div> generated by functions.php
				
		$( "#slider-range" ).slider({
			range: true,
			min: parseInt($("#slider-range").attr("data-min")),
			max: parseInt($("#slider-range").attr("data-max")),
			values: [ parseInt($("#slider-range").attr("data-currentmin")), parseInt($("#slider-range").attr("data-currentmax")) ],
			slide: function( event, ui ) {
				$( "#displaymin" ).html( convert_to_minsec(ui.values[ 0 ]) );
				$( "#displaymax" ).html( convert_to_minsec(ui.values[ 1 ]) );
				$( "#durationmin" ).val(ui.values[ 0 ]);
				$( "#durationmax" ).val(ui.values[ 1 ]);
				
			}
		});

// This utility function converts raw seconds into min:sec format.
// Used in the Quicktips duration slider.

	function convert_to_minsec(time) {
		if (time == parseInt(time)) {	
			var min = Math.floor(time/60);
			var sec = Math.round(time % 60);
			// pad with leading zero
			if (sec < 10) {
				sec = "0" + sec + "";
			}
			time = min + ":" + sec;
		}
		return time;	
	}
		
	
// Site-wide contact form animation/interaction

	jQuery("#contactform").hide();
	jQuery("#contact > a").click(function(event) {
		event.preventDefault();
		jQuery("#contactform").slideToggle();
		return false;
	});
	jQuery(".closecontactform a").click(function(event) {
		event.preventDefault();
		jQuery("#contactform").slideUp();
		return false;
	});


// Quicktips suggestion form animation/interaction.
// A little kludgey. I'm in a hurry. Apologies to neatnik coders.

	jQuery("#suggestions h2").click(function(event) {
		if (!jQuery(this).hasClass("active")) {
		
		jQuery("#suggestions").animate({ right: 0 }, 1000).children("h2").addClass("active");
		
		} else {
		
		jQuery("#suggestions").animate({ right: "-256px" }, 1000).children("h2").removeClass("active");		
		
		}
	});
	
	jQuery("#suggestions .closecontactform a").click( function(event) {
		jQuery("#suggestions").animate({ right: "-256px" }, 1000).children("h2").removeClass("active");
	});


// Fix lousy AJAX arrow on Contact Form 7 forms
	    				jQuery("img.ajax-loader").attr('src','/wp-content/themes/briantetrault/images/ajax-loader.gif');



// Blog e-mail newsletter subscription form interaction/animation

	jQuery("#mailinglist").click(function() {
		jQuery(this).hide();
		jQuery("#signup").slideDown();
	});


// Closing up this document.ready block

});


// OuterHTML function for use in jQuery cycle plugin

jQuery.fn.outerHTML = function() {
return jQuery('<div></div>').append( this.clone() ).html();
}


// Activate jQuery cycle plug-in for slideshows
jQuery(document).ready(function() {

	if (jQuery(".slideshow").length) {
	
		// If a slideshow exists on this page,
		// add the class "gallery" to the body. 
		// This helps the CSS work.
		
		jQuery("body").addClass("gallery");
		
		// disable thumbnail links so they don't accidentally pull up high-res images.
		jQuery("#thumbnails a").click(function(a) {
			a.preventDefault();
		});
		
		// disable the big player link too.
		jQuery("#player").click(function(a) {
			a.preventDefault();
		});
		
		if (jQuery("body.home").length) {
		// this is the home page, which takes a slightly different slideshow
		
		jQuery("#photoslideshow").cycle ( {
			fx: 'fade',
			speed: 'fast',
			timeout: 4000
			});
	
		} else {
		
		jQuery("#photoslideshow").cycle ( {
			fx: 'fade',
			speed: 'fast',
			timeout: 0,
			pager: "#thumbnails",
			pagerAnchorBuilder: function(idx, slide) { 
				return "#thumbnails li:eq("+idx+") a"; 
			},
			before: function(currSlide, nextSlide) {
				var seq = jQuery(nextSlide).index();
				var title= jQuery("#thumbnails li:eq(" + seq + ") span").html();
				jQuery("#caption").html(title);
			
			}
			});
	
		if (jQuery("#videoslideshow").length) {
		
		// this page has a video slideshow!
				
		// load caption
		var title = jQuery("#thumbnails a:eq(0)").attr("title");
		var link = jQuery("#thumbnails a:eq(0)").attr("href");
		jQuery("#caption").text(title);
			
		// initialize player
		var so = new SWFObject('/wp-content/themes/briantetrault/mediaplayer/player.swf','mpl','775','443','0');
		so.addParam('allowfullscreen','true');
		so.addParam('allowscriptaccess','always');
		so.addParam('wmode','opaque');
		so.addVariable('file',link);
		so.addVariable('bufferlength','6');
		so.addVariable('controlbar','over');
		so.addVariable('icons','false');
		so.addVariable('autostart','true');
		so.write('player');

		// set up thumbnails
		jQuery("#thumbnails a").click(function(a) {
			a.preventDefault();
			
			var link = jQuery(this).attr("href");
			var player = document.getElementById('mpl');
			
			player.sendEvent("STOP");
			player.sendEvent("LOAD",link);

			var title = jQuery(this).attr("title");
			jQuery("#caption").text(title);
			return false;
		
		});	
		}
		}
	}
});


// This is all stuff the previous developer left behind.
// Honestly, I'm not sure what it does, and I don't have time to
// fully investigate and troubleshoot. But it doesn't seem to be 
// hurting anything or impacting performace so I'm going to be 
// a little sloppy and let it go. To whomever is reading this, please
// accept my apologies. -- jw

jQuery(document).ready(function($){
	// Focus & Blur FX
	jQuery.fn.valueFx = function() {
		return this.each(function(){
			// Vars
			var $field = $(this);
			var $value = ($(this).attr("data-default") != "") ? $(this).attr("data-default") : $(this).val();
			var $newVal;
			
			if ($(this).val() == $value) {
				$field.addClass('placeholder');
			}
			
			// Event handlers
			$field.focus(function(){
				if($field.hasClass('placeholder')){
					if (($value == '') || ($value == $value)) {
						$field.removeClass('placeholder').addClass('focus').val('');
						$newVal = $(this).val();
					}
				}
			});
			$field.blur(function(){
				$newVal = $(this).val();
				if ($newVal == '') {
					$field.val($value).removeClass('focus').addClass('placeholder');
				} else {
					$field.removeClass('placeholder').removeClass('valueFx');
				}
			});
		});
	};
	// Mailto
	jQuery.fn.mailto = function() {
	   return this.each(function(){
	     var email = $(this).html().replace(/\s*\(.+\)\s*/, "@");
	     $(this).before('<a href="mailto:' + email + '" rel="nofollow" title="Email ' + email + '">' + email + '</a>').remove();
	   });
	};
		// Advanced CSS
		if ($('p:has(cite)').length > 0){
			$('p:has(cite)').addClass('citation');	
		}
		$('#sidebar h2:first').addClass('top');
		$('input:checkbox').addClass('checkbox');
		$('#navigation').each(function(){
			$(this)
				.find('li:first')
				.addClass('first');
			$(this)
				.find('li:last')
				.addClass('last');
		});
		$('#content').each(function(){
			$(this)
				.find('h3:first')
				.addClass('first');
			$(this)
				.find('h5:first')
				.addClass('first');
		});
		$('div.home-news-2').each(function(){
			$(this)
				.find('p:last')
				.addClass('last');
		});
		$('div.home-news-3').each(function(){
			$(this)
				.find('p:last')
				.addClass('last');
		});
		// Focus & Blur
		if ($('input.valueFx').length > 0) {
			$('input.valueFx').valueFx();
		}
		// Safe Mailto
		if ($('a.email').length > 0) {
			$('a.email').mailto();
		}
		// Gallery Viewer (KPL)
		if ($('#gallery-nav').length > 0) {
			// Thumbnail click events
			$('#gallery-nav li a').live('click',function(){
				$('#gallery-nav li').removeClass('active');
				$(this).parent().addClass('active');
				$('#gallery-nav li p').hide();
				$(this).parent().find('p').show();
				var link = $(this).attr('href');
				var title = $(this).attr('title');
				// Check if it's a video link
				if(title.indexOf('vimeo.com') >= 0) {
					$('#gallery-image-holder').hide();
					var clip_id = title.split('/');
					clip_id = clip_id.pop();
					var vimeoEmbed = 'http://vimeo.com/moogaloop.swf?clip_id='+clip_id+'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=CC9966&amp;fullscreen=0';
					$('#gallery-video-holder')
						.find('param[name="movie"]').val(vimeoEmbed).end()
						.find('embed').attr('src',vimeoEmbed).end()
						.show();
				} else { 
				// Not a video, just an image
					$('#gallery-image-holder').show().css("background-image", "url("+link+")"); 
				}
				return false;
			});
			// Trigger the first item in the list when the page loads
			$('#gallery-nav li:first a').trigger('click');
		}
	});
(jQuery);
