function youtube(url, size) {
	if(url === null){return '';}
	
	size = (size === null) ? "big" : size;
	var vid = url.replace(/^[^v]+v.(.{11}).*/,"$1");

	return (size == "small") ? "http://img.youtube.com/vi/"+vid+"/2.jpg" : "http://img.youtube.com/vi/"+vid+"/0.jpg";
}

jQuery(document).ready(function()
{			
	if ( $('#horses table').length > 0 && $('#horses table').attr('class') != 'no-filter' ) {
		$('#horses table').dataTable( { "oLanguage": { "sSearch": "Filter by keyword:" } });
	}
	
	$('A[rel="external"]').click( function() { window.open( $(this).attr('href') ); return false; });
	
	$('input[title]').each(function() {
		if( $(this).val() === '' && $(this).attr('attr') !== '' ) { $(this).val($(this).attr('title')); }		
		$(this).focus(function() { if($(this).val() == $(this).attr('title')) { $(this).val('').addClass('focused'); } });
		$(this).blur(function() { if($(this).val() === '') { $(this).val($(this).attr('title')).removeClass('focused'); } });
	});
	
	if ( $('#entryform').length > 0 ) {
		$('#entryform').validate({
			rules: {
				title: { required: true, maxlength: 19 },
				field_id_5: { required: true, maxlength: 19 },
				field_id_4: { required: true, maxlength: 19 },
				field_id_7: { required: true, maxlength: 19 }
			}
		});
	}
	
	$('#horse h4').click(function() { $(this).next().slideToggle("slow"); });
	
	if ( $('#media').length > 0 )
	{
		$('#media-display').addClass('js-enabled');
		$('#media').addClass('js-enabled');
		$('#media-display').append('<div id="thumbs"><div class="container"></div></div>');
		$('#media-display').append('<div id="viewport"></div>');
		
		$('#media .media:first').clone().appendTo("#viewport");
		
		var thumbs = new Array();
		
		$('#media .media').each(function(i)
		{
			thumbs[i] = ( $(this).attr('rel') == 'video' ) ? youtube($(this).find('span').attr('title'), 'small') : $(this).find('span').attr('title');
		});
		
		$.each(thumbs, function(i, val) {
			i++; // Increment counter to match :nth-child number
			$('#thumbs .container').append('<a href="#" id="'+i+'"><img src="'+val+'" width="70" height="50" alt="Thumbnail" /></a>');
		});
		
		$('#thumbs .container a').click(function() {
			$('#viewport').children().remove();
			$('#viewport').append($('#media .media:nth-child('+$(this).attr('id')+')').clone());
			return false;
		});
	}
	
	/*if ( $('#horses_dam').length > 0 )		{ $("#horses_dam").autosuggest({field_id: 'horses_dam', url: "/ajax/auto-suggest/title/"});				}
	if ( $('#horses_name').length > 0 )		{ $("#horses_name").autosuggest({field_id: 'horses_name', url: "/ajax/auto-suggest/horse/"});			}
	if ( $('#horses_sire').length > 0 )		{ $("#horses_sire").autosuggest({field_id: 'horses_sire', url: "/ajax/auto-suggest/sire/"});			}
	if ( $('#horses_damsire').length > 0 )	{ $("#horses_damsire").autosuggest({field_id: 'horses_damsire', url: "/ajax/auto-suggest/damsire/"});	}
	if ( $('#keywords').length > 0 )		{ $("#keywords").autosuggest({field_id: 'keywords', url: "/ajax/auto-suggest/"});	}*/
	
});
