var imageRotator = '';
var vehicleRotator = '';
var promotionsGallery = '';
var promoHeight = '';
var bannerHeight = '';
var minHeight = '';
$(document).ready(function(){
	$('#topMenu').superfish(
  {
    speed:       'fast',
    autoArrows:  true,
    dropShadows: false
  });
$(".leadButton").removeAttr("onclick");
  	Shadowbox.init({
    players: ['html', 'iframe']
	  });
  imageRotator = new quicklinkScroller(
  {
    id: "#imageRotatorContainer",
    speed: 1000,
    autoScroll: true,
    scrollTime: 3,
    pauseOnRoll: true,
    windowElement: "#rotatorImages",
    targetElement: ".rotatorImage",
    loop: true,
    itemsInView: 1,
    objectName: "imageRotator",
    transitionType: "fade",
    imageCount: false
  });

  vehicleRotator = new Shiftr(
  {
    bindTo: '#preowned',
    animation: 'slide_horizontal',
    jumpAmount: 3
  });

  $('#promotions br').each(function() {
    $(this).remove();
  });
  $('#promotions #promoBanner').each(function() {
    $(this).addClass('promoBanners');
  });
  $('#promotions #promoBanner img').each(function() {
    $(this).remove('style');
  });

  promotionsGallery = new Shiftr(
  {
    bindTo: '#promotions',
    animation: 'slide_horizontal',
    jumpAmount: 1
  });

  $('#featuresContainer .tab.off').each(function(i, elem){
    $('#'+$(elem).attr('tab')).hide();
  });

  bindClickEvent();
  bindSearchEvent();
  bindDropdownClickEvent();
  bindSearchClickEvent();
  bindDocClick();
});

function setTabHeight()
{
  promoHeight = $('#'+$('.tab.active').attr('tab')).attr('height');
  var activeTab = $('.tab.active').attr('tab');
  switch (activeTab)
  {
    case 'promotions':
      bannerHeight = $('#promoBanner:first').outerHeight();
      break;
    case 'preowned':
      bannerHeight = $('#vehicleSpecials').outerHeight();
      break;
    case 'motorsports':
      bannerHeight = $('#motorsportsContent').outerHeight();
      break;
    case 'trailers':
      bannerHeight = $('#trailersContent').outerHeight();
      break;
  }

  $('#'+$('.tab.active').attr('tab')).css({ 'height' : bannerHeight+'px' });
}

function setHeight() 
{
  $('#featuredContent').toggle();
  minHeight = $(window).height() - $('#superContainer').height();
  if(minHeight < 0)
    minHeight = 0;
  $('#featuredContent').toggle();

  $('#featuredContent').css({'min-height' : minHeight+'px'});

  $('.tab').each(function() {
    $('#'+$(this).attr('tab')).attr('height', $('#'+$(this).attr('tab')).height());
  });

  setTabHeight();

  if(promoHeight > bannerHeight)
    $('#expandContent').show();

  $('#expandContent').click(function(){
    $('#'+$('.tab.active').attr('tab')).animate({ height : promoHeight+'px' }, 1000);
    $(this).hide();
  });
}

function bindClickEvent() 
{
  $('#featuresContainer .tab').click(function(){
    if(!$(this).hasClass('active') && !$(this).hasClass('exlink')) {
      $('.tabbedPage').stop(true, true);
      $('#'+$('.tab.active').attr('tab')).fadeOut('250');
      $('#featuresContainer .tab.active').removeClass('active').addClass('off');

      $(this).addClass('active').removeClass('off');
      $('#'+$(this).attr('tab')).fadeIn(500, function() {
        setTabHeight();
      });
      bindClickEvent();
    }
  });
}

function bindSearchEvent()
{
  $('#searchContainer .searchField').click(function(){
    if($(this).next().hasClass('dd')) {
      hideDD();
    } else {
      hideDD();
      $(this).next().addClass('dd').show();
    }
  });
}

function bindDropdownClickEvent()
{
  $('#searchContainer .searchDropdown li').click(function() {
    $(this).parent().parent().prev().html($(this).html());
    $(this).parent().parent().prev().attr('value', $(this).attr('value'));
    var dropdown = $(this).parent().parent().attr('id');
    if(dropdown == "makeDropdown") {
      $('#modelDropdown  li:first').click();
      $('#bodytypeDropdown  li:first').click();
    }
    hideDD();
    $(this).unbind('click');
    SetToSearchModel();
  });
}

function bindDocClick()
{
  $(document).click(function(e)
  {
    var matched = false;
    if($(e.target).hasClass('searchField') == true)
      matched = true;

    if(matched == false)
      hideDD();
  });
}

function bindSearchClickEvent()
{
  $('#searchContainer .submit_button').click(function() {
    var searchMake = validateSearch($('#usedMake').attr('value'));
    var searchModel = validateSearch($('#usedModel').attr('value'));
    var searchBody = validateSearch($('#usedBodytype').attr('value'));

    window.location='used.php?s2=1&s0=0&search.make='+searchMake+'&search.model='+searchModel+'&search.body='+searchBody;
  });
}

function validateSearch(val)
{
  if(val <= 0)
    return '';
  else
    return val;
}

function hideDD()
{
  $('#searchContainer .dd').hide().removeClass('dd');
}

function createXmlDOMObject(xmlString)
{
    var xmlDoc = null;

    if( ! window.DOMParser )
    {
        xmlDoc = new ActiveXObject( "Microsoft.XMLDOM" );
        xmlDoc.async = false;
        xmlDoc.loadXML( xmlString );
    } else {
        parser = new DOMParser();
        xmlDoc = parser.parseFromString( xmlString, "text/xml" ) ;
    }

    return xmlDoc;
}

function parseImageXml(xml)
{
  var xmlObj = createXmlDOMObject(xml);

  $(xmlObj).find("images").children().each(function(){
    var imageURL = $(this).attr("url");       
    var linkLocation = $(this).attr("link");
    
    if($(this).attr("newWindow") == "true")
      var newWindow = ' target="_blank"';
    else
      var newWindow = '';       
    
    document.write('<div class="imageContainer">' + '<a href="' + linkLocation + '"' + newWindow + '"><img src="' + imageURL + '" /></a></div>');
  });
}

// Application Buttons //
var ISOCODE;
function showStatus(sMsg) {//used often
  //setTimeout("window.status = \'" + sMsg + "\';", 0);
    window.status = sMsg ;
    return true ;
}
function openWindow(url,width,height){
  newWin = window.open(url, 'viewWin', 'toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1,left='+(screen.width-width)/2+',top='+(screen.height-height)/2+',width=' + width + ',height=' + height + '');
}   

function changeImg(imgName,imgPre) {
  if(document.images){
    document.images[imgName].src = eval(imgPre + '.src');
  }
}

function changeStyle(obj, new_style) {
    obj.className = new_style;
}
  
