$(function() {

	$('a.rolloverimage').each(function() {
		
		$([$('img', this).attr('src')]).preload();
		
		if (!$(this).hasClass('active')) { return; };
		
		var imagePathInfo = $('img', this).attr('src').replace('_over', '').split('.');
		var imagePath = imagePathInfo.slice(0, -1).join('.');
		var imageExtension = imagePathInfo[imagePathInfo.length - 1];
		
		$('img', this).attr('src', imagePath + '_over' + '.' + imageExtension);
		
	});
	
	$('a.rolloverimage').mouseenter(function() {
		
		var imagePathInfo = $('img', this).attr('src').replace('_over', '').split('.');
		var imagePath = imagePathInfo.slice(0, -1).join('.');
		var imageExtension = imagePathInfo[imagePathInfo.length - 1];
	
		$('img', this).attr('src', imagePath + '_over' + '.' + imageExtension);
		
	}).mouseleave(function() {
		
		if ($(this).hasClass('active') || $(this).hasClass('over')) { return; };
		
		var imagePathInfo = $('img', this).attr('src').replace('_over', '').split('.');
		var imagePath = imagePathInfo.slice(0, -1).join('.');
		var imageExtension = imagePathInfo[imagePathInfo.length - 1];
	
		$('img', this).attr('src', imagePath + '.' + imageExtension);
		
	});
	
	initPopovers();
	
	$('#video').flash({
		swf: 'swfs/videoplayer.swf',
		wmode: 'transparent',
		height: 240,
		flashvars: {
			videoContentPath: '../videos/intro.mp4',
			autoPlay: true,
			repeatPlay: true,
			videoClickAction: 'playDemoVideo()'
		}
	});
	
});

var basehref = $('base').attr('href');

function initCufon() {
	Cufon.replace('.cufon_neutra', { hover: true });
}
initCufon();

$.fn.preload = function() {
    this.each(function() {
        (new Image()).src = this;
    });
}

function initPopovers() {
	$('a.popover').fancybox({
		margin: 0,
		padding: 0,
		overlayColor: '#000',
		overlayOpacity: .4,
		onComplete : function() {
			initPopovers();
			initForm('#fancybox-outer');
		}
	});
}

function playDemoVideo() {
	$('#video').flash({
		swf: 'swfs/videoplayer.swf',
		wmode: 'transparent',
		height: 240,
		flashvars: {
			videoContentPath: '../videos/demo.mp4',
			videoStillContentPath: 'videos/demo.jpg',
			autoPlay: true
		}
	});
}

function loadBlogHeadlines(limit) {
	if (typeof(limit) == 'undefined') limit = 5;
	
	$.ajax({
        type: "GET",
		url: "blog.daviddeeble.com.rss.xml.php",
		dataType: "xml",
		success: function(xml) {
 			updateBlogHeadlines(xml, limit);
		}
	});
}

function updateBlogHeadlines(xml, limit) {
	var html = '';
	
	$(xml).find('item').each(function(i) {
		if(i >= limit) return false;
		
		var title = $(this).find('title').text();
		var description = $(this).find('description').text();
		var link = $(this).find('link').text();
		
		html += '<div class="daves_blog_postlistitem">';
		html += '<h4 class="daves_blog_postlistitem_title">' + title + '</h4>';
		html += description;
		html += ' &nbsp;<a href="' + link + '" target="_blank">read more</a>';
		html += '</div>';
		
		
	});
	
	$('#contenttabitem_daves_blog #daves_blog_postlist').html(html);
}
