// MM_Disclose.js
//
// Expanding div function. Controls a disclosable div block.
//
// Copyright (C) 2007 Mixtur Interactive, Inc. All rights reserved.
// http://www.mixtur.com, 1-866-268-3994. Unlicensed use is prohibited.
//
// Mark Eissler, mark@mixtur.com
//
// $Id: MM_Disclose.js,v 1.1 2010/03/16 01:25:41 mark Exp $
//

/*
 IMPORTANT:  This Mixtur software is supplied to you by Mixtur Interactive, Inc. ("Mixtur")
 in consideration of your agreement to the following terms, and your use, installation, 
 or modification of this Mixtur software constitutes acceptance of these terms.  If you do
 not agree with these terms, please do not use, install, or modify this Mixtur software.
 
 In consideration of your agreement to abide by the following terms, and subject to these 
 terms, Mixtur grants you a personal, non-exclusive license, under MixturÕs copyrights in 
 this original Mixtur software (the "Mixtur Software"), to use, reproduce and modify the
 Mixtur Software.
 
 IMPORTANT:  THIS AGREEMENT DOES NOT PROVIDE FOR NOR DOES IT ALLOW REDISTRIBUTION OF THIS
 MIXTUR SOFTWARE. YOU MAY NOT REDISTRIBUTE THIS SOFTWARE WITH OR WITHOUT MODIFICATIONS.

 Neither the name, trademarks, service marks or logos of Mixtur Interactive, Inc. may be 
 used to endorse or promote products derived from the Mixtur Software without specific 
 prior written permission from Mixtur. Except as expressly stated in this notice, no other
 rights or licenses, express or implied, are granted by Mixtur herein, including but not
 limited to any patent rights that may be infringed by your derivative works or by other
 works in which the Mixtur Software may be incorporated.
 
 The Mixtur Software is provided by Mixtur on an "AS IS" basis.  MIXTUR MAKES NO WARRANTIES, 
 EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, 
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE MIXTUR SOFTWARE OR ITS 
 USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
 
 IN NO EVENT SHALL MIXTUR BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL 
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 
 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, 
 REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE MIXTUR SOFTWARE, HOWEVER CAUSED AND 
 WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR 
 OTHERWISE, EVEN IF MIXTUR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


//
// USAGE INSTRUCTIONS
//
// dclose icon
// -----------
// This class requires two icons: dclose_on.png and dclose_off.png. They are
// used to indicate the state of the disclosable div block.
//
// The default dclose icon dimensions are 12px by 12px. If your icons have a
// different dimension (that is, if dclose_on.png and dclose_off.png don't have
// a 12x12 dimension) then you need to call mmjs_dcloseItemWithIconDimensions()
// explicitly instead of mmjs_dcloseItem(), which will use the 12x12 default.
//
//
// resimages path
// --------------
// This class requires that you call the init() method before calling anything
// else (in this class) so that path information is setup (for finding resimages,
// resolving urls, etc.). In the code example below, we simply call the helper
// functions from MM_Util.js:
//
//  findRootUrl()
//  findImagesUrl()
//
// To use those, you must have the correct information setup in MM_Util.js.
//
//
// how to use this class
// ---------------------
// Naturally, you need to load the class in the HEAD section of your page. After
// that, you can create an disclosable control like this:
//
// <script type="text/javascript">
//      mmjs_dcloseInit(findRootUrl(), findImagesUrl());
//      mmjs_dcloseAdd("01", "company/about/staff--board-of-directors.html#01");
//      mmjs_dclosePlace("01");
// </script>
//
// Where "01" is the id attribute assigned to the id attribute of the dclose DIV
// tag AND is also the base of the id attribute for the IMG tag (which becomes
// 'img_ID'--the 'img' part is appended to the id attribute's value).
// 
// The URL ('company/about...') is tricky. It must resolve to the same page and
// then just add an anchor (#) to the item. With Joomla (or any CMS) this can be
// confusing because the browser will resolve unresolved links by simply adding
// the docroot. You need the absolute URL.
//
// NOTE: You only call the init() method once. This is done to set the object's
// path information.
//


// global vars
var dclose_array = new Array();	// a global array of disclosable_items

// location of the root folder relative to the page that invokes this
// script.
//
// Note: this var is set with mmjs_dcloseInit().
var path_root;

// location of images relative to the page that invokes this script
//
// Note: this var is set with mmjs_dcloseInit().
var path_img;

// defaults
var def_iconW = 12;
var def_iconH = 12;

// mmjs_toggleDisclose()
//
// Toggle the discloseable container. Swap controller images too.
//
function mmjs_dcloseToggle(id) { 
	
	// MOZ, SAF, IE6
    if(document.getElementById) {
		if (document.getElementById(id).style.display == 'none') {
			document.getElementById(id).style.display = 'block';
			_swapOn(id);			
		} else {
			_swapOff(id);
			document.getElementById(id).style.display = 'none';			
		}
		
        return;		
	}
	
	// MSIE
    if (document.all.id.style.visibility == "none"){
        document.all.id.style.display = 'block';
    } else {
        _swapOff(id);
        document.all.id.style.display = 'none';
    }
    
    // legacy netscape
    if (document.layers) {	
        if (document.id.display == "none"){
            document.id.display = 'block';
            _swapOn(id);
        } else {
            _swapOff(id);
            document.id.display = 'none';
        }
        
        return; 
    }
    
    
    return;
}


// mmjs_dcloseInit()
//
// Initialize the path_img variable so that it points at the url for images
// relative to the page that the function was called from.
//
// Note: This function must be called before any other function in here.
//
function mmjs_dcloseInit(root_path, image_path) {

	// set the root folder
	path_root = root_path;
	
	// set the path
	path_img = image_path;
	
	return;
}


// mmjs_dcloseItem
//
// Create an an object with the on and off graphics, url (link) and other
// stuff needed to support swapping src graphic manipulations.
//
// This method assumes the default dimensions (12px by 12px) will be used for
// the dclose icon.
//
function mmjs_dcloseItem(id, link) {

    return mmjs_dcloseItemWithIconDimensions(id, link, def_iconW, def_iconH);
}

// mmjs_dcloseItemWithIconDimensions
//
// Create an an object with the on and off graphics, url (link) and other
// stuff needed to support swapping src graphic manipulations.
//
// This method explicitly sets the dimensions of the icon that is swapped
// when the div is disclosure is toggled.
//
function mmjs_dcloseItemWithIconDimensions(id, link, iconW, iconH) {

	this.divId = id;
	this.imgId = 'img_'+id;     // icon's img block id attribute
	this.iLink = link;
	this.iH = iconH;           // dclose icon height in pixels
	this.iW = iconW;           // dclose icon width in pixels
	this.iOn = new Image(iconW, iconH);
	this.iOff = new Image(iconW, iconH);
	
	this.iOn.src = eval('"' + path_img + 'dclose_on.png"');
	this.iOff.src = eval('"' + path_img + 'dclose_off.png"');
}


// mmjs_dcloseAdd()
//
// Create a disclosable item for the parameters specified. Then add the item
// to the dclose_array.
//
// You would call this function after dcloseInit(). Then you would place the
// item later on in the page using the hotSpotPlace() function and specifying 
// whatever you passed for the "name" parameter.
//
// This method assumes the default dimensions (12px by 12px) will be used for
// the dclose icon.
//
function mmjs_dcloseAdd(id, link) {

    return mmjs_dcloseAddWithIconDimensions(id, link, def_iconW, def_iconH);
}


// mmjs_dcloseAddWithIconDimensions()
//
// Create a disclosable item for the parameters specified. Then add the item
// to the dclose_array.
//
// You would call this function after dcloseInit(). Then you would place the
// item later on in the page using the hotSpotPlace() function and specifying 
// whatever you passed for the "name" parameter.
//
// This method explicitly sets the dimensions of the icon that is swapped
// when the div is disclosure is toggled.
//
function mmjs_dcloseAddWithIconDimensions(id, link, iconW, iconH) {

	// make a new dcloseItem
	var theDcloseItem = new mmjs_dcloseItemWithIconDimensions(id, link, iconW, iconH);

    // add it to the hs_array
    dclose_array.push(theDcloseItem);
    
    return;
}


// mmjs_dclosePlace()
//
// This function places a dclose graphic that has been preloaded into 
// the dclose_array (disclosable array) defined in the global variables.
//
// Note: the dcloseMake() function is more flexible as we don't have to
// predefine an array in here.
//
function mmjs_dclosePlace(id) {

	for (var i in dclose_array) {
		if(dclose_array[i].divId == id) {
		
			// 
			// write out the anchor tag
			//
			
			// write the onClick part
			document.write('<a href="' + dclose_array[i].iLink + '" onClick="mmjs_dcloseToggle(\'' + dclose_array[i].divId + '\'); return true;" >');
			
			// write the image tag
			document.write('<img class="disclose_box_button" src="' + dclose_array[i].iOff.src + '" id="'+dclose_array[i].imgId+'"></a>');

		}	
	}

	return;
}


// mmjs_dcloseMake
//
// Create a new disclosable item and insert all of the code into the page.
// This just saves us the hassle of having to write out all of the code by
// hand.
//
// Whenever a call is made to this function, the on and off images will
// be preloaded.
//
function mmjs_dcloseMake(id, link, h, w) {

	// make a new hotSpot_item
	var theDcloseItem = new mmjs_dcloseItem(id, link, h, w);
	
	
	// 
	// write out the anchor tag
	//
	
    // write the onClick part
    document.write('<a href="' + theDcloseItem.iLink + '" onClick="mmjs_dcloseToggle(\'' + theDcloseItem.divId + '\'); return true;" >');
	
	// write the image tag
	document.write('<img class="disclose_box_button" src="' + theDcloseItem.iOff.src + '" id="' + theDcloseItem.imgId + '"></a>');
	
	return;
}


//
// PRIVATE METHODS
//

// _swapOn()
//
// Swap the "on" image into the name image's src.
//
function _swapOn(id) {

	// find the correct image in the dclose_array
	for (var i in dclose_array) {
		if(dclose_array[i].divId == id) {
            el = document.getElementById(dclose_array[i].imgId);
            el.src = dclose_array[i].iOn.src;
		}	
	}
}


// _swapOff()
//
// Swap the "off" image into the name image's src.
//
function _swapOff(id) {

	// find the correct image in the dclose_array
	for (var i in dclose_array) {
		if(dclose_array[i].divId == id) {
            el = document.getElementById(dclose_array[i].imgId);
            el.src = dclose_array[i].iOff.src;
		}	
	}
}






