$(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();
	
});

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

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

function initPopovers() {
	$('a.fancybox').fancybox({
		margin: 0,
		padding: 0,
		overlayColor: '#000',
		overlayOpacity: .4,
		onStart: function (items, index, opts) {
			if ($(items[index]).hasClass('preventDefault')) return false;
		},
		onComplete : function() {
			initPopovers();
		}
	});
}
