/**
 * @author jim
 */

//
// Showable
// showOn(), showOff() lockOn() lockOff() unlock()
//
function EmmShowable(element)
{
	this.element = element;
	this.showCount = 0;
	$(this.element).hide();
	this.bLocked = false;		
	
	this.showOn = function()
	{
		if (!this.bLocked)
		{
			
			if (this.radioGroup != undefined)
			{
		    	jQuery.each(this.radioGroup, function()
		    	    	{
		    	    		this.EmmShowable.showOff();
		    	    	});					
			}
			
			this.showCount++;
			$(this.element).show();
		}
	}	
	
	this.showOff = function()
	{
		if (!this.bLocked)
		{
			this.showCount--;
			if (this.showCount <=0)
			{
				this.showCount = 0;
				$(this.element).hide();
			}
		}
	}	

	this.lockOn = function()
	{
		this.showOn();		
		this.bLocked = true;			
	}

	this.lockOff = function()
	{
		this.showOff();	
		this.bLocked = true;			
	}

	this.unlock = function()
	{
		this.bLocked = false;
	}
}


//
// EmmHoverTrigger
//
function EmmHoverTrigger(element)
{
	this.element = element;
    $(this.element).bind("mouseenter", {obj: this}, function(e) { e.data.obj.hoverOn(); });
    $(this.element).bind("mouseleave", {obj: this}, function(e) { e.data.obj.hoverOff(); });    	
    
	this.popOns = [];
	this.popOffs = [];

    
    this.hoverOn = function()
    {
    	//this.showOn();
    	
    	jQuery.each(this.popOns, function()
    	{
    		this.EmmShowable.showOn();
    	});	
    }

    this.hoverOff = function()
    {
    	//this.showOff();
    	
    	jQuery.each(this.popOffs, function()
    	{
    		this.EmmShowable.showOff();
    	});	
    }    
    
    this.addPopOn = function(popupElem)
    {	
    	this.popOns.push(popupElem);		
    }    
    
}

//
//
//

function setHoverClasses(selector, offClass, onClass)
{
	$(selector).removeClass(onClass);
	$(selector).addClass(offClass);	
	
    $(selector).hover(
        function() {   // out    
			$(this).removeClass(offClass);					
			$(this).addClass(onClass);		
		},
		function() { // out
			$(this).removeClass(onClass);					
			$(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)
{
    $(selector).unbind('mouseover').unbind('mouseout');

	$(selector).removeClass(classToRemove);
	$(selector).addClass(classToLock);		
}
 
function hoverFirstShowSecond(triggerSel, targetSel)
{
	var trig = $(triggerSel);
	
	if (trig.EmmHoverTrigger == undefined)
		trig.EmmHoverTrigger = new EmmHoverTrigger(trig);

	$(targetSel).each( function() 
	{
		if (this.EmmShowable == undefined)		
			this.EmmShowable = new EmmShowable(this);
		trig.EmmHoverTrigger.addPopOn(this);
	});
}

function fakeHoverOn(triggerSel)
{
	$(triggerSel).each( function() 
	{
	    if (this.EmmHoverTrigger != undefined)
            this.EmmHoverTrigger.hoverOn();
    });
}

function showOnlyOneOfThese(selector)
{
	var elemList = [];
	
	$(selector).each( function() 
			{
				elemList.push(this); 
			});
	               
	$(selector).each( function() 
		{
			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 = $(selector);
	
	for (idx = 0; idx < elems.length; idx++)
	{
		e = elems[idx];
		h = $(e).height();
		if (h > maxH)
			maxH = h;		
	}
	
	for (idx = 0; idx < elems.length; idx++)
	{
		$(elems[idx]).height(maxH);
	}
}

//
// put this in the a href tag to open a new window rel="external"
//
function allowNewWindow()
{
    //$('a[rel=external]').attr('target','_blank');
    $('a.opennew').attr('target','_blank'); 
}

//
// I think this is part of the latest jQuery
//
 $.fn.delay = function( time, name ) {

    return this.queue( ( name || "fx" ), function() {
        var self = this;
        setTimeout(function() { $.dequeue(self); } , time );
    } );

};









