﻿function redirectTo(url, bidAmount, auctionId) {
    if (url === "")
        return;

    if (bidAmount === "")
        bidAmount = "0";

    window.location = url + "?amount=" + bidAmount + "&auctionid=" + auctionId;
}

function ajaxBid(url, amount, callback, redirectUrl, timeoutMilliseconds) {
    disablePlacebidBox();
    var auctionId = $("input[id$='PageAuctionId']").val();

    if (typeof(timeoutMilliseconds) == "undefined")
        timeoutMilliseconds = 0;

    $.ajax({
        url: url,
        type: "POST",
        data: { auctionId: auctionId, amount: amount },
        success: function(response, status, xhr) {
            try {
                if (callback != null) {
                    callback(response);
                }

                lightBoxContent().html(response);
            }
            catch (error) {
                ajaxLog("Success Error", xhr);
                failSafeAction(function() { lightBoxContent().html($('#redirectContent').html()); });
                window.setTimeout(function() { trackAndRedirect("/virtual/goal/bid/script_error_success", redirectUrl, amount, auctionId); }, 3000);
            }
        },
        complete: function(xmlHttpRequest, textStatus) {
            if (textStatus === "success") {
                return;
            }

            if (textStatus === "timeout") {
                ajaxLog(textStatus, xmlHttpRequest);
                failSafeAction(function() { lightBoxContent().html($('#redirectContent').html()); });
                window.setTimeout(function() { trackAndRedirect("/virtual/goal/bid/timeout", redirectUrl, amount, auctionId); }, 3000);
            }
            else {
                ajaxLog(textStatus, xmlHttpRequest);
                failSafeAction(function() { lightBoxContent().html($('#redirectContent').html()); });
                window.setTimeout(function() { trackAndRedirect("/virtual/goal/bid/script_error", redirectUrl, amount, auctionId); }, 3000);
            }
        },
        timeout: timeoutMilliseconds
    });
}


function failSafeAction(action) {

    try {
            action();
        }
    catch (err) { }
    
}

function trackAndRedirect(trackingPage, redirectUrl, amount, auctionId)
{
    track(trackingPage);
    redirectTo(redirectUrl, amount, auctionId);
}

function ajaxLog(step, xmlHttpRequest) {
    try {
         var logMessage = "";

         if (!xmlHttpRequest && typeof XMLHttpRequest != 'undefined') 
         {
            logMessage = xmlHttpRequest.responseText + " : " + xmlHttpRequest.status;
         } 
                
        $.post("/LocalLogger.asmx/Log", { step: step, logMessage: logMessage });
    }
    catch (err) { }
}

function postToMvc(url, data) {
    $.post(url, data,
            function(response, status, xhr) {
                lightBoxContent().html(response);

                if (response.Url) {
                    document.location = response.Url;
                }
            });
}

function setupLightboxWithAction(buttonElementName, url, amountElementName, action, redirectUrl, timeoutMilliseconds) {
    $(buttonElementName).fancybox({
        'hideOnContentClick': false,
        'modal': true,
        'transitionIn': 'fade',
        'transitionOut': 'fade',
        'autoDimensions': true,
        'overlayOpacity': 0.62,
        'overlayColor': '#000000',
        'onStart': function() {
            lightBoxContent().html("");
            resizeBidBox(300, 140);
        },
        'onComplete': function() {
            action(url, $(amountElementName).val().trim(), null, redirectUrl, timeoutMilliseconds);
        }
    });
}

function showAjaxLoader() {
    var x = lightBoxContent().width() / 2 - 12;
    var y = lightBoxContent().height() / 2 - 12;

    lightBoxContent().empty();
    lightBoxContent().append($('#loadingContent').clone().css({ left: x, top: y, position: 'relative' })).css('overflow', 'auto');
}

function lightBoxContent() {
    return $('#lightBoxContent');
}

function setupAjaxEvents() {
    lightBoxContent().ajaxSend(function() {
        showAjaxLoader();
    });

    lightBoxContent().ajaxComplete(function() {
        addOverflowAuto();
    });
}

function OnError() {
    addOverflowAuto();

    track("/virtual/goal/bid/error");

    lightBoxContent().html($('#errorContent').html());
    resizeBidBox(300, 140);
}

function track(page) {
    try {
        var pageTracker = _gat._getTracker("UA-7094448-2");
        pageTracker._setDomainName(".tradera.com");
        pageTracker._trackPageview(page);
    }
    catch (err) { }
}
function addOverflowAuto() {
    $(this).css('overflow', 'auto');
}
