var ap_AutoSlider = function ()
{                    
	this.step = 1;//for 1 variant
    this.delay = 10000;
    this.speedAnim = 700;

    this.mainContWidth = $('#adboard_autoslider').width();
    this.loadedBlock = $('#loadeddata');
    this.loadedBlocks = $('#loadeddata > div');
    this.block1 = $('#al_moveCont1')[0];
    this.block2 = $('#al_moveCont2')[0];

    this.autoCnt =  $('#loadeddata > div').length;
    this.currentAutoIndx = 0;
    this.currentBlockIndx = 1;  
                     
    this.init();
    setTimeout('apSlider.run()', this.delay);            
}

ap_AutoSlider.prototype.init = function ()
{
    var block1Width = 0;
    var block2Width = 0;
    var maskWidth = $("#adboard_autoslider").width();
    var autoBlockWidth  = (maskWidth-6) /3;  
    
    for (var i=0; i<this.autoCnt; i++)
    {
    	this.addBlock(this.block1, new Array(this.loadedBlocks[i]), 'auto1_'+i);
    	this.addBlock(this.block2, new Array(this.loadedBlocks[i]), 'auto2_'+i);
    	
    	$('#auto1_'+i).width(autoBlockWidth);
    	$('#auto2_'+i).width(autoBlockWidth);    	    	
    	
        block1Width += $('#auto1_'+i).width()+2;
        block2Width += $('#auto2_'+i).width()+2;                    	
    }
    
    $(this.block1).width( block1Width );
    $(this.block2).width( block2Width );
    
    this.setStack(this.block1, this.block2);
}                
ap_AutoSlider.prototype.setStack = function (el1, el2)
{
    var left1 = parseInt($(el1).css('left'));
    $(el2).css('left', left1+ $(el1).width()+'px');
}                
ap_AutoSlider.prototype.addBlock = function(block, aContents, id)
{
	var content = '';
	for (var cIndx in aContents){
		content += $(aContents[cIndx]).html(); 
	}
    $(block).append('<div id="'+id+'" style="border:1px solid #ddd;height:99%;float:left;">'+content+'</div>');
}

ap_AutoSlider.prototype.run = function()
{
    var leftBlock;
    var rightBlock;                    
    if( parseInt($(this.block1).css('left')) < parseInt($(this.block2).css('left')) ){
        leftBlock = this.block1;
        rightBlock = this.block2;
    }   
    else{
        leftBlock = this.block2;
        rightBlock = this.block1;
    }

    var step = $('#auto1_'+this.currentAutoIndx).width()+2;    
    this.currentAutoIndx++;
    if (this.currentAutoIndx == this.autoCnt){
        this.currentAutoIndx = 0;
    }
    /*
	step += $('#auto1_'+this.currentAutoIndx).width()+2;
    this.currentAutoIndx++;
    if (this.currentAutoIndx == this.autoCnt){
        this.currentAutoIndx = 0;
    } 
    */    
    
    var left = parseInt( $(rightBlock).css('left'));
    if(left-step < 0 ){
        this.setStack(rightBlock, leftBlock);
        var leftBlock_temp = leftBlock;
        leftBlock = rightBlock;
        rightBlock = leftBlock_temp;
    }                    

    var left = parseInt( $(leftBlock).css('left') );
    var width = $(leftBlock).width();  

    $(leftBlock).animate({"left": "-="+step+"px"}, this.speedAnim);
    $(rightBlock).animate({"left": width+left-step+"px"}, this.speedAnim);

    setTimeout('apSlider.run()', this.delay);                                            
}

/* old variant */
ap_AutoSlider.prototype.run_ = function()
{
	var leftBlock;
	var rightBlock;
	
	if( parseInt($(this.block1).css('left')) < parseInt($(this.block2).css('left')) ){
		leftBlock = this.block1;
		rightBlock = this.block2;
	}	
	else{
        leftBlock = this.block2;
        rightBlock = this.block1;
	} 

    var left = parseInt( $(leftBlock).css('left') );
    var width = $(leftBlock).width();  
	$(leftBlock).css('left', (left-this.step)+'px');                	
	$(rightBlock).css('left', (width+left-this.step)+'px');
    
    var left = parseInt( $(rightBlock).css('left'));
    if(left < 0 ){
        this.setStack(rightBlock, leftBlock);
    }                        	
    
    setTimeout('apSlider.run()', this.speed);
}        