
//Homepage Featured Gallery
$(document).ready(function(){
	$("#featured > ul").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, false);
});
 
$(window).load(function () {
    ResizeContainer();
});

$(document).ready(function() {
    var nav = window.location.pathname;
    $('ul[id^="ni"] li[^="link"] a[href$="' + nav + '"] ').addClass('activeLink');
});

function SetActiveLink(id)
{
    $('#' + id + ' a').addClass('activeLink');
}

function ResizeContainer()
{  
    var difference = 0;
    
    $('div[id^="mod"]').each(function()
    {
        var currentDifference = parseInt($(this).height() - $(this).css('min-height').replace('px', ''));
        
        if(currentDifference > difference)
        {
            difference = currentDifference;
        }
    });
    
    $('#container').height($('#container').height() + difference);
}

function ResizeModule(name, type)
{
    if(type == 'Form')
    {
        if (typeof(Page_Validators) != 'undefined') 
        {
            if(!Page_IsValid)
            {
                if(!$('.' + name).parent().attr('style'))
                {
                    var offset = 10; 
                    var numberOfValidators = $('span[id*="RequiredFieldValidator"]').length;
                
                    $('.' + name).parent().height($('.' + name).parent().height() + (offset * numberOfValidators));
                    ResizeContainer();
                }
            }
        }
   }
   else
   {
       var spaceBetweenContainer = 10;
       var newHeight = parseInt($(name).height()) + parseInt($(name).css('top').replace('px', '')) + spaceBetweenContainer;
       $('#container').height(newHeight);
   }
}

function ClearInputText(selector)
{
    $(selector).val('');
}

function SetCursorToEndOfText(textControlID)
{
    var text = document.getElementById(textControlID);
    
    if (text != null && text.value.length > 0)
    {
        if (text.createTextRange)
        {
            var fieldRange = text.createTextRange();
            fieldRange.moveStart('character', text.value.length);
            fieldRange.collapse();
            fieldRange.select();
        }
    }
}

String.prototype.endsWith = function(a){ return this.substr(this.length - a.length) === a; }
String.prototype.startsWith = function(a){ return this.substr(0, a.length) === a; }
String.prototype.trim = function(){ return this.replace(/^\s*/, '').replace(/\s*$/, ''); }
String.prototype.trimEnd = function(){ return this.replace(/\s+$/, ""); }
String.prototype.trimStart = function(){ return this.replace(/^\s+/, ""); }

function ShowFullImage(imageSelector)
{
    $(imageSelector).fadeIn(500);
}

function HideFullImage(imageSelector)
{
    $(imageSelector).hide();
}

function SendFinanceApplication(zip, appID, vehicleID)
{
    var geocoder = new google.maps.Geocoder();
    
    if(vehicleID == 0)
    {
        geocoder.geocode({ 'address': zip}, 
        
            function(results, status) 
            {
                if (status == google.maps.GeocoderStatus.OK) 
                {
                    $.get("/ajax/find-nearest-dealer.aspx", { lat: results[0].geometry.location.lat(), lng: results[0].geometry.location.lng(), radius: 500 },
                        
                        function(data)
                        {
                            $.get("/ajax/send-finance-application.aspx", { dealerID: data, appID: appID, vehicleID:0  },
                        
                                function(data2)
                                {
                                }
                        
                            );
                        }
                        
                    );
                } 
            }
       );
   }
   else
   {
       $.get("/ajax/send-finance-application.aspx", { dealerID: 0, appID: appID, vehicleID: vehicleID  },  function(data){});
   }
}
