﻿var LargePic='#pdPic'; //first part of div id
    var SmallPic='#sImg';  //first part of div id
    
    var tempI=0;
   

var idOffset=0;


function move1(offset)
{
localOffset=offset+'px';
for(i=thisCounterImg;i>0;--i)
   $(SmallPic+i).css({'position':'relative','left':localOffset});  
}   


function idByPos(pos)
{

    for(i=1;i<posArr.length;++i)
    {
        if(posArr[i]==pos)
            return i;        
    }

}


function posById(id)
{
    for(i=1;i<posArr.length;++i)
    {
        if(i==id)
        return posArr[i];
    }

}

function newIdByOldId(oldId)
{
tempI++;
    count=parseInt(thisCounterImg);
    idOffset=parseInt(idOffset);
  
  localOffset=parseInt(oldId) + idOffset;
  
  newId=localOffset;
  
  if(localOffset < 1)
  {
    
    if(idOffset < 0-count)
      {
         idOffset+=count;
         localOffset+=count;
      }
    newId=count+localOffset;  
  }
  if(localOffset > count)
  {
    
    idOffset-=count;
    localOffset-=count;
    newId=localOffset;
  
  }
  
//  if(tempI % 10 == 0)
//  alert('oldId(' + oldId + '), newId(' +newId + '), offset(' + idOffset + '), localOffset(' + localOffset + ')');
  
return newId;
}


function sImgClick(id)
{
id=parseInt(id);
    PicSlider2(posById(parseInt(id)));

}

function clearBorder()
{
    
    for(i=0; i< thisCounterImg ; ++i)
    {
    
       $(SmallPic+i).css({'border':'none','width':'40px','height':'40px'});
    
    }   

}

function PicSlider2(nextId)
{
//bextId=parseInt(nextId);
    //LocalID=posArr[next_id];
   
    //alert(obj_idArr[nextId]);
    if(document.getElementById('comObjID') != null)
     document.getElementById('comObjID').value=obj_idArr[nextId];
    
    $('.commentsRow').hide();
    
    $('#commentRow'+nextId).show();
    
    
    ///
    actionURL=newAdrPath + '?picsid=' + nextId ;
  //  alert(actionURL);
    
    ///
    

if(thisCounterImg >= parseInt(nextId) && parseInt(nextId)>0)
{

    lastId=thisLargeImg;
    $(LargePic+thisLargeImg).hide();
    $(LargePic+nextId).show();
    $(SmallPic+idByPos(thisLargeImg)).css({'border':'none','height':'40px','width':'40px'});
  
    $(SmallPic+idByPos(nextId)).css({'border':'solid 2px red','width':'36px','height':'36px'});
    thisLargeImg=nextId;
 }
 else
 {
     if(nextId <1)
        nextId=thisCounterImg;
    if(nextId > thisCounterImg)
        nextId=1;
        
    PicSlider(nextId);
 }
 
 
    //need to add class for small image

}

function PicSlider(id)
{
id=parseInt(id);

  if(id==0)
     id=thisCounterImg;

    if(id>thisCounterImg || id<1)
       id=1;
    
    
 

    sendid=posArr[id];
    oldLargeImg=thisLargeImg;
  
    PicSlider2(id);
    
//    alert(newIdByOldId(parseInt(id)));
    if(newIdByOldId(parseInt(id)) > 15)
        {
            
            n=(parseInt(thisCounterImg) + 8) - parseInt(newIdByOldId(parseInt(id)));
          //  alert(thisCounterImg + '+' + 8+' - ' + parseInt(newIdByOldId(id)) );
            
            
            for(i=n;i>0;i--)
            {
            
               setTimeout('ScrollSrc(1)',i*50);
            }
        }
    
    if((newIdByOldId(parseInt(id)) > 8) && (oldLargeImg == parseInt(id)-1 || parseInt(id) == 1))
        {
            ScrollSrc(1);
   //         alert('id = ' + id + ', oldImg = ' + oldLargeImg);
        }
        else if ((newIdByOldId(parseInt(id)) > 8))
        {
            
         n=(parseInt(thisCounterImg) + 8) - parseInt(newIdByOldId(parseInt(id)));
         //n=8 + parseInt(thisCounterImg) - newIdByOldId(parseInt(id));
          //  alert(thisCounterImg + '+' + 8+' - ' + parseInt(newIdByOldId(id)) );
            
           // n+=8;
           // n=parseInt(newIdByOldId(id))-8;
         
            for(i=n;i>=0;i--)
            {
                
                
               setTimeout('ScrollSrc(1)',i*50);
            }
        }
      
  
}


/// scroller 




var bufSrc="";
var bufPos=null;


function putSrcFromArr()
{
    for(i=1;i <= thisCounterImg; ++i)
    {    
        document.getElementById('sImg'+i).src=srcArr[i];
       // document.getElementById('sImg'+i).id=posArr[i];
     
    }
}

function scrollRight()
{
    bufSrc=srcArr[1];
    bufPos=posArr[1];
    
    $(SmallPic+thisLargeImg).css({'border':'none','width':'40px','height':'40px'});
    for(i=1;i < thisCounterImg; ++i)
    {
        
        srcArr[i]=srcArr[i+1];
        posArr[i]=posArr[i+1];
    
    }
    srcArr[thisCounterImg]=bufSrc;
    posArr[thisCounterImg]=bufPos;
    
    
    idOffset--;
    
}

function scrollLeft()
{
    bufSrc=srcArr[thisCounterImg];
    bufPos=posArr[thisCounterImg];
    
    for(i=thisCounterImg;i>1;--i)
    {
        srcArr[i]=srcArr[i-1];
        posArr[i]=posArr[i-1];
    }
    srcArr[1]=bufSrc;
    posArr[1]=bufPos;
    
    idOffset++;

}


function InsertBorder()
{
   
   $(SmallPic+newIdByOldId(thisLargeImg)).css({'border':'solid 2px red','width':'36px','height':'36px'});
    


}

function ScrollSrc(vpravo) //inteface function
{

if(thisCounterImg > 15)
{
   SliderBusy=true;
    if(vpravo)
    scrollRight();   
    else
    scrollLeft();
    

   putSrcFromArr();
 
   clearBorder();   
   InsertBorder();

   SliderBusy=false;
}
}

//when pics = id



