/**
 * @author Adam J. McIntyre - ajm@amodernfable.com
 */

PB = function(){
    var sliders = [];
    var sliderGroup = {};
    var totalAmount = 335;
    return{
        addPath : function(pathName) { 
            alert('adding path...') 
            var totalSliders = sliders.length;
            
            var el = document.getElementById('sliders');
           
            var h = '<div class="container_slider"><div class="leftCol col"><a href="#">' + pathName + '</a>' +
                '</div><div class="midCol col"><div id="bg_slider' + totalSliders + '" class="bar_off">' +
                '<img id="thumb_slider' + totalSliders + '" src="' + MEDIA_URL + '/media/images/icons/icon_sliderThumb.gif"></div>' +
                '<div id="bg_slider' + totalSliders + '_grow" class="bar grow">&nbsp;</div></div>' +
                '<div class="rightCol col"><a id="ex' + totalSliders + '" href="#learnMore" onclick="PB.expand(this.id);">' +
                'Learn More</a> <span class="pipe">|</span><a href="#edit">x</a></div><div class="clear">&nbsp;</div>' +
                '<div id="ex' + totalSliders + '_expand" class="expand">' + obj.getLearnMoreText(pathName) + '</div></div>';
                         
            el.innerHTML += h;    
            
            obj.addSlider(YAHOO.widget.Slider.getHorizSlider('bg_slider'+totalSliders,'thumb_slider'+totalSliders,0,335,10));
            obj.getSlider(totalSliders).setValue(162,true);
            obj.getSlider(totalSliders).subscribe('change',PB.setHiliteBg)                
        },
        addOtherPath : function(el){
            var obj = el;
            // Load up connection, etc.
            var callback = {
                success : function(o){
                    var anim = new YAHOO.util.ColorAnim(el.parentNode,{
                        backgroundColor : { to : '#DEFF3F' }
                    },
                    0);
                    var f = function(){
                        new YAHOO.util.ColorAnim(el.parentNode, {
                            backgroundColor : { to : '#EEE' }
                        },
                        0).animate();
                        var ps = obj.previousSibling;
                        if(ps.nodeType == 3) ps = ps.previousSibling;
                        
                        ps.style.display = 'none';
                        obj.style.display = 'none';
                    }
                    anim.onComplete.subscribe(f);
                    anim.animate();
                },
                failure : function(o){
                    alert("We've encountered while adding your path. Please try again.")
                },
                error : function(o){
                    alert("We've encountered while adding your path. Please try again.")
                }
            }  
            
            // YAHOO.util.Connect.asyncRequest('GET',pathurl + el.id,callback,null);
            callback.success();
        },
		getRealValue : function() { 
			return Math.round(this.getValue() * window.scale_factor); 
		},
        addSlider : function(sl) { sliders.push(sl); },
        addSliderGroup : function(n,sl,totalValue){
            if(sliderGroup[n]){
                sliderGroup[n].sliders.push(sl);
            }
            else{
                totalValue = totalValue || 100;
                sliderGroup[n] = { 'totalValue' : totalValue, 'currentValue' : 0, 'sliders' : []};
                sliderGroup[n].sliders.push(sl);
            }
        },
        sliderGroupCapValues : function(grp){
            var sGrp = sliderGroup[grp];
            var allSl = sliderGroup[grp].sliders;
            sGrp.currentValue = 0;
            
            for(var i = 0; i < allSl.length; i++){
                sGrp.currentValue += allSl[i].getValue();
            }
			//sGrp.currentValue = getSlidersSum()
            for(var i = 0; i < allSl.length; i++){
				constraint = sGrp.totalValue - (sGrp.currentValue - allSl[i].getValue());
				allSl[i].thumb.setXConstraint(0,constraint);
            }
        },
        sliderGroupAdjustMarker : function(grp){
            var pct = 100 - ((sliderGroup[grp].currentValue/sliderGroup[grp].totalValue) * 100);
            var el = document.getElementById(grp + "_meter_grow");
            var lbl = document.getElementById(grp + "_meter_label");
            
            //el.style.height = pct + '%';
            el.style.height = (65 * (pct/100)) + 'px';
            lbl.innerHTML = 100-Math.round(pct) + '% Full';        
        },        
        getLearnMoreText : function(pathName){
            // send a request to get 'learn more' info for this path
            alert('sending request for learn more...')
            return 'foo bar baz. lorem ipsum amit sit dolor.'
        },
        getSlider : function(idx) { return sliders[idx]; },
        getSliders : function() { return sliders },
        getValues : function(){ 
            values = [];
            for(var i = 0; i < sliders.length; i++){
                tmp = {};
                tmp.id = sliders[i].id;
                tmp.value = sliders[i].getValue();
                values.push(tmp);    
            }
            return values;
        },
		getSlidersSum : function() { 
			values = this.getValues()
			total = 0
			for(var i = 0; i < values.length; i++){
				total += values[i].value
			}
			return total
		},
		fillingBucketValid : function(group) { 
			
			total = 0
			desired_value = window.max_value
			
			for(var i = 0; i < sliders.length; i++) {
				if (sliders[i].sliderGroup == group) {
					total += sliders[i].getValue()
				}
			}
			
			if (total < desired_value) {
				return false;
			}
			else {
				return true;
			}
			
		},
		getGroups : function() { 
			
			groups = []
			for(var i = 0; i < sliders.length; i++){
				group = sliders[i].sliderGroup
				if (group && groups.indexOf(group) == -1) {
					groups.push(group)
				}
			}
		
			return groups
			
		},
		fillingBucketsValid : function() { 
			
			groups = this.getGroups()
		
			desired_value = groups.length * window.max_value
			total = this.getFillingSum()
			
			if (total < desired_value) {
				return false;
			}
			else {
				return true;
			}
			
		},
		getFillingSum : function() { 
			total = 0
			for(var i = 0; i < sliders.length; i++){
				if (sliders[i].sliderGroup) {
					total += sliders[i].getValue()
				}
			}
			return total
		},
        getValuePercent : function(val) {
            return Math.round((val / totalAmount) * 100);            
        },
        savePath : function() { alert('saving path...') },
        setHiliteBg : function(ch){
            var bgEl = this.id + '_grow';
            if(ch <= 3){
                YAHOO.util.Dom.setStyle(bgEl,'visibility','hidden');
            }
            else{
                YAHOO.util.Dom.setStyle(bgEl,'visibility','visible');
                YAHOO.util.Dom.setStyle(bgEl,'width',ch+1+'px');
            }
        },
        expand : function(elId) { 
            var exEl = document.getElementById(elId + '_expand');
            if(YAHOO.util.Dom.hasClass(exEl,'open')){
                new YAHOO.util.Anim(exEl, {
                    height : { to: 0 }
                    },
                    0,
                    YAHOO.util.Easing.easeIn
                ).animate();
                YAHOO.util.Dom.removeClass(exEl, 'open');
            }
            else{
                new YAHOO.util.Anim(exEl, {
                    height : { to: 30 }
                    },
                    0,
                    YAHOO.util.Easing.easeOut
                ).animate();                
                YAHOO.util.Dom.addClass(exEl, 'open');
            }
        },
        init : function(){
            obj = this;
            return obj;
        }    
    }
}().init();
