var iAdCount = 0;
var iCurrentAd = 1;
var iNextId = 2;
var hRotator = 0;
var bCancel = false;

function featuredCar(strMake, strModel, strLink)
{
	$.post(
	        '/ajax.php?x=resetCar',
	        {
	            make : strMake,
	            model : strModel
	        },
	        function(data)
	        {
	        	document.location.href = strLink;
	        }
	     );
	
	return false;
}

function prepopMakeModel(strMake, strModel)
{
	$('#vehicleMakeInline').val(strMake);
	
	$.post(
        '/ajax.php?x=getVehicleOptions',
        {
            option : 'models',
            make : strMake
        },
        function(data)
        {
            $('#vehicleModelInline').html('<option value="">Select Model</option>' + data);
            $('#vehicleModelInline').val(strModel);
            
            changeModel("Inline");
        }
     );
}

function changeCategory(strSuffix)
{
 
	resetModel(strSuffix);
    resetEngine(strSuffix);
    resetFuel(strSuffix);
    resetYear(strSuffix);
  
    $('#vehicleSubModel' + strSuffix).hide();
    $('#vehicleSubModelHolder' + strSuffix).hide();
    
    var strCategory = $('#vehicleCategory' + strSuffix).val();
     
    $.post(
       '/ajax.php?x=getVehicleOptions',
       {
           option : 'makes',
           category : strCategory
       },
       function(data)
       {
    	   if (1==1 || strCategory == '33631' || strCategory == '33635' || strCategory == '33654')
    	    {
    	 	   $('#vehicleMake' + strSuffix).html('<option value="">Select Make</option>' + data);
    	        $('#vehicleMakeHolder' + strSuffix).show(); 
    	    } else {   
    	    	resetMake(strSuffix);    	
    	    }
    	   
       }
    );
}

function changeMake(strSuffix)
{
	 resetModel(strSuffix);
     resetEngine(strSuffix);
     resetFuel(strSuffix);
     resetYear(strSuffix);
     $('#vehicleSubModel' + strSuffix).hide();
     $('#vehicleSubModelHolder' + strSuffix).hide();
     
     var strMake = $('#vehicleMake' + strSuffix).val();
     var strCategory = $('#vehicleCategory' + strSuffix).val();
     
     $.post(
        '/ajax.php?x=getVehicleOptions',
        {
            option : 'models',
            make : strMake,
            category : strCategory
        },
        function(data)
        {
            $('#vehicleModel' + strSuffix).html('<option value="">Select Model</option>' + data);
            $('#vehicleModelHolder' + strSuffix).show();
        }
     );
}

function changeModel(strSuffix)
{
	resetEngine(strSuffix);
	resetFuel(strSuffix);
    resetYear(strSuffix);
    var strMake = $('#vehicleMake' + strSuffix).val();
    var strModel = $('#vehicleModel' + strSuffix).val();
    var strCategory = $('#vehicleCategory' + strSuffix).val();
    
    $.post(
        '/ajax.php?x=getVehicleOptions',
        {
            option : 'hasSubModels',
            make : strMake,
            model : strModel,
            category : strCategory
        },
        function(data)
        {
            if (data == 'true')
            {
                $.post(
                    '/ajax.php?x=getVehicleOptions',
                    {
                        option : 'subModels',
                        make : strMake,
                        model : strModel,
                        category : strCategory
                    },
                    function(data)
                    {
                        $('#vehicleSubModel' + strSuffix).html('<option value="">Sub Model</option>' + data);
                        $('#vehicleSubModel' + strSuffix).show();
                        $('#vehicleSubModelHolder' + strSuffix).show();
                    }
                );
            }
            else
            {
                $('#vehicleSubModel' + strSuffix).hide();
                $('#vehicleSubModelHolder' + strSuffix).hide();
                
                $.post(
                    '/ajax.php?x=getVehicleOptions',
                    {
                        option : 'engines',
                        make : strMake,
                        model : strModel,
                        category : strCategory,
                        submodel : ''
                    },
                    function(data)
                    {
                        $('#vehicleEngine' + strSuffix).html('<option value="">Engine</option>' + data);
                        $('#vehicleEngineHolder' + strSuffix).show();
                    }
                );
            }
        }
    );
}

function changeSubModel(strSuffix)
{
	resetEngine(strSuffix);
	resetFuel(strSuffix);
    resetYear(strSuffix);
    var strMake = $('#vehicleMake' + strSuffix).val();
    var strModel = $('#vehicleModel' + strSuffix).val();
    var strSubModel = $('#vehicleSubModel' + strSuffix).val();
    var strCategory = $('#vehicleCategory' + strSuffix).val();
    
    $.post(
       '/ajax.php?x=getVehicleOptions',
       {
           option : 'engines',
           make : strMake,
           model : strModel,
           submodel : strSubModel,
           category : strCategory
       },
       function(data)
       {
           $('#vehicleEngine' + strSuffix).html('<option value="">Engine</option>' + data);
           $('#vehicleEngineHolder' + strSuffix).css('display', 'inline-block');
       }
    )
}

function changeEngine(strSuffix)
{
    var strMake = $('#vehicleMake' + strSuffix).val();
    var strModel = $('#vehicleModel' + strSuffix).val();
    var strEngine = $('#vehicleEngine' + strSuffix).val();
    var strCategory = $('#vehicleCategory' + strSuffix).val();
    resetYear(strSuffix);
    
    $.post(
       '/ajax.php?x=getVehicleOptions',
       {
           option : 'fuel',
           make : strMake,
           model : strModel,
           submodel : getSubModelValue(strSuffix),
           engine : strEngine,
           category : strCategory
       },
       function(data)
       {
           $('#vehicleFuel' + strSuffix).html('<option value="">Fuel</option>' + data);
           $('#vehicleFuelHolder' + strSuffix).show();
       }
    )
}

function changeFuel(strSuffix)
{
    var strMake = $('#vehicleMake' + strSuffix).val();
    var strModel = $('#vehicleModel' + strSuffix).val();
    var strEngine = $('#vehicleEngine' + strSuffix).val();
    var strCategory = $('#vehicleCategory' + strSuffix).val();
    var strFuel = $('#vehicleFuel' + strSuffix).val();
   
    $.post(
       '/ajax.php?x=getVehicleOptions',
       {
           option : 'years',
           make : strMake,
           model : strModel,
           submodel : getSubModelValue(strSuffix),
           engine : strEngine,
           fuel : strFuel,
           category : strCategory
       },
       function(data)
       {
           $('#vehicleYear' + strSuffix).html('<option value="">Year</option>' + data);
           $('#vehicleYearHolder' + strSuffix).show();
       }
    )
}

$(document).ready(function()
{
	/*$('div#makeScrollWrapper').jcarousel({
        auto: 2,
        wrap: 'circular',
        scroll:9
    });*/
	
//	$("#vehicleCategory").val('33631');
//	$("#vehicleCategory").trigger('change');
	
    adRotatorStart();
});

function selectVehicle(strSuffix, bHasCategory)
{
	var strMake = $('#vehicleMake' + strSuffix).val();
    var strModel = $('#vehicleModel' + strSuffix).val();
    var strEngine = $('#vehicleEngine' + strSuffix).val();
    var strYear = $('#vehicleYear' + strSuffix).val();
    var strSubModel = $('#vehicleSubModel' + strSuffix).val();
    var strFuel = $('#vehicleFuel' + strSuffix).val();
    var strCategory = $('#vehicleCategory' + strSuffix).val();
    
    var strDesc = $('#vehicleCategory option:selected').text()     
   // if (strMake == "")
//	{
	//	alert("Please fully specify a vehicle.");
		//return;
//	}
    
    $('#carSelection' + strSuffix).css('display', 'none');
    $('#pleaseWaitCar' + strSuffix).css('display', 'block');
    
    if (bHasCategory && strCategory == "")
    {
    	$('#vehicleCategoryError' + strSuffix).show();
    }
    else
    {
    	$('#vehicleCategoryError' + strSuffix).hide();
    	
	    $.post(
	        '/ajax.php?x=setVehicle',
	        {
	            make : strMake,
	            model : strModel,
	            fuel : strFuel,
	            engine : strEngine,
	            year : strYear,
	            category : strCategory,
	            selection : strDesc
	        },
	        function(data)
	        {
	            document.location.href = '/products/byvehicle';
	        }
	    )
    }
}

function adPositionClick(event, strNextId)
{
  clearTimeout(hRotator);
  
  iNextId = strNextId;
  bCancel = true;
    
  hRotator = setTimeout("_rotateToAdDown(" + iCurrentAd + ")", 1000);
  
  return false;
}

function adRotatorStart()
{
	iAdCount = $('#rotatorCount').val();
  hRotator = setTimeout("_rotateToAdDown(1)", 2000);
}

function _rotateToAdDown(iAd)
{
  bCancel = false;
  
  $("#ad" + iAd).fadeOut('slow', function()
  {
    if (!bCancel)
    {
     hRotator = setTimeout("_rotateToAdUp(" + (iNextId) + ")", 10);
    }
  });
}

function _rotateToAdUp(iAd)
{
  if (iAd > iAdCount)
  {
    iAd = 1;
  }
  
  $(".adButton").removeClass("selected");
  $("#adButton" + iAd).addClass("selected");
  
  $(".adHeader").css("display", "none");
  $("#adHeader" + iAd).css("display", "block");
  
  
  
  iNextId = iAd + 1;
  
  if (iNextId > iAdCount)
  {
    iNextId = 1;
  }
  
  iCurrentAd = iAd;
 
  $("#ad" + iAd).fadeIn(2000, function()
  {
    if (!bCancel)
    {
      hRotator = setTimeout("_rotateToAdDown(" + iAd + ")", 3000);
    }
  });
}


function vrmLookup(strSuffix)
{
    var strReg = $('#vehicleReg' + strSuffix).val();

    $('#vrmLookup' + strSuffix).hide();
    $('#pleaseWaitCarVrm' + strSuffix).show();
    
    $.post(
        '/ajax.php?x=vrmLookup',
        {
            reg : strReg
        },
        function(data)
        {
        	if (data == "")
        	{
        		document.location.href = '/products/byvehicle';
        	}
        	else
        	{
        		$('#vrmLookup' + strSuffix).show();
        	    $('#pleaseWaitCarVrm' + strSuffix).hide();
        		$('#vrmError').show();
        	}
        }
    );
    return false;
}

function deleteVehicle()
{
    $.post(
        '/ajax.php?x=deleteVehicle',
        {
        },
        function(data)
        {
            window.location.reload();
        }
    );
    return false;
}

function getSubModelValue(strSuffix)
{
    if ($('#vehicleSubModel' + strSuffix).css('display') != 'none')
    {
        return $('#vehicleSubModel' + strSuffix).val();
    }
    else
    {
        return '';
    }
}

function resetMake(strSuffix)
{
    $('#vehicleMake' + strSuffix).html('<option value="">Select Make</option>');
    $('#vehicleMakeHolder' + strSuffix).hide();
}

function resetModel(strSuffix)
{
    $('#vehicleModel' + strSuffix).html('<option value="">Select Model</option>');
    $('#vehicleModelHolder' + strSuffix).hide();
}

function resetEngine(strSuffix)
{
    $('#vehicleEngine' + strSuffix).html('<option value="">Engine</option>');
    $('#vehicleEngineHolder' + strSuffix).hide();
}

function resetFuel(strSuffix)
{
    $('#vehicleFuel' + strSuffix).html('<option value="">Fuel</option>');
    $('#vehicleFuelHolder' + strSuffix).hide();
}

function resetYear(strSuffix)
{
    $('#vehicleYear' + strSuffix).html('<option value="">Year</option>');
    $('#vehicleYearHolder' + strSuffix).hide();
}

function pleaseSelectCar(objLink)
{
	var arrOffset = $(objLink).position();
    var iWidth = $(objLink).outerWidth();
    var iHeight = $(objLink).outerHeight();
    
    var iPromptHeight = $('#pleaseSelectVehicle').outerHeight();
    var iPromptWidth = $('#pleaseSelectVehicle').outerWidth();
    
    var iNewTop = arrOffset.top - iPromptHeight;
    var iNewLeft =  arrOffset.left + (iWidth / 2) - (iPromptWidth / 2) + 30;

    $('#pleaseSelectVehicle').css({top: iNewTop, left: iNewLeft});
    $('#pleaseSelectVehicle').fadeIn(1000, function()
    {
    	$('#pleaseSelectVehicle').delay(1000).fadeOut(1000);
    });
}
