var cstHistoryR2_Product_DeptId = 0;
var cstHistoryR2_ProductId = 1;
var cstHistoryR2_ProductName = 2;
var cstHistoryR2_ProductUrl = 3;
var cstHistoryR2_ImgUrl = 4;
var cstHistoryR2_Price = 5;
var cstHistoryR2_OldPrice = 6;

var cstHistoryR2_DeptId = 0;
var cstHistoryR2_DeptName = 1;
var cstHistoryR2_DeptUrl = 2;

function LoadHistory(cookieName) {
    var sHistoryElementList = '';
    var sHistoryProductList = '';
    var sHistoryDeptList = '';

    var cookieContent = $.cookie(cookieName);

    var isFilled = false;

    if (cookieContent != null && cookieContent != '' && cookieContent.split(cookieSeparator).length > 1) {
        cookieContent = urldecode(cookieContent.split(cookieSeparator)[1]);

        sHistoryElementList = cookieContent.split(listHistorySeparator);

        if (sHistoryElementList[0] != null) {
            sHistoryProductList = sHistoryElementList[0].split(elementHistorySeparator);

            if (sHistoryProductList != '') {
                isFilled = true;
                for (var i = 1; i < MaxProducts + 1; i++)
                    FillProduct(i, sHistoryProductList[i - 1]);
            }
        }

        if (sHistoryElementList.length > 1) {
            if (sHistoryElementList[1] != null) {
                isFilled = true;

                sHistoryDeptList = sHistoryElementList[1].split(elementHistorySeparator);

                for (var i = 0; i < MaxDept; i++)
                    FillDept(i + 1, sHistoryDeptList[i]);
            }
        }
    }

    if (!isFilled)
        FillEmptyDept();

    if (document.getElementById('historyProduct1').parentNode.style.display != 'none') {
        document.getElementById('HistoryProductList').style.display = 'block';
        document.getElementById('HistoryBloc').style.display = 'block';
    }

    if (document.getElementById('historyDept1').parentNode.style.display != 'none') {
        document.getElementById('HistoryDeptList').style.display = 'block';
        document.getElementById('HistoryBloc').style.display = 'block';
    }
}

function FillDept(position, historyDept) {
    if (trim(historyDept) == '')
        return;

    var sAttributs = historyDept.split(attributHistorySeparator);

    document.getElementById('historyDept' + position).innerHTML = sAttributs[cstHistoryR2_DeptName];
    if (sAttributs.length > cstHistoryR2_DeptUrl) document.getElementById('historyDept' + position).href = sAttributs[cstHistoryR2_DeptUrl];

    if (document.getElementById('historyDept' + position).innerHTML != '') {
        document.getElementById('historyDept' + position).parentNode.style.display = 'block';
    }
}

function FillEmptyDept() {
    var dept1 = document.getElementById('historyDept1');
    dept1.innerHTML = "Pas d'historique";
    dept1.parentNode.style.display = 'block';
    dept1.parentNode.className = 'nohistory';
}

function FillProduct(position, historyProduct) {
    if (trim(historyProduct) == '')
        return;

    var sAttributs = historyProduct.split(attributHistorySeparator);

    if (sAttributs.length > cstHistoryR2_OldPrice) {
        document.getElementById('historyProduct' + position).title = sAttributs[cstHistoryR2_ProductName];
        if (sAttributs.length > cstHistoryR2_ProductUrl) document.getElementById('historyProduct' + position).href = sAttributs[cstHistoryR2_ProductUrl];
        if (sAttributs.length > cstHistoryR2_ImgUrl) document.getElementById('historyProductImg' + position).src = ProductImageUrlRoot + sAttributs[cstHistoryR2_ImgUrl];

        document.getElementById('historyDisplay' + position).children[0].title = sAttributs[cstHistoryR2_ProductName];
        document.getElementById('historyDisplay' + position).children[0].children[0].innerHTML = sAttributs[cstHistoryR2_ProductName];
        if (sAttributs.length > cstHistoryR2_ProductUrl) document.getElementById('historyDisplay' + position).children[0].href = sAttributs[cstHistoryR2_ProductUrl];

        document.getElementById('historyDisplay' + position).children[0].children[1].children[0].innerHTML = "";

        if (parseFloat(sAttributs[cstHistoryR2_OldPrice]) != 0) {
            document.getElementById('historyDisplay' + position).children[0].children[1].children[0].innerHTML = sAttributs[cstHistoryR2_OldPrice];
        }

        if (sAttributs[cstHistoryR2_Price] != '') {
            sAttributs[cstHistoryR2_Price] = sAttributs[cstHistoryR2_Price].replace(',', '.');

            if (sAttributs[cstHistoryR2_Price].indexOf('.') >= 0)
                sAttributs[cstHistoryR2_Price] = sAttributs[cstHistoryR2_Price].replace('.', '<span>&euro;') + '</span>';

            document.getElementById('historyDisplay' + position).children[0].children[1].children[1].innerHTML = sAttributs[cstHistoryR2_Price];
        }
        else
            document.getElementById('historyDisplay' + position).children[0].children[1].children[1].innerHTML = '<!-- --><span><!-- --></span>'
    }

    if (document.getElementById('historyProduct' + position).title != '') {
        document.getElementById('historyProduct' + position).parentNode.style.display = 'block';
    }
}

function urldecode(str) {
    if (str == null)
        return '';

    return decodeURIComponent(str.replace(/\+/g, '%20'));
}

function trim(str) {
    if (str == null)
        return '';

    return str.replace(/^\t+/g, '').replace(/\t+$/g, '').replace(/^\s+/g, '').replace(/\s+$/g, '');
}

/****** ! DOCUMENT READY EVENT ******/

jQuery(document).ready(function() {
    LoadHistory(historyCookieName);
});

