var iAdCount = 0;
var iCurrentAd = 1;
var iNextId = 2;
var hRotator = 0;
var bCancel = false;
var bAdClicked = 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 changeMake(strSuffix)
{
	 resetModel(strSuffix);
     resetEngine(strSuffix);
     resetYear(strSuffix);
     $('#vehicleSubModel' + strSuffix).css('display', 'none');
     $('#vehicleSubModelHolder' + strSuffix).css('display', 'none');
     
     var strMake = $('#vehicleMake' + strSuffix).val();
     
     $.post(
        '/ajax.php?x=getVehicleOptions',
        {
            option : 'models',
            make : strMake
        },
        function(data)
        {
            $('#vehicleModel' + strSuffix).html('<option value="">Select Model</option>' + data);
        }
     );
}

function changeModel(strSuffix)
{
	resetEngine(strSuffix);
    resetYear(strSuffix);
    var strMake = $('#vehicleMake' + strSuffix).val();
    var strModel = $('#vehicleModel' + strSuffix).val();
    
    $.post(
        '/ajax.php?x=getVehicleOptions',
        {
            option : 'hasSubModels',
            make : strMake,
            model : strModel
        },
        function(data)
        {
            if (data == 'true')
            {
                $.post(
                    '/ajax.php?x=getVehicleOptions',
                    {
                        option : 'subModels',
                        make : strMake,
                        model : strModel
                    },
                    function(data)
                    {
                        $('#vehicleSubModel' + strSuffix).html('<option value="">Sub Model</option>' + data);
                        $('#vehicleSubModel' + strSuffix).css('display', 'inline-block');
                        $('#vehicleSubModelHolder' + strSuffix).css('display', 'inline-block');
                    }
                );
            }
            else
            {
                $('#vehicleSubModel' + strSuffix).css('display', 'none');
                $('#vehicleSubModelHolder' + strSuffix).css('display', 'none');
                
                $.post(
                    '/ajax.php?x=getVehicleOptions',
                    {
                        option : 'engines',
                        make : strMake,
                        model : strModel
                    },
                    function(data)
                    {
                        $('#vehicleEngine' + strSuffix).html('<option value="">Engine</option>' + data);
                    }
                );
            }
        }
    );
}

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

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

$(document).ready(function()
{
	/*$('div#makeScrollWrapper').jcarousel({
        auto: 2,
        wrap: 'circular',
        scroll:9
    });*/
	
    
    $('#selectVehicle').click(function()
    {
        selectVehicle('');
        return false;
    });
    
    adRotatorStart();
});

function selectVehicle(strSuffix)
{
	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();
    
    if (strMake == "" || strModel == "" || strEngine == "" || strYear =="")
    	{
    		alert("Please fully specify a vehicle.");
    		return;
    	}
    
    $('#carSelection' + strSuffix).css('display', 'none');
    $('#pleaseWaitCar' + strSuffix).css('display', 'block');
    
    $.post(
        '/ajax.php?x=setVehicle',
        {
            make : strMake,
            model : strModel,
            submodel : strSubModel,
            engine : strEngine,
            year : strYear
        },
        function(data)
        {
            window.location.reload();
        }
    )
}

function adPositionClick(event, strNextId)
{
  clearTimeout(hRotator);
  
  iNextId = strNextId;
  bCancel = true;
  bAdClicked = 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).hide('slide', {direction: "left"}, 1000, 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).show('slide', {direction: "right"}, 1000, function()
  {
    if (!bCancel && !bAdClicked)
    {
      hRotator = setTimeout("_rotateToAdDown(" + iAd + ")", 3000);
    }
  });
}


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

    $('#vrmLookup' + strSuffix).css('display', 'none');
    $('#pleaseWaitCarVrm' + strSuffix).css('display', 'block');
    
    $.post(
        '/ajax.php?x=vrmLookup',
        {
            reg : strReg
        },
        function(data)
        {
        	if (data == "")
        	{
        		window.location.reload();
        	}
        	else
        	{
        		 $('#pleaseWaitCarVrm' + strSuffix).css('display', 'none');
        		 $('#vrmLookup' + strSuffix).css('display', 'block');
        		 $("#vrmLookupError" + strSuffix).html("Sorry your registration was not found, please check and try again");
        	}
        }
    );
    return false;
}

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

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

function resetModel(strSuffix)
{
    $('#vehicleModel' + strSuffix).html('<option value="">Select Model</option>');
    $('#vehicleSubModelHolder' + strSuffix).css('display', 'inline-block');
}

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

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

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);
    });
}
