var brochurePhotoCount;
var brochurePhotoIndex;
var brochurePhotoScroll = 4;
var brochurePhotosScroller;
var brochurePhotosEffects;
var mainPhotoSrc;
var photosForVideo = 4;
var videoAvailable = false;

window.addEvent('domready', function()
  {
    if (window.ie6)
    {
      window.addEvent('load', nvgReloadedInit);
    }
    else
    {
      
      nvgReloadedInit();
    }
  }
);

function nvgReloadedInit()
{
  if (window.location.href.indexOf('showroom.php') >= 0)
  {
    nvgModelsInit();
  }
  else if (window.location.href.indexOf('brochure') >= 0)
  {
    
    if (window.location.href.indexOf('print=true') < 0)
    {
      
      nvgBrochureInit();
    }
  }
}

function roundModelStyles()
{
  if (NiftyCheck())
  {
    Rounded('div.ModelStyles', 'all', 'transparent', '#333');
    Rounded('div.ModelStylesDataEdge', 'bottom', '#333', '#fff');
    Rounded('li.ModelStylesMenuItem', 'top', '#333', 'transparent');
    Rounded('li.ModelStylesMenuItemSelected', 'top', '#333', 'transparent');
    Rounded('li.ModelStylesMenuClose', 'all', '#333', 'transparent', 'small');
    Rounded('div#Colours', 'all', 'transparent', '#333');
    Rounded('div#ColoursDataEdge', 'bottom', '#333', '#fff');
    Rounded('li#ColoursMenuClose', 'all', '#333', 'transparent', 'small');
  }
}

function nvgModelsInit()
{
  roundModelStyles();
}

function nvgBrochureInit()
{
  
  if ($('brochureSearch'))
  {
    if ($defined(brochureSearchDealer) && $defined(brochureSearchJatoId) && $defined(brochureSearchJatoLocale))
    {
      //new Request
      //(
      //  {
      //    url: '/showroom-brochure.php',
      //    method: 'get',
          //evalScripts: true,
      //    onComplete: function(data)
      //    {
            //$("brochureSearch").innerHTML = data;
            //updateResults('', '', '');
       //   },
      //    data:
       //   {
       //     //'d_id': brochureSearchDealer,
            //'jato_id': brochureSearchJatoId,
            //'jato_locale': brochureSearchJatoLocale,
            //'per_page': ($defined(brochureSearchPerPage) ? brochureSearchPerPage : 4)
      //    }
      //  }
      //).send();
    }
  }

  roundModelStyles();

  if (NiftyCheck())
  {
    Rounded('div#MakesMenu', 'all', 'transparent', '#dde', 'border #666');
  }

  var makeDivs = $$('div.Make');
  if (makeDivs.length > 1)
  {
    $('brochureShowroom').onclick = toggleMakesMenu;
  }
  else if (makeDivs.length > 0)
  {
    $('brochureShowroom').onclick = makeDivs[0].onclick;
  }

  $('mainPhotoEnlarge').onclick = showBrochurePhotoGallery;

  brochurePhotoCount = $$('li.brochurePhoto').length;
  if (brochurePhotoCount > brochurePhotoScroll)
  {
    brochurePhotoIndex = 1;

    brochurePhotosScroller = new Fx.Scroll('brochurePhotos',
      {
        wait: false,
        duration: 1000,
        offset: { 'x': -2, 'y': 0 },
        transition: Fx.Transitions.Cubic.easeInOut,
        onStart: function() { 
					$('brochurePhotos').set('tween', {duration: '250'});
					$('brochurePhotos').tween('opacity', '0.6');
        },
				onComplete: function() {
					$('brochurePhotos').set('tween', {duration: '250'});
					$('brochurePhotos').tween('opacity', '1.0');
				}
      }
    );

    $('photoLeft').addEvent('click', nvgBrochurePhotoLeft);
    $('photoRight').addEvent('click', nvgBrochurePhotoRight);
  }
  else if (brochurePhotoCount <= 1)
  {
    brochurePhotoIndex = -1;
    $('photoSlider').setStyle('display', 'none');
  }

  if (brochurePhotoCount < photosForVideo)
  {
    $('brochureVideo').setStyle('display', 'none');
  }
  else
  {
    videoAvailable = true;
  }

  var specsAccordion =
    new Fx.Accordion($$('div.brochureSpecsHeading'),
                     $$('div.brochureSpecsGroup'),
                     { alwaysHide: false, duration: 300,
                       onActive: function(heading)
                                 {
                                   var headingContent = heading.getFirst();
                                   headingContent.originalColor = headingContent.getStyle('background-color');
                                   headingContent.setStyle('background-color', '#F3C061');

                                   var selectorImage = headingContent.getFirst();
                                   if (selectorImage.get('tag') == 'img')
                                   {
                                     selectorImage.setProperties({ src: 'Strathcom/images/showroom/accordionSelected.gif', alt: 'selected' });
                                   }
                                 },
                       onBackground: function(heading)
                                     {
                                       var headingContent = heading.getFirst();
                                       if (headingContent.originalColor)
                                       {
                                         headingContent.setStyle('background-color', headingContent.originalColor);
                                       }

                                       var selectorImage = headingContent.getFirst();
                                       if (selectorImage.get('tag') == 'img')
                                       {
                                         selectorImage.setProperties({ src: 'Strathcom/images/showroom/accordionSelector.gif', alt: 'select' });
                                       }
                                     }
                     });
   updateResults(1);
}

function nvgBrochurePhotoLeft()
{
  if (brochurePhotoIndex - brochurePhotoScroll > 0)
  {
    brochurePhotoIndex = brochurePhotoIndex - brochurePhotoScroll;
    brochurePhotosScroller.toElement('brochurePhoto' + brochurePhotoIndex);
  }
}

function nvgBrochurePhotoRight()
{
  if (brochurePhotoIndex + brochurePhotoScroll < brochurePhotoCount)
  {
    brochurePhotoIndex = brochurePhotoIndex + brochurePhotoScroll;
    brochurePhotosScroller.toElement('brochurePhoto' + brochurePhotoIndex);
  }
}

function changeMake(jatoMake)
{
  if (jatoMake)
  {
    window.location = '/dcm2/nvgModels.html?make=' + jatoMake;
  }
}

function showModelStyles(stylesDivID)
{
  var stylesDivs = $$('div.ModelStyles');
  stylesDivs.each
  (
    function(stylesDiv)
    {
      if (stylesDiv.id == stylesDivID)
      {
        stylesDiv.setStyle('display', 'block');

        if (!stylesDiv.initFx)
        {
          stylesDiv.initFx = true;

          var yearDivs = $$('div#' + stylesDiv.id + ' div.ModelYear');
          yearDivs.each
          (
            function(yearDiv)
            {
              if (yearDiv.getStyle('display') != 'none')
              {
                yearDiv.accordionFx = 
                  new Fx.Accordion($$('div#' + yearDiv.id + ' div.ModelStylesHeading'),
                                   $$('div#' + yearDiv.id + ' div.ModelStylesGroup'),
                                   { alwaysHide: true, duration: 300,
                                     onActive: function(heading)
                                               {
                                                 var headingContent = heading.getFirst();
                                                 headingContent.originalColor = headingContent.getStyle('background-color');
                                                 headingContent.setStyle('background-color', '#F3C061');

                                                 var selectorImage = headingContent.getFirst();
                                                 if (selectorImage.get('tag') == 'img')
                                                 {
                                                   selectorImage.setProperties({ src: 'Strathcom/images/showroom/accordionSelected.gif', alt: 'selected' });
                                                 }
                                               },
                                     onBackground: function(heading)
                                                   {
                                                     var headingContent = heading.getFirst();
                                                     if (headingContent.originalColor)
                                                     {
                                                       headingContent.setStyle('background-color', headingContent.originalColor);
                                                     }

                                                     var selectorImage = headingContent.getFirst();
                                                     if (selectorImage.get('tag') == 'img')
                                                     {
                                                       selectorImage.setProperties({ src: 'Strathcom/images/showroom/accordionSelector.gif', alt: 'select' });
                                                     }
                                                   }
                                   });
              }
            }
          );
        }	
      }
      else
      {
        stylesDiv.setStyle('display', 'none');
      }
    }
  );
}

function hideModelStyles(stylesDivID)
{
  $(stylesDivID).setStyle('display', 'none');
}

function showModelYear(stylesDivID, year)
{
  var yearDivs = $$('div#' + stylesDivID + ' div.ModelYear');
  yearDivs.each
  (
    function(yearDiv)
    {
      if (yearDiv.id == stylesDivID + 'Year' + year)
      {
        yearDiv.setStyle('display', 'block');

        if (!yearDiv.accordionFx)
        {
          yearDiv.accordionFx = 
            new Fx.Accordion($$('div#' + yearDiv.id + ' div.ModelStylesHeading'),
                             $$('div#' + yearDiv.id + ' div.ModelStylesGroup'),
                             { alwaysHide: true, duration: 300,
                               onActive: function(heading)
                                         {
                                           var headingContent = heading.getFirst();
                                           headingContent.originalColor = headingContent.getStyle('background-color');
                                           headingContent.setStyle('background-color', '#F3C061');

                                           var selectorImage = headingContent.getFirst();
                                           if (selectorImage.get('tag') == 'img')
                                           {
                                             selectorImage.setProperties({ src: 'Strathcom/images/showroom/accordionSelected.gif', alt: 'selected' });
                                           }
                                         },
                               onBackground: function(heading)
                                             {
                                               var headingContent = heading.getFirst();
                                               if (headingContent.originalColor)
                                               {
                                                 headingContent.setStyle('background-color', headingContent.originalColor);
                                               }

                                               var selectorImage = headingContent.getFirst();
                                               if (selectorImage.get('tag') == 'img')
                                               {
                                                 selectorImage.setProperties({ src: 'Strathcom/images/showroom/accordionSelector.gif', alt: 'select' });
                                               }
                                             }
                             });
        }
      }
      else
      {
        yearDiv.setStyle('display', 'none');
      }
    }
  );

  var yearMenuItems = $$('div#' + stylesDivID + ' li');
  yearMenuItems.each
  (
    function(yearMenuItem)
    {
      if (yearMenuItem.hasClass('ModelStylesMenuItem') || yearMenuItem.hasClass('ModelStylesMenuItemSelected'))
      {
        if (yearMenuItem.innerHTML.indexOf(year) >= 0)
        {
          yearMenuItem.className = 'ModelStylesMenuItemSelected';
        }
        else
        {
          yearMenuItem.className = 'ModelStylesMenuItem';
        }
      }
    }
  );
}

function viewBrochure(vehicleID)
{
  if (vehicleID)
  {
    window.location = '/dcm2/nvgBrochure.html?id=' + vehicleID;
  }
}

function changeBrochureMainPhoto(normalPhoto, fullPhoto)
{
  $('mainPhotoEnlarge').href = fullPhoto;
  $('mainPhoto').src = normalPhoto;
}

function showBrochurePhotoGallery(e)
{
  var event = new Event(e);
  var url = this.href;
	
  event.preventDefault();

  if (!url.test(/%[A-Fa-f0-9]{2}/))
  {
    if ($type(encodeURI) == 'function')
    {
      url = encodeURI(url);
    }
    else
    {
      url = escape(url);
    }
  }
  this.blur();
  TB_show('', url, 'brochurePhotos');

  this.onclick = showBrochurePhotoGallery;

  return(false);
}

function toggleMakesMenu()
{
  var makesMenu = $('MakesMenu');
  var display;

  try
  {
    display = makesMenu.getStyle('display');
  }
  catch(e)
  {
    display = 'none';
  }

  if (display != 'none')
  {
    makesMenu.setStyle('display', 'none');
  }
  else
  {
    makesMenu.setStyle('display', 'block');
  }
}

function printBrochure()
{
  var url = window.location.href.replace(/\&vid=(\d+)\D*\&/, "&vid=$1&print=true&");
  window.open(url, '_blank');
}

function showColours()
{
  $('Colours').setStyle('display', 'block');
  mainPhotoSrc = $('mainPhoto').src;
}

function hideColours()
{
  $('Colours').setStyle('display', 'none');

  if (mainPhotoSrc)
  {
    $('mainPhoto').src = mainPhotoSrc;
  }
}

function showColourPhoto(colourPhoto)
{
  $('mainPhoto').src = colourPhoto;
}

function showVideo(dealerID, vehicleID)
{
  if (videoAvailable)
  {
    window.open('http://easy-api.robbg.dev/showroom/nvgVideoDialog?dealerID=' + dealerID + '&vehicleID=' + vehicleID, 'video' + vehicleID,
                'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=340,height=355,left=440,top=362');
  }
}

function viewResults(dealerID, localMake, localModel, bodyCode, localTrim, modelYear, drivenWheels, jatoDoors)
{
  var url = '/dcm-search/results_top.html?d_id=' + dealerID + '&class=NEW&make_name=' + localMake + '&model_name=' + localModel;

  if (bodyCode)
  {
    url += '&body_styles=' + bodyCode;
  }

  if (localTrim)
  {
    url += '&trim=' + localTrim;
  }

  if (modelYear)
  {
    url += '&year_from=' + modelYear + '&year_to=' + modelYear;
  }

  if (drivenWheels)
  {
    url += '&drive=' + drivenWheels;
  }

  if (jatoDoors)
  {
    url += '&doors=' + jatoDoors;
  }

  window.location = url;
}

function viewNoResults(dealerID, vehicleID)
{
  window.location = '/dcm2/novehicleform.html?d_id=' + dealerID + '&vehicle_id=' + vehicleID;
}

function showVehicleType(vehicleTypeID)
{
  var vehicleTypeDivs = $$('div.Type');
  vehicleTypeDivs.each
  (
    function(vehicleTypeDiv)
    {
      if (vehicleTypeDiv.id == vehicleTypeID)
      {
        vehicleTypeDiv.setStyle('display', 'block');

      }
      else
      {
        vehicleTypeDiv.setStyle('display', 'none');
      }
    }
  );
}

