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

var intvl;
var sliders = yuiQuery('div.slider_section div.bar_off');
var thumbs = yuiQuery('div.bar_off img:first-child');
//var growers = yuiQuery('div.bar.grow');

// Filling bucket questions
var count = 0;
for(var i = 0; i < sliders.length; i++){
	var slider = YAHOO.widget.Slider.getHorizSlider(sliders[i],thumbs[i],window.min_value,window.max_value,2)
	slider.animationDuration = 0
	PB.addSlider(slider);
    PB.addSliderGroup(sliders[i].getAttribute('group'),slider,window.max_value);
    slider.setValue(0,true);
    slider.sliderGroup = sliders[i].getAttribute('group');
    slider.subscribe('change',PB.setHiliteBg);
    slider.subscribe('change',function(){
		document.getElementById(this.id + '_score').innerHTML = Math.round(this.getValue()/2) + '%';
		listenerUpdate(this, Math.round(this.getValue()/2))
        PB.sliderGroupCapValues(this.sliderGroup);
        PB.sliderGroupAdjustMarker(this.sliderGroup);
		
    });
    
    count++;
}

// Scale questions
var txt_sliders = yuiQuery('div.slider_section div.bar_ticks');
var txt_thumbs = yuiQuery('div.bar_ticks img:first-child');
for(var i = 0; i < txt_sliders.length; i++){
   
	var txt_slider = YAHOO.widget.Slider.getHorizSlider(txt_sliders[i],txt_thumbs[i],100,100,5)
	PB.addSlider(txt_slider);
	txt_slider.subscribe('change',function(){
		listenerUpdate(this, Math.round(((this.getValue() + 100)/2)))
    });

}

var txtSzGroups = yuiQuery('div.col div.bar div.grow');
addTextSizes(txtSzGroups);

var txtBars = yuiQuery('div.col div.bar');
for(var i = 0; i < txtBars.length; i++){
    var el = txtBars[i];
    var sTag = el.getElementsByTagName('span')[0];
    var sz = parseInt(el.style.width,10);
    
    if(sz >= 84) YAHOO.util.Dom.addClass(sTag,'big');
    else if(sz < 84 && sz >= 16) YAHOO.util.Dom.addClass(sTag,'med');
    else if(sz < 16) YAHOO.util.Dom.addClass(sTag,'sm');

    YAHOO.util.Event.addListener(txtBars[i],'mouseover',function(){
        var o = this;
        clearInterval(intvl);
        intvl = setInterval(function(){
            var tt = document.getElementById('tooltip');
            var tt_body = document.getElementById('tt_body');
            tt_body.innerHTML = YAHOO.util.Dom.getElementsByClassName('ttData','div',o)[0].innerHTML;
            
            var x = YAHOO.util.Dom.getX(o) + o.offsetWidth + 20;
            var y = YAHOO.util.Dom.getY(o);
            tt.style.left = x + 'px';
            tt.style.top = y + 'px';
            
            tt.style.display = 'block';
        },500);     
    });
    YAHOO.util.Event.addListener(txtBars[i],'mouseout',function(){
            clearInterval(intvl);
            document.getElementById('tooltip').style.display = 'none';
    });
}



/***
 * "Magic" box dropdown code
 */

function getMBData(type){
    //document.getElementById('load_bars').innerHTML = document.getElementById('bars_for_' + this.id).innerHTML;
    /*
     * var callback = {
     *     success : function(o){
     *         document.getElementById('load_bars').innerHTML = o.responseText;
     *     },
     *     failure : function(o){ alert('There was a problem. Please try again.'},
     *     error : function(o){ alert('We have encountered an error. Please try again'}
     * }
     * YAHOO.util.Connect('GET','URL_HERE',callback);
     */
    alert('Loading HTML for ' + type + '...\nCould be done with above code by loading into a page, or via Ajax request above.')    
}

//Add listeners to mouseover/mouseout on box                            
YAHOO.util.Event.addListener('ddBox','mouseover',function(){
    YAHOO.util.Dom.addClass(this,'open');
    YAHOO.util.Dom.removeClass(this,'closed');    
});
YAHOO.util.Event.addListener('ddBox','mouseout',function(){
    YAHOO.util.Dom.addClass(this,'closed');
    YAHOO.util.Dom.removeClass(this,'open');
});

// Add listeners to arrow images
// Forward
/*
YAHOO.util.Event.addListener('img_forward','click',function(){
    // Determine how many items we have to see when we should wrap back around the list.
    if(! this.totalSize) this.totalSize = yuiQuery("ul#ddBox li.selectable").length;

    var fEl = document.getElementById('ddBox').firstChild;
    if(fEl.nodeType == 3) fEl = fEl.nextSibling; // Mozilla will add extra text nodes
    
    // Fetch replacement el based on ID
    var newLiNumber = parseInt(fEl.id.substr(fEl.id.indexOf('_')+1))+1;
    if(newLiNumber >= this.totalSize || newLiNumber < 0) newLiNumber = 0;
    
    var el = document.getElementById('bars_'+ newLiNumber);

    if (!YAHOO.util.Dom.hasClass(el, 'noSelect')) {
        var fc = document.getElementById('ddBox').firstChild;
        if(fc.nodeType == 3) fc = fc.nextSibling;
        
        var nd = el.cloneNode(true);
        nd.id = "barsCopy_" + newLiNumber;
        nd.className = ' selected';
        fc.parentNode.replaceChild(nd,fc);   
        
        getMBData(nd.innerHTML);
    }        
});

// And back.
YAHOO.util.Event.addListener('img_back','click',function(){
    if(! this.totalSize) this.totalSize = yuiQuery("ul#ddBox li.selectable").length;
    var fEl = document.getElementById('ddBox').firstChild;
    if(fEl.nodeType == 3) fEl = fEl.nextSibling; // Mozilla will add extra text nodes
    
    // Fetch replacement el based on ID
    var newLiNumber = parseInt(fEl.id.substr(fEl.id.indexOf('_')+1))-1;
    if(newLiNumber < 0 || newLiNumber >= this.totalSize) newLiNumber = this.totalSize - 1;

    var el = document.getElementById('bars_'+ newLiNumber);

    if (!YAHOO.util.Dom.hasClass(el, 'noSelect')) {
        var fc = document.getElementById('ddBox').firstChild;
        if(fc.nodeType == 3) fc = fc.nextSibling;
        
        var nd = el.cloneNode(true);
        nd.id = "barsCopy_" + newLiNumber;
        nd.className = ' selected';
        fc.parentNode.replaceChild(nd,fc);   
        
        getMBData(nd.innerHTML);
    }        
});

// Add click events to elements in list
var boxLis = yuiQuery('div.ddBox_container div.boxShim ul#ddBox li');
for(var i = 0; i < boxLis.length; i++){
    YAHOO.util.Event.addListener(boxLis[i],'click',function(){
        if (!YAHOO.util.Dom.hasClass(this, 'noSelect')) {
            var fc = document.getElementById('ddBox').firstChild;
            fc.className = 'button';
            if(fc.nodeType == 3) fc = fc.nextSibling;
            
            var nd = this.cloneNode(true);
            nd.className += ' selected';
            fc.parentNode.replaceChild(nd,fc);   
            
            getMBData(nd.innerHTML);
            YAHOO.util.Dom.addClass(this.parentNode,'closed');
            YAHOO.util.Dom.removeClass(this.parentNode,'open');
        }
    });    
}
*/

function setHiddenInputVal(id, val) {
	var fld_id = window.input_response+id
	var fld = document.getElementById(fld_id)
	fld.value = val;
}

function reportQuizInputs() {
	var quiz_items = yuiQuery('.quiz_question');
	for(var i = 0; i < quiz_items.length; i++){
		alert("id"+quiz_items[i].id+" value "+quiz_items[i].value)
	}
}

function listenerUpdate(whichSlider, newVal) {  
	setHiddenInputVal(whichSlider.id, newVal)
}

// What about mult buckets per page
function formBucketSubmit(frm) {
	
	var total = 0;
	var groups = PB.getGroups()
	var display_error = false;
	for(var i = 0; i < groups.length; i++){
		if (!PB.fillingBucketValid(groups[i])) {
			errorMsg( "Please adjust the sliders until the bucket is 100% full", groups[i])
			display_error = true;
		}
	}

	if (display_error){ 
		return false;
	}
	else {
		return true;
	}
	
}

function errorMsg(msg, element, append) {
	el = document.getElementById(element)
	full_msg = "<ul class=\"errorlist\"><li>"+msg+"</li></ul>";
	if (append == 1) {
		el.innerHTML += full_msg;		
	}
	else {
		el.innerHTML = full_msg;
	}
	
}
