$ = function (id) {
	return document.getElementById(id);
};

getXmlHttp = function(){
  var xmlhttp;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
};

/* Класс Авто */
Auto = function (type, alias, title, description, isfull, container) {
	this.Type = type;
	this.Alias = alias;
	this.Title = title;
	this.Description = description;
	this.IsFull = isfull;
	this.Img = document.createElement('img');
    //this.Img.onload = function () { try { /*alert(this.complete + "Загрузилась " + this.src);*/ } catch (e) {} };
    this.Img.style.zIndex = 0;
    if (container != null)
		container.insertBefore(this.Img, container.childNodes[0]);
  this.Img.style.opacity= 1;
  this.Img.style.filter= "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
  this.Img.style.filter= "alpha(opacity=100)";
   	this.Img.src = "/img/auto/"+this.Alias+",x.jpg";
	this.next = null;
	this.prev = null;
};

Slider = function(){
	this.container = null;
	this.nodeImg = null;
	this.nodeImgNext = null;
	
	this.nodeTxt = null;
	this.nodeTxtTitle = null;
	this.nodeTxtDesc = null;
	this.nodeTxtLink = null;
	
	this.Init = function(){
			this.container = $("slideshow");
			this.nodeImg = this.container.childNodes[0];
			this.nodeImgNext = document.createElement('img');
    		this.nodeImgNext.onload = function () { try { /*alert(this.complete + "Загрузилась " + this.src);*/ } catch (e) {} };
    		this.nodeImgNext = this.container.appendChild(this.nodeImgNext);
			this.nodeImgNext.style.zIndex = 0;
			this.nodeImgNext.style.opacity= 0;
			this.nodeImgNext.style.filter= "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
			this.nodeImgNext.style.filter= "alpha(opacity=0)";
			
			
			this.nodeTxt = $("slidetxt");
			for(i=0;i<this.nodeTxt.childNodes.length;i++){
				if(this.nodeTxt.childNodes[i].tagName=="H3")
					this.nodeTxtTitle = this.nodeTxt.childNodes[i];
				else if(this.nodeTxt.childNodes[i].tagName=="p")
					this.nodeTxtDesc == this.nodeTxt.childNodes[i];
				else if(this.nodeTxt.childNodes[i].tagName=="A")
					this.nodeTxtLink = this.nodeTxt.childNodes[i];
			}
	}; // метод Init()
};

animate = function(tagId,alfa,step){
 div = $(tagId);
 var item = new Array();
 //Выбираем все рисунки слайдшоу
 for(c=i=0;i<div.childNodes.length;i++){
  if (div.childNodes[i].tagName=="IMG"){
   item[c] = div.childNodes[i];
   c++;
  }
 }
 last = item[item.length-1];
 next = item[item.length-2];
 //делаем верхний в стопке(текущий) рисунок более прозрачным
 last.style.opacity= alfa/100;
 last.style.filter= "progid:DXImageTransform.Microsoft.Alpha(opacity="+alfa+")";
 last.style.filter= "alpha(opacity="+alfa+")";
 
 nodeTxt.style.opacity= alfa/100;
 nodeTxt.style.filter= "progid:DXImageTransform.Microsoft.Alpha(opacity="+alfa+")";
 nodeTxt.style.filter= "alpha(opacity="+alfa+")";

 if ((alfa-step)>0){
  //если еще не достигли полной прозрачности верхнего рисунка - продолжаем анимацию
   setTimeout("animate('"+tagId+"',"+(alfa-step)+","+step+");",50);
 }else{
  //если достигли полной прозрачности верхнего рисунка
  //делаем абсолютно непрозрачным следующий рисунок
  next.style.opacity= 1;
  next.style.filter= "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
  next.style.filter= "alpha(opacity=100)";
  // перелистываем авто и заполняем данные
  indexAuto--;
  if(indexAuto < 0)
  	indexAuto = autos.length-1;
  for(i=0;i<nodeTxtTitle.childNodes.length;i++)
	nodeTxtTitle.removeChild(nodeTxtTitle.childNodes[i]);
  nodeTxtTitle.appendChild(document.createTextNode("— "+autos[indexAuto].Title)); 
  for(i=0;i<nodeTxtDesc.childNodes.length;i++)
	nodeTxtDesc.removeChild(nodeTxtDesc.childNodes[i]);
  nodeTxtDesc.appendChild(document.createTextNode(autos[indexAuto].Description)); 
  nodeTxtLink.href = "/order/"+autos[indexAuto].Type.toLowerCase()+"."+autos[indexAuto].Alias;
  nodeTxtLink2.href = "/autopark/"+autos[indexAuto].Alias;

  nodeTxt.style.opacity= 1;
  nodeTxt.style.filter= "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
  nodeTxt.style.filter= "alpha(opacity=100)";
  
  // а верхний рисунок перемещаем в низ стопки
  tmp = last;
  div.removeChild(last);
  div.insertBefore(tmp,item[0]);
  tmp.style.opacity= 1;
  tmp.style.filter= "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
  tmp.style.filter= "alpha(opacity=100)";

setTimeout( "slideSwitch('"+tagId+"',1000)", 6000 );
 }
};

//эта функция делает видимым блок с рисунками для слайдшоу (изначально он невидим, чтобы избежать мерцания во время загрузки картинок) и запускает анимацию
slideSwitch = function(tagId,speed){
 div = document.getElementById('slideshow');
 if (div.style.visibility!="visible"){
      div.style.visibility = "visible";
 }
 items = div.getElementsByTagName('img');
 if (items.length>0){
  animate(tagId,100,10);
 }
};
var autos = new Array();
var indexAuto = 0;
var nodeTxt = null;
var nodeTxtTitle = null;
var nodeTxtDesc = null;
var nodeTxtLink = null;
var nodeTxtLink2 = null;
Init = function(){
	var req = getXmlHttp();
	req.open("POST", "/ajax/autolist.xml", true); 
	req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	req.onreadystatechange = function() {
	  if (req.readyState == 4) {
		if(req.status == 200) {
			//alert(req.responseText);
			var al = req.responseXML.getElementsByTagName("Auto");
			var container = $("slideshow");
			for( var i=al.length-1; i>=0; i-- )
			{
				var auto = al.item(i);
				var type = auto.getAttribute("Type" ).toString();
				//alert(auto.getElementsByTagName("Alias").length+"---"+auto.getElementsByTagName("Alias")[0].childNodes[0].nodeValue);
				var alias = auto.getElementsByTagName("Alias").length>0?auto.getElementsByTagName("Alias")[0].childNodes[0].nodeValue:"";
				var title = auto.getElementsByTagName("Title").length>0?auto.getElementsByTagName("Title")[0].childNodes[0].nodeValue:"";
				var description = auto.getElementsByTagName("ShortDescription").length>0?auto.getElementsByTagName("ShortDescription")[0].childNodes[0].nodeValue:"";
				var isfull = false;
				if(i==al.length-1)
					autos[i] = new Auto(type, alias, title, description, isfull, null);
				else
					autos[i] = new Auto(type, alias, title, description, isfull, container);
				//alert(i+":"+autos[i].Type+"-"+autos[i].Alias+"-"+autos[i].Title+"-"+autos[i].Description);
			}
			indexAuto = autos.length-1;
			setTimeout( "slideSwitch('slideshow',1000);",2000 );
		}
	  }
	};
	req.send("");

	nodeTxt = $("slidetxt");
	for(i=0;i<nodeTxt.childNodes.length;i++){
		if(nodeTxt.childNodes[i].tagName=="H3")
			nodeTxtTitle = nodeTxt.childNodes[i];
		else if(nodeTxt.childNodes[i].tagName=="P")
			nodeTxtDesc = nodeTxt.childNodes[i];
		else if(nodeTxt.childNodes[i].tagName=="DIV")
		{
			for(j=0;j<nodeTxt.childNodes[i].childNodes.length;j++)
				if(nodeTxt.childNodes[i].childNodes[j].tagName=="A")
				{
						if(nodeTxtLink==null)
							nodeTxtLink = nodeTxt.childNodes[i].childNodes[j];
						else
							nodeTxtLink2 = nodeTxt.childNodes[i].childNodes[j];
				}
		}
	}
};

