function stonestrong_swf_replace(video,index) {
	if($(video).children('span.url').html().indexOf('youtube.com')>0) {
		// Get the data ready
		var url = $(video).children('span.url').html().replace(/<!--.*URL" -->/,'').replace(/<!-- .*ditable -->/,'');
		var vid = url.replace(/http\:\/\/(www\.)?youtube\.com\/watch\?v\=/,'');
		
		// Prepare the markup
		$(video).children('span.url').remove();
		$(video).append('<div class="container" id="swfreplace_'+index+'" />');
		
		// Swfobject
		var location = 'http://www.youtube.com/v/'+vid+'&rel=0&border=0';
		var moviename = 'youtube_'+index;
		var object = 'swfreplace_'+index;
		var so = new SWFObject(location, moviename, "425", "373", "8", "#ffffff");
		so.addParam("wmode", "transparent");
		so.write(object);
	}
	else {
		// Skip, there's no YouTube URL here
		return false;
	}
}
function stonestrong_ready() {
	// Dealer login form
	$('form.dealerLogin').submit(function() {
		if($('#username').val()=='') {
			window.alert("Please enter your username.");
			return false;
		}
		if($('#password').val()=='') {
			window.alert("Please enter your password.");
			 return false;
		}
		else {
			return true;
		}
	});
	$('form.dealerLogin a.submit').click(function() {
		$(this).parents('form').trigger('submit');
		return false;
	});
	
	// Case Studies
	$('div.case_studies').attr('class','caseStudies');
	$('div.caseStudies div.photos')
		.append('<div class="arrows"><span class="left" /><span class="right" /></div>');
	$('div.caseStudies div.photos li.photo0').addClass('active');
	$('div.caseStudies div.photos span.left').addClass('inactive').click(function() {
		var currentImg = $(this).parents('div.section')
			.children('div.photos')
			.children('ul')
			.children('li.active');
		var currentPos = parseInt($(currentImg).attr('class').replace(/.*photo([0-9]+).*/,'$1'))
		// Are we already at the beginning?
		if(currentPos==0)
			return false;
		// If not, show the next image
		$(this).parent().children('span').removeClass('inactive');
		var prevPos = currentPos-1;
		if(prevPos>=0) {
			$(currentImg).removeClass('active');
			$(currentImg).parent().children('li.photo'+prevPos).addClass('active');
		}
		if(currentPos<=1)
			$(this).addClass('inactive');
		return false;
	});
	$('div.caseStudies div.photos span.right').click(function() {
		var currentImg = $(this).parents('div.section')
			.children('div.photos')
			.children('ul')
			.children('li.active');
		var currentPos = parseInt($(currentImg).attr('class').replace(/.*photo([0-9]+).*/,'$1'))
		var total = parseInt($(currentImg).parent().children('li').length);
		// Are we already at the end?
		if(currentPos==(total-1))
			return false;
		// If not, show the previous image
		$(this).parent().children('span').removeClass('inactive');
		var nextPos = currentPos+1;
		if(nextPos<total) {
			$(currentImg).removeClass('active');
			$(currentImg).parent().children('li.photo'+nextPos).addClass('active');
		}
		if(nextPos==(total-1))
			$(this).addClass('inactive');
		return false;
	});
	
	// Block Info
	//$('div.blockInfo:not(.patterns) div.section img').each(function(i) {
	//	var smlUrl = $(this).attr('src');
	//	var lrgUrl = smlUrl.replace('small','large');
	//	$(this).replaceWith('<a href="'+lrgUrl+'" class="thickbox"><img src="'+smlUrl+'" /></a>');
	//});
	
	// Video Center
	if($('div.videos').children('div.video').length>0) {
		$('div.videos div.video').each(function(i) {
			stonestrong_swf_replace($(this),i);
		});
	}
	
	// External links
	$("a[@href^=http]").not("[@href*='stonestrong.fbx.uppercaseclients.com/']").bind("click", function(){
		return !window.open(this.href);
	});
}

$(document).ready(function() {
	stonestrong_ready();
	
	// Look for incomplete items.
	if ($('.incomplete').length > 0) {
		$('body').prepend('<div id="incomplete">' + $('.incomplete').length + ' incomplete items on this page.');
		$('#incomplete').append($('<span>Highlight</span>').click(function(){$('.incomplete').addClass('incomplete-highlight')}));
		$('#incomplete').append($('<span>Unhighlight</span>').click(function(){$('.incomplete-highlight').removeClass('incomplete-highlight')}));
		$('#incomplete').append($('<span>Close</span>').click(function(){$('.incomplete-highlight').removeClass('incomplete-highlight');$('#incomplete').remove()}));
	}
});