function hideRequestNotes() {
    $('#requestAuctionInitiate').hide();
    $('#requestAuctionPending').hide();
    $('#requestAuctionFinished').hide();
    $('#requestAuctionAttemptLimit').hide();
}

function requestAuction(id) { 
    $.getJSON('http://www.omins.co.nz/pornbrokers/api/request/'+id+'/', null, function(data) {
        hideRequestNotes();
        if(data.status == 'failed') {
	    $('#requestAuctionAttemptLimit').text(data.error).show();
        } else {
	    $('#requestAuctionPending').show();
	    updateSecondCounter(id, 60);
	}
    });
}

function checkStatus(id) {
    $.getJSON('http://www.omins.co.nz/pornbrokers/api/request/'+id+'/status/', null, function(data) {
        if(data.status == 'failed') {
            hideRequestNotes();
	    $('#requestAuctionError').show();
        } 

	if(data.status == 'current') {
            hideRequestNotes();
	    $('#requestAuctionDescription').hide();
	    $('#requestAuctionFinished').show();
	}

    });
}
       

function updateSecondCounter(id, sec) {
    if($('#requestAuctionPending:visible').size() <= 0) {
	return;
    }

    // Check the status every 2 seconds
    if(sec/2%1 == 0) {
	checkStatus(id);
    }
    
    if(sec>0) {
	$('#second_countdown').text(sec);
	setTimeout("updateSecondCounter("+id+", "+--sec+");", 1000);
    } else {
	hideRequestNotes();
	$('#requestAuctionError').show();
    }
}

function setJsOk() {
    $('.jsok').css({ display: "block" });
    $('.jsnotok').remove()
}

function addAccessory(id) {
    $('#accessory_'+id)[0].value=1;
    $('#add_qty_'+id).css({ display: "block" });
    $('#add_to_order_'+id).css({ display: "none" });
}
function deleteAccessory(id) {
    $('#accessory_'+id)[0].value=0;
    $('#add_qty_'+id).css({ display: "none" });
    $('#add_to_order_'+id).css({ display: "block" });
}

function adjustAccessoryQty(id, dir) {
    var curr = document.getElementById('accessory_'+id);

    if(dir == 'down') {
        curr.value = parseInt(curr.value) > 0 ? parseInt(curr.value)-1 : 0;
    } else {
        curr.value = parseInt(curr.value)+1;
    }

    if(curr.value == 0) {
        deleteAccessory(id);
    }
}

function categoryChildrenShowMore(id) {
  var sct = $('#category_children_show_more_'+id);
  var lnk = $('#category_children_show_more_link_'+id);

	lnk.hide();
	sct.show();
}

$(document).ready(function() {
    setJsOk();

    $('a.highslide').bind('click', function() {
      return hs.expand(this, {captionId:''});
    });
});

// remove the registerOverlay call to disable the controlbar
  hs.registerOverlay({
    thumbnailId: null,
    overlayId: 'controlbar',
    position: 'top right',
    hideOnMouseOut: true
  });
  hs.graphicsDir = '//static.snipesoft.net.nz/libs/js/highslide-4.1.7/highslide/graphics/';
  hs.outlineType = 'rounded-white';

