var tick = 20000;
var mvItems = {};
setTimeout (function () {
  for (var gid in mvItems) {
    tick += mvItems[gid]['duration'];
    var progress_total = tick/10000;
    
    var pr_to = 0;
    var pr_do = 0;
    var pr_mv = 0;
    var pr_ti = 0;
    var pr_di = 0;
    
    if (progress_total >= 0 && progress_total < 0.5) {
      pr_to = progress_total * 2;
      pr_do = 0;
      pr_mv = 0;
      pr_ti = 0;
      pr_di = 0;
    }
    if (progress_total >= 0.5 && progress_total < 1) {
      pr_to = 1;
      pr_do = (progress_total - 0.5) * 2;
      pr_mv = 0;
      pr_ti = 0;
      pr_di = 0;
      
      if (in_next &&  mv_act_now[gid] != in_next) {
            in_move = 0;
               mvShowItem (gid,in_next,1);
        in_next = null;
      }      
    }
    if (progress_total >= 1 && progress_total < 2) {
      pr_to = 1;
      pr_do = 1;
      pr_mv = progress_total - 1;
      pr_ti = 0;
      pr_di = 0;
    }
    if (progress_total >= 2 && progress_total < 2.5) {
      mv_act_opened[gid] = mv_act_next[gid];

      pr_to = 1;
      pr_do = 1;
      pr_mv = 1;
      pr_di = (progress_total - 2)*2;
      pr_ti = 0;
    }
    if (progress_total >= 2.5 && progress_total < 3) {
      pr_to = 1;
      pr_do = 1;
      pr_mv = 1;
      pr_di = 1;
      pr_ti = (progress_total - 2.5)*2;
    }
    if (progress_total >= 3) {
      pr_to = 1;
      pr_do = 1;
      pr_mv = 1;
      pr_ti = 1;
      pr_di = 1;
    }

    
    if (pr_mv == 0) {
      var dd = 1 - 1*delta(pr_do);
      __el(mv_act_opened[gid]+'it').style.opacity = 1*dd;
      __el(mv_act_opened[gid]+'it').style.filter = 'alpha(opacity='+parseInt(dd*100)+')';
      __el(mv_act_opened[gid]+'ic').style.opacity = dd;
      __el(mv_act_opened[gid]+'ic').style.filter = 'alpha(opacity='+parseInt(100*dd)+')';
    }
    if (pr_mv == 1) {
      var dd = 1*delta(pr_di);
      var sp = 0.30+0.70*dd;
      if (sp > 1) { sp = 1 }

      __el(mv_act_opened[gid]+'it').style.opacity = 1*dd;
      __el(mv_act_opened[gid]+'it').style.filter = 'alpha(opacity='+parseInt(dd*100)+')';
      __el(mv_act_opened[gid]+'ic').style.opacity = dd;
      __el(mv_act_opened[gid]+'ic').style.filter = 'alpha(opacity='+parseInt(100*dd)+')';
    }
    
    
    if (in_move) {

      var maxv = 0;
      var maxi = -1;
      var total = 0;
      var itm = mvItems[gid]['items'];
      for (var i=0;i<itm.length;i++) {
        var r = Math.floor ((itm[i]['to'] - itm[i]['from']) * delta(pr_mv) + itm[i]['from']);
        itm[i]['current'] = r;
        if (r > maxv) {
          maxv = r;
          maxi = i;
        }
        total += r;
      }

      if (total != mvItems[gid]['total']) {
        itm[maxi]['current'] += mvItems[gid]['total'] - total;
      }

      for (var i=0;i<itm.length;i++) {
        var el = __el(itm[i]['id']);
        if (el) {
          el.style.width = itm[i]['current']+'px';
//          __el(itm[i]['id']+'i').style.left = itm[i]['current'] - mvItems[gid]['max_width'] + 'px';
        }
      }
    }
    
    
    

    if (progress_total >= 3) {
      in_move = 0;

      if (in_next &&  mv_act_now[gid] != in_next) {
        mv_act_now[gid] = 0;
        mvShowItem (gid,in_next,1);
        in_next = null;
      }      

    }           
    
  }
  
  setTimeout(arguments.callee, 5);
},50);

function d(progress) {
    return 1 - Math.sin((1 - progress) * Math.PI/2);
}
function delta(progress) {
    if (progress < .5)
        return d(2 * progress) / 2;
    else
        return (2 - d(2 * (1 - progress))) / 2;
}

function mvAddGroup (id,width,duration,min_width,max_width) {
  mvItems[id] = {};
  mvItems[id]['items'] = [];
  mvItems[id]['total'] = width;
  mvItems[id]['min_width'] = min_width;
  mvItems[id]['max_width'] = max_width;
  mvItems[id]['duration'] = duration;
  mvItems[id]['start'] = 0;
}

function mvAddItem (grp,id,cr) {
  mvItems[grp]['items'].push ({
    id: id,
    current: cr?cr:mvItems[grp]['min_width'],
    from: cr?cr:mvItems[grp]['min_width'],
    to: cr?cr:mvItems[grp]['min_width']
  });
  if (cr) {
    mv_act_opened[grp] = id;
    mv_act_next[grp] = id;
    mv_act_now[grp] = id;
  }
}

var mv_act_now = {};
var mv_act_opened = {};
var mv_act_next = {};
var in_move = 0;
var in_next;
var canmove = 1;
function mvShowItem (grp,id,auto) {
  in_next = id;
  if (in_move) return;
  if (mv_act_now[grp] == id) return;
  if (1) {
	  mv_act_next[grp] = id;
	  mv_act_now[grp] = id;
	  in_move = 1;
	  canmove = 0;
	

  var sub = 0;
  	
	

	  mvItems[grp]['start'] = 0; // new Date().getTime() - parseInt(sub);
	 tick = 0;
	  for (var i=0;i<mvItems[grp]['items'].length;i++) {
	    mvItems[grp]['items'][i]['from'] = mvItems[grp]['items'][i]['current'];
	    if (mvItems[grp]['items'][i]['id'] == id) {
	      mvItems[grp]['items'][i]['to'] = mvItems[grp]['max_width'];
	    } else {
	      mvItems[grp]['items'][i]['to'] = mvItems[grp]['min_width'];
	    }
	  }
  }
}


function loadi_completed () {

}


function FindEnter (e,v) {
  var ev = null;
  window.event ? ev = window.event : ev = e;
  if (ev && ev.keyCode == 13) {
    location.href  = 'http://atlantpacif.ru/pages/inside/?open=search&text='+encodeURIComponent(v);
  }
}

