﻿var auctionID = -1;
var bidID = -1;
var bid_counter = 1;
var sku = '';
var buy_enabled = false;
var counter = 0;
var now_at = -1;
var images = new Array();
var auction_timer = -1;


function process_bids(bids, id) {
    var c = bids.length;
    for (var i = 0; i < c; i++) {
        $('#winning_bidders table').prepend('<tr><td>' + bid_counter + '. </td><td>' + bids[i].d + '</td></tr>');
        bid_counter++;
    }
    if (bidID == -1 && bidID != id) {
        $('#winning_bidders_data').slideDown('slow');
    }
    bidID = id;
}

function process_auction_data_full(response) {
    auctionID = parseInt(response.aID,10);
    if (sku != response.sku) {
        bidID = -1;
        bid_counter = 1;
        sku = response.sku;
        $('#longdesc').html(response.d);
        $('#rrp').html('RRP: £' + response.r);
        $('#desc_content').html(response.d);
        $('#design_details_content').html(response.dde);
        $('#gemstoneinfo').html(response.gi);
        $('#desc_content').html(response.w);
        $('#largeimage').html('<img src="' + response.i + '400">');
        $('#smallimages').html('');
        if (response.i1 !== null) {
            $('#imagepreload').html('<img src="' + response.i1 + '">');
            $('#smallimages').html('<img id="small_1" src="' + response.i1 + '65" onMouseOver="swap(\'' + response.i1 + '400\');" onMouseOut="swap(\'' + response.i + '400\');">');
        }
        if (response.i2 !== null) {
            $('#imagepreload').append('<img src="' + response.i2 + '">');
            $('#smallimages').append('<img id="small_2" src="' + response.i2 + '65" onMouseOver="swap(\'' + response.i2 + '400\');" onMouseOut="swap(\'' + response.i + '400\');">');
        }
        if (response.i3 !== null) {
            $('#imagepreload').append('<img src="' + response.i3 + '">');
            $('#smallimages').append('<img id="small_3" src="' + response.i3 + '65" onMouseOver="swap(\'' + response.i3 + '400\');" onMouseOut="swap(\'' + response.i + '400\');">');
        }
        if (response.i4 !== null) {
            $('#imagepreload').append('<img src="' + response.i4 + '">');
            $('#smallimages').append('<img id="small_4" src="' + response.i4 + '65" onMouseOver="swap(\'' + response.i4 + '400\');" onMouseOut="swap(\'' + response.i + '400\');">');
        }
        $('#winning_bidders table tr').remove();
    }
    process_auction_data(response);
}
function close_auction() {
    window.location.replace("auctionclosed.aspx");
}
function full_details_refresh() {
    $.ajax({
        url: "AJAX/AuctionDetails.asmx/allauctionData",
        type: "POST",
        dataType: "json",
        data: "{bidid:" + bidID + "}",
        contentType: "application/json; charset=utf-8",
        success: function(msg) {
            process_auction_data_full(msg.d);
        }
    });
}
function process_auction_data(response) {
    if (response.ae == "0") {
        close_auction();
        return;
    }
    if (auctionID < parseInt(response.aID,10)) {
        full_details_refresh();
    }
    else {
        if (response.hb == 1) {
            $('#buy_button').html('<img src="Images/secured.png" alt="You have purchased this item" />');
            buy_enabled = false;
        }
        else if ((response.be == "1" && !buy_enabled) || response.bID != bidID) {
            var html = "<table id=\"buttoncontainer\"><tr>";
            var skus = response.p.length;
            if (skus > 0) {
                for (var i = 0; i < skus; i++) {
                    html += "<td class=\"buynow\" onclick=\"place_bid('" + response.p[i].pid + "'); return false;\">" + response.p[i].d + "</td>";
                }
                html += "</tr></table>";
            }
            else {
                html = '<a href="#" onclick="place_bid(' + response.id + '); return false;"><img src="Images/buy.png" alt="Click to add to your basket"/></a>';
            }
            $('#buy_button').html(html);
            buy_enabled = true;
        }
        else if (response.q === 0 || (response.be == "0" && buy_enabled)) {
            $('#buy_button').html('<img src="Images/closed.png" alt="Auction Closed" />');
            buy_enabled = false;
        }
        now_at = response.na;
        $('#nowat').html('Now At: £' + now_at);
        $('#quantity_value').html(response.q);
        process_bids(response.b, response.bID);
    }
}

function bid_result(response) {
    if (response.result === 0) {
        window.location = 'login.aspx';
        //  $("#login").dialog("open");
    }
    else if (response.result == 2) {
        alert(response.error);
        location.reload(true);
    }
    else if (response.result == 1) {
        buy_enabled = false;
        $("#paid_price").html("£" + response.m);
        //  $("#bid_success").dialog("open");
    }
}
function place_bid(pid) {
    $('#buy_button').html('<img src="Images/wait.png" alt="Processing...">');
    $.ajax({
        url: "AJAX/AuctionDetails.asmx/bid",
        type: "POST",
        dataType: "json",
        data: "{'auctionID': " + auctionID + ", 'price': " + now_at + ",'pid':"+pid+"}",
        contentType: "application/json; charset=utf-8",
        success: function(msg) {
            bid_result(msg.d);
        }
    });
}

function swap(image) {
    $('#largeimage').html("<img src=\"" + image + "\">");
}
function auction_ajax() {
    counter++;
    $.ajax({
        url: "AJAX/AuctionDetails.asmx/auctionData",
        type: "POST",
        dataType: "json",
        data: "{bidid:" + bidID + "}",
        contentType: "application/json; charset=utf-8",
        success: function(msg) {
            process_auction_data(msg.d);
        }
    });
}
$(document).ready(function() {
    auction_ajax();
    setTimeout(function() {
        auction_timer = setInterval(auction_ajax, 5000);
    }, 1500);
    $("#bid_success").dialog({
        bgiframe: true,
        height: 140,
        modal: true,
        draggable: false,
        autoOpen: false,
        resizable: false
    });
    $('#design_details_header, #desc_header').click(function() {
        $(this).next('div').slideToggle('normal');
    });
});