function InWinManager()
{
    var self            = this;
    var arrInWins       = new Array();
    var currentInWin    = null;
	var bToggleInWins   = false;
	var enabled         = true;
	
	this.init = function(bToggleInWins1){
		bToggleInWins = bToggleInWins1;
	}
	
    this.add = function(inWin)
    {
       arrInWins.push(inWin);	
	   inWin.init(self);
    }
    
    this.toggleInWin = function(inWin)
    {
        if (!enabled) return;
		if (inWin==currentInWin)
		{
			inWin.hide();
			currentInWin = null;
		}
		else 
		{
			if (bToggleInWins) self.hideCurrentInWin();
			inWin.show();
			currentInWin = inWin;
		}
    }
    
    this.hideCurrentInWin = function(){
		if (bToggleInWins)
		{
			if (currentInWin) self.toggleInWin(currentInWin);
		}    	
    }
	
	window.onresize = function(){
    	resetInWinPositions();
    }
    
    var resetInWinPositions = function(){
    	for (i=0; i<arrInWins.length; i++)
    	{
    		arrInWins[i].resetPosition();    			
    	}    	
    }
    
    this.remove = function()
	{
		for (var i=0; i<arrInWins.length; i++)
		{
			if (arrInWins[i]==currentInWin)
			{
				arrInWins.splice(i, 1);
			}
		}
	}
	
	this.enable = function()
	{
	    enabled = true;
	}
	
	this.disable = function()
	{
	    enabled = false;
	}
}

function inWin(context1, alignment, inWinType1, className1, inWinContentHolder1, arrInWinCloser1, toggleEvent1)
{
    var self                 =    this;
    var inWinId              =    YAHOO.util.Dom.generateId();
    var inWinManager         =    null;
    var context              =    context1 // Sets the element for the bubble to align to and the click event to stick to.
    if (typeof context1=="string") context = YAHOO.util.Dom.get(context1);
    var inWinAlign           =    alignment[0];
    var contextAlign         =    alignment[1];
    var inWinType            =    inWinType1;
    var className            =    className1;
    var inWinContentHolder   =    inWinContentHolder1;
    var arrInWinCloser       =    arrInWinCloser1;
    var toggleEvent          =    "click";
    if (toggleEvent1) toggleEvent = toggleEvent1;
    this.visible             =    false;
      
    this.init = function(inWinManager1)
    {
        
		inWinManager = inWinManager1; // Reference to inWinManager in order to fire the toggle method

    	setInWinType();    	
    	
        self.myOverlay = new YAHOO.widget.Overlay
        (
            inWinId, 
            {
                context:[context, inWinAlign, contextAlign],
                visible: false,
                monitorresize: false,
				constraintoviewport: true,
				iframe: false/*,
                effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25}*/
            }
        );
               
		self.myOverlay.setHeader(""); 
		self.myOverlay.setBody(getInWinContent()); 
		self.myOverlay.setFooter("");
		YAHOO.util.Event.addListener(window, "load", function()
			{
				self.myOverlay.render(document.body);		
				YAHOO.util.Dom.setStyle(inWinId, "position", "absolute"); //In order for the inWin's not to take up space inside the browser

				if (isBrowserIE())
				{
					context.attachEvent("on"+toggleEvent, self.toggle);
				}
				else
				{
					context.addEventListener(toggleEvent, self.toggle, false);
				}
				
				if (arrInWinCloser.length!=0)
				{
					for (a=0; a<arrInWinCloser.length; a++)
					{
						YAHOO.util.Event.addListener(arrInWinCloser[a], "click", self.toggle, 1, true);
					}		
				}
			}
		)	
    }
    
    this.toggle= function(e){   
    	self.resetPosition();
		inWinManager.toggleInWin(self);
		YAHOO.util.Event.stopEvent(e);
    }
    
    this.hide = function(){
    	self.myOverlay.hide();
    }
    
    this.show = function(){
    	self.myOverlay.show();
    }
    
    this.resetPosition = function(){
    	self.myOverlay.cfg.setProperty("context", [context, inWinAlign, contextAlign]);
    }
      
    function setInWinType(){
   		YAHOO.widget.Module.CSS_MODULE  = "";
        YAHOO.widget.Overlay.CSS_OVERLAY = "";
    	YAHOO.widget.Module.CSS_HEADER  = "";
		YAHOO.widget.Module.CSS_BODY    = "";
		YAHOO.widget.Module.CSS_FOOTER  = "";
    	switch(inWinType){
    		case "bubble":
    			YAHOO.widget.Module.CSS_MODULE = "bubble";
      			YAHOO.widget.Overlay.CSS_OVERLAY = className;
        		YAHOO.widget.Module.CSS_HEADER = "bubble_top";
        		YAHOO.widget.Module.CSS_BODY = "bubble_content clearfix";
        		YAHOO.widget.Module.CSS_FOOTER = "bubble_bottom";
        		break;
        	case "complete_bubble":
        		YAHOO.widget.Module.CSS_MODULE = "bubble";
        		YAHOO.widget.Overlay.CSS_OVERLAY = className;
        		break;
        	default:
        		YAHOO.widget.Overlay.CSS_OVERLAY = className;
    	}        
    }
    
    function getInWinContent(){
    	inWinContentHolder = YAHOO.util.Dom.get(inWinContentHolder);
    	inWinContent = inWinContentHolder.innerHTML;
    	inWinContentHolder.innerHTML = "";
    	return inWinContent; 
    }
}
