﻿var showTopOfferte = 0;
var showTopProduttori = 0;
var currentPosition = 0;

//home page top counter

function circleTopOfferte(diff) {
    currentPosition += diff;
    if (currentPosition < 0) currentPosition += topofferte.length;
    if (currentPosition >= topofferte.length) currentPosition -= topofferte.length;
    var num = currentPosition;
    for (var i = 0; i < showTopOfferte; i++) {
        try {
            if (topofferte[num]) {
                //alert('url(' + topofferte[num]['img'] + ')')
                gE('to_img' + i).style.backgroundImage = 'url(' + topofferte[num]['img'] + ')';
                gE('to_link' + i).href = topofferte[num]['link'];
                gE('to_link_' + i).href = topofferte[num]['link'];

                var modelElement = gE('to_link_' + i);
                var newModelElement = document.createTextNode(topofferte[num]['model']);
                if (!modelElement.firstChild) {
                    modelElement.appendChild(newModelElement);
                }
                else {
                    modelElement.replaceChild(newModelElement, modelElement.firstChild);
                }

                var cittaElement = gE('to_citta' + i);
                var newCittaElement = document.createTextNode(topofferte[num]['citta']);
                if (!cittaElement.firstChild) {
                    cittaElement.appendChild(newCittaElement);
                }
                else {
                    cittaElement.replaceChild(newCittaElement, cittaElement.firstChild);
                }

                /*var priceElement = gE('to_price' + i);
                var newPriceElement = document.createTextNode(topofferte[num]['price']);
                if (!priceElement.firstChild) {
                    priceElement.appendChild(newPriceElement);
                }
                else {
                    priceElement.replaceChild(newPriceElement, priceElement.firstChild);
                }*/
                num++;
            }
        } catch (e) { alert(e); }
        if (num >= topofferte.length) num = 0;
    }
}

function initTopOfferte(_showTopOfferte) {
    if (gE('to_img0') == null) {
        window.setTimeout('initTopOfferte(' + _showTopOfferte + ');', 600);
    }
    else {
        showTopOfferte = _showTopOfferte;
        circleTopOfferte(0);
    }
}

// list page top counter (flat)

function circleTopOfferte_tcflat(diff) {
    currentPosition += diff;
    if (currentPosition < 0) currentPosition += topofferte.length;
    if (currentPosition >= topofferte.length) currentPosition -= topofferte.length;
    var num = currentPosition;
    for (var i = 0; i < showTopOfferte; i++) {
        try {
            if (topofferte[num]) {
                gE('to_img' + i).src = topofferte[num]['img'];
                gE('to_link' + i).href = topofferte[num]['link'];
                gE('to_link_make_' + i).href = topofferte[num]['link'];

                if (gE('to_link_model_' + i) != null) {
                    gE('to_link_model_' + i).href = topofferte[num]['link'];
                }

                var makeElement = gE('to_link_make_' + i);
                var newMakeElement = document.createTextNode(truncateString(topofferte[num]['make']));
                if (!makeElement.firstChild) {
                    makeElement.appendChild(newMakeElement);
                }
                else {
                    makeElement.replaceChild(newMakeElement, makeElement.firstChild);
                }

                if (gE('to_link_model_' + i) != null) {
                    var modelElement = gE('to_link_model_' + i);
                    var newModelElement = document.createTextNode(truncateString(topofferte[num]['model']));
                    if (!modelElement.firstChild) {
                        modelElement.appendChild(newModelElement);
                    }
                    else {
                        modelElement.replaceChild(newModelElement, modelElement.firstChild);
                    }
                }

                else if (gE('to_model' + i) != null) {
                    var modelElement = gE('to_model' + i);
                    var newModelElement = document.createTextNode(truncateString(topofferte[num]['model']));
                    if (!modelElement.firstChild) {
                        modelElement.appendChild(newModelElement);
                    }
                    else {
                        modelElement.replaceChild(newModelElement, modelElement.firstChild);
                    }
                }

                var cittaElement = gE('to_citta' + i);
                var newCittaElement = document.createTextNode(truncateString(topofferte[num]['citta']));
                if (!cittaElement.firstChild) {
                    cittaElement.appendChild(newCittaElement);
                }
                else {
                    cittaElement.replaceChild(newCittaElement, cittaElement.firstChild);
                }

                /*var priceElement = gE('to_price' + i);
                var newPriceElement = document.createTextNode(truncateString(topofferte[num]['price']));
                if (!priceElement.firstChild) {
                    priceElement.appendChild(newPriceElement);
                }
                else {
                    priceElement.replaceChild(newPriceElement, priceElement.firstChild);
                }*/

                var powerElement = gE('to_power' + i);
                var newPowerElement;
                if (topofferte[num]['power']) {
                    newPowerElement = document.createTextNode(truncateString(topofferte[num]['power'] + _power));
                }
                else {
                    newPowerElement = document.createTextNode('');
                }
                if (!powerElement.firstChild) {
                    powerElement.appendChild(newPowerElement);
                }
                else {
                    powerElement.replaceChild(newPowerElement, powerElement.firstChild);
                }

                var fuelElement = gE('to_fuel' + i);
                var newFuelElement;
                if (topofferte[num]['fuel']) {
                    newFuelElement = document.createTextNode(truncateString(topofferte[num]['fuel']));
                }
                else {
                    newFuelElement = document.createTextNode('');
                }
                if (!fuelElement.firstChild) {
                    fuelElement.appendChild(newFuelElement);
                }
                else {
                    fuelElement.replaceChild(newFuelElement, fuelElement.firstChild);
                }

                num++;
            }
        } catch (e) { }
        if (num >= topofferte.length) num = 0;
    }
}

function initTopOfferte_tcflat(_showTopOfferte) {
    if (gE('to_img0') == null) {
        window.setTimeout('initTopOfferte_tcflat(' + _showTopOfferte + ');', 300);
    }
    else {
        showTopOfferte = _showTopOfferte;
        circleTopOfferte_tcflat(0);
    }
}

function truncateString(attributeValue) {
    var maxLength = 15;
    var returnValue;
    if (attributeValue.length >= maxLength) {
        returnValue = attributeValue.substring(0, maxLength - 3) + '...';
    }
    else {
        returnValue = attributeValue;

    }
    return returnValue;
}


//top produttori
//*************************************

function circleTopProduttori(diff) {
    currentPosition += diff;
    if (currentPosition < 0) currentPosition += topProduttori.length;
    if (currentPosition >= topProduttori.length) currentPosition -= topProduttori.length;
    var num = currentPosition;
    for (var i = 0; i < showTopProduttori; i++) {
        try {
            if (topProduttori[num]) {
                //alert('url(' + topProduttori[num]['img'] + ')')
                gE('tp_img' + i).style.backgroundImage = 'url(' + topProduttori[num]['img'] + ')';
                gE('tp_link' + i).href = topProduttori[num]['link'];
                num++;
            }
        } catch (e) { alert(e); }
        if (num >= topProduttori.length) num = 0;
    }
}

function initTopProduttori(_showTopProduttori) {
    if (gE('tp_img0') == null) {
        window.setTimeout('initTopProduttori(' + _showTopProduttori + ');', 100);
    }
    else {
        showTopProduttori = _showTopProduttori;
        circleTopProduttori(0);
    }
}

// list page top counter (flat)

function circleTopProduttori_tcflat(diff) {
    currentPosition += diff;
    if (currentPosition < 0) currentPosition += topProduttori.length;
    if (currentPosition >= topProduttori.length) currentPosition -= topProduttori.length;
    var num = currentPosition;
    for (var i = 0; i < showTopProduttori; i++) {
        try {
            if (topProduttori[num]) {
                gE('tp_img' + i).src = topProduttori[num]['img'];
                gE('tp_link' + i).href = topProduttori[num]['link'];
                

                num++;
            }
        } catch (e) { }
        if (num >= topProduttori.length) num = 0;
    }
}

function initTopProduttori_tcflat(_showTopProduttori) {
    if (gE('tp_img0') == null) {
        window.setTimeout('initTopProduttori_tcflat(' + _showTopProduttori + ');', 100);
    }
    else {
        showTopProduttori = _showTopProduttori;
        circleTopProduttori_tcflat(0);
    }
}


