﻿$('document').ready(function () {

    $("#LoginLink").click(function () {
        doLogin(function (user) {
            if (user != null)
                window.location.reload();
        });
    });

    $("#RegisterLink").click(function () {
        doRegistration(function (user) {
            if (user != null)
                window.location.reload();
        });
    });

    $('#facebookPage').click(function() {
        s.linkTrackVars = 'eVar1,events';
        s.linkTrackEvents = s.events = 'event1';
        s.eVar1 = 'fb:follow';
        s.tl(this, 'o', 'Follow Facebook Click');
    });
    
    getZipCode(function (zipCode) {
        $("#Zipcode input[type=text]").val(zipCode);
    });

    $('#formAdvSearch').validate();
    //Makes change event handler
    $("#ddlMake").change(function () {
        var make = $("#Make option:selected").val();
        var url = "/JsonServices/Models/?make=" + make + "&year=" + null;
        var type = $("#ddlModel").closest("div").attr("text");

        $.ajax({
            url: url,
            success: function (jsonData) {
                PopulateDropDown($("#ddlModel"), type, jsonData);
            }
        });
    });

    function searchAdvanced() {
        var make = $("#ddlMake option:selected").val();
        var model = null;

        if ($("#ddlModel option:selected").val()) {
            model = $("#ddlModel option:selected").text();
        }

        var price = $('#price').val();
        var distance = $("#distance option:selected").val();
        var zipcode = $('#zipcode').val();

        var query = "#";

        if (make) {
            query += ' make:"' + make + '"';
        }

        if (model) {
            query += ' model:"' + model + '"';
        }

        if (price > 0) {
            query += " price:" + price;
        }

        if (zipcode) {
            query += " zipcode:" + zipcode;
            if (distance) {
                query += " distance:" + distance;
            }
        }

        var searchUrl = '/find-your-car' + query.replace('# ', '#');
        window.location.href = searchUrl;
    }

    $('#adv_submit input[type=button]').click(function () {
        var price = $('#price').val();
        var placeholder = $('#price').attr('placeholder');

        if (price === placeholder) {
            $('#price').val('');
        }
        if ($('#formAdvSearch').valid()) {
            searchAdvanced();
        }
        else {
            $('#price').val(placeholder);
        }
    });

    function init() {
        getFeaturedListings();
    }

    init();

});

var featuredTimout;
function setListings(listings) {
    $("#featured_results")
        .hide()
        .find("#listingsHolder")
        .empty()
        .append($("#featuredResultsTemplate").tmpl(listings))
        .closest("#featured_results")
        .fadeIn();
        featuredTimout = setInterval("getFeaturedListings()", 10000);
}

function getFeaturedListings() {
    clearTimeout(featuredTimout);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: "/listing/featuredlistings",
        success: function (listings) {
            var count = listings.length;

            if (count > 0) {
                setListings(listings);
            }
        }
    });
}
