﻿//Vehicle viewer pretty box initialisation
$(document).ready(function() {
    
    $("a[rel^='prettyPhoto']").prettyPhoto({
        animationSpeed: 'normal', /* fast/slow/normal */
        padding: 40, /* padding for each side of the picture */
        opacity: 0.35, /* Value betwee 0 and 1 */
        showTitle: false, /* true/false */
        allowresize: true, /* true/false */
        counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
        theme: 'light_rounded' /* light_rounded / dark_rounded / light_square / dark_square */
    });
});

/////////////////////
//View Car functions
function LaunchCarViewer(vehicle) {
    $("#launchthickbox").attr("href", "/ViewCarSection/ViewCarSpec.aspx?KeepThis=true&VehicleID=" + vehicle + "&height=625&width=710");
    $("#launchthickbox").click();
}

function LaunchAlternativeStockViewer() {
    $("#AdvancedSearchLoader").css({ 'display': 'block' });
    var paramList = '{' + '"newSearch":"' + $("#quickSearchType_1").attr("checked") + '"' + ',';
    paramList += '"model":"' + $('select#ddlAdvSearchModel').val() + '"' + ',';
    paramList += '"variant":"' + $('select#ddlAdvSearchVariant').val() + '"' + ',';
    paramList += '"transmission":"' + $('select#ddlAdvSearchTrans').val() + '"' + ',';
    paramList += '"fuelType":"' + $('select#ddlAdvSearchFuelType').val() + '"' + ',';
    paramList += '"location":"' + $('select#ddlAdvSearchLocation').val() + '"' + ',';
    paramList += '"locationName":"' + $('select#ddlAdvSearchLocation') + '"' + ',';
    paramList += '"priceRange":"' + $('input#ctl00_amount').val() + '"' + ',';
    paramList += '"yearRange":"' + $('input#ctl00_amount3').val() + '"' + ',';
    paramList += '"mileage":"' + $('input#ctl00_amount2').val() + '"' + ',';
    //Optional Extras
    paramList += '"airCon":"' + $("#oeAirCon").attr("checked") + '"' + ',';
    paramList += '"cdPlayer":"' + $("#oeCDPlayer").attr("checked") + '"' + ',';
    paramList += '"parkingSensors":"' + $("#oeParkingSensors").attr("checked") + '"' + ',';
    paramList += '"satNav":"' + $("#oeSatNav").attr("checked") + '"' + ',';
    paramList += '"sunRoof":"' + $("#oeSunroof").attr("checked") + '"' + ',';
    paramList += '"tintedGlass":"' + $("#oeTintedGlass").attr("checked") + '"}';

    $.ajax({
        type: "POST",
        url: "/ViewCarSection/ViewAlternativeStock.aspx/SelectAlternativeStock",
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: function(msg) {
            $("#AdvancedSearchLoader").css({ 'display': 'none' });
            hideHtmlForNonSearchPage();
            //$("#searchResultsAlternative").html(msg.d);
            $("#searchResults").html(msg.d);
            //$("#launchthickboxnocars").click();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    })

}

/*function ViewAlternativeStockViewerResults(locationType, model, id, locationName, searchType) {
    //alert(locationType + ' ' + model + ' ' + id);
    var searchSpec = '{' + '"locationType":"' + locationType + '"' + ',';
    searchSpec += '"model":"' + model + '"' + ',';
    searchSpec += '"locationName":"' + locationName + '"' + ',';
    searchSpec += '"searchType":"' + locationName + '"' + ',';
    searchSpec += '"id":"' + id + '"}';
    $.ajax({
        type: "POST",
        url: "/ViewCarSection/ViewAlternativeStock.aspx/DisplayAlternativeStock",
        contentType: "application/json; charset=utf-8",
        data: searchSpec,
        dataType: "json",
        success: function(msg) {
            $("#searchResults").html(msg.d);
            CloseAlternativeStockViewer();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    })
}*/

function LaunchEnquiryForm(dealership, vehicleID, fullReg, modelDesc, colour, fuelType, transmission, price) {
    var enqVehicle = '{' + '"dealershipName":"' + dealership + '"' + ',';
    enqVehicle += '"vehicleID":"' + vehicleID + '"' + ',';
    enqVehicle += '"fullReg":"' + fullReg + '"' + ',';
    enqVehicle += '"modelDesc":"' + modelDesc + '"' + ',';
    enqVehicle += '"colour":"' + colour + '"' + ',';
    enqVehicle += '"fuelType":"' + fuelType + '"' + ',';
    enqVehicle += '"transmission":"' + transmission + '"' + ',';
    enqVehicle += '"price":"' + price + '"}';
    $.ajax({
        type: "POST",
        url: "/ViewCarSection/ViewEnquiryForm.aspx/createForm",
        contentType: "application/json; charset=utf-8",
        data: enqVehicle,
        dataType: "json",
        success: function(msg) {
            $("#vehicleEnqContainerView").html(msg.d);
            $("#viewEnqFormFromSearchAnchor").click(); 
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    })
}

function ClosePdfViewer() {
    $("#vehiclePdfContainer").html('');
    $("#vehiclePdfContainer").css({ 'display': 'none' });
}

function CloseEnquiryForm() {
    tb_remove();
    $("#vehicleEnqContainerView").html('');
    $("#vehicleEnqContainerView").css({ 'display': 'none' });
}

function CloseAlternativeStockViewer() {
    $("#TB_imageOff").unbind("click");
    $("#TB_closeWindowButton").unbind("click");
    $("#TB_window").fadeOut("fast", function() { $('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove(); });
    $("#TB_load").remove();
    if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
        $("body", "html").css({ height: "auto", width: "auto" });
        $("html").css("overflow", "");
    }
    document.onkeydown = "";
    document.onkeyup = "";
    return false;
}

function LaunchPdfViewer(vehicle) {
    var pdfVehicle = '{' + '"vehicleID":"' + vehicle + '"}';
    $.ajax({
        type: "POST",
        url: "/ViewCarSection/ViewCarPdf.aspx/createPdf",
        contentType: "application/json; charset=utf-8",
        data: pdfVehicle,
        dataType: "json",
        success: function(msg) {
            $("#vehiclePdfContainer").css({ 'display': 'block' });
            $("#vehiclePdfContainer").html(msg.d + '<div style=\'background-color:white;border:1px solid #999;\'><img style=\"cursor:pointer;\" onclick="ClosePdfViewer();" onmouseover=\"javascript:this.src=\'/images/buttons/viewcar/close-on-white.jpg\';\"  onmouseout=\"javascript:this.src=\'/images/buttons/viewcar/close-off-white.jpg\';\" src=\'/images/buttons/viewCar/close-off-white.jpg\' /></div>');
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            
        }
    })
}

/////////////////////////////////
// Default search on page load //
/////////////////////////////////
$(document).ready(function() {


//    if (window.location.search.indexOf('Quick') != -1) {
//        QuickSearchFromAnotherPage('1', '10', '0', 'price ASC');
//    }

    var searchTypePos = window.location.search.indexOf('type');
    var sSearchType = 'New';
    if (searchTypePos != -1)
        sSearchType = window.location.search.substring(searchTypePos + 5);

    if (window.location.search.indexOf('Quick') != -1) {
        QuickSearchFromAnotherPage('1', '10', '0', 'price ASC');
    }
    else if ($.cookie('iqspec') != null) {
        registerAdvancedSearchFromCookie('1', '10', '0', 'price ASC');
    }
    else {
        var pages = 1;
        var pageSize = 10;
        var rowCount = 0;
        var sortOrder = 'price ASC';
        var sLocation = '4'; //default initial search to group
        var paramList = '{' + '"location":"' + sLocation + '"' + ',';
        paramList += '"pageIndex":"' + pages + '"' + ',';
        paramList += '"pageSize":"' + pageSize + '"' + ',';
        paramList += '"rowCount":"' + rowCount + '"' + ',';
        paramList += '"searchType":"' + sSearchType + '"' + ',';
        paramList += '"sortOrder":"' + sortOrder + '"}';
        $("#AdvancedSearchLoader").css({ 'display': 'block' });
        $.ajax({
            type: "POST",
            url: "/helpers/siteHelper.aspx/InitialSearch",
            contentType: "application/json; charset=utf-8",
            data: paramList,
            dataType: "json",
            success: function(msg) {
                $("#AdvancedSearchLoader").css({ 'display': 'none' });
                var totalRows = msg.d.substring(msg.d.indexOf('[TotalRows:') + 11, msg.d.indexOf(']'));
                var car;

                if (totalRows == '1')
                    car = ' car';
                else
                    car = ' cars';

                if (totalRows == '<br />') totalRows = 0;

                $("#searchResults").html(msg.d);
                $("#totalRowCount").html('<span class=\'totalCarsFoundText\'>' + totalRows + car + ' found</span>');
                $("#totalRowCount").effect("pulsate");
            }
        })
    };
    
    setModelText();
    setLocationText();
    setTransmissionText();
    setFuelTypeText();

});

function QuickSearchFromAnotherPage(pages, pageSize, rowCount, sortOrder) {
    var searchTypePos = window.location.search.indexOf('Type');
    var sSearchType = 'New';
    if (searchTypePos != -1)
        sSearchType = window.location.search.substring(searchTypePos + 5);

    $.cookie('iqsrch', 'quick', { expires: 7 });
    $("#AdvancedSearchLoader").css({ 'display': 'block' });
    var paramList = '{' + '"model":"' + $.cookie('iqquickspec').substring($.cookie('iqquickspec').indexOf('mdl') + 3, $.cookie('iqquickspec').indexOf('tns')) + '"' + ',';
    paramList += '"transmission":"' + $.cookie('iqquickspec').substring($.cookie('iqquickspec').indexOf('tns') + 3, $.cookie('iqquickspec').indexOf('flt')) + '"' + ',';
    paramList += '"fuelType":"' + $.cookie('iqquickspec').substring($.cookie('iqquickspec').indexOf('flt') + 3, $.cookie('iqquickspec').indexOf('lcn')) + '"' + ',';
    paramList += '"location":"' + $.cookie('iqquickspec').substring($.cookie('iqquickspec').indexOf('lcn') + 3) + '"' + ',';
    paramList += '"pageIndex":"' + pages + '"' + ',';
    paramList += '"pageSize":"' + pageSize + '"' + ',';
    paramList += '"rowCount":"' + rowCount + '"' + ',';
    paramList += '"searchType":"' + sSearchType + '"' + ',';
    paramList += '"sortOrder":"' + sortOrder + '"}';
    
    $.ajax({
        type: "POST",
        url: "/helpers/siteHelper.aspx/QuickSearch",
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: function(msg) {
            $("#AdvancedSearchLoader").css({ 'display': 'none' });
            
            var totalRows = msg.d.substring(msg.d.indexOf('[TotalRows:') + 11, msg.d.indexOf(']'));
            var car;
            if (totalRows == '1')
                car = ' car';
            else
                car = ' cars';

            if (totalRows == '<br />') totalRows = 0;
            $("#searchResults").html(msg.d);
            $("#totalRowCount").html('<span class=\'totalCarsFoundText\'>' + totalRows + car + ' found</span>');
            $("#totalRowCount").effect("pulsate");
        }
    });
    
    setModelText();
    setLocationText();
    setTransmissionText();
    setFuelTypeText();
};


function InitialSearchPaging(pages, pageSize, rowCount, sortOrder) {
    var sSearchType = '';
    var sLocation = '4'; //default initial search to group
    if (window.location.search.indexOf('type') != -1)
        sSearchType = window.location.search.substring(6);
    else
        sSearchType = 'New';

    $("#AdvancedSearchLoader").css({ 'display': 'block' });
    var paramList = '{' + '"location":"' + sLocation + '"' + ',';
    paramList += '"pageIndex":"' + pages + '"' + ',';
    paramList += '"pageSize":"' + pageSize + '"' + ',';
    paramList += '"rowCount":"' + rowCount + '"' + ',';
    paramList += '"searchType":"' + sSearchType + '"' + ',';
    paramList += '"sortOrder":"' + sortOrder + '"}';
    $.ajax({
        type: "POST",
        url: "/helpers/siteHelper.aspx/InitialSearch",
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: function(msg) {
            $("#AdvancedSearchLoader").css({ 'display': 'none' });
            hideHtmlForNonSearchPage();
            var totalRows = msg.d.substring(msg.d.indexOf('[TotalRows:') + 11, msg.d.indexOf(']'));
            var car;
            if (totalRows == '1')
                car = ' car';
            else
                car = ' cars';

            if (totalRows == '<br />') totalRows = 0;
            $("#searchResults").html(msg.d);
            $("#totalRowCount").html('<span class=\'totalCarsFoundText\'>' + totalRows + car + ' found</span>');
            $("#totalRowCount").effect("pulsate");
        }
    })
}
