var rotatorTimer=new Array;
rotatorTimer[1]=null;
rotatorTimer[2]=null;

var rotatorSpeed=new Array;
rotatorSpeed[1]=7000;
rotatorSpeed[2]=7000;

var initializationSpeed=new Array;
initializationSpeed[1]=2000;
initializationSpeed[2]=2000;

var currentProduct=new Array;
currentProduct[1]=0;
currentProduct[2]=0;

var lastProduct=new Array;
lastProduct[1]=0;
lastProduct[2]=0;

var opacityState=new Array;
opacityState[1]=0;
opacityState[2]=0;

var opacityStep=new Array;
opacityStep[1]=2;
opacityStep[2]=2;

var opacitySpeed=new Array;
opacitySpeed[1]=20;
opacitySpeed[2]=20;

var opacityTimer=new Array;
opacityTimer[1]=null;
opacityTimer[2]=null;

var rotatorArea=new Array;
rotatorArea[1]="topProductImage";
rotatorArea[2]="topProduct";

var rotatorStatusBarArea=new Array;
rotatorStatusBarArea[1]="topProductImageStatusBar";
rotatorStatusBarArea[2]="topProductsStatusBar";

var topProductsQuantity=new Array;

function StartTopProductRotator(section){
    if(currentProduct[section]>0){
        //continuing in rotating after break
        setObjectOpacity(objGet(rotatorArea[section]+currentProduct[section]),100)
    }else{
        setObjectOpacity(objGet(rotatorArea[section]+"0"),100)
        objGet(rotatorArea[section]+"0").style.visibility="visible";
    }
    if(rotatorTimer[section]!=null){
        //there is some timeout => remove it
        clearTimeout(rotatorTimer[section]);
        rotatorTimer[section]=null;
    }
    rotatorTimer[section]=setTimeout(function(){
                                        RotateProduct(section)
                                        },initializationSpeed[section]);
}

function StopTopProductRotator(section) {
    clearTimeout(rotatorTimer[section]);
}

function RotateProduct(type){
    lastProduct[type]=currentProduct[type];
    currentProduct[type]++;
    if(currentProduct[type]==topProductsQuantity[type]){
        currentProduct[type]=0;
    }
    DisplayProduct(currentProduct[type],lastProduct[type],false,type);
}

function ShowSpecificProduct(which,type){
    lastProduct[type]=currentProduct[type];
    currentProduct[type]=which;
    DisplayProduct(currentProduct[type],lastProduct[type],true,type);
}

function DisplayProduct(current,old,interrupted,type){
    var temp;
    clearTimeout(rotatorTimer[type]);
    //stop rotation untill products changing will be done
    rotatorTimer[type]=null;
    if(interrupted){
        if(current!=old){
            opacityState[type]=0;
            if(old==0){
                temp=topProductsQuantity[type]-1
            }else{
                temp=old-1
            }
                
            if(opacityTimer[type]!=null){
                clearTimeout(opacityTimer[type]);
                opacityTimer[type]=null
                if(getObjectOpacity(objGet(rotatorArea[type]+temp))>0){
                    objGet(rotatorArea[type]+temp).style.visibility="hidden";
                    setObjectOpacity(objGet(rotatorArea[type]+temp),0);
                    objGet(rotatorArea[type]+old).style.visibility="visible";
                    setObjectOpacity(objGet(rotatorArea[type]+old),100);
                }
            }
            objGet(rotatorArea[type]+current).style.visibility="visible";
            ChangeProductOpacity(type);
        }
    }else{
        if(current!=old){
        objGet(rotatorArea[type]+current).style.visibility="visible";
        ChangeProductOpacity(type);
        }
    }
    
    //mark status bar under current rotation
    var ListerLinks=objGet(rotatorStatusBarArea[type]).getElementsByTagName("a");
    ListerLinks[old].setAttribute("class","");
    ListerLinks[old].setAttribute("className","");
    ListerLinks[current].setAttribute("class","active");
    ListerLinks[current].setAttribute("className","active");
}

function ChangeProductOpacity(type){
    clearTimeout(opacityTimer[type]);
    opacityTimer[type]=null;
    if(opacityState[type]>=100){
        //changing done
        objGet(rotatorArea[type]+lastProduct[type]).style.visibility="hidden";
        opacityState[type]=0;
        //start next rotation cycle
        rotatorTimer[type]=setTimeout(function(){
            RotateProduct(type)
            },rotatorSpeed[type]);
        return;
    }
    opacityState[type]+=opacityStep[type];
    setObjectOpacity(objGet(rotatorArea[type]+lastProduct[type]),100-opacityState[type])
    setObjectOpacity(objGet(rotatorArea[type]+currentProduct[type]),opacityState[type])
    opacityTimer[type]=setTimeout(function(){
        ChangeProductOpacity(type)
        },opacitySpeed[type]);
}

