var nameModels = ['', 'accord', 'civic-typer', 'civic4d', 'civic5d', 'crv', 'jazz', 'legend', 'pilot', '', '',    '', '', '', '', '', '', '', '', '', '',   'powertech1','powertech2','powertech3','powertech4'];
//var linkModels = ['', 'accord', 'civictyper', 'civic4d', 'civic5d', 'crv', 'jazz', 'legend', 'pilot', '', '',     '', '', '', '', '', '', '', '', '', '',   'powertech1','powertech2','powertech3','powertech4'];
var linkModels = ['', 'accord', 'civictyper', 'civic4d', 'civic5d', 'crv', 'jazz', 'legend', 'pilot' ];
var sublink = ['review', 'basket', 'engine',    'carriage', 'salon', 'features',    'safety', 'accessories', 'price',  'exterior', 'interior', 'colors'];
var sublinkTitle = ['Обзор', 'Кузов', 'Двигатель',    'Шасси', 'Салон', 'Характеристики',    'Безопасность', 'Аксессуары', 'Цены',    'Экстерьер', 'Интерьер', 'Цвета'];
var menuMatrix = [
  [],
  [1,1,1, 1,1,1, 1,1,1, 1,1,1],  //accord
  [1,1,1, 0,1,1, 1,1,1, 1,1,1],  //civic-typer
  [1,1,1, 1,1,1, 1,1,1, 1,1,1],  //civic-4d
  [1,1,1, 1,1,1, 1,1,1, 1,1,1],  //civic-5d
  [1,1,1, 0,1,1, 1,1,1, 1,1,1],  //cr-v
  [1,1,1, 0,1,1, 1,1,1, 1,1,1],  //jazz
  [1,1,1, 0,1,1, 1,1,1, 1,1,1],  //legend
  [1,1,1, 0,1,1, 1,1,1, 1,1,1]   //pilot 
];
var imgModelsOver = [];
var imgModelsOut = [];
var loaded = 0;
var modelContainerNewPosition = 58;
var hidePanelTimer = -1;
var lastModelID = -1;
var currentModelContainerOpacity = 100;


function hideLastModelSelected()
{
  if (lastModelID != -1)
  {
    var item = $("item"+lastModelID);
    var img = $("imgModel"+lastModelID);
    item.className = 'item'+lastModelID;
    if (loaded)
    {
      img.src = imgModelsOut[lastModelID].src;
    }
    lastModelID = -1;
  }
}

function setModelContainerOpacity(num)
{
  currentModelContainerOpacity = num;
  $('modelContainer').style.filter = 'alpha(opacity=' + currentModelContainerOpacity + ')';;
  $('modelContainer').style.MozOpacity = currentModelContainerOpacity/100;
  $('modelContainer').style.opacity = currentModelContainerOpacity/100;
}

function showModelContainer()
{
  if ($('modelContainer').style.display != 'block')
  {
    setModelContainerOpacity(100);
    $('modelContainer').style.display = 'block';
  }
}

function hideModelContainerTimer()
{
  currentModelContainerOpacity -= 10;
  if (currentModelContainerOpacity < 0)
  {
    currentModelContainerOpacity = 0;
    $('modelContainer').style.display = 'none';
  } else {
    setTimeout('hideModelContainerTimer()', 50);
  }
  setModelContainerOpacity(currentModelContainerOpacity);
}

function hideModelContainer()
{

  hideModelContainerTimer();
  hideLastModelSelected();
}

function modelContainerMove()
{
  var leftStr = new String($('modelContainer').style.left);
  var leftInt = new Number(leftStr.substr(0, leftStr.length-2));
  if (Math.abs(leftInt - modelContainerNewPosition) > 1)
  {
    var delta = (modelContainerNewPosition - leftInt)/2;
    var nw = leftInt + delta;
    $('modelContainer').style.left = nw + 'px';
    //if ($('debug').innerHTML.length > 200) $('debug').innerHTML = '';
    //$('debug').innerHTML += "<li>" + $('modelContainer').style.left + ' | ' + delta;
  } else {
    $('modelContainer').style.left = modelContainerNewPosition;
  }
}


// загружаем изображения названий моделей
function preloadModels()
{
  for (var i = 1; i < nameModels.length; i++)
  {
    //over
    imgModelsOver[i] = new Image(); imgModelsOver[i].src = 'images/design/logo-'+nameModels[i]+'-over.gif';
    //out
    imgModelsOut[i] = new Image(); imgModelsOut[i].src = 'images/design/logo-'+nameModels[i]+'.gif';
  }
  loaded = 1;
}

function modelOver(id)
{
  //отключаем таймер на скрытие
  clearTimeout(hidePanelTimer);

  hideLastModelSelected();
  lastModelID = id;

  var item = $("item"+id);
  var img = $("imgModel"+id);
  item.className = 'item'+id+'-over';
  if (loaded)
  {
    img.src = imgModelsOver[id].src;
	//alert(imgModelsOver[id].src);
  }
  for (var i = 1; i < nameModels.length; i++)
  {
    if (id == i)
    {
        if ( $('model'+i) )  $('model'+i).style.display = 'block';
    } else
    {
	if ( $('model'+i) )  $('model'+i).style.display = 'none';
    }
  }

  if ( id>20 ) modelContainerNewPosition = 74 + (id-4)*4;
  else modelContainerNewPosition = 58 + id*12;
  showModelContainer();
}

function modelOut(id)
{
  //включаем таймер на скрытие
  hidePanelTimer = setTimeout('hideModelContainer()', 1000);
}

function modelContainerOver()
{
  //отключаем таймер на скрытие
  clearTimeout(hidePanelTimer);
}

function modelContainerOut()
{
  //включаем таймер на скрытие
  hidePanelTimer = setTimeout('hideModelContainer()', 1000);
}

function init()
{
  $('modelContainer').style.left = 58;
  setInterval('modelContainerMove()', 50);
}

function linksGenerate()
{
  for (var i = 1; i < nameModels.length; i++)
  {
    var menu = $('modelMenu'+i);
    var out = "";

    if ( menu )
    {
	    for (var k = 0; k < 4; k++)
	    {
	      out += '<div class="menu' + (k+1) + '">';
	      for (var j = k*3; j < (k+1)*3; j++)
	      {
		if ( menuMatrix[i]!=undefined )
		{
		        if (menuMatrix[i][j] == 1)
		        {
		          out += '<a href="' + linkModels[i] + '/' + sublink[j] + '.html" class="menuitem">' + sublinkTitle[j] + '</a><br>';
		        } else {
		          out += '<span class="menuitem-disable">' + sublinkTitle[j] + '</span><br>';
		        }
		}
	      }
	      out += '</div>';
	    }
	menu.innerHTML = out;
    }//end_ if
    
  }
}
