/**
 * @fileOverview This file contains all psBreadCrumbs code
 * @author <a href='http://wiki.element115.net/index.php/User:Schopra'>schopra</a> 
 */

/**
 * psBreadcrumbs
 * 
 * @namespace
 * @author schopra
 * @example
 * required HTML format (nested ULs): 
 * 
 * div.Navigation#bread_crumbs
 *   ul
 *     li.root
 *       a (home)
 *       ul
 *         li
 *           a (page one)
 *           ul
 *             li
 *               a (page two)
 *             -li
 *           -ul
 *         -li
 *       -ul
 *     -li
 *   -ul
 * -div
 * -------------------------------------
 * onload, execute:
 * 
 * psBreadcrumbs.register("bread_crumbs",{protect:[1,2,-2,-1]});
 * 
 * this will "protect" the 1st, 2nd, 2nd to last, and last links from 
 * being collapsed & expanded.
 */
var psBreadcrumbs = {
	duration: 0.4,
	defaultSelectorID: "bread_crumbs",
	characterWidthPx: 9,
	linkMinCharacters: 3,
	protectedLinks: [1], // be default the first link and last link are protected
	/**
	 * Interprets options, sets protected links, creates link objects, etc.
	 * @function
	 * @param {String} selectorID
	 * @param {Object} options
	 */
	register: function(selectorID,options){
		if(typeof selectorID ==="undefined"){
			selectorID = this.defaultSelectorID;
		}
		this.wrapper = $(selectorID);
		// wrapper must be set to block explicitly or IE6 will give 0 for .getWidth()!
		this.wrapper.setStyle({'display':'block'});
		this.limitWidth = (parseInt(this.wrapper.getWidth(),10) - parseInt(this.wrapper.getStyle('padding-left'),10) - parseInt(this.wrapper.getStyle('padding-right'),10));
		this.wrapper.setStyle({
			'overflow': "hidden",
			'width': this.limitWidth+"px"
		});
		
		this.linkMinWidth = (parseInt(this.linkMinCharacters,10)*parseInt(this.characterWidthPx,10));
		this.sumOfWidths = 0;
		this.numberOfLinks = this.wrapper.select("a").length;
		
		if(typeof options !== "undefined" && options.protect !== "undefined"){
			this.protectedLinks = options.protect;
		}
		
		for(var i=0;i<this.protectedLinks.length;i++){
			if(this.protectedLinks[i]<0){
				this.protectedLinks[i] = parseInt(this.protectedLinks[i],10) + parseInt(this.numberOfLinks,10); 
			}else{
				this.protectedLinks[i] = this.protectedLinks[i]-1;
			}
		}
		
		this.wrapper.select("a").each(function(element,iteration){
			$(element).setStyle({
				overflow: "hidden",
				cssFloat: "left"
			});
			$(element).up('li').setStyle({cssFloat: "left"});
			var thisDimensions = $(element).getDimensions();
			// calculating sumOfWidths before subtracting padding
			this.sumOfWidths = parseInt(this.sumOfWidths,10)+parseInt(thisDimensions.width,10);
			
			thisDimensions.width = parseInt(thisDimensions.width,10)-parseInt($(element).getStyle('padding-right'),10);
			
			this["link_"+iteration] = {
					el: $(element),
					text: element.innerHTML,
					normalHeight: thisDimensions.height,
					maxWidth: thisDimensions.width,
					protect: ($A(this.protectedLinks).indexOf(iteration)!=-1)?true:false,
							open: true
			};
			
			var localObj = this["link_"+iteration];
			
			
			$(element).setStyle({
				height: localObj.normalHeight+"px"
			});
			
			
			element.observe("mouseover",function(){
				//alert("mouseover");
				psBreadcrumbs.expand(localObj);
			});
			element.observe("mouseout",function(){
				//alert("mouseout");
				psBreadcrumbs.contract(localObj);
			});
		}.bind(this));
		
		//this["link_"+(this.numberOfLinks-1)].protect = true;
		
		// if sum of maxWidths is greater than this.limitWidth, must contract some links, start from
		// end and stop when sumOfWidths is smaller than limitWidth
		if(this.sumOfWidths>this.limitWidth){
			for(var j=(this.numberOfLinks-1);j>0;j--){
				if(!this["link_"+j].protect && this.sumOfWidths>this.limitWidth){
					this.contract(this["link_"+j],function(){
						this.updateSum();
					}.bind(this));
				}else{
					this["link_"+j].protect = true;
				}
			}
		}
		
		this.wrapper.down("ul").setStyle({
			cssFloat: "left",
			width: "10000px"
		});
		
	},
	/**
	 * updates this.sumOfWidths
	 * @function
	 */
	updateSum: function(){
		var j = 0;
		var theSum = 0;
		while(typeof this["link_"+j]!=="undefined"){
			var thisLinkWidth = (this["link_"+j].open)?parseInt(this["link_"+j].el.getWidth(),10):this.linkMinWidth;
			theSum = parseInt(theSum,10)+parseInt(thisLinkWidth,10);
			j++;
		}
		this.sumOfWidths = theSum;
	},
	/**
	 * Contracts the given link object (unless it's protected)
	 * @function
	 * @param {Object} obj a link object
	 */
	contract: function(obj,afterFinish){
		if(!obj.protect){
			new Effect.Morph(obj.el,{
				style: "width: "+this.linkMinWidth+"px",
				duration: this.duration,
				afterFinish: function(){
					if(typeof afterFinish!=="undefined"){
						afterFinish();
					}
				}
			});
			obj.open = false;
		}
	},
	/**
	 * Expands the given link object (unless it's protected)
	 * @function
	 * @param {Object} obj a link object
	 */
	expand: function(obj){
		if(!obj.protect){
			new Effect.Morph(obj.el,{
				style: "width: "+obj.maxWidth+"px",
				duration: this.duration
			});
			obj.open = true;
		}
	}
};

document.observe("dom:loaded",function(){
	//psBreadcrumbs.register("bread_crumbs",{protect:[1,2,-2,-1]});
});



