
var actOpenedSO = null;
var actOpenedSOIdx = 0;
var displayedSO = null;
var SOrotationStep = 5000;
var SOrotation = null

function setAutoSoRotation(){
  if(!SOrotation){
    SOrotation = setInterval('displayNextSo()',SOrotationStep); 
  }
}

function unsetAutoSoRotation(){
  if(SOrotation){
    clearInterval(SOrotation); 
    SOrotation = null
  }
}

function displayNextSo(){
  var showNowIdx = actOpenedSOIdx+1;
  if(showNowIdx >= displayedSO.length){
    showNowIdx = 0; actOpenedSOIdx = 0;
  }else{
    actOpenedSOIdx += 1;
  }  
  switchSOffer(displayedSO[showNowIdx]);
}

function dynSOfferSwitch(eid,idx){
  if(eid != actOpenedSO){
    actOpenedSO = eid;
    actOpenedSOIdx = idx;
    switchSOffer(eid);
  }
}

function switchSOffer(eid){
  var superContainer = document.getElementById('soffersArea'); 
  var divs = superContainer.getElementsByTagName('DIV');
  var elem = null;
  var elem2 = null;
  var t = null;
  
  for(var i=0; i<divs.length; i++){
    elem = divs[i];
    if(elem.id){
      t = elem.id.split('_');
      //so_adddata_ID
      if(t[0] == 'so' && t[1] == 'adddata'){
        //alert(elem.id);
        elem2 = document.getElementById('soArr'+t[2]);
        if(t[2] == eid){
          //soarrdown.png
          //soArrID
          if(elem.style.display == 'block'){
            //elem.style.display = 'none'; 
            elem2.style.backgroundImage = '';
            soRollIn(eid);
          }else{
            elem.style.display = 'block';
            elem2.style.backgroundImage = 'url(layout/soarrdown.png)';
            soRollOut(eid);
          }
        }else{ 
          //elem.style.display = 'none'; 
          elem2.style.backgroundImage = '';
          if(elem.style.display == 'block'){
            soRollIn(t[2]);
          }
        }
      }
    }
  }
  
}

setAutoSoRotation();

var actHForEid = new Array();
var actTimeoutForEid = new Array();
var actTimeoutForEid2 = new Array();

function soRollOut(eid){
  var elem = document.getElementById('so_adddata_'+eid);
  if(elem.style.display == 'none'){
    elem.style.display = 'block'; 
  }
  var final_height = (document.getElementById('so_adddata_img_'+eid) ? 148 : 66);
  var step = 3;
  actHForEid[eid] = 0;
  if(actTimeoutForEid[eid]){
    clearTimeout(actTimeoutForEid[eid]);
  }
  if(actTimeoutForEid2[eid]){
    clearTimeout(actTimeoutForEid2[eid]);
  }
  soRollOutStep(eid,final_height,step);
}

function soRollOutStep(eid,maxh,step){
  var elem = document.getElementById('so_adddata_'+eid);
  if(actHForEid[eid]+step <= maxh){
    elem.style.height = (actHForEid[eid]+step)+'px';
    actHForEid[eid] = (actHForEid[eid]+step);
  }else{
    elem.style.height = (maxh)+'px';
    actHForEid[eid] = (maxh);
  }
  if(actHForEid[eid] < maxh){
    actTimeoutForEid[eid] = setTimeout("soRollOutStep("+eid+","+maxh+","+step+")",7); 
  }
}

function soRollIn(eid){
  //var elem = document.getElementById('so_adddata_'+eid);
  //var actH = actHForEid[eid];
  if(!actHForEid[eid]){
    var elem = document.getElementById('so_adddata_'+eid);
    actHForEid[eid] = (document.getElementById('so_adddata_img_'+eid) ? 148 : 66);;
  }
  var step = 3;
  if(actTimeoutForEid[eid]){
    clearTimeout(actTimeoutForEid[eid]);
  }
  if(actTimeoutForEid2[eid]){
    clearTimeout(actTimeoutForEid2[eid]);
  }
  soRollInStep(eid,step);
}

function soRollInStep(eid,step){
  var elem = document.getElementById('so_adddata_'+eid);
  if(actHForEid[eid] >= step){
    elem.style.height = (actHForEid[eid]-step)+'px';
    actHForEid[eid] = (actHForEid[eid]-step);
  }else{
    elem.style.height = '0px';
    elem.style.display = 'none';
    actHForEid[eid] = 0;
  }
  if(actHForEid[eid] >= 0){
    actTimeoutForEid2[eid] = setTimeout("soRollInStep("+eid+","+step+")",7);
  }
}

function getNewImages(){
  var newIm = new Array();
  var r = null;
  var found = 0;
  var t = 0;
  var upTo = Math.min(actImages.length,4);
  var cont = false;
  //alert(allImages.length);
  if(allImages.length >= 8){ //all can be replaced
    while(newIm.length < upTo){
      cont = false;
      r = Math.floor(Math.random()*allImages.length);
      t = allImages[r];
      
      for(var i=0; i<upTo; i++){
        if(t == actImages[i]){
          cont = true;
        }
      }
      if(cont) continue;
      for(var i=0; i<newIm.length; i++){
        if(t == newIm[i]){
          cont = true;
          continue;
        }
      }
      if(cont) continue;
      newIm[newIm.length] = t; 
    }
    for(var i=0; i<4; i++){
      var elem = document.getElementById('camImg'+i);
      var elem2 = document.getElementById('camImgLink'+i);
      elem.src = '?incamera_img:'+newIm[i];
      //elem2.href = '?incamera_img:'+newIm[i]+':800:600';
      elem2.href = '?gallery&f='+newIm[i];
      actImages[i] = newIm[i];
    }
  }
}

