/*
CSS Browser Selector v0.4.0 (Nov 02, 2010)
Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/
function css_browser_selector(u) { var ua = u.toLowerCase(), is = function(t) { return ua.indexOf(t) > -1 }, g = 'gecko', w = 'webkit', s = 'safari', o = 'opera', m = 'mobile', h = document.documentElement, b = [(!(/opera|webtv/i.test(ua)) && /msie\s(\d)/.test(ua)) ? ('ie ie' + RegExp.$1) : is('firefox/2') ? g + ' ff2' : is('firefox/3.5') ? g + ' ff3 ff3_5' : is('firefox/3.6') ? g + ' ff3 ff3_6' : is('firefox/3') ? g + ' ff3' : is('gecko/') ? g : is('opera') ? o + (/version\/(\d+)/.test(ua) ? ' ' + o + RegExp.$1 : (/opera(\s|\/)(\d+)/.test(ua) ? ' ' + o + RegExp.$2 : '')) : is('konqueror') ? 'konqueror' : is('blackberry') ? m + ' blackberry' : is('android') ? m + ' android' : is('chrome') ? w + ' chrome' : is('iron') ? w + ' iron' : is('applewebkit/') ? w + ' ' + s + (/version\/(\d+)/.test(ua) ? ' ' + s + RegExp.$1 : '') : is('mozilla/') ? g : '', is('j2me') ? m + ' j2me' : is('iphone') ? m + ' iphone' : is('ipod') ? m + ' ipod' : is('ipad') ? m + ' ipad' : is('mac') ? 'mac' : is('darwin') ? 'mac' : is('webtv') ? 'webtv' : is('win') ? 'win' + (is('windows nt 6.0') ? ' vista' : '') : is('freebsd') ? 'freebsd' : (is('x11') || is('linux')) ? 'linux' : '', 'js']; c = b.join(' '); h.className += ' ' + c; return c; }; css_browser_selector(navigator.userAgent);
function goToByScrollByY(y) {
    $('html,body').animate({ scrollTop: y }, 'slow');
}
function goToByScrollBy(id) {
    goToByScrollByObj($('#' + id));
}
function goToByScrollByObj(el) {
    $('html,body').animate({ scrollTop: el.offset().top }, 'slow');
}
function goToByScrollByObjOffset(el, offset) {
    $('html,body').animate({ scrollTop: el.offset().top + offset }, 'slow');
}
function isValidEmail(email) {
    var regex = /([\w\d\-_]+)(\.[\w\d\-_]+)*@([\w\d\-_]+\.)([\w\d\-_]+\.)*([\w]{2,3})/;
    if (regex.test(email))
        return true;
    else
        return false;
}
function isInteger(input) {
    if (input.match(/^[0-9]+$/)) {
        return true;
    } else {
        return false;
    }
}
function setupDefaultTextReset(obj, message) {
    obj.click(function () {
        if (obj.val() == message)
            obj.val('');
    });
    obj.blur(function () {
        if (obj.val() == '')
            obj.val(message);
    });
}
$(document).ready(function () {
    matchHeight();
    $('.jsScrollToTop').click(function (e) {
        e.preventDefault();

        goToByScrollByY(0);
    });
    $('.jsShareTwitter').click(function () {
        var url = 'http://twitter.com/share?url=' + encodeURIComponent(window.location);
        if ($(this).attr('href') != '#')
            url = 'http://twitter.com/share?url=' + encodeURIComponent($(this).attr('href'));
        window.open(url, 'twitter', 'width=500,height=400');
        return false;
    });
    $('.jsShareFacebook').click(function () {
        var url = 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(window.location);
        if ($(this).attr('href') != '#')
            url = 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent($(this).attr('href'));
        window.open(url, 'facebook', 'width=1000,height=600');
        return false;
    });
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//					
    // brand hover 
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//	
    $('.brand').mouseover(function () {
        $(this).find('ul').css('background-position', '0px -60px');
    }).mouseout(function () {
        $(this).find('ul').css('background-position', '0px 0px');
    });
    $('.brandfooter').mouseover(function () {
        $(this).find('ul').css('background-position', '0px -60px');
    }).mouseout(function () {
        $(this).find('ul').css('background-position', '0px bottom');
    });
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//					
    // column correction
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//	
    $('.inner .block:nth-child(3n + 1)').css('margin-right', '0').css('margin-bottom', '22px');
    $('.dealerObj .a:nth-child(4n + 4)').css('margin-right', '0').css('margin-bottom', '2px');


    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//					
    // global submenu 
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//			
    $('.sb-used').mousedown(function () {
        $(this).addClass('active');
        $('.sb-new').removeClass('active');
    });
    $('.sb-new').mousedown(function () {
        $(this).addClass('active');
        $('.sb-used').removeClass('active');
    });
    $('.sb-used,.sb-new').click(function (e) {
        e.preventDefault();
    });

    //opening & closing of menus
    $('#a_NavMake, #a_NavModel, #a_NavBody, #a_NavOptions').click(function (e) {
        var divId = $(this).attr('id').replace('a_', 'div_');

        //some checking just for models
        $('#div_NavMake p.message').hide();
        if (divId == 'div_NavModel') {
            if ($('#' + divId + ' a').length == 0) {
                //open the make list instead
                $('#a_NavMake').trigger('click');
                $('#div_NavMake p.message').show();

                e.preventDefault();
                return false;
            }
        }

        $('.sb-used').addClass('disabled');
        $('.sb-make').addClass('disabled');
        $('.sb-model').addClass('disabled');
        $('.sb-body').addClass('disabled');
        $('.sb-options').addClass('disabled');

        if (!$(this).hasClass('active')) {
            $(this).removeClass('disabled');
            $(this).addClass('active');
            $('#' + divId).slideDown(300);

            //hide other menus which are open
            $('.popmenu div').each(function () {
                if ($(this).attr('id') &&
                $(this).attr('id').indexOf('div_') == 0 &&
                $(this).attr('id') != divId) {
                    $(this).hide();
                    var aId = $(this).attr('id').replace('div_', 'a_');
                    $('#' + aId).removeClass('active');
                }
            });
        }
        else {
            $(this).removeClass('active');
            $('#' + divId).slideUp(300, function () {
                $(this).removeClass('active');
            });
        }

        e.preventDefault();
    });
    $('#div_NavMake, #div_NavModel, #div_NavBody, #div_NavOptions').mouseleave(function () {
        $(this).slideUp(300, function () {
            var aId = $(this).attr('id').replace('div_', 'a_');
            $('#' + aId).removeClass('active');
        });
    });

    //make click
    $('#div_NavMake a').click(function (e) {
        e.preventDefault();
        //get models from selected brand
        var el = $(this);

        if (!el.hasClass('active')) {
            $('#div_NavMake a').removeClass('active');
            el.addClass('active');
            var makeCode = el.attr('id').replace('aNavMake_', '');
            if (makeCode == 'All') {
                //pick a model first
                $('#div_NavModel').html('');
                $('#a_NavMake').html('<span>Any Make</span>');
                $('#a_NavMake').removeAttr('rel');
            }
            else {
                vehicleSearchShowWait();
                $('#a_NavMake').html('<span class="selected ' + makeCode + '">' + el.html() + '</span>');
                $('#a_NavMake').attr('rel', makeCode);
                $.post("/handlers/VehicleSearch.ashx", { op: 'getModels', make: makeCode }, function (data) {
                    vehicleSearchHideWait();
                    if (!data.Error) {
                        $('#div_NavModel').html(data.Output);
                        $('#div_NavModel a').bind('click', navModelClick);

                        //check if we need to show the model nav
                        if ($('#div_NavMake .message').is(':visible')) {
                            $('#div_NavMake .message').hide();
                            $('#a_NavModel').trigger('click');
                        }
                        $('.searchBar .sb-search').trigger('click');
                    }
                    else {

                    }
                }).error(function () {

                });
            }

            //get body styles from selected brand
            var modelCode = 'Any';
            vehicleSearchShowWait();
            $('#a_NavModel').html('<span>Any Model</span>');
            $('#a_NavModel').attr('rel', modelCode);
            $.post("/handlers/VehicleSearch.ashx", { op: 'getBodyStyles', make: makeCode, model: modelCode }, function (data) {
                vehicleSearchHideWait();
                if (!data.Error) {
                    $('#div_NavBody').html(data.Output);
                    $('#div_NavBody a').bind('click', navBodyClick);
                }
                else {

                }
            }).error(function () {

            });
        }
    });

    //model click
    $('#div_NavModel a').bind('click', navModelClick);

    function navModelClick(e) {
        //get body styles from selected brand
        var el = $(this);
        var makeCode = $('#a_NavMake').attr('rel');

        if (el.attr('id') != 'aNavModel_Any') {
            $('#aNavModel_Any').removeClass('active');
            if (el.hasClass('active'))
                el.removeClass('active');
            else
                el.addClass('active');
        }
        else {
            $('#div_NavModel a').removeClass('active');
            el.addClass('active');
        }

        if ($('#div_NavModel a.active').length == 0) {
            $('#aNavModel_Any').trigger('click');
            return false;
        }

        var modelHtmls = '';
        var modelCodes = '';
        $('#div_NavModel a.active').each(function () {
            modelHtmls += $(this).html() + ', ';
            modelCodes += $(this).attr('id').replace('aNavModel_', '') + ',';
        });
        if (modelHtmls.length > 2)
            modelHtmls = modelHtmls.substring(0, modelHtmls.length - 2);
        if (modelCodes.length > 1)
            modelCodes = modelCodes.substring(0, modelCodes.length - 1);

        if (modelHtmls.length > 10)
            modelHtmls = modelHtmls.substring(0, 10) + '..';

        $('#a_NavModel').html('<span>' + modelHtmls + '</span>');
        $('#a_NavModel').attr('rel', modelCodes);

        vehicleSearchShowWait();
        $.post("/handlers/VehicleSearch.ashx", { op: 'getBodyStyles', make: makeCode, model: modelCodes }, function (data) {
            vehicleSearchHideWait();
            if (!data.Error) {
                $('#div_NavBody').html(data.Output);
                $('#div_NavBody a').bind('click', navBodyClick);

                if ($('#a_NavBody').attr('rel') &&
                    $('#a_NavBody').attr('rel') != 'Any') {
                    //check if body style is available
                    if ($('#div_NavBody a#aNavBody_' + $('#a_NavBody').attr('rel')).length == 0) {
                        //body style not available, reset
                        $('#a_NavBody').attr('rel', 'Any');
                        $('#a_NavBody').html('<span>Any</span>');
                    }
                }
            }
            else {

            }
        }).error(function () {

        });
        e.preventDefault();
    }

    $('#div_NavBody a').bind('click', navBodyClick);
    function navBodyClick(e) {
        e.preventDefault();

        var el = $(this);

        if (el.attr('id') != 'aNavBody_Any') {
            $('#aNavBody_Any').removeClass('active');
            if (el.hasClass('active'))
                el.removeClass('active');
            else
                el.addClass('active');
        }
        else {
            $('#div_NavBody a').removeClass('active');
            el.addClass('active');
        }

        if ($('#div_NavBody a.active').length == 0) {
            $('#aNavBody_Any').trigger('click');
            return false;
        }

        var bodyHtmls = '';
        var bodyCodes = '';
        $('#div_NavBody a.active').each(function () {
            bodyHtmls += $(this).html() + ', ';
            bodyCodes += $(this).attr('id').replace('aNavBody_', '') + ',';
        });
        if (bodyHtmls.length > 2)
            bodyHtmls = bodyHtmls.substring(0, bodyHtmls.length - 2);
        if (bodyCodes.length > 1)
            bodyCodes = bodyCodes.substring(0, bodyCodes.length - 1);

        if (bodyHtmls.length > 14)
            bodyHtmls = bodyHtmls.substring(0, 14) + '..';

        $('#a_NavBody').html('<span>' + bodyHtmls + '</span>');
        $('#a_NavBody').attr('rel', bodyCodes);
    }

    //search options
    $('#ulNavOption_Transmission a, #ulNavOption_Drive a, #ulNavOption_EngineSize a, #ulNavOption_FuelType a, #ulNavOption_Doors a, #ulNavOption_PriceFrom a, #ulNavOption_PriceTo a, #ulNavOption_Year a, #ulNavOption_Kms a').click(function (e) {
        $(this).parents('ul').eq(0).find('a').removeClass('active');
        $(this).addClass('active');
        navUpdateSearchOptionsStrip();
        e.preventDefault();
    });
    $('#div_NavOptions .btn-clearoption').click(function () {
        $('#div_NavOptions a').removeClass('active');
        navUpdateSearchOptionsStrip();
    });
    function navUpdateSearchOptionsStrip() {
        if ($('#div_NavOptions a').hasClass('active')) {
            $('.m-optionsset').show();

            var allAny = true;
            $('#spanNavOption_Transmission').html('Any');
            if ($('#ulNavOption_Transmission a.active').length > 0) {
                $('#spanNavOption_Transmission').html($('#ulNavOption_Transmission a.active').html());
                allAny = false;
            }

            $('#spanNavOption_Drive').html('Any');
            if ($('#ulNavOption_Drive a.active').length > 0) {
                $('#spanNavOption_Drive').html($('#ulNavOption_Drive a.active').html());
                allAny = false;
            }

            $('#spanNavOption_FuelType').html('Any');
            if ($('#ulNavOption_FuelType a.active').length > 0) {
                $('#spanNavOption_FuelType').html($('#ulNavOption_FuelType a.active').html());
                allAny = false;
            }

            $('#spanNavOption_Doors').html('Any');
            if ($('#ulNavOption_Doors a.active').length > 0) {
                $('#spanNavOption_Doors').html($('#ulNavOption_Doors a.active').html());
                allAny = false;
            }

            $('#spanNavOption_EngineSize').html('Any');
            if ($('#ulNavOption_EngineSize a.active').length > 0) {
                $('#spanNavOption_EngineSize').html($('#ulNavOption_EngineSize a.active').html());
                allAny = false;
            }

            var priceDisplay = '';
            if ($('#ulNavOption_PriceFrom a.active').length > 0) {
                priceDisplay += $('#ulNavOption_PriceFrom a.active').html();
                allAny = false;
            }
            else {
                priceDisplay += 'Any';
            }
            priceDisplay += ' - ';
            if ($('#ulNavOption_PriceTo a.active').length > 0) {
                priceDisplay += $('#ulNavOption_PriceTo a.active').html();
                allAny = false;
            }
            else {
                priceDisplay += 'Any';
            }

            $('#spanNavOption_Price').html(priceDisplay);

            $('#spanNavOption_Year').html('Any');
            if ($('#ulNavOption_Year a.active').length > 0) {
                $('#spanNavOption_Year').html($('#ulNavOption_Year a.active').html());
                allAny = false;
            }

            $('#spanNavOption_Kms').html('Any');
            if ($('#ulNavOption_Kms a.active').length > 0) {
                $('#spanNavOption_Kms').html($('#ulNavOption_Kms a.active').html());
                allAny = false;
            }

            if (allAny)
                $('.m-optionsset').hide();
        }
        else {
            $('.m-optionsset').hide();
        }
    }

    $('.searchBar .sb-search').click(function (e) {
        e.preventDefault();
        //build url
        var url = '';
        if ($('.searchBar .sb-new').hasClass('active')) {
            url = '/main-nav/new-vehicles.aspx';
        }
        else {
            url = '/main-nav/used-vehicles.aspx';
        }

        if ($('#a_NavMake').attr('rel'))
            url += '?make=' + encodeURIComponent($('#a_NavMake').attr('rel'));
        else
            url += '?make=Any';

        if ($('#a_NavModel').attr('rel'))
            url += '&model=' + encodeURIComponent($('#a_NavModel').attr('rel'));
        else
            url += '&model=Any';

        if ($('#a_NavBody').attr('rel'))
            url += '&body=' + encodeURIComponent($('#a_NavBody').attr('rel'));
        else
            url += '&body=Any';

        if ($('#ulNavOption_Transmission a.active').length > 0)
            url += '&transmission=' + encodeURIComponent($('#ulNavOption_Transmission a.active').html());
        else
            url += '&transmission=Any';

        if ($('#ulNavOption_Drive a.active').length > 0)
            url += '&drive=' + encodeURIComponent($('#ulNavOption_Drive a.active').html());
        else
            url += '&drive=Any';

        if ($('#ulNavOption_EngineSize a.active').length > 0)
            url += '&enginesize=' + encodeURIComponent($('#ulNavOption_EngineSize a.active').html());
        else
            url += '&enginesize=Any';

        if ($('#ulNavOption_FuelType a.active').length > 0)
            url += '&fueltype=' + encodeURIComponent($('#ulNavOption_FuelType a.active').html());
        else
            url += '&fueltype=Any';

        if ($('#ulNavOption_Doors a.active').length > 0)
            url += '&doors=' + encodeURIComponent($('#ulNavOption_Doors a.active').html());
        else
            url += '&doors=Any';

        if ($('#ulNavOption_PriceFrom a.active').length > 0)
            url += '&pricefrom=' + encodeURIComponent($('#ulNavOption_PriceFrom a.active').html());
        else
            url += '&pricefrom=Any';

        if ($('#ulNavOption_PriceTo a.active').length > 0)
            url += '&priceto=' + encodeURIComponent($('#ulNavOption_PriceTo a.active').html());
        else
            url += '&priceto=Any';

        if ($('#ulNavOption_Year a.active').length > 0)
            url += '&year=' + encodeURIComponent($('#ulNavOption_Year a.active').html());
        else
            url += '&year=Any';

        if ($('#ulNavOption_Kms a.active').length > 0)
            url += '&kms=' + encodeURIComponent($('#ulNavOption_Kms a.active').html());
        else
            url += '&kms=Any';

        window.location = url;
    });

    $('#aSortPriceAsc, #aSortPriceDesc, #aSortAgeDesc, #aSortAgeAsc, #aSortKmAsc, #aSortKmDesc').click(function (e) {
        e.preventDefault();

        if (!$(this).hasClass('active')) {
            $('.filterbar span a').removeClass('active');
            $(this).addClass('active');

            vehicleSearchShowWait();

            if ($('.divWatchlistContainer').length > 0) {
                //watchlist mode
                var type = $(this).attr('id').replace('aSort', '').replace('Asc', '').replace('Desc', '');
                var dir = $(this).attr('id').replace('aSort', '').replace('Price', '').replace('Age', '').replace('Km', '');
                $.post("/handlers/Wishlist.ashx", { op: 'sort', sorttype: type, sortdir: dir }, function (data) {
                    vehicleSearchHideWait();
                    if (!data.Error) {
                        $('#divSearchResultsWrap').html(data.Message);
                    }
                    else {

                    }
                }).error(function () {

                });
            }
            else {
                //normal mode
                var view = $('.gallerySelector>a.active').attr('id').replace('aSearch_', '');
                var type = $(this).attr('id').replace('aSort', '').replace('Asc', '').replace('Desc', '');
                var dir = $(this).attr('id').replace('aSort', '').replace('Price', '').replace('Age', '').replace('Km', '');
                $.post("/handlers/VehicleSearch.ashx", { op: 'sort', view: view, sorttype: type, sortdir: dir, numdisplaying: $('#divSearchResultsWrap a.lvItem, #divSearchResultsWrap .gvItem').length, searchparams: document.location.href, siteids: $('#hdSiteIds').val() }, function (data) {
                    vehicleSearchHideWait();
                    if (!data.Error) {
                        $('#divSearchResultsWrap').html(data.Output);
                    }
                    else {

                    }
                }).error(function () {

                });
            }
        }
    });

    $('#aMoreResults').click(function (e) {
        e.preventDefault();

        vehicleSearchShowWait();
        var view = $('.gallerySelector>a.active').attr('id').replace('aSearch_', '');
        var type = $('.filterbar a.active').attr('id').replace('aSort', '').replace('Asc', '').replace('Desc', '');
        var dir = $('.filterbar a.active').attr('id').replace('aSort', '').replace('Price', '').replace('Age', '').replace('Km', '');
        $.post("/handlers/VehicleSearch.ashx", { op: 'moreResults', view: view, sorttype: type, sortdir: dir, numdisplaying: $('#divSearchResultsWrap a.lvItem, #divSearchResultsWrap .gvItem').length, searchparams: document.location.href, siteids: $('#hdSiteIds').val() }, function (data) {
            vehicleSearchHideWait();
            if (!data.Error) {
                $('#divSearchResultsWrap').append(data.Output);
                $('#divSearchResultsWrap').delegate(".jsWishlistAdd, .jsWishlistRemove", "click", function (e) {
                    e.preventDefault();
                    assignToWishListFunction($(this));
                });
                if (!data.HasMoreResults)
                    $('#aMoreResults').hide();
            }
            else {

            }
        }).error(function () {

        });
    });

    $('#aSearch_ListView, #aSearch_GalleryView').click(function (e) {
        e.preventDefault();
        var el = $(this);
        if (!$(this).hasClass('active')) {
            $('#aSearch_ListView, #aSearch_GalleryView').removeClass('active');
            $(this).addClass('active');
            vehicleSearchShowWait();
            var view = $(this).attr('id').replace('aSearch_', '');
            var type = $('.filterbar a.active').attr('id').replace('aSort', '').replace('Asc', '').replace('Desc', '');
            var dir = $('.filterbar a.active').attr('id').replace('aSort', '').replace('Price', '').replace('Age', '').replace('Km', '');
            $.post("/handlers/VehicleSearch.ashx", { op: 'changeView', view: view, sorttype: type, sortdir: dir, numdisplaying: $('#divSearchResultsWrap a.lvItem, #divSearchResultsWrap .gvItem').length, searchparams: document.location.href, siteids: $('#hdSiteIds').val() }, function (data) {
                vehicleSearchHideWait();
                if (!data.Error) {
                    $('#divSearchResultsWrap').html(data.Output);

                    if (el.attr('id') == 'aSearch_GalleryView') {
                        $('.gvItem .gvControls a').bind('click', slidingGalleryImageClick);
                    }
                }
                else {

                }
            }).error(function () {

            });
        }
    });

    //Hide hides if less than or equal to six vehicles
    /*$(document).ready(function () {
    var leftbutton = $('.csObj a.aL');
    var rightbutton = $('.csObj a.aR');
    var length = $('.csObj .csInner a').length;
    if (length <= 6) {
    leftbutton.hide();
    rightbutton.hide();
    }
    });*/

    //sliding popular vehicles gallery
    $('.csObj a.aL').click(function (e) {
        //add images to the left
        var length = $('.csObj .csInner a').length;
        if (length > 6)
            length = 6;
        for (var i = 0; i < length; i++) {
            var el = $('.csObj .csInner a').eq($('.csObj .csInner a').length - 1);
            el.detach();
            $('.csObj .csInner').prepend(el);
        }
        $('.csObj .csInner').css('margin-left', '-980px');
        $('.csObj .csInner').animate({ marginLeft: '0px' });
        e.preventDefault();
    });
    $('.csObj a.aR').click(function (e) {
        $('.csObj .csInner').animate({ marginLeft: '-980px' }, function () {
            //add images to the right
            var counter = 0;
            $('.csObj .csInner a').each(function () {
                if (counter < 6) {
                    $(this).detach();
                    $(this).hide();
                    $('.csObj .csInner').append($(this));
                    $(this).fadeIn(100);
                    counter++;
                }
            });

            $('.csObj .csInner').css('margin-left', '0px');
        });

        e.preventDefault();
    });

    $('.detailObj .detailThumb a').click(function (e) {
        e.preventDefault();
        $('.detailObj .featureGallery img').attr('src', $(this).attr('rel'));
    });

    //wishlist
    function assignToWishListFunction(ctl) {
        var el = ctl;

        if (el.nextAll('input.type').length > 0 &&
            el.nextAll('input.id').length > 0) {
            var itemtype = el.nextAll('input.type').eq(0).val();
            var itemid = el.nextAll('input.id').eq(0).val();
            var op = 'add';
            if (ctl.hasClass('jsWishlistRemove'))
                op = 'remove';

            vehicleSearchShowWait();
            $.post("/handlers/Wishlist.ashx", { op: op, itemtype: itemtype, itemid: itemid }, function (data) {
                vehicleSearchHideWait();
                if (!data.Error) {
                    if (op == 'add') {
                        el.removeClass('jsWishlistAdd');
                        el.addClass('jsWishlistRemove');

                        if (el.hasClass('btn-addtowatchlist-sm')) {
                            el.removeClass('btn-addtowatchlist-sm');
                            el.addClass('btn-removewatchlist-sm');
                        }
                        else if (el.hasClass('btn-addtowatchlist')) {
                            el.removeClass('btn-addtowatchlist');
                            el.addClass('btn-removewatchlist');
                        }
                    }
                    else {
                        el.removeClass('jsWishlistRemove');
                        el.addClass('jsWishlistAdd');

                        if (el.hasClass('btn-removewatchlist-sm')) {
                            el.removeClass('btn-removewatchlist-sm');
                            el.addClass('btn-addtowatchlist-sm');
                        }
                        else if (el.hasClass('btn-removewatchlist')) {
                            el.removeClass('btn-removewatchlist');
                            el.addClass('btn-addtowatchlist');
                        }

                        if ($('.divWatchlistContainer').length > 0) {
                            //we are on the watchlist page, remove element
                            el.parents('a.lvItem').remove();

                            var counter = 0;
                            $('#divSearchResultsWrap a.lvItem').each(function () {
                                if (counter % 2 == 1) {
                                    ctl.css('margin-right', '0px');
                                    ctl.css('margin-bottom', '10px');
                                }
                                else {
                                    ctl.css('margin-right', '10px');
                                    ctl.css('margin-bottom', '10px');
                                }
                                counter++;
                            });
                        }
                    }
                    $('.spanWatchlistCount').html(data.ItemCount);
                }
                else {

                }
            }).error(function () {

            });
        }
    }

    $('.jsWishlistAdd, .jsWishlistRemove').click(function (e) {
        e.preventDefault();
        assignToWishListFunction($(this));
    });

    $('.btn-closefeature').click(function (e) {
        e.preventDefault();
        $(this).parents('.featureObj').eq(0).slideUp(500);
    });


    //index Gallery    
    if ($('.indexGallery, .brandGalleryHome').length > 1) {
        homeGalleryScroll();
    }
    function homeGalleryScroll() {
        setTimeout(function () {
            var currentImg = $('.indexGallery:visible, .brandGalleryHome:visible');
            var nextImg;
            if (currentImg.next('.indexGallery, .brandGalleryHome').length > 0)
                nextImg = currentImg.next();
            else
                nextImg = $('.indexGallery, .brandGalleryHome').eq(0);
            currentImg.fadeOut(500);
            nextImg.fadeIn(500, function () {
                homeGalleryScroll();
            });
        }, 4000);
    }
   
    //save search
    $('#aSaveSearchToggle').click(function (e) {
        e.preventDefault();

        if ($('.saveSearchObj').is(':visible'))
            $('.saveSearchObj').slideUp(500);
        else
            $('.saveSearchObj').slideDown(500);
    });
    $('.saveSearchObj a.close').click(function (e) {
        e.preventDefault();

        $('.saveSearchObj').slideUp(500);
    });

    $('.saveSearchObj label span').click(function () {
        var el = $(this);
        el.fadeOut(300, function () {
            el.next().focus();
        });
    });
    $('.saveSearchObj input').focus(function () {
        $(this).prev().fadeOut(300);
    });
    $('.saveSearchObj input').blur(function () {
        if ($(this).val() == '') {
            $(this).prev().fadeIn(300);
        }
    });
    $('#aSaveSearchAlert').click(function (e) {
        e.preventDefault();

        $('.saveSearchObj .success').hide();
        $('.saveSearchObj .error').hide();
        $('.saveSearchObj .error').html('');

        if ($('#tbSaveSearchName').val() == '') {
            $('.saveSearchObj .error').append('Please enter your name<br />');
        }
        if ($('#tbSaveSearchEmail').val() == '') {
            $('.saveSearchObj .error').append('Please enter your email<br />');
        }
        else if (!isValidEmail($('#tbSaveSearchEmail').val())) {
            $('.saveSearchObj .error').append('Please enter your valid email<br />');
        }

        if ($('.saveSearchObj .error').html() != '') {
            $('.saveSearchObj .error').show();
        }
        else {
            $('#aSaveSearchAlert').hide();
            $('.saveSearchObj .loading').fadeIn(500);
            $.post("/handlers/VehicleSearch.ashx", { op: 'subscribe', name: $('#tbSaveSearchName').val(), email: $('#tbSaveSearchEmail').val(), searchparams: document.location.href }, function (data) {
                $('.saveSearchObj .loading').hide();
                if (!data.Error) {
                    $('.saveSearchObj fieldset').fadeOut(300);
                    $('.saveSearchObj .success').fadeIn(500);
                }
                else {
                    $('.saveSearchObj .error').fadeIn(500);
                    $('.saveSearchObj .error').html(data.Message)
                }
            }).error(function () {
                $('.saveSearchObj .loading').hide();
                $('.saveSearchObj .error').html('An error has occurred. Please try again later.');
            });
        }
    });

    //lightbox
    function repositionLightbox() {
        var x = ($(window).width() - $('.lightbox').width()) / 2;
        $('.lightbox').css('left', x + 'px');
    }
    function repositionLightboxUmbraco() {
        $('.lightbox').css('left', '70px');
    }
    $(window).resize(function () {
        repositionLightbox();
    });
    $('.bgLightbox, .jsCloseLightbox').click(function (e) {
        e.preventDefault();

        $('.bgLightbox').fadeOut(300);
        $('.lightbox').fadeOut(300);
    });

    //enquire about this vehicle
    $('.jsEnquireVehicle').click(function (e) {
        e.preventDefault();

        $('.bgLightbox').fadeIn(300);
        $('.lbEnquire').fadeIn(300);

        repositionLightbox();

        goToByScrollByY(0);
    });
    $('.lbEnquire fieldset label span').click(function () {
        var el = $(this);
        el.fadeOut(300, function () {
            el.next().focus();
        });
    });
    $('.lbEnquire fieldset input').focus(function () {
        $(this).prev().fadeOut(300);
    });
    $('.lbEnquire fieldset input').blur(function () {
        if ($(this).val() == '') {
            $(this).prev().fadeIn(300);
        }
    });
    $('.lbEnquire .mlEnquire').click(function (e) {
        e.preventDefault();

        $('.lbEnquire .success').hide();
        $('.lbEnquire .error').hide();
        $('.lbEnquire .error').html('');

        if ($('#tbLBEnquireEnquiry').val() == '') {
            $('.lbEnquire .error').append('Please enter your enquiry<br />');
        }
        if ($('#tbLBEnquireName').val() == '') {
            $('.lbEnquire .error').append('Please enter your name<br />');
        }
        if ($('#tbLBEnquireEmail').val() == '') {
            $('.lbEnquire .error').append('Please enter your email<br />');
        }
        else if (!isValidEmail($('#tbLBEnquireEmail').val())) {
            $('.lbEnquire .error').append('Please enter your valid email<br />');
        }

        if ($('.lbEnquire .error').html() != '') {
            $('.lbEnquire .error').show();
        }
        else {
            $('.lbEnquire .mlEnquire').hide();
            $('.lbEnquire .loading').fadeIn(500);

            $.post("/handlers/VehicleEnquiry.ashx", { enquiry: $('#tbLBEnquireEnquiry').val(), name: $('#tbLBEnquireName').val(), email: $('#tbLBEnquireEmail').val(), phone: $('#tbLBEnquirePhone').val(), vehicleId: $('#hdVehicleId').val() }, function (data) {
                $('.lbEnquire .loading').hide();
                $('.lbEnquire .mlEnquire').show();
                if (!data.Error) {

                    $('.lbEnquire .success').fadeIn(500);
                }
                else {
                    $('.lbEnquire .error').fadeIn(500);
                    $('.lbEnquire .error').html(data.Message)
                }
            }).error(function () {
                $('.lbEnquire .loading').hide();
                $('.lbEnquire .error').html('An error has occurred. Please try again later.');
            });
        }
    });

    //share by email
    $('.jsShareByEmail').click(function (e) {
        e.preventDefault();

        $('.bgLightbox').fadeIn(300);
        $('#divLBShareByEmail').fadeIn(300);

        repositionLightbox();

        goToByScrollByY(0);
    });

    $('#tbLBShareFriendEmail').keyup(function (e) {
        if (e.keyCode == 188 ||
                e.keyCode == 59 ||
                e.keyCode == 13) {
            updateFriendEmail();
        }
    });
    $('#tbLBShareFriendEmail').blur(function () {
        if ($(this).val() != '') {
            updateFriendEmail();
        }
    });
    function updateFriendEmail() {
        var emailTo = $('#tbLBShareFriendEmail').val().replace(',', '').replace(';', '');
        if (isValidEmail(emailTo)) {
            $('#divLBShareByEmailEmails').append('<a href="#" class="shareLink">' + emailTo + '</a>');
            $('#tbLBShareFriendEmail').val('');

            $('#divLBShareByEmailEmails a').unbind('click');
            $('#divLBShareByEmailEmails a').bind('click', deleteFriendEmail);
        }
    }

    $('#divLBShareByEmailEmails a').bind('click', deleteFriendEmail);
    function deleteFriendEmail() {
        $(this).remove();
        return false;
    }

    $('#divLBShareByEmail fieldset label span').click(function () {
        var el = $(this);
        el.fadeOut(300, function () {
            el.next().focus();
        });
    });
    $('#divLBShareByEmail fieldset input').focus(function () {
        $(this).prev().fadeOut(300);
    });
    $('#divLBShareByEmail fieldset input').blur(function () {
        if ($(this).val() == '') {
            $(this).prev().fadeIn(300);
        }
    });
    $('#tbLBShareFriendEmail').val('');
    $('#tbLBShareFromName').val('');
    $('#tbLBShareFromEmail').val('');
    $('#tbLBShareMessage').val('');
    $('#divLBShareByEmail .mlEnquire').click(function (e) {
        e.preventDefault();

        $('#divLBShareByEmail .success').hide();
        $('#divLBShareByEmail .error').hide();
        $('#divLBShareByEmail .error').html('');

        if ($('#divLBShareByEmailEmails a').length == 0) {
            $('#divLBShareByEmail .error').append("Please enter at least one of your friend's email<br />");
        }
        if ($('#tbLBShareFromName').val() == '') {
            $('#divLBShareByEmail .error').append('Please enter your name<br />');
        }
        if ($('#tbLBShareFromEmail').val() == '') {
            $('#divLBShareByEmail .error').append('Please enter your email<br />');
        }
        else if (!isValidEmail($('#tbLBShareFromEmail').val())) {
            $('#divLBShareByEmail .error').append('Please enter your valid email<br />');
        }

        if ($('#divLBShareByEmail .error').html() != '') {
            $('#divLBShareByEmail .error').show();
        }
        else {
            $('#divLBShareByEmail .mlEnquire').hide();
            $('#divLBShareByEmail .loading').fadeIn(500);

            var emailTos = '';
            $('#divLBShareByEmailEmails a').each(function () {
                emailTos += $(this).html() + ',';
            });
            var vehicleId = '';
            if ($('#hdVehicleId').length > 0) {
                vehicleId = $('#hdVehicleId').val();
            }

            var pageurl = document.location.href;
            if ($('.jsShareByEmail').attr('href') != '#') {
                pageurl = $('.jsShareByEmail').attr('href');
            }
            $.post("/handlers/ShareByEmail.ashx", { emailTos: emailTos, fromName: $('#tbLBShareFromName').val(), fromEmail: $('#tbLBShareFromEmail').val(), message: $('#tbLBShareMessage').val(), pageurl: pageurl, vehicleId: vehicleId }, function (data) {
                $('#divLBShareByEmail .loading').hide();
                $('#divLBShareByEmail .mlEnquire').show();
                if (!data.Error) {
                    $('#divLBShareByEmailEmails').html('');
                    $('#divLBShareByEmail .success').fadeIn(500);
                }
                else {
                    $('#divLBShareByEmail .error').fadeIn(500);
                    $('#divLBShareByEmail .error').html(data.Message)
                }
            }).error(function () {
                $('#divLBShareByEmail .loading').hide();
                $('#divLBShareByEmail .mlEnquire').show();
                $('#divLBShareByEmail .error').html('An error has occurred. Please try again later.');
            });
        }
    });

    //RG Added 23/09/2011

    $('#ctl00_ctlImage_fulImageUpload').change(function (e) {
        alert($('#ctl00_ctlImage_fulImageUpload').val());

        $.post("/handlers/NewsImage.ashx", {}, function (data) {
            $('#divLBImage .loading').hide();
            if (!data.Error) {
                $('#divLBImage .success').html('Worked');
                $('#divLBImage .success').fadeIn(500);
            }
            else {
                $('#divLBImage .error').fadeIn(500);
                $('#divLBImage .error').html(data.Message);
                $('#divLBImage a.btn-bglight').show();
            }
        }).error(function (xhr, ajaxOptions, thrownError) {
            $('#divLBImage .loading').hide();
            $('#divLBImage .error').fadeIn(500);
            $('#divLBImage .error').html('An error has occurred. Please try again later. ' + xhr.responseText);
        });
    });

    //--News Article Header
    $('.jsNewsArticle').click(function (e) {
        e.preventDefault();

        $('.bgLightbox').fadeIn(300);
        $('#divLBNews').fadeIn(300);

        repositionLightboxUmbraco();

        $('#divLBNews input').val('');
        //set header text
        if ($('#ctl00_ChildContent_hdTitle').val() != '') {
            $('#tbLBText').val($('#ctl00_ChildContent_hdTitle').val());
            $('#tbLBText').prev().fadeOut(300);
        }

        goToByScrollByY(0);
    });

    $('#divLBNews label span').click(function () {
        var el = $(this);
        el.fadeOut(300, function () {
            el.next().focus();
        });
    });
    $('#divLBNews input[type="text"]').focus(function () {
        $(this).prev().fadeOut(300);
    });
    $('#divLBNews input[type="text"]').blur(function () {
        if ($(this).val() == '') {
            $(this).prev().fadeIn(300);
        }
    });
    $('#divLBNews a.btn-bglight').click(function (e) {
        e.preventDefault();
        saveTitle();
    });
    $('#tbLBText').keyup(function (e) {
        if (e.keyCode == 13) {
            saveTitle();
        }
    });

    function saveTitle() {
        $('#divLBNews .success').hide();
        $('#divLBNews .error').hide();
        $('#divLBNews .error').html('');

        if ($('#tbLBText').val() == '') {
            $('#divLBNews .error').append('Please enter your text<br />');
        }

        if ($('#divLBNews .error').html() != '') {
            $('#divLBNews .error').show();
        }
        else {
            $('#divLBNews a.btn-bglight').hide();
            $('#divLBNews .loading').fadeIn(500);

            $('#ctl00_ChildContent_lblTitle').html($('#tbLBText').val());
            $('#ctl00_ChildContent_hdTitle').val($('#tbLBText').val());
            $('#divLBNews .loading').hide();
            $('#divLBNews a.btn-bglight').show();
            $('.bgLightbox').fadeOut(300);
            $('.lightbox').fadeOut(300);
        }
    }

    //--News Article Synopsis
    $('.jsSynopsis').click(function (e) {
        e.preventDefault();

        $('.bgLightbox').fadeIn(300);
        $('#divLBSynopsis').fadeIn(300);

        repositionLightboxUmbraco();
        $('#divLBSynopsis input').val('');
        //set header text
        if ($('#ctl00_ChildContent_hdSynopsis').val() != '') {
            $('#tbLBSynopsis').val($('#ctl00_ChildContent_hdSynopsis').val());
            $('#tbLBSynopsis').prev().fadeOut(300);
        }

        goToByScrollByY(0);
    });

    $('#divLBSynopsis label span').click(function () {
        var el = $(this);
        el.fadeOut(300, function () {
            el.next().focus();
        });
    });
    $('#divLBSynopsis textarea').focus(function () {
        $(this).prev().fadeOut(300);
    });
    $('#divLBSynopsis textarea').blur(function () {
        if ($(this).val() == '') {
            $(this).prev().fadeIn(300);
        }
    });
    $('#divLBSynopsis a.btn-bglight').click(function (e) {
        e.preventDefault();
        saveSynopsis();
    });


    function saveSynopsis() {
        $('#divLBSynopsis .success').hide();
        $('#divLBSynopsis .error').hide();
        $('#divLBSynopsis .error').html('');

        if ($('#tbLBSynopsis').val() == '') {
            $('#divLBSynopsis .error').append('Please enter your text<br />');
        }

        if ($('#divLBSynopsis .error').html() != '') {
            $('#divLBSynopsis .error').show();
        }
        else {
            $('#divLBSynopsis a.btn-bglight').hide();
            $('#divLBSynopsis .loading').fadeIn(500);

            $('#ctl00_ChildContent_lblSynopsis').html($('#tbLBSynopsis').val());
            $('#ctl00_ChildContent_hdSynopsis').val($('#tbLBSynopsis').val());
            $('#divLBSynopsis .loading').hide();
            $('#divLBSynopsis a.btn-bglight').show();
            $('.bgLightbox').fadeOut(300);
            $('.lightbox').fadeOut(300);
        }
    }

    //--News Article Author
    $('.jsAuthor').click(function (e) {
        e.preventDefault();

        $('.bgLightbox').fadeIn(300);
        $('#divLBAuthor').fadeIn(300);

        repositionLightboxUmbraco();

        $('#divLBAuthor input').val('');
        //set text
        if ($('#ctl00_ChildContent_hdAuthor').val() != '') {
            $('#tbLBAuthor').val($('#ctl00_ChildContent_hdAuthor').val());
            $('#tbLBAuthor').prev().fadeOut(300);
        }

        goToByScrollByY(0);
    });

    $('#divLBAuthor label span').click(function () {
        var el = $(this);
        el.fadeOut(300, function () {
            el.next().focus();
        });
    });
    $('#divLBAuthor input[type="text"]').focus(function () {
        $(this).prev().fadeOut(300);
    });
    $('#divLBAuthor input[type="text"]').blur(function () {
        if ($(this).val() == '') {
            $(this).prev().fadeIn(300);
        }
    });
    $('#divLBAuthor a.btn-bglight').click(function (e) {
        e.preventDefault();
        saveAuthor();
    });
    $('#tbLBText').keyup(function (e) {
        if (e.keyCode == 13) {
            saveAuthor();
        }
    });

    function saveAuthor() {
        $('#divLBAuthor .success').hide();
        $('#divLBAuthor .error').hide();
        $('#divLBAuthor .error').html('');

        if ($('#tbLBAuthor').val() == '') {
            $('#divLBAuthor .error').append('Please enter your text<br />');
        }

        if ($('#divLBAuthor .error').html() != '') {
            $('#divLBAuthor .error').show();
        }
        else {
            $('#divLBAuthor a.btn-bglight').hide();
            $('#divLBAuthor .loading').fadeIn(500);

            $('#ctl00_ChildContent_lblAuthor').html($('#tbLBAuthor').val());
            $('#ctl00_ChildContent_hdAuthor').val($('#tbLBAuthor').val());
            $('#divLBAuthor .loading').hide();
            $('#divLBAuthor a.btn-bglight').show();
            $('.bgLightbox').fadeOut(300);
            $('.lightbox').fadeOut(300);
        }
    }


    //--News Article Body
    $('.jsBody').click(function (e) {
        e.preventDefault();

        $('.bgLightbox').fadeIn(300);
        $('#divLBBody').fadeIn(300);

        repositionLightboxUmbraco();

        $('#divLBBody input').val('');
        //set text
        if ($('#ctl00_ChildContent_hdBody').val() != '') {
            //function on LightBoxBodt.ascx
            SetText($('#ctl00_ChildContent_hdBody').val());
            $('#elm1').prev().fadeOut(300);

        }

        goToByScrollByY(0);
    });

    $('#divLBBody a.btn-bglight').click(function (e) {
        e.preventDefault();
        saveBody();
    });

    function saveBody() {
        $('#divLBBody .success').hide();
        $('#divLBBody .error').hide();
        $('#divLBBody .error').html('');

        if ($('#tbLBBody').val() == '') {
            $('#divLBBody .error').append('Please enter your text<br />');
        }

        if ($('#divLBBody .error').html() != '') {
            $('#divLBBody .error').show();
        }
        else {
            $('#divLBBody a.btn-bglight').hide();
            $('#divLBBody .loading').fadeIn(500);

            $('#ctl00_ChildContent_lblBody').html(GetText());
            $('#ctl00_ChildContent_hdBody').val(GetText());

            $('#divLBBody .loading').hide();
            $('#divLBBody a.btn-bglight').show();
            $('.bgLightbox').fadeOut(300);
            $('.lightbox').fadeOut(300);
        }
    }

    //--News Article OtherNews
    $('.jsOtherNews').click(function (e) {
        e.preventDefault();

        $('.bgLightbox').fadeIn(300);
        $('#divLBOtherNews').fadeIn(300);

        repositionLightboxUmbraco();

        $('input[type=checkbox]').each(function () {
            if ($(this).attr('id').indexOf('|') > -1) {
                var substr = $(this).attr('id').split('|');
                var found = isChecked(substr[0]);
                if (found == true) {
                    $(this).attr('checked', 'checked');
                }

            }

        });


        goToByScrollByY(0);
    });

    function isChecked(id) {

        var ids = $('#ctl00_ChildContent_hdOtherNews').val();
        if (ids.indexOf(',') > -1) {
            var array = ids.split(',');
            for (i = 0; i < array.length; i++) {
                if (array[i] == id)
                    return true;
            } /*
            $.each(array, function (counter, str) {

                if (str == id)
                    return true;
            });*/
        }
        else {
            if (ids == id)
                return true;
        }

        return false;
    }

    $('#divLBOtherNews #btnAdd').click(function (e) {
        e.preventDefault();
        $('input[type=checkbox]').each(function () {
            if ($(this).is(':checked')) {
                sList += $(this).attr('name') + '|' + $(this).attr('id') + ',';
                var substr = $(this).attr('id').split('|');
                displayList += '<a href="#">' + substr[1] + '</a>';
            }
        });
    });

    $('#divLBOtherNews a.btn-bglight').click(function (e) {
        e.preventDefault();
        saveOtherNews();
    });
    $('#tbLBText').keyup(function (e) {
        if (e.keyCode == 13) {
            saveOtherNews();
        }
    });


    function saveOtherNews() {
        $('#divLBOtherNews .success').hide();
        $('#divLBOtherNews .error').hide();
        $('#divLBOtherNews .error').html('');

        if ($('#tbLBOtherNews').val() == '') {
            $('#divLBOtherNews .error').append('Please enter your text<br />');
        }

        if ($('#divLBOtherNews .error').html() != '') {
            $('#divLBOtherNews .error').show();
        }
        else {
            $('#divLBOtherNews a.btn-bglight').hide();
            $('#divLBOtherNews .loading').fadeIn(500);

            var sList = "";
            var displayList = "";
            $('input[type=checkbox]').each(function () {
                if ($(this).is(':checked')) {
                    sList += $(this).attr('name') + '|' + $(this).attr('id') + ',';
                    var substr = $(this).attr('id').split('|');
                    displayList += '<a href="#">' + substr[1] + '</a>';
                }
            });

            $('#ctl00_ChildContent_lblOtherNewsLinks').html(displayList);
            $('#ctl00_ChildContent_hdOtherNews').val(sList);


            $('#divLBOtherNews .loading').hide();
            $('#divLBOtherNews a.btn-bglight').show();
            $('.bgLightbox').fadeOut(300);
            $('.lightbox').fadeOut(300);
        }
    }

    //--News Article Image
    $('.jsNewsImage').click(function (e) {
        e.preventDefault();

        $('.bgLightbox').fadeIn(300);
        $('#divLBImage').fadeIn(300);

        repositionLightboxUmbraco();


        goToByScrollByY(0);
    });

    $('#divLBImage label span').click(function () {
        var el = $(this);
        el.fadeOut(300, function () {
            el.next().focus();
        });
    });
    $('#divLBImage input[type="text"]').focus(function () {
        $(this).prev().fadeOut(300);
    });
    $('#divLBImage input[type="text"]').blur(function () {
        if ($(this).val() == '') {
            $(this).prev().fadeIn(300);
        }
    });
    $('#divLBImage a.btn-bglight').click(function (e) {
        e.preventDefault();
        saveImage();
    });
    $('#tbLBText').keyup(function (e) {
        if (e.keyCode == 13) {
            saveImage();
        }
    });

    function saveImage() {
        $('#divLBImage .success').hide();
        $('#divLBImage .error').hide();
        $('#divLBImage .error').html('');

        if ($('#tbLBImage').val() == '') {
            $('#divLBImage .error').append('Please enter your text<br />');
        }

        if ($('#divLBImage .error').html() != '') {
            $('#divLBImage .error').show();
        }
        else {
            $('#divLBImage a.btn-bglight').hide();
            $('#divLBImage .loading').fadeIn(500);

            $('#ctl00_ChildContent_lblImage').html($('#tbLBImage').val());
            $('#ctl00_ChildContent_hdImage').val($('#tbLBImage').val());
            $('#divLBImage .loading').hide();
            $('#divLBImage a.btn-bglight').show();
            $('.bgLightbox').fadeOut(300);
            $('.lightbox').fadeOut(300);
        }
    }

    //RG END

    //--login
    $('.jsLogin').click(function (e) {
        e.preventDefault();

        $('.bgLightbox').fadeIn(300);
        $('#divLBLogin').fadeIn(300);

        repositionLightbox();

        $('#divLBLogin input').val('');

        goToByScrollByY(0);
    });
    $('#divLBLogin .jsSwitchToSignUp').click(function (e) {
        e.preventDefault();
        $('#divLBLogin').slideUp(200, function () {
            $('#divLBSignUp').slideDown(400);
        });
    });

    $('#divLBLogin label span').click(function () {
        var el = $(this);
        el.fadeOut(300, function () {
            el.next().focus();
        });
    });
    $('#divLBLogin input[type="text"], #divLBLogin input[type="password"]').focus(function () {
        $(this).prev().fadeOut(300);
    });
    $('#divLBLogin input[type="text"], #divLBLogin input[type="password"]').blur(function () {
        if ($(this).val() == '') {
            $(this).prev().fadeIn(300);
        }
    });
    $('#divLBLogin a.btn-bglight').click(function (e) {
        e.preventDefault();
        attemptLogin();
    });
    $('#tbLBLoginEmail, #tbLBLoginPassword').keyup(function (e) {
        if (e.keyCode == 13) {
            attemptLogin();
        }
    });
    function attemptLogin() {
        $('#divLBLogin .success').hide();
        $('#divLBLogin .error').hide();
        $('#divLBLogin .error').html('');

        if ($('#tbLBLoginEmail').val() == '') {
            $('#divLBLogin .error').append('Please enter your email<br />');
        }
        else if (!isValidEmail($('#tbLBLoginEmail').val())) {
            $('#divLBLogin .error').append('Please enter your valid email<br />');
        }
        if ($('#tbLBLoginPassword').val() == '') {
            $('#divLBLogin .error').append('Please enter your password<br />');
        }

        if ($('#divLBLogin .error').html() != '') {
            $('#divLBLogin .error').show();
        }
        else {
            $('#divLBLogin a.btn-bglight').hide();
            $('#divLBLogin .loading').fadeIn(500);
            var remember = 'false';
            if ($('#cbLBLoginRemember').is(':checked'))
                remember = 'true';
            $.post("/handlers/Member.ashx", { op: 'login', email: $('#tbLBLoginEmail').val(), password: $('#tbLBLoginPassword').val(), remember: remember }, function (data) {
                $('#divLBLogin .loading').hide();
                if (!data.Error) {
                    //redirect
                    //window.location = data.Message;
                    location.reload();
                }
                else {
                    $('#divLBLogin .error').fadeIn(500);
                    $('#divLBLogin .error').html(data.Message);
                    $('#divLBLogin a.btn-bglight').show();
                }
            }).error(function () {
                $('#divLBLogin .loading').hide();
                $('#divLBLogin .error').html('An error has occurred. Please try again later.');
            });
        }
    }

    //--Follow Keep up
    $('.jsFollowKeepUp').click(function (e) {
        e.preventDefault();

        $('.bgLightbox').fadeIn(300);
        $('#divLBFollowKeepUp').fadeIn(300);

        $('#divLBFollowKeepUp a.btn-bglight').attr('rel', $(this).attr('rel'));


        if ($(this).attr('rel') == 2) {
            $('#divFollowTopText').html($('#divFollowTopText').html().replace('{0}', "News & Events"));
        }
        else if ($(this).attr('rel') == '3') {
            $('#divFollowTopText').html($('#divFollowTopText').html().replace('{0}', "Motorsport News"));
        }
        else if ($(this).attr('rel') == 4) {
            $('#divFollowTopText').html($('#divFollowTopText').html().replace('{0}', "Videos"));
        }

        repositionLightbox();

        $('#divLBFollowKeepUp input').val('');

        goToByScrollByY(0);

    });

    $('#divLBFollowKeepUp label span').click(function () {
        var el = $(this);
        el.fadeOut(300, function () {
            el.next().focus();
        });
    });

    $('#divLBFollowKeepUp a.btn-bglight').click(function (e) {
        e.preventDefault();
        SubscripUser($(this).attr('rel'), $(this).attr('rev'));

    });

    function SubscripUser(type, makes) {
        $('.success').hide();
        $('.error').hide();

        var emailAddress = $('#tbLBFollowEmail').val();

        if (emailAddress == '') {
            $('.error').html('Please enter you email address');
            $('.error').show();

            return false;
        }
        if (type == 3)
            makes = 'Motorsport';
        if (type == 4)
            makes = 'Video';


        $.post("/handlers/FollowKeepUpHandler.ashx", { op: 'AddFollowKeepUp', email: emailAddress, makes: makes, type: type }, function (data) {
            if (!data.Error) {
                $('.success').html('News alerts saved');
                $('.success').show();
            }
            else {
                $('.error').html(data.message);
                $('.error').show();
            }
        }).error(function () {
            alert('Error');
        });
    }

    //--Follow Keep up END



    //--sign up
    $('.jsSignUp').click(function (e) {
        e.preventDefault();

        $('.bgLightbox').fadeIn(300);
        $('#divLBSignUp').fadeIn(300);

        repositionLightbox();

        $('#divLBSignUp input').val('');

        goToByScrollByY(0);
    });
    $('#divLBSignUp .jsSwitchToLogin').click(function (e) {
        e.preventDefault();
        $('#divLBSignUp').slideUp(200, function () {
            $('#divLBLogin').slideDown(400);
        });
    });

    $('#divLBSignUp label span').click(function () {
        var el = $(this);
        el.fadeOut(300, function () {
            el.next().focus();
        });
    });
    $('#divLBSignUp input').focus(function () {
        $(this).prev().fadeOut(300);
    });
    $('#divLBSignUp input').blur(function () {
        if ($(this).val() == '') {
            $(this).prev().fadeIn(300);
        }
    });
    $('#divLBSignUp a.btnLightboxL').click(function (e) {
        e.preventDefault();

        $('#divLBSignUp .success').hide();
        $('#divLBSignUp .error').hide();
        $('#divLBSignUp .error').html('');

        if ($('#tbLBSignUpEmail').val() == '') {
            $('#divLBSignUp .error').append('Please enter your email<br />');
        }
        else if (!isValidEmail($('#tbLBSignUpEmail').val())) {
            $('#divLBSignUp .error').append('Please enter your valid email<br />');
        }
        if ($('#tbLBSignUpPassword').val() == '') {
            $('#divLBSignUp .error').append('Please enter your password<br />');
        }
        if ($('#tbLBSignUpConfirmPassword').val() == '') {
            $('#divLBSignUp .error').append('Please enter your confirmation password<br />');
        }
        else if ($('#tbLBSignUpPassword').val() != $('#tbLBSignUpConfirmPassword').val()) {
            $('#divLBSignUp .error').append('Password and password confirmation must match<br />');
        }

        if ($('#divLBSignUp .error').html() != '') {
            $('#divLBSignUp .error').show();
        }
        else {
            $('#divLBSignUp a.btnLightboxL').hide();
            $('#divLBSignUp .loading').fadeIn(500);
            $.post("/handlers/Member.ashx", { op: 'signup', email: $('#tbLBSignUpEmail').val(), password: $('#tbLBSignUpPassword').val() }, function (data) {
                $('#divLBSignUp .loading').hide();
                if (!data.Error) {
                    //redirect
                    window.location = data.Message;
                }
                else {
                    $('#divLBSignUp .error').fadeIn(500);
                    $('#divLBSignUp .error').html(data.Message);
                    $('#divLBSignUp a.btnLightboxL').show();
                }
            }).error(function () {
                $('#divLBSignUp .loading').hide();
                $('#divLBSignUp .error').html('An error has occurred. Please try again later.');
            });
        }
    });


    //--forgot password
    $('#divLBLogin .jsSwitchToForgotPassword').click(function (e) {
        e.preventDefault();
        $('#divLBLogin').slideUp(200, function () {
            $('#divLBResetPassword').slideDown(400);
        });

        $('#divLBResetPassword input').val('');
    });
    $('#divLBResetPassword .jsSwitchToSignUp').click(function (e) {
        e.preventDefault();
        $('#divLBResetPassword').slideUp(200, function () {
            $('#divLBSignUp').slideDown(400);
        });
    });

    $('#divLBResetPassword label span').click(function () {
        var el = $(this);
        el.fadeOut(300, function () {
            el.next().focus();
        });
    });
    $('#divLBResetPassword input').focus(function () {
        $(this).prev().fadeOut(300);
    });
    $('#divLBResetPassword input').blur(function () {
        if ($(this).val() == '') {
            $(this).prev().fadeIn(300);
        }
    });
    $('#divLBResetPassword a.btnLightboxR').click(function (e) {
        e.preventDefault();
        resetPassword();
    });
    $('#tbLBResetPasswordEmail').keyup(function (e) {
        if (e.keyCode == 13) {
            resetPassword();
        }
    });
    function resetPassword() {
        $('#divLBResetPassword .success').hide();
        $('#divLBResetPassword .error').hide();
        $('#divLBResetPassword .error').html('');

        if ($('#tbLBResetPasswordEmail').val() == '') {
            $('#divLBResetPassword .error').append('Please enter your email<br />');
        }
        else if (!isValidEmail($('#tbLBResetPasswordEmail').val())) {
            $('#divLBResetPassword .error').append('Please enter your valid email<br />');
        }

        if ($('#divLBResetPassword .error').html() != '') {
            $('#divLBResetPassword .error').show();
        }
        else {
            $('#divLBResetPassword a.btnLightboxR').hide();
            $('#divLBResetPassword .loading').fadeIn(500);
            $.post("/handlers/Member.ashx", { op: 'resetpassword', email: $('#tbLBResetPasswordEmail').val() }, function (data) {
                $('#divLBResetPassword .loading').hide();
                if (!data.Error) {
                    $('#divLBResetPassword .success').fadeIn(500);
                    $('#divLBResetPassword .success').html(data.Message);
                }
                else {
                    $('#divLBResetPassword .error').fadeIn(500);
                    $('#divLBResetPassword .error').html(data.Message);
                    $('#divLBResetPassword a.btnLightboxR').show();
                }
            }).error(function () {
                $('#divLBResetPassword .loading').hide();
                $('#divLBResetPassword .error').html('An error has occurred. Please try again later.');
            });
        }
    }
});
//gallery sliding images
function slidingGalleryImageClick() {
    var spanGallery = $(this).parent().parent().find('.gvGallery');
    var imgCurrent = spanGallery.find('img:visible');
    if ($(this).hasClass('gvL')) {
        //prev
        var imgPrev = imgCurrent.prev();
        if (imgCurrent.index() == 0)
            imgPrev = spanGallery.find('img').eq(spanGallery.find('img').length - 1);
        imgCurrent.fadeOut(200);
        imgPrev.fadeIn(200);
    }
    else {
        //next
        var imgNext = imgCurrent.next();
        if (imgCurrent.index() == spanGallery.find('img').length - 1)
            imgNext = spanGallery.find('img').eq(0);
        imgCurrent.fadeOut(200);
        imgNext.fadeIn(200);
    }
    return false;
}
function vehicleSearchShowWait() {
    
}
function vehicleSearchHideWait() {

}
function matchHeight() {
    $('.jsAutoH').css('height', 'auto');
    $('.jsAutoH').each(function () {
        //find max height
        var maxHeight = 0;
        $(this).parent().find('.jsAutoH').each(function () {
            if ($(this).height() > maxHeight)
                maxHeight = $(this).height();
        });
        if ($(this).hasClass('js2Level')) {
            $(this).parent().parent().find('.jsAutoH').each(function () {
                if ($(this).height() > maxHeight)
                    maxHeight = $(this).height();
            });
        }
        $(this).css('height', maxHeight + 'px');
    });
}

//Other Makes
$(document).ready(function () {

    $('#ddlOtherMakes').change(function (e) {
        var view = $('.gallerySelector>a.active').attr('id').replace('aSearch_', '');
        $.post("/handlers/VehicleSearch.ashx", { op: 'othermakes', view: view, make: $(this).val() }, function (data) {
            vehicleSearchHideWait();
            if (!data.Error) {
                $('#divSearchResultsWrap').html(data.Output);                                
            }
            else {

            }
        }).error(function () {
            alert('Error');
        });

    });

});
