/**
 * @author Alexander Farkas
 * v. 1.02
 */

         //  step 
var step = 39;
        //  time
var maxtime = 800; 
 
function toArray(strg)
{
    if(strg != undefined)
    {
        strg = strg.replace(/left|top/g,'0px');
        strg = strg.replace(/right|bottom/g,'100%');
        strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
        var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
        return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
    }
}
 
function slideTo(id, value){
    var el = $(".cnt"+id);
    if(el.length>0){
        var cssValue = el.css("backgroundPosition");
        
        if(cssValue == undefined || cssValue == null)
        {
            cssValue = el.css('background-position-x') + " " + el.css('background-position-y');
        }

        var cssValueArray = toArray(cssValue);
        
        var curValue = Math.round(Math.abs(cssValueArray[2]/step ));
        if(value==100500){
            value = curValue+1;
        }
        timer=Math.abs(value-curValue)*maxtime;
        if(value>9){
             slideTo((id-1)+"",100500);
        }
        el.animate({backgroundPosition:"(0px -"+(value*step)+"px)"},timer,'easeOutElastic',function(){
            if(value>9){
                //  add!
                el.css({backgroundPosition: '0px 0px'});
            }
        });
    }
    
    /*
    //  num
    if(currentValue[2]==-390){
        currentValue[2]=0;
        el.css("backgroundPosition","(0px 0px)");
    }
    if(currentValue[2]==-351){
        myPoint = 0;
        currentValue[2]=0;
    }
    if(value==100500){
        value = myPoint+1;
    }

    var moveValue = Math.abs(myPoint-value);
    var timer = moveValue*maxtime;
    console.log("value:"+value+" | moveValue:"+moveValue+" | "+myPoint+"("+currentValue[2]+")");
    */
    
 }
 
 
 
(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                
                if(start == undefined || start == null)
                {
                    //start = el.css('background-position-x') + " " + el.css('background-position-y');
                    start = '0px 0px';
                }
        
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

           function toArray(strg){
               if(strg != undefined)
               {
                   strg = strg.replace(/left|top/g,'0px');
                   strg = strg.replace(/right|bottom/g,'100%');
                   strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
                   var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
                   return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
               }
           }
        }
	});
})(jQuery);


//  counter by ctepeo [reactive.by]
$(document).ready(function(){
    if($(".counter").length>0){
        //  init
        $(".counter").append("<div class=\"cnt1\"></div><div class=\"cnt2\"></div><div class=\"cnt3\"></div><div class=\"cnt4\"></div><div class=\"cnt5\"></div>");
        //  first value

        var defvalue = [0,0,7,8,5];
           
        slideTo(1, defvalue[0]);
        slideTo(2, defvalue[1]);
        slideTo(3, defvalue[2]);
        slideTo(4, defvalue[3]);
        slideTo(5, defvalue[4]);
        
        intervalID = window.setInterval('slideTo(5, 100500)', 3000);
        
    }
    
});
