﻿//-----------------------------------------------------------------------
// Copyright (C) Motorwebs Corporation. All rights reserved.
//-----------------------------------------------------------------------

var _inv = "";
var _vin = "";
var _pos = "";
var _total = "";
var _index = 0;
var _interval = null;
var _gallery = false;

function pageLoad() {
    Sys.Application.add_navigate(onStateChanged);
    $addHandler(window, 'resize', onGalleryResize);
}

function pageUnload() {
    Sys.Application.remove_navigate(onStateChanged);
    $removeHandler(window, 'resize', onGalleryResize);
}

function onStateChanged(sender, state) {

    var so = state.get_state();

    var s = so.s;
    if (s == undefined) s = "";

    var request = new Motorwebs.NET35.InventoryDisplay.DetailPostRequest();

    switch (s) {
        case "sp":
            setRequest(request, so);
            request.ScrollDirection = so.dir;
            InventoryDisplay.DetailEvents(request, onSuccess, onFailure, "Scroll");
            break;
        case "sv":
            setRequest(request, so);
            InventoryDisplay.DetailEvents(request, onSuccess, onFailure, "Similar");
            break;
        default:
            setRequestFromUrl(request);
            InventoryDisplay.DetailEvents(request, onSuccess, onFailure, "Similar");
            break;
    }
}

function scrollPost(direction) {
  showLoader();
  getDetailVars();
  Sys.Application.addHistoryPoint({ s: "sp", dir: direction, inv: _inv, vin: _vin, pos: _pos, tot: _total });
}

function similarVehiclesPost(inv, vin) {
  showLoader();
  getDetailVars();
  Sys.Application.addHistoryPoint({ s: "sv", inv: inv, vin: vin, pos: _pos, tot: _total });
}

function sendMail() {
  showLoader();
  // validate form
  if (!validateForm()) return false;

  var fs = $get('FormSubmit');
  fs.disabled = true;

  // create form post
  var values = '';
  var names = $get("FieldNames").value;
  var namesArr = names.split(',');
  for (var i = 0; i < namesArr.length; i++) {
    if (i > 0) values += '|';
    values += $get(namesArr[i]).value;
}
  
  // call web service	
  InventoryDisplay.SendMail(names, values, onSuccess, onFailure, "SendMail");
  return false;
}

function getDetailVars() {

  // set request properties	held in array
  var propArr = eval($get("PropertyArray").value);
  Array.forEach(propArr, setProps);

  function setProps(arr) {
    switch (arr[0]) {
      case 'inv':
        _inv = arr[1];
        break;
      case 'vin':
        _vin = arr[1];
        break;
      case 'pos':
        _pos = arr[1];
        break;
      case 'total':
        _total = arr[1];
        break;
    }
  }
}

function setRequest(request, so) {

  request.Filter = $get("Filter").value;
  request.BrowseState = $get("BrowseState").value;

  // set request properties	held in array
  var propArr = eval($get("PropertyArray").value);
  Array.forEach(propArr, setProps);

  function setProps(arr) {
    switch (arr[0]) {
      case 'state':
        request.State = arr[1];
        break;
      case 'vehState':
        request.VehState = arr[1];
        break;
      case 'inv':
        request.InventoryID = so.inv;
        break;
      case 'vin':
        request.VIN = so.vin;
        break;
      case 'pos':
        request.Position = so.pos;
        break;
      case 'total':
        request.Total = so.tot;
        break;
      case 'sort':
        request.SortBy = arr[1];
        break;
    }
  }
}

function setRequestFromUrl(request) {

  request.Filter = $get("Filter").value;
  request.BrowseState = $get("BrowseState").value;

  // set request properties	held in array
  var propArr = eval($get("PropertyArray").value);
  Array.forEach(propArr, setProps);

  function setProps(arr) {
    switch (arr[0]) {
      case 'state':
        request.State = arr[1];
        break;
      case 'vehState':
        request.VehState = arr[1];
        break;
      case 'sort':
        request.SortBy = arr[1];
        break;
    }
  }

  var url = location.search;
  var urlArr = url.split('&');

  var prmArr = null;
  for (var i = 0; i < urlArr.length; i++) {
    prmArr = urlArr[i].split('=');
    switch (prmArr[0]) {
      case 'inv':
        request.InventoryID = prmArr[1];
        break;
      case 'vin':
        request.VIN = prmArr[1];
        break;
      case 'pos':
        request.Position = prmArr[1];
        break;
      case 'total':
        request.Total = prmArr[1];
        break;
    }
  }
}

function getCurrentDetailVars() {
  _inv = $get("Inv").value;
  _vin = $get("VIN").value;
  _pos = $get("Pos").value;
  _total = $get("Total").value;
}

function onSuccess(response, context, methodName) {
  switch (context) {
    case 'SendMail':
      var smr = $get('SendMailResponse');
      smr.innerHTML = response;
      smr.style.visibility = 'visible';
      break;
    case "Scroll":
      insertContent(response);
      break;
    case "Similar":
      insertContent(response);
      break;
    case "ImageGallery":

      var scrollWidth = (document.documentElement.scrollWidth ? document.documentElement.scrollWidth : document.body.scrollWidth);
      var scrollHeight = (document.documentElement.scrollHeight ? document.documentElement.scrollHeight : document.body.scrollHeight);

      // enable overlay
      var ol = $get("ImageGallery-Overlay");
      var point = $common.getLocation(ol);
      Sys.UI.DomElement.setLocation(ol, -point.x, -point.y);
      $common.setElementOpacity(ol, .5);
      ol.style.width = scrollWidth + "px";
      ol.style.height = scrollHeight + "px";
      ol.style.display = "block";

      // enable large image scroll
      var gal = $get("ImageGallery");
      gal.innerHTML = response.Html;
      var width = $common.parseUnit($common.getCurrentStyle(gal, "width"));
      var height = $common.parseUnit($common.getCurrentStyle(gal, "height"));

      var cb = $common.getClientBounds();
      var x = Math.round(cb.width / 2 - width.size / 2);
      var y = Math.round(cb.height / 2 - height.size / 2);
      if (y < 0) y = 0;
      Sys.UI.DomElement.setLocation(gal, x, y);
      gal.style.display = "block";

      var imageCount = $get('ImageCount1');
      if (imageCount != null) imageCount.innerHTML = "( " + (_index + 1) + " of " + images.length + " )";

      startSlideShow1();

      break;
  }

  hideLoader();
}

function showGallery(vehicleID) {
  _gallery = true;
  var request = new Motorwebs.NET35.InventoryDisplay.DetailPostRequest();
  request.State = "ImageGallery";
  request.VehicleID = vehicleID;
  InventoryDisplay.DetailEvents(request, onSuccess, onFailure, "ImageGallery");
}

function closeGallery() {
  _gallery = false;
  var ol = $get("ImageGallery-Overlay");
  ol.style.display = "none";
  var gal = $get("ImageGallery");
  gal.style.display = "none";
}

function onGalleryResize() {
  if (_gallery) {

    var scrollWidth = (document.documentElement.scrollWidth ? document.documentElement.scrollWidth : document.body.scrollWidth);
    var scrollHeight = (document.documentElement.scrollHeight ? document.documentElement.scrollHeight : document.body.scrollHeight);

    // enable overlay
    var ol = $get("ImageGallery-Overlay");
    var point = $common.getLocation(ol);
    Sys.UI.DomElement.setLocation(ol, -point.x, -point.y);
    $common.setElementOpacity(ol, .5);
    ol.style.width = scrollWidth + "px";
    ol.style.height = scrollHeight + "px";

    // enable large image scroll
    var gal = $get("ImageGallery");
    var width = $common.parseUnit($common.getCurrentStyle(gal, "width"));
    var height = $common.parseUnit($common.getCurrentStyle(gal, "height"));
    var cb = $common.getClientBounds();
    var x = Math.round(cb.width / 2 - width.size / 2);
    var y = Math.round(cb.height / 2 - height.size / 2);
    if (y > 0) Sys.UI.DomElement.setLocation(gal, x, y);
  }
}

function insertContent(response) {
    index = 0;
    thumbs = eval(response.Thumbs);
    images = eval(response.Images);
    if (typeof(imagesLarge) != "undefined") imagesLarge = eval(response.ImagesLarge);
    document.title = response.Title;
    $get('ContentAJAX').innerHTML = response.Html;
    vehicleID = response.VehicleID;
    mediaID = response.MediaID;

    clearInterval(interval);
    var imageCount = $get('ImageCount');
    if (imageCount != null) imageCount.innerHTML = (index + 1) + " of " + images.length;
    startSlideShow();

    if (mediaID != "") {
        lastImagesTab = null;
        lastImagesContent = null;
        if (typeof (videoFirstTab) != "undefined") {
            if (videoFirstTab) showVideo();
        }
    }

    if (typeof(evoxVideoUrl) != "undefined") {
        lastImagesTab = null;
        lastImagesContent = null;
        showExteriorSpin();
        showInteriorSpin();
    }
}



function onFailure(error, context, methodName) {
  var msg = "The " + methodName + " method called by " + context + " Failed! - " + error.get_message();
  alert(msg);
}

function showLoader() {
  var loader = $get("AjaxLoader");
  if (loader != null) {

    var b = $common.getBounds(loader);

    var cb = $common.getClientBounds();
    var w = cb.width;
    var h = cb.height;

    var x = Math.round(w / 2 - b.width / 2);
    var y = Math.round(h / 2 - b.height / 2);

    Sys.UI.DomElement.setLocation(loader, x, y);
    $common.setVisible(loader, true);    
  }
}

function hideLoader() {
  var loader = $get("AjaxLoader");
  if (loader != null) {
    //Sys.UI.DomElement.setVisible(loader, false)
    loader.style.visibility = "hidden";
  }
}

function scroll1(direction) {
    var thumb;
    var thumbCount = 0;
    var pos = 0;
    var startPos = 0;
    var loopCount = 0;

    if (thumbs.length > 7) {
        thumbCount = 7;
        startPos = 1;
        loopCount = 7;
    }
    else
        if (thumbs.length > 5) {
        thumbCount = 5;
        startPos = 2;
        loopCount = 6;
    }
    else
        if (thumbs.length > 3) {
        thumbCount = 3;
        startPos = 3;
        loopCount = 5;
    }

    if (thumbCount == 0) return;

    if (direction == 'left')
        _index = (_index == 0) ? thumbs.length - 1 : _index - 1;
    else
        _index = (_index >= (thumbs.length - 1)) ? 0 : _index + 1;

    pos = (Math.floor(thumbCount / 2) + 1) * -1;

    for (var i = startPos; i <= loopCount; i++) {

        thumb = document.getElementById("ThumbGallery" + i);
        if (!thumb) return;

        pos += 1;

        if (pos < 0) {
            thumb.src = (_index + pos >= 0) ? thumb.src = thumbs[_index + pos].url : thumbs[thumbs.length + (_index + pos)].url;
        }
        else {
            if (pos == 0) {
                thumb.src = thumbs[_index].url;
            }
            else {
                thumb.src = (_index + pos >= thumbs.length) ? thumbs[(_index + pos) - thumbs.length].url : thumbs[_index + pos].url;
            }
        }
    }

    document.getElementById("Image1").src = imagesLarge[_index].url;
    var imageCount = document.getElementById("ImageCount1");
    if (imageCount != null) imageCount.innerHTML = "( " + (_index + 1) + " of " + imagesLarge.length + " )";
}

function thumbClick1(pos) {
  stopSlideShow1();
  if (pos > 0) {
    if ((_index + pos) > thumbs.length)
      _index = (_index + pos) - thumbs.length;
    else
      _index = _index + pos;
    scroll1('left');
  }
  else {
    if ((_index + pos) >= 0)
      _index = _index + pos;
    else
      _index = thumbs.length + (_index + pos);
    scroll1('right');
  }
}

function leftScrollClick1() {
  stopSlideShow1();
  scroll1('left');
}

function rightScrollClick1() {
  stopSlideShow1();
  scroll1('right');
}

function startSlideShow1() {
  if (!slideShow) return;
  if (slideShowCheckbox_uncheck) return;

  if (slideShowCheckbox) {
    var chkBox = document.getElementById("SlideShowCheckbox1");
    if (chkBox != null) chkBox.checked = true;
  }
  clearInterval(_interval);
  _interval = window.setInterval("scroll1('right')", slideShowInterval);
}

function stopSlideShow1() {
  if (slideShowCheckbox) {
    var chkBox = document.getElementById("SlideShowCheckbox1");
    if (chkBox != null) chkBox.checked = false;
  }
  clearInterval(_interval);
}

function slideShowCheckboxClick1() {
    var chkBox = document.getElementById("SlideShowCheckbox1");
    if (chkBox.checked) {
        slideShowCheckbox_uncheck = false;
        slideShow = true;
        startSlideShow1();
    }
    else
        stopSlideShow1();
}

function swapImage1(pos) {
    stopSlideShow1();
    _index = pos - 1;
    document.getElementById("Image1").src = imagesLarge[_index].url;
}