﻿function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; } 
    }
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

//function goToByScroll(id){  
//    $('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');  
//} 

/* Engine */
var LabelCounterIDs = new Array();
var LabelAuctionPriceIDs = new Array();
var LabelBidsIDs = new Array();
var LabelCurrentProductPriceIDs = new Array();
var LabelUserNameIDs = new Array();
var LabelBidTypeIDs = new Array();
var _labelLastBidsCountIDs = new Array();
var _buttonBidIDs = new Array();
var _buttonBidGrayIDs = new Array();
var _buttonSoldIDs = new Array();
var _auctionStartEndedIDs = new Array();
var _auctionPriceTimeIDs = new Array();
var _auctionPriceCounterIDs = new Array();
var DisplayType = new Array();
var LabelAuctionPriceDetailID;
var _labelUserNameID;
var _labelPlacedDetailID;
var _labelFreeBidsDetailID;
var _labelPlacedAmountDetailID;
var _labelFreeBidsAmountDetailID;
var _labelSavingAmountDetailID;
var _labelRebateAmountDetailID;
var _labelPurchaseAmountDetailID;
var _labelCustomerBidsID;
var _labelBidRobotCurrentBidsID;
var updateTimeIndex;
var updateTimeDetail;
var timerIndex;
var timerDetail;
var _currency;
var facteurToEur;
var IdAuction;
var IdsAuction;
var _customerGuid;
var AuctionIndexSign;
var AuctionDetailSign;
var typeBidNormalLocalized;
var typeBidRobotLocalized;
var _lastBidsCountLocalized;
var AuctionStatusPendingLocalized;
var AuctionStatusPausedLocalized;
var AuctionStatusCompletedLocalized;
var _auctionTimerDayLocalized;

var UpdateAuctionIndexData = function() {
    var startDate = new Date;
    FrontOffice.WebService.Counter.UpdateAuctionIndexData(IdsAuction, _customerGuid, AuctionIndexSign, startDate, CallBackIndex, CallBackIndexErreur);
}

var UpdateAuctionDetailData = function() {
    var startDate = new Date;
    FrontOffice.WebService.Counter.UpdateAuctionDetailData(IdAuction, _customerGuid, AuctionDetailSign, startDate, CallBackDetail, CallBackDetailErreur);
}

var AddAuctionIndex = function(idsAuction, guid, sign) {
    IdsAuction = idsAuction;
    _customerGuid = guid
    AuctionIndexSign = sign;

    updateTimeIndex = 1000;

    timerIndex = setTimeout("UpdateAuctionIndexData()", updateTimeIndex);
}

var AddAuctionDetail = function(idAuction, guid, sign) {
    IdAuction = idAuction;
    _customerGuid = guid;
    AuctionDetailSign = sign;

    updateTimeDetail = 1000;

    timerDetail = setTimeout("UpdateAuctionDetailData()", updateTimeDetail);
}

var CallBackIndex = function(result) {
    var splitResult = result.split("#");
    if (splitResult.length > 0) {
        var overTimer = 0;
        if (_customerGuid != undefined || _customerGuid != null || _customerGuid != '') {
            var splitData = splitResult[0].split("|");
            overTimer = splitData[0];

            if (splitData.length == 2) {
                UpdateText(_labelCustomerBidsID, splitData[1]);
            }
        }
        else
            overTimer = splitResult[0];

        if (overTimer > 500) {
            updateTimeIndex = 1000 - 500;
        }
        else {
            updateTimeIndex = 1000 - overTimer;
        }
        updateTimeIndex = 800;
    }

    if (splitResult.length > 1) {
        for (var i = 1; i < splitResult.length; i++) {
            if (splitResult[i] != undefined && splitResult[i] != null && splitResult[i] != '') {
                UpdateAuction(splitResult[i]);
            }
        }
    }

    if (timerIndex != undefined && timerIndex != null && timerIndex != '')
        clearTimeout(timerIndex);

    timerIndex = setTimeout("UpdateAuctionIndexData()", updateTimeIndex);
}

var CallBackIndexErreur = function(error) {
}

var CallBackDetail = function(result) {
    var splitResult = result.split("#");
    if (splitResult.length > 0) {
        var overTimer = 0;
        if (_customerGuid != undefined && _customerGuid != null && _customerGuid != '') {
            var splitData = splitResult[0].split("|");

            overTimer = splitData[0];

            if (splitData.length == 10) {
                UpdateText(_labelCustomerBidsID, splitData[1]);
                UpdateText(_labelPlacedDetailID, splitData[2]);
                UpdateText(_labelFreeBidsDetailID, splitData[3]);
                UpdateText(_labelPlacedAmountDetailID, splitData[4]);
                UpdateText(_labelFreeBidsAmountDetailID, splitData[5]);
                UpdateText(_labelSavingAmountDetailID, splitData[6]);
                UpdateText(_labelRebateAmountDetailID, splitData[7]);
                UpdateText(_labelPurchaseAmountDetailID, splitData[8]);
                UpdateText(_labelBidRobotCurrentBidsID, splitData[9]);                
            }
        }
        else
            overTimer = splitResult[0];

        if (overTimer > 500) {
            updateTimeDetail = 1000 - 500;
        }
        else {
            updateTimeDetail = 1000 - overTimer;
        }
        updateTimeIndex = 800;
    }

    if (splitResult.length > 1) {
        if (splitResult[1] != undefined && splitResult[1] != null && splitResult[1] != '') {
            UpdateAuction(splitResult[1]);
        }
    }

    if (timerDetail != undefined && timerDetail != null && timerDetail != '')
        clearTimeout(timerDetail);

    timerDetail = setTimeout("UpdateAuctionDetailData()", updateTimeDetail);
}

var CallBackDetailErreur = function(error) {
}

var UpdateAuction = function(data) {
    var splitData = data.split("|");
    var idAuction = splitData[0];
    var counter = splitData[1];
    var auctionPrice = splitData[2];

    if (splitData[5] == 2) {
        UpdateText(LabelCounterIDs[idAuction], GetDisplayTime(counter));
    }

    UpdateText(LabelAuctionPriceIDs[idAuction], _currency + ' ' + auctionPrice);
    if (DisplayType[idAuction] == "3")
        UpdateText(LabelAuctionPriceDetailID, _currency + ' ' + auctionPrice);

    var bidsInner = "";
    var productPriceBidsInner = "";
    var userNameBidsInner = "";
    var typeBidsInner = "";

    splitBids = splitData[3].split(";");
    if (splitBids.length > 0) {
        switch (DisplayType[idAuction]) {
            case "1":
                {
                    for (var i = 0; i < splitBids.length; i++) {
                        var splitCustomerAuctionBid = splitBids[i].split("/");
                        if (splitCustomerAuctionBid[1] != undefined) {
                            bidsInner += (i + 1) + " - " + String.Limit(splitCustomerAuctionBid[1], 14);
                            bidsInner += "<br/>";
                        }
                    }
                    UpdateText(LabelBidsIDs[idAuction], bidsInner);
                    break;
                }
            case "2":
                {
                    var splitCustomerAuctionBid = splitBids[0].split("/");
                    if (splitCustomerAuctionBid[1] != undefined) {
                        bidsInner = String.Limit(splitCustomerAuctionBid[1], 12);
                        bidsInner += "<br/>";
                    }
                    UpdateText(LabelBidsIDs[idAuction], bidsInner);
                    break;
                }
            case "3":
                {
                    var splitCustomerAuctionBid = splitBids[0].split("/");
                    if (splitCustomerAuctionBid[1] != undefined) {
                        bidsInner = String.Limit(splitCustomerAuctionBid[1], 12);
                        bidsInner += "<br/>";
                    }
                    UpdateText(_labelUserNameID, bidsInner);

                    bidsInner = "";

                    for (var i = 0; i < splitBids.length; i++) {
                        var splitCustomerAuctionBid = splitBids[i].split("/");
                        if (splitCustomerAuctionBid[0] != undefined && splitCustomerAuctionBid[0] != '') {
                            if (i == 0) productPriceBidsInner += "<b>";
                            productPriceBidsInner += _currency + ' ' + splitCustomerAuctionBid[0] + "<br/>";
                            if (i == 0) productPriceBidsInner += "</b>";
                        }
                        if (splitCustomerAuctionBid[1] != undefined && splitCustomerAuctionBid[1] != '') {
                            if (i == 0) userNameBidsInner += "<b>";
                            userNameBidsInner += String.Limit(splitCustomerAuctionBid[1], 9) + "<br/>";
                            if (i == 0) userNameBidsInner += "</b>";
                        }
                        if (splitCustomerAuctionBid[2] != undefined && splitCustomerAuctionBid[2] != '') {
                            if (i == 0) typeBidsInner += "<b>";
                            switch (splitCustomerAuctionBid[2]) {
                                case "1":
                                    typeBidsInner += typeBidNormalLocalized;
                                    break;
                                case "2":
                                    typeBidsInner += typeBidRobotLocalized;
                                    break;
                            }
                            if (i == 0) typeBidsInner += "</b>";
                            typeBidsInner += "<br/>";
                        }
                    }
                    UpdateText(LabelCurrentProductPriceIDs[idAuction], productPriceBidsInner);
                    UpdateText(LabelUserNameIDs[idAuction], userNameBidsInner);
                    UpdateText(LabelBidTypeIDs[idAuction], typeBidsInner);
                    UpdateText(_labelLastBidsCountIDs[idAuction], String.Format(_lastBidsCountLocalized, splitData[6]));
                    break;
                }
            case "4":
                {
                    break;
                }
        }
    }

    if (splitData[4] == 1) {
        new Effect.Highlight(LabelCounterIDs[idAuction], { duration: 0.6, startcolor: '#ff0000', endcolor: '#ffffff', restorecolor: '' });
    }

    // pending
    if (splitData[5] == 1) {
        $get(_buttonBidIDs[idAuction]).style.display = 'none';
        $get(_buttonBidGrayIDs[idAuction]).style.display = '';
        $get(_buttonSoldIDs[idAuction]).style.display = 'none';
        $get(_auctionStartEndedIDs[idAuction]).style.visibility = 'visible';
        UpdateText(LabelCounterIDs[idAuction], AuctionStatusPendingLocalized);
    }

    // started
    if (splitData[5] == 2) {
        $get(_buttonBidIDs[idAuction]).style.display = '';
        $get(_buttonBidGrayIDs[idAuction]).style.display = 'none';
        $get(_buttonSoldIDs[idAuction]).style.display = 'none';
        if (_auctionStartEndedIDs[idAuction] != undefined && _auctionStartEndedIDs[idAuction] != null && _auctionStartEndedIDs[idAuction] != '')
            $get(_auctionStartEndedIDs[idAuction]).style.visibility = 'hidden';
        if (_auctionPriceTimeIDs[idAuction] != undefined && _auctionPriceTimeIDs[idAuction] != null && _auctionPriceTimeIDs[idAuction] != '')
            $get(_auctionPriceTimeIDs[idAuction]).style.display = '';
        if (_auctionPriceCounterIDs[idAuction] != undefined && _auctionPriceCounterIDs[idAuction] != null && _auctionPriceCounterIDs[idAuction] != '')
            $get(_auctionPriceCounterIDs[idAuction]).style.display = 'none';    
    }

    // paused
    if (splitData[5] == 3) {
        $get(_buttonBidIDs[idAuction]).style.display = 'none';
        $get(_buttonBidGrayIDs[idAuction]).style.display = '';
        $get(_buttonSoldIDs[idAuction]).style.display = 'none';
        if (_auctionStartEndedIDs[idAuction] != undefined && _auctionStartEndedIDs[idAuction] != null && _auctionStartEndedIDs[idAuction] != '')
            $get(_auctionStartEndedIDs[idAuction]).style.visibility = 'hidden';
        UpdateText(LabelCounterIDs[idAuction], AuctionStatusPausedLocalized);
    }

    // completed
    if (splitData[5] == 4) {
        $get(_buttonBidIDs[idAuction]).style.display = 'none';
        $get(_buttonBidGrayIDs[idAuction]).style.display = 'none';
        $get(_buttonSoldIDs[idAuction]).style.display = '';
        if (_auctionStartEndedIDs[idAuction] != undefined && _auctionStartEndedIDs[idAuction] != null && _auctionStartEndedIDs[idAuction] != '')
            $get(_auctionStartEndedIDs[idAuction]).style.visibility = 'visible';
        UpdateText(LabelCounterIDs[idAuction], AuctionStatusCompletedLocalized);
        UpdateText(_auctionStartEndedIDs[idAuction], splitData[8]);
    }
}

var GetDisplayTime = function(counter) {
    var days = Math.floor(counter / 86400);
    var hours = 0;
    if (counter >= 86400) {
        hours = Math.floor((counter % 86400) / 3600);
    }
    else {
        hours = Math.floor(counter / 3600);
    }
    var minutes = Math.floor(((counter % 86400) % 3600) / 60);
    var secs = ((counter % 86400) % 3600) % 60;

    if (hours < 10) {
        hours = "0" + hours;
    }
    if (minutes < 10) {
        minutes = "0" + minutes;
    }
    if (secs < 10) {
        secs = "0" + secs;
    }

    if (counter >= 86400) {
        return days + _auctionTimerDayLocalized + " " + hours + ":" + minutes + ":" + secs;
    }
    else {
        return hours + ":" + minutes + ":" + secs;
    }
}

var UpdateText = function(id, value) {
    if (id != undefined && id != null && id != '') {
        var obj = $get(id);
        if (obj != undefined && obj != null) {
            $get(id).innerHTML = value;
        }
    }
}

String.Limit = function(value, limitCount) {
    value = value.toLowerCase();
    if (value.length <= limitCount) return value;
    return value.substring(0, limitCount) + "...";
}

String.Format = function(text) {

    //check if there are two arguments in the arguments list
    if (arguments.length <= 1) {
        //if there are not 2 or more arguments there's nothing to replace
        //just return the original text
        return text;
    }

    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;

    for (var token = 0; token <= tokenCount; token++) {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"),
        arguments[token + 1]);
    }
    
    return text;
}
