//
// jq_emmlib
//
// stuff for emmgraphics using jquery
//

EmmGlobals = new Object;
EmmGlobals.version = 1.2;


//
// Showable
// showOn(), showOff() lockOn() lockOff() unlock()
//
function EmmShowable(element, animParms)
{
	this.element = element;
	this.showCount = 0;
	
	//jQuery(this.element).fadeTo(0,0).hide();
	jQuery(this.element).hide();
	
	this.bLocked = false;		
	
	jQuery(this.element).css("z-index", "6"); // so when shown its in front.	
	
	this.animParms = '';
	if (animParms != undefined)
		this.animParms = animParms;
	
	this.isShown = function()
	{		
		return (this.showCount > 0);
	}
	
	this.showOn = function(parms)
	{
		if (parms == undefined)
			parms = this.animParms;
			
		if (!this.bLocked)
		{
			
			if (this.radioGroup != undefined)
			{
		    	jQuery.each(this.radioGroup, function()
		    	{
		    		if (this.EmmShowable != undefined)  		    		
		    			this.EmmShowable.showOff();
		    	});					
			}
			
			this.showCount++;
		
			//jQuery(this.element).fadeTo(0,0); // seems like the fade out never quite "sticks"? So this is necessary?
			//jQuery(this.element).show().fadeTo(this.fadeMs, 1.0);
			jQuery(this.element).show(parms);
		}
	}	
	
	this.showOff = function(parms)
	{
		if (parms == undefined)
			parms = this.animParms;
		
		if (!this.bLocked)
		{
			this.showCount--;
			if (this.showCount <=0)
			{
				this.showCount = 0;
				
				//jQuery(this.element).fadeOut(this.fadeMs, 0.0, function() { this.hide() });
				jQuery(this.element).hide(parms);				
			}
		}
	}	

	this.lockOn = function()
	{
		this.showOn();		
		this.bLocked = true;			
	}

	this.lockOff = function()
	{
		this.showOff();	
		this.bLocked = true;			
	}

	this.unlock = function()
	{
		this.bLocked = false;
	}
}

//
// HoverImg (somethign that changes its img "src" when hovered
// In many cases you want something else to triggger it, tho (like
// in a menu, where you want the menu "container" to trigger a
// label image
//
function EmmHoverImage(element, hoverImg, trigger)
{
	if (trigger == undefined)
		trigger = element;
	
	this.element = element;
	this.defaultImgSrc = jQuery(this.element).attr('src');
	this.hoverImgSrc = hoverImg;
	this.bLocked = false;
	this.bHovered = false;
	this.hoverCnt = 0;			
			
	
	jQuery(trigger).bind("mouseenter", {obj: this}, function(e) { e.data.obj.hoverOn(); });
	jQuery(trigger).bind("mouseleave", {obj: this}, function(e) { e.data.obj.hoverOff(); }); 

    this.hoverOn = function() 
    {
    	if (!this.bLocked)
    	{
    		this.hoverCnt++;			
    		jQuery(this.element).attr('src', this.hoverImgSrc);				
    	}
    }
 
    this.hoverOff = function() 
    {
    	if (!this.bLocked)
    	{
    		this.hoverCnt--;
    		if (this.hoverCnt <=0)
    		{	
    			this.hoverCnt = 0;			
    			jQuery(this.element).attr('src', this.defaultImgSrc);
    		}			
    	}	
    }
 
    this.lockOn = function() 
    {
    	jQuery(this.element).attr('src', this.hoverImgSrc);
    	this.bLocked = true;
    }

    this.lockOff = function() 
    {
    	jQuery(this.element).attr('src', this.defaultImgSrc);
    	this.bLocked = true;	
    }
 
    this.lockImg = function() 
    {
    	jQuery(this.element).attr('src', imgSrc);
    	this.bLocked = true;	 
    }
 
    this.unlock = function() 
    {
    	this.bLocked = false;
		
    	if (this.bHovered)	
    		jQuery(this.element).attr('src', this.hoverImgSrc);
    	else	
    		jQuery(this.element).attr('src', this.defaultImgSrc);
    }

}


//
// EmmHoverTrigger
//
function EmmHoverTrigger(element)
{
	this.element = element;
    jQuery(this.element).bind("mouseenter", {obj: this}, function(e) { e.data.obj.hoverOn(); });
    jQuery(this.element).bind("mouseleave", {obj: this}, function(e) { e.data.obj.hoverOff(); });    	
    
	this.popOns = [];
	this.popOffs = [];
	this.sympatheticHovers = []; // elements to "hover" if this is hovered
	
	this.bIsHovered = false; // so cascading mouseenter()s don;t do anything bad
    
    this.hoverOn = function()
    {
    	if (this.bIsHovered == false)
    	{    	
    		jQuery.each( this.sympatheticHovers, function()
    		{
    			this.mouseenter(); 
    		});	    
    		
    		jQuery.each(this.popOns, function()
    		{
    			if (this.EmmShowable != undefined)
    			{
    				this.EmmShowable.showOn(); 				
    			}
    		});	
    		
    		
    		this.bIsHovered = true;
    	}
    }

    this.hoverOff = function()
    {
    	if (this.bIsHovered == true)
    	{ 
    	
	    	jQuery.each( this.popOffs, function()
	    	{
	    		if (this.EmmShowable != undefined)  
	    			this.EmmShowable.showOff();    				
	    	});	
	    	
	    	jQuery.each(this.sympatheticHovers, function()
	    	{
	    		this.mouseleave();    		
	    	});	     	
    	
    		this.bIsHovered = false;
    	}
    }    
    
    this.addPopOn = function(popupElem)
    {	
    	this.popOns.push(popupElem);		
    }    

    this.addPopOff = function(popupElem)
    {	
    	this.popOffs.push(popupElem);		
    }        
    
    // Mostly this is so menus can force their triggers to act 
    // hovered as long as the menu is hovered
    this.addSympatheticHover = function(hoverElem)
    {	
    	this.sympatheticHovers.push(hoverElem);		
    }          
}

//
// EmmClickTrigger. 
//
function EmmClickTrigger(element)
{
	this.element = element;
    jQuery(this.element).bind("click", {obj: this}, function(e) { e.data.obj.click(); });
 
	this.showOns = [];
	this.showToggles = [];
	
    this.click = function()
    {
        jQuery.each( this.showOns, function()
 		{
 			if (this.EmmShowable != undefined)
 			{
 				var emms = this.EmmShowable;
 				if (!emms.isShown())
 					emms.showOn();          				
 			}
 		});	
        
        jQuery.each( this.showToggles, function()
         		{
         			if (this.EmmShowable != undefined)
         			{
         				var emms = this.EmmShowable;
         				if (emms.isShown())
         					emms.showOff();   
         				else
         					emms.showOn();
         			}
         		});	
        
        
 	}
 
    this.addShowOn = function(elem)
    {	
    	this.showOns.push(elem);		
    }
    
    this.addShowToggle = function(elem)
    {	
    	this.showToggles.push(elem);		
    }    
}
       

//
//
//

function setHoverClasses(selector, offClass, onClass)
{
	jQuery(selector).removeClass(onClass);
	jQuery(selector).addClass(offClass);	
	
    jQuery(selector).hover(
        function() {   // out    
			jQuery(this).removeClass(offClass);					
			jQuery(this).addClass(onClass);		
		},
		function() { // out
			jQuery(this).removeClass(onClass);					
			jQuery(this).addClass(offClass);	
		}
	);	
	
}

// In case you want to cause a previously hovered thing to be locked at a given state
// Sorta assumes that there might be a class already applied which you have to get rid of
// classToLock is the class that I want it to have

function lockHoverClasses(selector, classToLock, classToRemove)
{
    jQuery(selector).unbind('mouseover').unbind('mouseout');

	jQuery(selector).removeClass(classToRemove);
	jQuery(selector).addClass(classToLock);		
}


function setHoverImage(selector, imageUrl, triggerSel)
{	
	var targ = jQuery(selector)[0];
	var trigger = targ;
	
	if (triggerSel != undefined)
		trigger =  jQuery(triggerSel)[0];
	
	if (targ.EmmHoverImage == undefined)
		targ.EmmHoverImage = new EmmHoverImage(targ, imageUrl, trigger);
}

// set bOn = 1 or 0. For locked/unlocked. Or leave it off for "locked"
function lockHoverImage(selector, bOn)
{
    var lockOn = 1;
	if (bOn != undefined)
		lockOn = bOn;     

	var elems = jQuery(selector);
	
	elems.each( function()
	{
		if (this.EmmHoverImage != undefined)
		{
			if (lockOn == 1)
				this.EmmHoverImage.lockOn();
			else
				this.EmmHoverImage.lockOff();
		}
    });
}


function setupMenu(menuSel, popupSel)
{
	var menu = jQuery(menuSel)[0];
	var popup = jQuery(popupSel)[0];	
	
	if (menu.EmmHoverTrigger == undefined)
		menu.EmmHoverTrigger = new EmmHoverTrigger(menu);
	
	if (popup.EmmShowable == undefined)		
		popup.EmmShowable = new EmmShowable(popup);	
	
	menu.EmmHoverTrigger.addPopOn(popup);
	menu.EmmHoverTrigger.addPopOff(popup);		

}

//use visiblity: none; to make things initially invisible
//<a href="#" onClick="showElement('#open1','slow');">
//<a href="#" onClick="hideElement('#open1');">
//
function showElement(selector, parms)
{
	var elems = jQuery(selector); 		
	elems.each( function()
	{
	    if (this.EmmShowable == undefined)
	    	this.EmmShowable = new EmmShowable(this);	
		
	    this.EmmShowable.showOn(parms);
	});  
}


function hideElement(selector, parms)
{
	var elems = jQuery(selector); // is we say jQuery(selector).each() then "this" is a jquery object
	elems.each( function()
	{
	    if (this.EmmShowable == undefined)
	    	this.EmmShowable = new EmmShowable(this);	
		
	    this.EmmShowable.showOff(parms);
	});   
}  


 
function hoverFirstShowSecond(triggerSel, targetSel)
{
	var trig = jQuery(triggerSel)[0];
	
	if (trig.EmmHoverTrigger == undefined)
		trig.EmmHoverTrigger = new EmmHoverTrigger(trig);

    var targ = jQuery(targetSel);
    targ.each( function()
    {
    	if (this.EmmShowable == undefined)		
    		this.EmmShowable = new EmmShowable(this);
	
    	trig.EmmHoverTrigger.addPopOn(this);
    	trig.EmmHoverTrigger.addPopOff(this);
    });
}

function fakeHoverOn(triggerSel)
{
	var elems = jQuery(triggerSel);
	
	elems.each( function() 
	{
	    if (this.EmmHoverTrigger != undefined)
            this.EmmHoverTrigger.hoverOn();
    });
}

function clickFirstShowSecond(triggerSel, targetSel)
{
	var trig = jQuery(triggerSel)[0];
	
	if (trig.EmmClickTrigger == undefined)
		trig.EmmClickTrigger = new EmmClickTrigger(trig);

    var targs = jQuery(targetSel);
    targs.each( function() 
    {
    	if (this.EmmShowable == undefined)		
    		this.EmmShowable = new EmmShowable(this);
    	trig.EmmClickTrigger.addShowOn(this);
    });
}


function showOnlyOneOfThese(selector)
{
	var elemList = jQuery(selector); 
	               
	elemList.each( function() 
	{
	    if (this.EmmShowable == undefined)		
	    	this.EmmShowable = new EmmShowable(this);
		this.EmmShowable.radioGroup = elemList;
	});	
	
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  return "";
}


function matchHeights(selector)
{
	var idx;
	var maxH = 0;
	
	elems = jQuery(selector);
	
	for (idx = 0; idx < elems.length; idx++)
	{
		e = elems[idx];
		h = jQuery(e).height();
		if (h > maxH)
			maxH = h;		
	}
	
	for (idx = 0; idx < elems.length; idx++)
	{
		jQuery(elems[idx]).height(maxH);
	}
}

//
// Popup stuff and darken the rest of the page.
//
//

function makePopup(selector, timeMs)
{
	jQuery(selector).each( function() 
	{
		if (this.EmmShowable == undefined)		
			this.EmmShowable = new EmmShowable(this, timeMs);	
				
	});
	
	if (jQuery("#cover_page").length  == 0)
	{
	
		jQuery('body').append('<div id="cover_page"></div>');
	
		var coverclass = 
		{
			//'visibility' : 'hidden',
			'position' : 'fixed',
			'left' : '0px',
			'top' : '0px',
			'width' : '100%',
			'height' : '100%',
			'background' : '#000000',
			'z-index' : '2'
			}		
		
		EmmGlobals.coverDiv = jQuery('#cover_page').fadeTo(0,0).css(coverclass).hide();
		
		EmmGlobals.coverDiv.fadeMs = timeMs;	
		
	}
	
	EmmGlobals.EmmPopup = null;
	
		
		
}

function showPopup(selector)
{
	popup = jQuery(selector)[0];
	                          
	if (popup.EmmShowable)
	{
		popup.EmmShowable.showOn();
		EmmGlobals.EmmPopup = popup;
		
		// gray the screen
		EmmGlobals.coverDiv.show();		
		EmmGlobals.coverDiv.fadeTo(EmmGlobals.coverDiv.fadeMs, .5);
	}
		
}


function hidePopup()
{
	if (EmmGlobals.EmmPopup != null)
	{
		EmmGlobals.EmmPopup.EmmShowable.showOff();
		EmmGlobals.EmmPopup = null;
		EmmGlobals.coverDiv.fadeTo(EmmGlobals.coverDiv.fadeMs, 0, function() {		EmmGlobals.coverDiv.hide(); });
	}
}



//
// put this in the a href tag to open a new window rel="external"
//
function allowNewWindow()
{
    //jQuery('a[rel=external]').attr('target','_blank');
    jQuery('a.opennew').attr('target','_blank'); 
}

//
//Align the botom of an element to the bottom of it's direct parent
//probably not necessary
//
function alignToParentBottom( selector, bottomOffsetPix)
{
	jQuery(selector).each( function()
	{
		jQuery(this).css("position", "absolute")		
		jQuery(this).css("bottom", bottomOffsetPix)
	});
}
//
//put this in the a href tag to open a new window rel="external"
//
function allowNewWindow()
{
 //jQuery('a[rel=external]').attr('target','_blank');
 jQuery('a.opennew').attr('target','_blank'); 
}

//
// I think this is part of the latest jQuery
//
// jQuery.fn.delay = function( time, name ) {
//    return this.queue( ( name || "fx" ), function() {
//        var self = this;
//        setTimeout(function() { jQuery.dequeue(self); } , time );
//    } );
//};









