/**
 * @author jim
 */

/*
 * PopupTrigger - Generic trigger object for popups
 * 
 *  HTML: 
 *  	<div id="triggerDiv">dfsd sdfsdfs fsf </div>
 * 
 *  JS:
 * 		thisTrigger = new PopupTrigger('triggerDiv');
 * 
 *      or, if you want it to be "held on" by whatever it pops up (classes are just like in setHoverClass )
 *
 * 		thisTrigger = new PopupTrigger('triggerDiv', onClass, offClass); 
 *
 *		thisTrigger.lockOn(); // tells it to always use the "on" imagery
 *
 *      thisTrigger.addPopup(popupElem);  add something with show() and hide() methods
 *      
 *      or
 *      
 *      pu = new Popup('popUpId', thisTrigger);
 * 
 */
 
function PopupTrigger(elementId, offClass, hoverClass)
{
	this.element = $(elementId);		

	this.bLocked = false;
	
	this.popups = [];

    this.hoverClass = hoverClass;
    this.offClass = offClass;

    if (this.hoverClass)
        this.element.removeClass(this.hoverClass);

    if (this.offClass)
        this.element.addClass(this.offClass);
   
    this.element.bind("mouseenter", {obj: this}, function(e) { e.data.obj.hoverOn(); });
    this.element.bind("mouseleave", {obj: this}, function(e) { e.data.obj.hoverOff(); });    
    
	
}

PopupTrigger.prototype.lockOn = function()
{
	this.bLocked = false; // just to make sure
	this.showOn();		
	this.bLocked = true;
}

PopupTrigger.prototype.lockOff = function()
{
	this.bLocked = false; // just to make sure
	this.showOff();		
	this.bLocked = true;
}

PopupTrigger.prototype.hoverOn = function()
{
	this.showOn();
	
	jQuery.each(this.popups, function()
	{
		this.showOn();
	});	
}

PopupTrigger.prototype.hoverOff = function()
{
	this.showOff();
	
	jQuery.each(this.popups, function()
	{
		this.showOff();
	});	
}

PopupTrigger.prototype.showOn = function()
{
	if (this.bLocked == false)
	{
		if (this.offClass)
			this.element.removeClass(this.offClass);

		if (this.hoverClass)
			this.element.addClass(this.hoverClass);
	}
}

PopupTrigger.prototype.showOff = function()
{
	if (this.bLocked == false)
	{
		if (this.hoverClass)
			this.element.removeClass(this.hoverClass);

		if (this.offClass)
			this.element.addClass(this.offClass);
	}
		
}

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


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


/*
 *  SlaveImg class - an image that has other elements change its src, but remembers its default
 * 
 *  HTML: 
 *  	<img id="slavetest" src="images2/slavetest_1.gif" />
 * 
 *  JS:
 * 		slaveThing = new SlaveImg('hovertest');
 *  
 *  	slaveThing.setSrc('imageUrl');
 *   	slaveThing.reset(); // puts it back
 */

/*
var SlaveImg = new Class({
	
    initialize: function(elementId)
	{
		
		this.element = $(elementId);
		this.defaultImgSrc = this.element.getProperty('src');	
						
    },
	
	setSrc: function(srcUrl)
	{
		this.element.setProperty('src', srcUrl);	
	},

	reset: function()
	{
		this.element.setProperty('src', this.defaultImgSrc);
	}

});

*/

/*
 *  Popup class - a div (or other element) that is normall invisible, and that
 *  some other thing (a "Trigger") causes to become visible.
 * 
 *  HTML: 
 *  	<div id="popupDiv" >Stuff in here </div>
 * 
 *  JS:
 * 		popupThing = new Popup('popupDiv', trigger);
 *  
 *  	popupThing.showOn(); // turns it on
 *      popupThing.showOff(); // turns it off
 *      
 *      popupThing.addSlave(slaveImg, slaveSrcUrl);    Sets popupThing as trigger for the slaveImg    
 *      
 * 
 */
 
function Popup(elementId, trigger)
{
	this.slaves = [];		
	this.trigger = trigger;
	this.element = $(elementId);
	this.showCount = 0;
	//this.element.css('visibility', 'hidden');
	this.element.hide();
	this.bLocked = false;
	this.element.css('z-index', 6); // default popup index
	   
    this.element.bind("mouseenter", {obj: this}, function(e) { e.data.obj.hoverOn(); });
    this.element.bind("mouseleave", {obj: this}, function(e) { e.data.obj.hoverOff(); });  
    
	trigger.addPopup(this);	
}

Popup.prototype.hoverOn = function()
{
	if (!this.bLocked)
	{		
		this.showOn();				
		this.trigger.showOn();		
	
		for (idx in this.slaves)
		{
			var slavePair = this.slaves[idx];
			slavePair[0].setSrc(slavePair[1]);
		}			
	}

	this.bHovered = true;		
}

Popup.prototype.hoverOff = function()
{
	if (!this.bLocked)
	{		
		this.showOff();
	
		this.trigger.showOff()
	
		for (idx in this.slaves)
		{
			var slavePair = this.slaves[idx];
			slavePair[0].reset();
		}		
		
	}	

	this.bHovered = false;		
} 

Popup.prototype.lockOn = function()
{
	this.element.css('z-index', 3);		
	this.showOn();		
	this.bLocked = true;			
}

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

Popup.prototype.unlock = function()
{
	this.bLocked = false;
	
	this.element.css('z-index', 6);		
	
	if (this.bHovered)	
	{
		this.showOn();				
	}
	else
	{	
		this.showOff()
	}		
}

Popup.prototype.showOn = function()
{
	if (!this.bLocked)
	{
		this.showCount++;
		//this.element.css('visibility', 'visible');
		this.element.show();
	}
}

Popup.prototype.showOff = function()
{
	if (!this.bLocked)
	{
		this.showCount--;
		if (this.showCount <=0)
		{
			this.showCount = 0;
			//this.element.css('visibility', 'hidden');	
			this.element.hide();
		}
	}
}

Popup.prototype.addSlave = function(slaveElement, slaveUrl)
{
	var slavePair = [];
	slavePair[0] = slaveElement;
	slavePair[1] = slaveUrl;
	
	this.slaves.push(slavePair);		
}

 

/*
 * Not a class or anything, but all I need for now...
 */

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);		
}
 
/*
 * A class to handle menu-like attribure transistions on mouse entry and exit
 * 
 */
 /*
var BftMenu = new Class({
	
    initialize: function(contentElemSelector, onAttr, offAttr, timeMs){
		
        elems = $$(contentElemSelector);
		
		time = timeMs / 10;
		
		elems.each(function(element) {
 
 			element.setStyles(offAttr);
 
			var fx = new Fx.Styles(element, {duration: time, wait: false, transition:Fx.Transitions.Quad.easeInOut});
 
			element.addEvent('mouseenter', function(){
				fx.start(onAttr);
			});
 
			element.addEvent('mouseleave', function(){
				fx.start(offAttr);
			});
 
		});
	
	}
	
});
 */
 
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 "";
}

//
// Align the botom of an element to the bottom of it's direct parent
// probably not necessary
//
function alignToParentBottom( selector, bottomOffsetPix)
{
	$(selector).each( function()
	{
		$(this).css("position", "absolute")		
		$(this).css("bottom", bottomOffsetPix)
	});
}

function alignToParentBottomX( selector, bottomOffsetPix)
{
	$(selector).each( function()
	{
		var ph = $(this).parent().height();
		var newY = ph - $(this).height() - bottomOffsetPix;
		$(this).css("margin-top", newY+"px");
	});
}

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);
	}
	

}

//
// this is included so I can open new windows class=opennew
//
function allowNewWindow()
{
    // moo    $$('a.opennew').setProperties({ target: '_blank' });
    $('a[rel=external]').attr('target','_blank'); 
}





