/**
 * @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 );
    } );

};

/*
 *  HoverImg class
 * 
 *  HTML: 
 *  	<img id="hovertest" src="images2/hovertest_1.gif" />
 * 
 *  JS:
 * 		hoverThing = new HoverImg('#hovertest', 'images2/hovertest_2.gif' );
 *  
 *  	hoverThing.lockOn();
 *  	hoverThing.lockOff();
 *  	hoverThing.unlock();  
 *  
 *      hoverThing.addSlave(slaveImg, slaveSrcUrl);    Sets hoverthing as trigger for the slaveImg
 *      
 *      hoverThing.addPopup(popupElem);  add something with showOn() and showOff() methods
 */


function HoverImg(elementId, hoverImg)
{
	this.element = $(elementId);
	this.defaultImgSrc = this.element.attr('src');
	this.hoverImgSrc = hoverImg;
	this.bLocked = false;
	this.bHovered = false;
	this.onCount = 0;			
	
	this.slaves = [];		
	this.popups = [];
				
    this.element.bind("mouseenter", {obj: this}, function(e) { e.data.obj.hoverOn(); });
    this.element.bind("mouseleave", {obj: this}, function(e) { e.data.obj.hoverOff(); });    		
			
}

 HoverImg.prototype.showOn = function() 
 {
	if (!this.bLocked)
	{
		this.onCount++;			
		this.element.attr('src', this.hoverImgSrc);				
	}
 }
 
 HoverImg.prototype.showOff = function() 
 {
	if (!this.bLocked)
	{
		this.onCount--;
		if (this.onCount <=0)
		{
			this.onCount = 0;			
			this.element.attr('src', this.defaultImgSrc);
		}			
	}	
 }
 
 HoverImg.prototype.hoverOn = function() 
 {
	this.showOn();
	
	jQuery.each(this.slaves, function()
	{
		this[0].setSrc(this[1]);
	});
			
	jQuery.each(this.popups,function()
	{
		this.showOn();
	});	
				
	this.bHovered = true;	 
 }
 
 HoverImg.prototype.hoverOff = function() 
 {
	this.showOff();

	jQuery.each(this.slaves, function()
	{
		this[0].reset();	
	});
		
	jQuery.each(this.popups, function()
	{
		this.showOff();
	});	

			
	this.isHovered = false; 
 }
 
 HoverImg.prototype.lockOn = function() 
 {
	this.element.attr('src', this.hoverImgSrc);
	this.bLocked = true;
 }

 HoverImg.prototype.lockOff = function() 
 {
	this.element.attr('src', this.defaultImgSrc);
	this.bLocked = true;	
 }
 
 HoverImg.prototype.lockImg = function() 
 {
	this.element.attr('src', imgSrc);
	this.bLocked = true;	 
 }
 
 HoverImg.prototype.unlock = function() 
 {
	this.bLocked = false;
		
	if (this.bHovered)	
		this.element.attr('src', this.hoverImgSrc);
	else	
		this.element.attr('src', this.defaultImgSrc);
 }
 
 HoverImg.prototype.addSlave = function(slaveElement, slaveUrl) 
 {
	var slavePair = [];
	slavePair[0] = slaveElement;
	slavePair[1] = slaveUrl;
		
	this.slaves.push(slavePair);	 
 }

 HoverImg.prototype.addPopup = function(popupElem) 
 {
		this.popups.push(popupElem);
 }
 









