if (typeof discover === 'undefined') {
    var discover = {};
}

discover.util = {
	/**
	 * Return an  HTTP Request object
	 * 
	 * @param {Object} config
	 */
	createRequest : function (config) {
	    config = config || {};
	    
	    var req = mcd.http.request({
	        'method'  : 'GET',
	        'uri' : config.uri,
			'data' : config.data || '',
	        'dataType' : config.dataType || 'xml',
	        'onreadystatechange' : function() {
                if (config.callback) config.callback.call(req);
	        }
	    });
	
	    return req;
	},
	
	/**
	 * Convert a NodeList/Collection to an actual Array
	 * 
	 * @param {Object} c
	 */
	collectionToArray : function(c){
	    var a = [];
	    
	    for (var i = 0, len = c.length; i < len; i++) {
	        a.push(c[i]);
	    }
	    return a;
	},
	
    pngFix: function() {
		var container = discover.rewardChooser.rewardsEl,
		    rewards = mcd.dom.getChildElements(container);
			 
        for(var i=0,l=rewards.length; i<l; i++) {
            var reward = rewards[i],
			    link = reward.getElementsByTagName('a')[0],
                img = mcd.dom.getElementsHaveAttribute('src', link, 'img')[0];
			
                img.parentNode.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img.src+"', sizingMethod='crop')";
                img.src = '/images/transparent-placeholder.gif';
            
        }
    },
	
    isIE6: function() {
        
        var matches = navigator.userAgent.match(/MSIE (\d+)\.(\d+);/);
        
        if (!matches) return false;
        
        return (matches[1] == 6);
    }(),
    
    isIE: function() {
        
        var matches = navigator.userAgent.match(/MSIE/);
        
        if (!matches) return false;
        
        return true;
    }(),
    
	/**
	 * Simple wrapper for console.log so I can turn it on
	 * and off in one place as well as handle its absence in IE
	 * 
	 * @param {String} message  The string you want to output to console
	 */
	trace : function (message) {
	    if (typeof console !== 'undefined') {
	        console.log(message);
	    }
	},
	
	/**
	 * Return the total # of items in an object
	 *  
	 * @param {Object} obj
	 */
    objectLength: function(obj) {
        var count = 0;
        
        for(var i in obj) count++;
        
        return count;
    },
	
    /**
     * Re-useable throbber toggler
     * 
     * @param state => 'hide' or 'show'
     */
    toggleThrobber : function (container, state) {
        switch(state) {
            case 'show':
                discover.util.toggleAjaxStatus(container, 'show');
                mcd.dom.removeClass(this.ajaxThrobber, 'hide');
                break;                  
            case 'hide':
                discover.util.toggleAjaxStatus(container, 'hide');
                mcd.dom.addClass(this.ajaxThrobber, 'hide');
                break;
        }       
    },
    
    /**
     * Re-useable ajax error message toggler
     * 
     * @param state => 'hide' or 'show'
     */
    toggleAjaxError : function (container, state) {
        switch(state){
            case 'show':
                discover.util.toggleAjaxStatus(container, 'show');
                mcd.dom.removeClass(this.ajaxError, 'hide');
                break;
            case 'hide':
                mcd.dom.addClass(this.ajaxError, 'hide');
                discover.util.toggleAjaxStatus(container, 'hide');
                break;
        }
    },
    
    /**
     * Re-useable Ajax Status toggler
     * Called from within the toggleAjaxError and toggleThrobber functions
     * 
     * @param state => 'hide' or 'show'
     */
    toggleAjaxStatus : function (container, state) {
        switch(state) {
            case 'show':
                container.appendChild(this.ajaxStatus);
                mcd.dom.removeClass(this.ajaxStatus, 'hide');
                break;
            case 'hide':
                document.body.appendChild(this.ajaxStatus);
                mcd.dom.addClass(this.ajaxStatus, 'hide');
                break;
        }
    },
	
	/**
	 * Merges objects together
	 * 
	 * @param {Object} Any number of arbitrary objects may be passed
	 */
	mergeObjects : function () {
		var out = {};
		
		for (var i = 0; i < arguments.length; i++) {
			for (var j in arguments[i]) {
				out[j] = arguments[i][j];
			}
		}
		
		return out;
	},
	
	init: function() {
        this.ajaxStatus = document.getElementById('ajax-status');
        this.ajaxError = document.getElementById('ajax-error');
        this.ajaxThrobber = document.getElementById('ajax-throbber');
	}
};

if ((typeof mcd.ui) === 'undefined') {
    mcd.ui = {};
}

/**
 * Get all radio buttons by name
 * 
 * @param {Object} form
 * @param {Object} name
 */
mcd.ui.radiogroup = function(form, name) {
    var list = [];
    
    for(var i=0,l=form.elements.length; i<l; i++) {
        var el = form.elements[i];
        
        if (el.tagName === "INPUT"
            && el.attributes['type'] 
            && el.attributes['type'].nodeValue === "radio"
            && el.attributes['name'] 
            && el.attributes['name'].nodeValue === name) {
            
            list.push(el);
        } 
    }
    
    return list;
};

/**
 * Get the value of the currently checked radio button in a group,
 *  or null if none.
 * 
 * @param {Object} form
 * @param {Object} name
 */
mcd.ui.checkedValue = function(form, name) {
    var list = mcd.ui.radiogroup(form, name);

    for (var i = 0, l = list.length; i < l; i++) {
        var el = list[i];
        if (el.checked) return el.getAttribute('value');
    }
    
    return null;
};
