var _Slider_Auto = false;
var _Slider_AutoTimer = null;
var _Slider_AutoDelay = 5000;

$(function(){
  $("select[label='Desired Vehicle']").each(function(e){
    Form_SelectVehicle(this);
  });

  $("a[class^=slider-arrow]").click(function(e){
    return SliderArrow_Click(this);
  }).dblclick(function(e){
    return SliderArrow_Click(this);
  });
  
  var arr = $("a[class^=slider-arrow].slider-auto");
  if (arr.length > 0) {
    _Slider_Auto = true;
    Slider_InitAuto();
  }
  
  $("div.slideshow-container-inner").each(function(e){
    SlideShow_Init($(this));
  });
});

function SliderArrow_Click(arrow) {
  var resetAuto = false;
  if (_Slider_AutoTimer) {
    window.clearTimeout(_Slider_AutoTimer);
    resetAuto = true;
  }

  var arr = $(arrow);
  var table = arr.closest("table").eq(0);
  var index = parseInt(table.attr("slider-index"));
  var maxIndex = parseInt(table.attr("max-index"));
  var panelSize = parseInt(table.attr("panel-size"));
  var viewport = table.find("div.slider-viewport").eq(0);
  var axis = "y";
  var animDelay = 200;
  
  var newIndex = index;
  
  if (arr.hasClass("slider-arrow-right")) {
      axis = "x";
      newIndex += 1;
  } else if (arr.hasClass("slider-arrow-left")) {
      axis = "x";
      newIndex -= 1;
  } else if (arr.hasClass("slider-arrow-bottom")) {
      newIndex += 1;
  } else if (arr.hasClass("slider-arrow-top")) {
      newIndex -= 1;
  }
    
  if (newIndex < 0) { newIndex = maxIndex; animDelay = 500; }
  if (newIndex > maxIndex) { newIndex = 0; animDelay = 500; }
  table.attr("slider-index", newIndex);

  viewport.scrollTo((newIndex * panelSize), { axis: axis, duration: animDelay });
  
  if (resetAuto) Slider_InitAuto();

  return false;
}

function Slider_AutoScroll() {
  window.clearTimeout(_Slider_AutoTimer);
  _Slider_AutoTimer = null;
  
  $("a[class^=slider-arrow].slider-auto").click();  
  
  Slider_InitAuto();
}

function Slider_InitAuto() {
  _Slider_AutoTimer = window.setTimeout("Slider_AutoScroll()", _Slider_AutoDelay);
}

function SlideShow_Init(container) {
  effect = container.attr("effect");
  speed = 1000 + parseInt(container.attr("speed")) * 100;
  timeout = parseInt(container.attr("speed")) * 1000; 
  
  container.cycle({
    fx :        effect,
    speed :     speed,
    timeout :   timeout
  });
}

// Form elements

function Form_SelectVehicle(list) {
  var vid = list.value;
  var div = $(list).next("div.formvehicle-details");
  if (vid=="") {
    div.html("");
    div.hide();
  } else {
    //div.html("");
    div.show();
    var url = _AppUrls.Clients + "ajax/formvehicle.aspx?ClientID=" + _ClientId + "&VehicleID=" + vid + "&TS=" + Number(new Date());
    $("#divVehicleInfo").load(url + " #divMain");
    div.load(url);
  }
}
function SelectMake(make) {
 var vtype = $("input[id$='xVehicleType']").get(0).value;

 var models = $("select.model").get(0);
 if (vtype != "O") {
   while (models.length > 0) models.remove(0);
   var selectedMake = make.options[make.selectedIndex].value;
   if (MakeList[selectedMake] === undefined) {
     var opt = document.createElement("OPTION");
     models.options.add(opt);
     opt.text = "Other";
     opt.value = "Other";
   } else {
     if (MakeList[selectedMake].length > 0) {
       //var om = false;
       for (i = 0; i < MakeList[selectedMake].length; i++) {
         if (MakeList[selectedMake][i] == "Other") {
           //om = true;
         }
         else {
           var opt = document.createElement("OPTION");
           models.options.add(opt);
           opt.text = MakeList[selectedMake][i];
           opt.value = MakeList[selectedMake][i];
         }
       }

       var opt = document.createElement("OPTION");
       models.options.add(opt);
       opt.text = "Other";
       opt.value = "Other";

     }
     else {

       var opt = document.createElement("OPTION");
       models.options.add(opt);
       opt.text = "All Models";
       opt.value = "All Models";
     }
   } 
 }
}
