﻿

//----------------------------
//General : CarAdLink Details
//----------------------------
function CarAdLink(carAdId, linkText, linkTitle) {

    this.id = carAdId;
    //    this.linkTitle = linkTitle.removeDoubleQuotes();
    //    this.linkText = linkText.removeDoubleQuotes();
    this.linkTitle = linkTitle;
    this.linkText = linkText;

}
//----------------------------
//History : CarAd List
//----------------------------

var HistoryCarAdClass = new Object();
//CarAds
HistoryCarAdClass.CarAdLinks = function() {
    var theList = $.cookieJar('h').get('d');
    // check the that theList is an Array object
    if (typeof theList != 'object' || theList.constructor != Array) {
        // theList wasn't an Array object, set it as an Arrray object
        theList = [];
    }
    return theList;

}
//Add CarAd
HistoryCarAdClass.insertCarAd = function(hId, lnkText, lnkTitle) {
    //check if we have it, if not then add it
    if (!this.exists(hId)) {

        var updatedList = this.CarAdLinks();

        //max 5 history
        if (updatedList.length == 5) {
            updatedList.pop(); //remove last item
        }

        updatedList.unshift(new CarAdLink(hId, lnkText, lnkTitle));

        this.save(updatedList);
    }
}
//Create HTML
HistoryCarAdClass.createHTML = function() {

    var lst = $("#historyList");
    if (this.CarAdLinks().length > 0) {

        var rootUrl = getUrl_Root();
        lst.empty();

        $.each(this.CarAdLinks(), function(i, o) {


            lst.append('<li class="ui-helper-clearfix ui-state-active ui-corner-all"><div class="b20"><a class="historyRemove" href="#" rel="' + o.id + '"><span class="ui-icon ui-icon-close icon"></span></a></div><div class="b150"><a class="historyLink" title="' + o.linkTitle + '" href="' + rootUrl + 'ad/' + o.id + '">' + o.linkText + '</a></div></li>');

        });

        lst.parent().parent().show();


        //Remove by Index
        lst.find('.historyRemove').each(function() {
            $(this).click(function() {

                HistoryCarAdClass.remove($(this).attr('rel'))

                return false;
            });
        });
        //track clicksw
        lst.find('.historyLink').each(function() {
            $(this).click(function() {

                pageTracker._trackPageview("/ad/clickHistory")

                return true;
            });
        });

    }
    else {
        lst.parent().parent().hide();

    }

}
//Remove CarAd by CarAdId
HistoryCarAdClass.remove = function(hId) {
    //check if we have it, if so then remove it
    if (this.exists(hId)) {

        var updatedList = this.CarAdLinks();
        $.each(this.CarAdLinks(), function(i, h) {

            if (h.id == hId) {

                updatedList.splice(i, 1);

                return false;
            }
        });

        this.save(updatedList);

    }
}

//CarAd Exists
HistoryCarAdClass.exists = function(hId) {

    var isExists = false;
    $.each(this.CarAdLinks(), function() {

        if (this.id == hId) {
            isExists = true;
            return false;

        }
    });

    return isExists;

}
//Save
HistoryCarAdClass.save = function(updatedList) {

    $.cookieJar('h').set('d', updatedList);
    //reprocess html
    HistoryCarAdClass.createHTML();

}