var fsStyle = {
    aPropertyToProcess: ['background-image']
    ,
    applyStyleToPage: function(oDoc)
    {
        var oStyles = this.getStyleProperties();
        var oStylesConverted = this.convertArrayStructure(oStyles);
        this.hackFixPlacement(oStylesConverted); /*modified by reference*/
        this.applyStyleRule(oStylesConverted, oDoc);
    }
    ,
    getStyleProperties: function()
    {
        var xmlCustom = fsXml.open(fs.getContainingFolder() + '/' + fsConfig.PATH_TO_XMLS + 'customs.xml');
        var oStyles = fsXml.getElementsByXpath(xmlCustom, '/customs/styles/style');

        return oStyles;
    }
    ,
    convertArrayStructure: function(oStyles)
    {
        var oStylesConverted = {};
        var sTempSelector = null;
        var sTempAttribute = null;
        var sTempValue = null;
        /*get selector*/
        for (var i=0;i<oStyles.length;i++) {
            sTempSelector = oStyles[i].getAttribute('selector');
            oStylesConverted[sTempSelector] = {}; /*instantiate and store to a temp obj*/

            for (var j=0; j < oStyles[i].childNodes.length; j++) {
                if (typeof(document.body.style.maxHeight) == 'undefined') {/*IE6*/

                    sTempAttribute = oStyles[i].childNodes[j].getAttribute('name');
                    sTempValue = oStyles[i].childNodes[j].text;
                    if (sTempValue != '') {
                        oStylesConverted[sTempSelector][sTempAttribute] = sTempValue;
                    }

                } else {/*FF, IE7*/
                    if (typeof(oStyles[i].childNodes[j].childNodes[0]) == 'object') {
                        sTempAttribute = oStyles[i].childNodes[j].getAttribute('name');
                        if (oStyles[i].childNodes[j].hasChildNodes) {
                            sTempValue = oStyles[i].childNodes[j].childNodes[0].nodeValue;
                            if (sTempValue != '') {
                                oStylesConverted[sTempSelector][sTempAttribute] = sTempValue;
                            }
                        }
                    }
                }
            }
        }
        return oStylesConverted;
    }
    ,
    hackFixPlacement: function(oStylesConverted) {
        var sFunctionNamePart = '';
        var sFunctionName = '';
        for (var sSelector in oStylesConverted) {
            for (var sAttribute in oStylesConverted[sSelector]) {

                for (var i in this.aPropertyToProcess) {
                    if (sAttribute == this.aPropertyToProcess[i]) {
                        sFunctionNamePart = sAttribute.replace(/(^|-)(.)/g,function(sRaw){sNew=sRaw.replace('-','');return sNew.toUpperCase();});
                        sFunctionName = ['handle', sFunctionNamePart].join('');
                        this[sFunctionName].call(this, oStylesConverted[sSelector]);
                    }
                }
            }
        }
    }
    ,
    applyStyleRule: function(oStyleConverted, oDoc) {
        oDoc = oDoc||document;
        var oSheet = oDoc.styleSheets[oDoc.styleSheets.length - 1];/*place all the new styles at the last point*/
        var aRule = [];
        for (var sSelector in oStyleConverted) {
            for (var sAttribute in oStyleConverted[sSelector]) {
                aRule.push([sAttribute,':',oStyleConverted[sSelector][sAttribute],';'].join(''));
            }
            if (oSheet.insertRule) { /*Firefox*/
                oSheet.insertRule([sSelector, ' {', aRule.join(''), '}'].join(''), oSheet.cssRules.length);
            } else if (oSheet.addRule) { /*IE*/
                for (var i in aRule) {
                    oSheet.addRule(sSelector, aRule[i]);
                }
            }
            aRule = []; /*purge all*/
        }
    }
    ,
    handleBackgroundImage: function(oSelector) {
        /*if is IE6 and file type is PNG*/
        if ((typeof(document.body.style.maxHeight) == 'undefined') && (oSelector['background-image'].match(/^url\(('|")?(.+).png\1?\)$/))){
            /*extracting and setting new value*/
            var sPathToImage = /^url\(('|")?(?:\.\.\/)?(.+)\1\)$/.exec(oSelector['background-image'])[2];
            var sAttributeValue = ["progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', sizingMethod='crop', src='",sPathToImage,"')"].join('');
            oSelector['filter'] = sAttributeValue;
            delete oSelector['background-image'];
        /*else if IE7 disable alpha image*/
        } else if (typeof(document.body.style.maxHeight) == 'undefined') {
            oSelector['filter'] = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='false')";
        }
    }
}
