//****************************************************************************
// CountryWindow code from http://jqueryfordesigners.com/demo/coda-bubble.html
//
// Assumes: div class of rolloverpopup: the div containing a single image and popup info
//          img class of trigger: the image that when moused over, invokes the popup
//          popup: the table containing the popup info.
//          
//****************************************************************************

function InitPopupWindows()
{
	// find the static div on the WTIA homepage
	/*var staticDivHeight = staticDiv.height();
	var staticDivWidth = staticDiv.width();
	var staticDivTop = staticDiv.offset().top;
	var staticDivLeft = staticDiv.offset().left;*/

// Look for rolloverpopups (divs)
$('.rolloverpopup').each(function () {
			
			// TIME needed to SHOW and HIDE the window. MUST BE at last 50 to avoid problem
			// of moving mouse in and out of region very fast causing effect not to appear.
			var time = 50;		
			
			var distance = 0;
            var hideDelay = 250;				// time before window disappears when mouse moves away

            var hideDelayTimer = null;

            var beingShown = false;		// in the process of animation?
            var shown = false;			// is currently being shown?
			
			// fetch the region this popup is for
			var rolloverindex = $(this).attr("rolloverindex");
			// fetch the area map trigger for this region
			var trigger = $(".rollovertrigger[rolloverindex='" + rolloverindex + "']");
            var info = $(this);					

			// hide the main div
			$(this).addClass("hidepopup");
			
			// with BOTH the rollovertrigger and popup objects, set the MOUSE OVER function
            $([trigger.get(0), info.get(0)]).mouseover(function () {
						
                if (hideDelayTimer) clearTimeout(hideDelayTimer);  // if timer active, kill it.
                
				// if we're currently being shown, just let that process continue
				if (beingShown || shown) {
                    return; // don't re-trigger the animation again
                } else {
                    // init: reset position of info box
                    beingShown = true;

					// SPECIAL FOR WTIA: Hide existing topnav div
					//$("div.toprightundercontainer").hide();
					
					// set the position of the popup box
                    info.css({
                        //top: 50,
                        //left: 53,
                        display: 'block'
						// now animate the popup to a position 'distance' away from initial position
                    }).animate({
                        top: '-=' + distance + 'px',
                        opacity: 1  
						// upon completion of animation, set vars.
                    }, time, 'linear', function() {
                        beingShown = false;
                        shown = true;
						
						// SPECIAL FOR WTIA: Hide existing topnav div
						$("div.toprightundercontainer").hide();

                    });
                }

                return false;  // return FALSE from the mouse over
				// with BOTH the trigger and popup objects, set the MOUSE OUT function
            }).mouseout(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer); // if timer active, kill it.
               	
				// wait 'hideDelay' then invoke the following function:
				 hideDelayTimer = setTimeout(function () {
                    hideDelayTimer = null;
					
					// now animate the popup to a position 'distance' away from initial position
                    info.animate({
                        top: '-=' + distance + 'px',
                        opacity: 0
						// upon completion of animation, set vars.
                    }, time, 'linear', function () {
                        shown = false;
						// turn off the display of the popup
                        info.css('display', 'none');
						
						// SPECIAL FOR WTIA: Hide existing topnav div
						$("div.toprightundercontainer").show();

                    });

                }, hideDelay); 

				// return FALSE from the mouse out
                return false;
            });
			
			// set the close window link to close the div via the mouseout function
			$("#closeregion",this).bind("click", function(){
				 //info.trigger("mouseout");
				 shown = false;
				 beingShown=false;
				 info.css('display', 'none');
				 info.addClass("hidepopup");
			});
        });


// Look for secondary trigger images 
$('img.imgtrigger').each(function () {
			
		// fetch the region this popup is for
		var rolloverindex = $(this).attr("rolloverindex");
		// fetch the area map trigger for this region
		var trigger = $(".rollovertrigger[rolloverindex='" + rolloverindex + "']");
			
		$(this).mouseover(function() {
					trigger.trigger('mouseover');
					return false;
								   }).mouseout(function () {
									trigger.trigger('mouseout');
									return false;  
								   });

							   });
}
