$(function() {

	$(window).resize(function() { updateBackgroundContainer(); jiggerSiteLayout(); });
	$('#backgroundcontainer img').imagesLoaded(function() { updateBackgroundContainer(); jiggerSiteLayout(); });
	
	initWelcomeList();
	initPopovers();
	
	$('img.roundedcorners').photoCorners();
	
	if ($('#drilldown_right').length > 0) initRightDrillDown();
	
	$('a.rolloverimage').mouseenter(function() {
		
		var imagePathInfo = $('img:first', this).attr('src').replace('_over', '').split('.');
		var imagePath = imagePathInfo[0];
		var imageExtension = imagePathInfo[1];
	
		$('img:first', this).attr('src', imagePath + '_over' + '.' + imageExtension);
		
	}).mouseleave(function() {
		
		var imagePathInfo = $('img:first', this).attr('src').replace('_over', '').split('.');
		var imagePath = imagePathInfo[0];
		var imageExtension = imagePathInfo[1];
	
		$('img:first', this).attr('src', imagePath + '.' + imageExtension);
		
	});
	
});

var basehref = $('base').attr('href');
 
var addthis_config = { ui_click: true }

function updateBackgroundContainer() {
	var siteHeight = $('#maincontainer').height() + $('#topbar').height() + $('#footer').height();
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();
	
	$('#backgroundcontainer').show();
	
	$('#backgroundcontainer').height(windowHeight);
	$('#backgroundcontainer').width(windowWidth);
	
	$('#backgroundcontainer img').removeAttr('width').attr('height', windowHeight);
	if ($('#backgroundcontainer img').width() < windowWidth) $('#backgroundcontainer img').removeAttr('height').attr('width', windowWidth);
}

function jiggerSiteLayout() {
// 	var siteHeight = $('#maincontainer').height() + $('#topbar').height() + $('#footer').height();
// 	var windowHeight = $(window).height();
// 	var windowWidth = $(window).width();
// 	var windowSiteHeightDifference = windowHeight - siteHeight;
// 	
// 	if (siteHeight < windowHeight) {
// 		$('#maincontainer').height($('#maincontainer').height() + (windowSiteHeightDifference - 36));
// 	}
	
	if ($('#bikelist').length <= 0) { return; }
	
	var sitePageContentOffset = $('#sitepagecontent').offset();
	var sitePageContentHeight = $('#sitepagecontent').height();
	var sitePageContentBottom = sitePageContentOffset.top + sitePageContentHeight;
	
	var rightColumnOffset = $('#subpagecontent>.rightcolumncontainer').offset();
	var rightColumnHeight = $('#subpagecontent>.rightcolumncontainer').height();
	var rightColumnBottom = rightColumnOffset.top + rightColumnHeight;
	
	if (sitePageContentBottom < rightColumnBottom) $('#sitepagecontent').height(sitePageContentHeight + (rightColumnBottom - sitePageContentBottom) + 15);
}

function initWelcomeList() {
	$('#welcomelist').mouseenter(function() {
		$('ul', this).fadeIn(0);
	}).mouseleave(function() {
		$('ul', this).fadeOut(0);
	});
	
	$('#welcomelist li a').mouseenter(function() {
		
		var imagePathInfo = $('img', this).attr('src').replace('_over', '').split('.');
		var imagePath = imagePathInfo[0];
		var imageExtension = imagePathInfo[1];
	
		$('img', this).attr('src', imagePath + '_over' + '.' + imageExtension);
		
	}).mouseleave(function() {
		
		var imagePathInfo = $('img', this).attr('src').replace('_over', '').split('.');
		var imagePath = imagePathInfo[0];
		var imageExtension = imagePathInfo[1];
	
		$('img', this).attr('src', imagePath + '.' + imageExtension);
		
	});
	
	$('#welcomelist li a').click(function() {
	
		var imageName = $('img', this).attr('name').replace('w_', '');
		
		$('#welcomelist>img').attr('src', 'images/welcome/'+imageName+'.gif');
		
	});
}

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

function initRightDrillDown() {
	$('#drilldown_right .drilldown_item .drilldown_item_header').each(function() {
	
		if ($('a', this).hasClass('disabled')) { return; }
	
		if ($('a', this).hasClass('active')) $(this).siblings('ul').toggle();
		
		$('a', this).click(function(event) { event.preventDefault(); });
	
		$(this).click(function(event) {
			var thisClickX = event.clientX - $(this).offset().left;
			
			if (thisClickX > 110) {
				$('a', this).toggleClass('active');
				$(this).siblings('ul').toggle();
			} else {
				window.location.href = basehref + $('a', this).attr('href');
			}
			
		});
		
	});
}
