$(function() {

	$(window).load(jiggerSiteLayout).resize(jiggerSiteLayout);
	$('#rightcolumncontainer img').imagesLoaded(jiggerSiteLayout);

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

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

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

function jiggerSiteLayout() {
	
	var contentContainer = $('#outercontainer').hasClass('home') ? $('#rightcolumncontainer') : $('#leftcolumncontainer');
	
	var windowHeight = $(window).height();
	var siteHeight = $('#outer').offset().top +  $('#outer').height() + $('#utilitynav').outerHeight();
	var contentContainerOffsetTop = contentContainer.offset().top;
	var contentContainerHeight = contentContainer.css('height', 'auto').height();
	var contentContainerFinalHeight;
	
	contentContainerFinalHeight = windowHeight > siteHeight ? windowHeight : siteHeight;
	
	if ((contentContainerFinalHeight - contentContainerOffsetTop) > contentContainerHeight) {
		contentContainer.height(contentContainerFinalHeight - contentContainerOffsetTop);
	}
	
	if ($('#mainwrapper').height() < 60) $('#mainwrapper').height(contentContainer.height() - 75);
	
}

function initPopovers() {
	$('a.fancybox, a.dailyspecials').fancybox({
		margin: 0,
		padding: 0,
		overlayColor: '#000',
		overlayOpacity: .4,
		onComplete : function() {
			initPopovers();
			console.log($('#popover_dailyspecial').width());
			if (typeof(_gaq) != 'undefined') _gaq.push(['_trackPageview']);
		}
	});
	
	$('a.youtubeplayer').fancybox({
		margin: 0,
		padding: 0,
		overlayColor: '#000',
		overlayOpacity: .4,
		showCloseButton: false,
		onComplete : function() {
			if (typeof(_gaq) != 'undefined') _gaq.push(['_trackPageview']);
		}
	});
}
