$(function() {
	// in the search form, treat the enter key like a mouse click
	$("#nav-search input").keypress(function(e){
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			$('input#btnSearch').click();
			return false;
		} else {
			return true;
		}
	});
	// submit search requests to fusionbot
	$("input#btnSearch").click(function(e){
		window.location = 'http://ss614.fusionbot.com/b/q?sn=155172918&k=' + $('#txtSearch').val();
		return false;
	});
	// open external links in a new window
	$("a[href^=http://]").attr("target","_blank");
	$("a[href^=http://blogs.popart.com/]").removeAttr("target");
	$("a[href^=http://www.popart.com/]").removeAttr("target");
});

function setSizeOnTheFly(objContainer) {

	var flyout = $(objContainer).children('div:first');

	var height = flyout.innerHeight();
	var newHeight = (height - 24) * -1;
	var width = flyout.innerWidth();
	var newWidth = (width - 24) * -1;

	var leftRight;
	var topBot;

	if($(objContainer).hasClass('top-left')) {
		leftRight = 'left';
		topBot = 'top';
	} else if($(objContainer).hasClass('top-right')) {
		leftRight = 'right';
		topBot = 'top';
	} else if($(objContainer).hasClass('bottom-left')) {
		leftRight = 'left';
		topBot = 'bottom';
	} else if($(objContainer).hasClass('bottom-right')) {
		leftRight = 'right';
		topBot = 'bottom';
	} else if($(objContainer).hasClass('blog')) {
		leftRight = 'left';
		topBot = 'top';
	}

	flyout.css(topBot, newHeight).css(leftRight, newWidth);
}
function resetSizeOnTheFly(objContainer) {

	var flyout = $(objContainer).children('div:first');

	var leftRight;
	var topBot;

	if($(objContainer).hasClass('top-left')) {
		leftRight = 'left';
		topBot = 'top';
	} else if($(objContainer).hasClass('top-right')) {
		leftRight = 'right';
		topBot = 'top';
	} else if($(objContainer).hasClass('bottom-left')) {
		leftRight = 'left';
		topBot = 'bottom';
	} else if($(objContainer).hasClass('bottom-right')) {
		leftRight = 'right';
		topBot = 'bottom';
	} else if($(objContainer).hasClass('blog')) {
		leftRight = 'left';
		topBot = 'top';
	}

	flyout.css(topBot, '-1px').css(leftRight, '-1px');
}
$(function(){
	$('.flyout-container a img')
		.mouseover(function() {
			$(this).parent().parent().children('div').toggleClass('hidden');
			setSizeOnTheFly($(this).parent().parent());
		})
		.mouseout(function() {
			$(this).parent().parent().children('div').toggleClass('hidden');
			resetSizeOnTheFly($(this).parent().parent());
		});

	$('.flyout')
		.mouseover(function() {
			$(this).toggleClass('hidden');
			setSizeOnTheFly($(this).parent());
		})
		.mouseout(function() {
			$(this).toggleClass('hidden');
			resetSizeOnTheFly($(this).parent());
		});

	$('.flyout-container').click(function() {
		if( $(this).children('a:first').attr('href').indexOf('http') < 0) 
			window.location.href = $(this).children('a:first').attr('href');
		else
			window.open($(this).children('a:first').attr('href'));
		return false;
	});
});