/**
 * @author Tim
 */
YAHOO.namespace("FLN");
var FLN = YAHOO.FLN;

FLN.Utils = function(){
    return {
        message: function(m){
            alert(m);
        },
        /**
         * gets an array of objects from the supplied array (objArr) with objects that have all the properties of the supplied object (propsObj)
         * @param {Array} objArr
         * @param {Object} propsObj
         * @return {Array} an array of objects
         */
        filterObjectArray: function(objArr, propsObj){
            var result = [];
            for (var i = 0; i < objArr.length; i++) {
                var obj = objArr[i];
                var numberOfMatchingProps = 0;
                //does this obj have any properties that match any of those in the propsObj?
                for (var j in obj) {
                    if (YAHOO.lang.hasOwnProperty(obj, j)) { //protect against properties of the prototype
                        //does the property that exists in obj exist in the propsObj?
                        if (YAHOO.lang.hasOwnProperty(propsObj, j)) {
                            //the property exists, does it have the same value?
                            if (propsObj[j] === obj[j]) {
                                numberOfMatchingProps++;
                            }
                        }
                    }
                }
                //how many properties are there in the propsObj?		
                var numberOfProps = 0;
                for (var k in propsObj) {
                    if (YAHOO.lang.hasOwnProperty(propsObj, k)) {
                        numberOfProps++;
                    }
                }
                if (numberOfMatchingProps == numberOfProps) {
                    result.push(obj);
                }
                
            }
            return result;
        },
        /**
         * from: http://www.koders.com/javascript/fid99C58E067D2F7DD53179D11D9F80B55E867F7CBD.aspx
         * this function will convert seconds into hours, minutes, and seconds in
         * CMITimespan type format - HHHH:MM:SS.SS (Hours has a max of 4 digits &
         * Min of 2 digits
         */
        convertTotalSeconds: function(ts){
            var sec = (ts % 60);
            
            ts -= sec;
            var tmp = (ts % 3600); //# of seconds in the total # of minutes
            ts -= tmp; //# of seconds in the total # of hours
            // convert seconds to conform to CMITimespan type (e.g. SS.00)
            sec = Math.round(sec * 100) / 100;
            
            var strSec = new String(sec);
            var strWholeSec = strSec;
            var strFractionSec = "";
            
            if (strSec.indexOf(".") != -1) {
                strWholeSec = strSec.substring(0, strSec.indexOf("."));
                strFractionSec = strSec.substring(strSec.indexOf(".") + 1, strSec.length);
            }
            
            if (strWholeSec.length < 2) {
                strWholeSec = "0" + strWholeSec;
            }
            strSec = strWholeSec;
            
            if (strFractionSec.length) {
                strSec = strSec + "." + strFractionSec;
            }
            
            
            if ((ts % 3600) != 0) 
                var hour = 0;
            else 
                var hour = (ts / 3600);
            if ((tmp % 60) != 0) 
                var min = 0;
            else 
                var min = (tmp / 60);
            
            if ((new String(hour)).length < 2) 
                hour = "0" + hour;
            if ((new String(min)).length < 2) 
                min = "0" + min;
            
            var rtnVal = hour + ":" + min + ":" + strSec;
            
            return rtnVal;
        },
        /**
         * gets a filename from a path. for example: "http://www.example.com/example.html" will return "example.html".
         * @param {string} href
         * @return {string} filename the filename with extension, like: file.html
         */
        getFileName: function(href){
            //does the href have a querystring or anchor? Cut it off if it does.
            if (href.indexOf('?') != -1) 
                href = href.split('?')[0];
            if (href.indexOf('#') != -1) 
                href = href.split('#')[0];
            var tempArr = href.split('/');
            var fileName = tempArr[tempArr.length - 1]
            return fileName;
        },
        
        /**
         * Swaps the css class on an element between "enabled" and "disabled"
         * @param {HTMLElement} el the HTML Element
         * @param {Boolean} enable should the element be enabled?
         */
        enableEl: function(el, enable){
            //YAHOO.util.Dom.removeClass(el, "disabled");
            //YAHOO.util.Dom.removeClass(el, "enabled");			
            if (enable) {
                YAHOO.util.Dom.replaceClass(el, "disabled", "enabled");
            }
            else {
                YAHOO.util.Dom.replaceClass(el, "enabled", "disabled");
            }
        },
        getRandomString: function(stringLength){
            var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
            var randomstring = "";
            for (var i = 0; i < stringLength; i++) {
                var rnum = Math.floor(Math.random() * chars.length);
                randomstring += chars.substring(rnum, rnum + 1);
            }
            return randomstring;
        }
        
    }
    
}();

/*
 //test FLN.filterObjectArray
 var a = [{
 pa: "1",
 pb: "2",
 pb2: "88",
 pb3: "77"
 }, {
 pa: "1",
 pb: "2"
 }, {
 pc: "3",
 pd: "4"
 }, {
 pe: "5",
 pf: "6"
 }];
 var r = FLN.Utils.filterObjectArray(a, {
 pa: "1"
 });
 alert(r.length);
 */

