// Fix background image flickering in IE
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) { }

function IsThisBrowserIE6() {
    return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined))
}

var isIE6 = IsThisBrowserIE6();

var OverFlyout = false;


var SolutionsFlyout = {
	init: function() {
		$('ul#homepage_solutions li a.listlink').each(function(i) {
			$(this).hover(SolutionsFlyout.hoverIn, SolutionsFlyout.hoverOut);
		});
	},
	hoverIn: function(event) {
		if (!OverFlyout){
			$flyout = $(this).parents('li').find('div.flyout').show();
			$('#scrollhandle').hide();
			// Calculate top position to vertically center flyout on link height
			top_v = ($flyout.outerHeight() / 2) - ($(this).outerHeight() / 2) - 62;
			$flyout.css( { top: '-' + top_v + 'px' } );
		};
	},
	hoverOut: function(event) {
		if (!OverFlyout){
			$flyout = $(this).parents('li').find('div.flyout').hide();	
			$('#scrollhandle').show();
		};
	}
};
$(document).ready(SolutionsFlyout.init);

var QuickLinksFlyout = {
	init: function() {
		$('div#quicklinks_panel_home div a.quicklinkslink').each(function(i) {
			$(this).hover(QuickLinksFlyout.hoverIn, QuickLinksFlyout.hoverOut);
		});
	},
	hoverIn: function(event) {
		if (!OverFlyout){
			$flyout = $(this).parent('div').find('div.qk_flyout').show();
		};
	},
	hoverOut: function(event) {
		if (!OverFlyout){
			$flyout = $(this).parent('div').find('div.qk_flyout').hide();	
		};
	}
};
$(document).ready(QuickLinksFlyout.init);

var HoldFlyout = {
	init: function() {
		$('div.flyout').each(function(i) {
			var $div = $(this);
			$div.hover(HoldFlyout.hoverIn, HoldFlyout.hoverOut);
			
		});
		
		$('div.qk_flyout').each(function(i) {
			var $divqk = $(this);
			$divqk.hover(HoldFlyout.hoverIn, HoldFlyout.hoverOut);
			
		});
	},
	hoverIn: function(event) {
	//	$('#spotlightText p').text("HoldFlyout.hoverIn triggered");
		OverFlyout = true;
		$flyout = $(this).show();
		$('#scrollhandle').hide();

	},
	hoverOut: function(event) {
	//	$('#spotlightText p').text("HoldFlyout.hoverOut triggered");
		OverFlyout = false;
		$flyout = $(this).hide();	
		$('#scrollhandle').show();
	}
};
$(document).ready(HoldFlyout.init);

var spotlightHeight = 0;
var scrollRatio = 1;

var SpotlightScroll = {
	init: function() {
		spotlightHeight = $('#spotlightTextInner').height();
		var scrollPane = spotlightHeight - 140;
		scrollRatio = scrollPane/111;
//		alert('spotlightHeight = ' + spotlightHeight + ' px \n scroll ratio = ' + scrollRatio + '\n IE6? = ' + isIE6);
	},
	updateSpotlightScroll: function() {	
		var scrollPos = $('#scrollhandle').position();
		var scrollPanePos = $('#spotlightTextOuter').position();
//		var scrollHandleTop = scrollPos.top - 521;
		var scrollHandleTop = scrollPos.top - scrollPanePos.top;
		if ( isIE6 ) { scrollHandleTop = scrollHandleTop - 5; };
		var scrollto = scrollHandleTop * scrollRatio;
		$("#spotlightTextOuter").scrollTop(scrollto);
//	$('#debugging').text(" scrollHandleTop = " + scrollHandleTop + " scrollRatio = " + scrollRatio + " scrollto = " + scrollto);
	}
};

$(document).ready(SpotlightScroll.init);

$(function() {
//	var spotlightHeight = $('#spotlightTextInner').height();
//	var scrollRatio = spotlightHeight/111;
//	alert('spotlightHeight = ' + spotlightHeight + ' px \n scroll ratio = ' + scrollRatio);


	$("#scrollhandle").draggable({
		start: function() {
			//counts[0]++;
			//updateCounterStatus($start_counter,counts[0]);
		},
		drag: function() {
			// counts[1]++;
			SpotlightScroll.updateSpotlightScroll();
		},
		stop: function() {
			//counts[2]++;
			//updateCounterStatus($stop_counter,counts[2]);
		},
		containment: 'parent'
	});
});

/*
function updateSpotlightScroll() {	
	var scrollPos = $('#scrollhandle').position();
	var scrollHandleTop = scrollPos.top - 521;
	var scrollto = scrollHandleTop * scrollRatio;
	$('#spotlightTextOuter').animate({scrollTop: '+=' + scrollto + 'px'});
	//$('#topic_slider').animate({scrollTop: '+=' + scrollto + 'px'}, 500);
	$('#debugging').text(" scrollHandleTop = " + scrollHandleTop + " scrollRatio = " + scrollRatio + " scrollto = " + scrollto);
	//$('span.count',$event_counter).text(new_count);
}
*/

var ExternalLinks = {
	init: function() {
		$('a[rel="external"]').attr( "target", "_blank" );
	}
};

$(document).ready(ExternalLinks.init);
