/**
 * Clears a text form element when it has the style 'clear-default'
 */
function clickClear(clearClass) {
	var clearClass = (clearClass) ? clearClass : 'clear-default';
	if ($('input.' + clearClass).size() > 0) {
		$('input.' + clearClass).each(function(index) {
			this.defaultValue = $(this).val();
			$(this).click(function() {
				if ($(this).val() == this.defaultValue) {
					$(this).val('');
				};
			})
			.focus(function() {
				if ($(this).val() == this.defaultValue) {
					$(this).val('');
				};
			})
			.blur(function() {
				if ($(this).val() == "") {
					$(this).val(this.defaultValue);
				};
			});
		});
		
		// Clear out form defaults on submit
		$('form').submit(function(event) {
			$('input.' + clearClass).each(function(index) {
				if ($(this).val() == this.defaultValue) {
					$(this).val('');
				};
			});
		});
	};
}

/**
 * Builds pull quote divs assuming you've wrappted your content with a span with the class: pullquote-left or pullquote-right
 */
function buildPullQuote () {
	$('span.pullquote-left, span.pullquote-right').each(function(index) {
		var contents = $.trim($(this).html());
		var firstCharacterCode = contents.charCodeAt(0);
		if (firstCharacterCode < 65 || firstCharacterCode > 96) {
			contents = '&hellip; ' + contents;
		};
		
		var lastCharacter = contents.charAt(contents.length - 1);
		if ("?!.".search(lastCharacter) < 0) {
			contents = contents + ' &hellip;';
		};
		var $parent = $(this).parent();
		var $pullquote = $('<div>').attr('class', $(this).attr('class')).html(contents);
		$parent.before($pullquote);
	});
}

function loadVideo(vimeoid, title) {
	$('#featured_video').html('<object width="320" height="191"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="bgcolor" value="#000000" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' + vimeoid + '&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=0" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=' + vimeoid + '&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=0" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="320" height="191" bgcolor="#000000"></embed></object>');
	$('#featured_video_title').html(title);
	return false;
}

function loadProfileVideo(vimeoid, title) {
	$('.person-detail #person-media #videoplayer').html('<object width="240" height="200"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="bgcolor" value="#000000" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' + vimeoid + '&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=0" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=' + vimeoid + '&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=0" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="240" height="200" bgcolor="#000000"></embed></object>');
	$('.person-detail #person-media h3').html(title);
	return false;
}

function scrollPlaylist () {
	$("div.media-container").scrollable({
		items: '#playlist',
		next: '#next',
		prev: '#previous'
	});
}

$(document).ready(function() {
	clickClear();
	buildPullQuote();
	scrollPlaylist();
});