﻿// FreeStyler Library // Copyright 2009 Ian Porter

function fsStyleVar(style,varName,varValue)
{
    this.style = style;
    varName = varName.replace(" ","");
    varValue = varValue.replace(" ","");
    this.varName = fsGetVarName(varName);
    varValue = fsConvertAnyHexToRgb(varValue);

    this.endNumbers =  varValue.match(this.pattNumber);
    this.template = varValue.replace(this.pattNumber ,"#number#");
    //alert("name:" + this.varName +"\nend:" +  varValue + "\ntemplate:"+this.template+"\nnumbers:"+this.endNumbers);
}

fsStyleVar.prototype.pattNumber = /[\d.]+/g;

fsStyleVar.prototype.init = function(parentElement)
{
    var startValue = parentElement ?
        fsGetStyle(parentElement,this.varName)  // will check computeted style
        : eval("this.style."+this.varName);     // this only checks inline styles if style is on an element
    var startValue = eval("this.style."+this.varName);
    if(startValue)
    {
        startValue = fsConvertAnyHexToRgb(startValue);
        this.startNumbers = startValue.match(this.pattNumber);
    }
    //alert("name:" + this.varName +"\nstart:" + startValue + "\ntemplate:"+this.template+"\nnumbers:"+this.startNumbers);
}

fsStyleVar.prototype.step = function(scaler)
{
    var newValue = this.template;
    if(this.startNumbers!=null)
        for (j=0;j<this.startNumbers.length;j++)
        {
            var val = this.startNumbers[j] - (this.startNumbers[j]-this.endNumbers[j]) * scaler;
            val = val.toFixed( this.varName.toLowerCase().search("color")>=0 ? 0:3 );  // colours must to be integers
            newValue = newValue.replace(/#number#/, val );
        }
    newValue = fsConvertAnyRgbToHex(newValue);    // IE likes hex colours
    
    if(this.varName=="display")
    {
        if(newValue=="none" && scaler<1.0) return;
        if(newValue!="none" && scaler>0.0) return;
    }
    
    try {
        //alert( "set style." +this.varName+"="+newValue);
        eval("this.style."+this.varName+" = newValue" );
    } catch(err){}
}

function fsGetVarName(str)
{
    str = str.toLowerCase();
    while( (pos = str.indexOf("-")) > 0 )
        str = str.substring(0,pos) + str.charAt(pos+1).toUpperCase() + str.substr(pos+2);
    return str;    
}

function fsConvertAnyHexToRgb(hexStr)
{
    var colours =  hexStr.match(/#[0123456789abcdef]{6}/gi);
    if(colours!=null)
        for (var i=0;i<colours.length;i++)
        {
            var r = parseInt(colours[i].substr(1,2),16);
            var g = parseInt(colours[i].substr(3,2),16);
            var b = parseInt(colours[i].substr(5,2),16);
            hexStr = hexStr.replace(/#[0123456789abcdef]{6}/i ,"rgb("+r+","+g+","+b+")");
        }
    var colours =  hexStr.match(/#[0123456789abcdef]{3}/gi);
    if(colours!=null)
        for (var i=0;i<colours.length;i++)
        {
            var r = parseInt(colours[i].substr(1,1),16);
            var g = parseInt(colours[i].substr(2,1),16);
            var b = parseInt(colours[i].substr(3,1),16);
            hexStr = hexStr.replace(/#[0123456789abcdef]{3}/i ,"rgb("+r+","+g+","+b+")");
        }
    return hexStr;
}

function fsConvertAnyRgbToHex(rgbStr)
{
    var colours =  rgbStr.match(/rgb\([0123456789, ]*\)/gi);
    if(colours!=null)
        for (var i=0;i<colours.length;i++)
        {
            colours[i] = colours[i].replace(/[rgb\(\) ]/gi, "");
            var vals = colours[i].split(",");
            var r = parseInt(vals[0],10).toString(16).toUpperCase();
            var g = parseInt(vals[1],10).toString(16).toUpperCase();
            var b = parseInt(vals[2],10).toString(16).toUpperCase();
            r = r.length==1 ? "0"+r : r;
            g = g.length==1 ? "0"+g : g;
            b = b.length==1 ? "0"+b : b;
            rgbStr = rgbStr.replace(/rgb\x28[0123456789, ]*\x29/i ,"#"+r+g+b);
        }
    return rgbStr;
}

function fsGetStyle(element, strCssRule)
{
	var result = "";
	if(document.defaultView && document.defaultView.getComputedStyle)
		result = document.defaultView.getComputedStyle(element, "").getPropertyValue(strCssRule);
	else if(element.currentStyle)
	{
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){ return p1.toUpperCase(); } );
		result = oElm.currentStyle[strCssRule];
	}
	return result;
}