﻿//scripts used to identify which tile is being acted upon using clientID.

function itemDeleteMessageon() {
    deletestatus = "on";
}

function itemDeleteMessageoff() {
    deletestatus = "off";
}

function showpopup(which) {
    //finds parent tile id
    //var itemtile = $(which).parent().get(0).id;
    
    //get all the values necessary for popup to populate the subsequent function call
    //var itemTitle = $("#" + itemtile + " span[name='itemtitlename']").contents();
    //showSearchItem(which, '[itembeneficiary]', 1, '[itemTitle]', '../img/127x127/box2.gif', '&#163;[itemprice]', '&#163;[itemdeposit]', 'included', '[lenderName] ([memberNo])', '[memberRating]', '[itemLocation]', '[itemDistance] miles away', false);
}
function actionOver(which, deletestatus) {
    var item = which;
    if (deletestatus == "on") {
        
        $("#" + item + " #actionlinks").hide();
        $("#" + item + " #deletemessage").show();
    } else {
    
        $("#" + item + " #actionlinks").show();
        $("#" + item + " #deletemessage").hide();
    }

}

function actionOut(which) {
    var item = which;

    $("#" + item + " #actionlinks").show();
    $("#" + item + " #deletemessage").hide();
}

function showSearchItem(which, charity, itemID, itemName, imageURL, cost, costPeriod, deposit, insurance, lender, lenderId, feedback, location, distance, flagged, source, requestable) {

    if (!(which == null)) {
        var parentOffset = $(which).parent().offset();

        $('#itemRolloverBox').fadeOut('fast', function() {
            // Fills the box with the data for the item in this element.
        fillPopup(charity, itemID, itemName, imageURL, cost, costPeriod, deposit, insurance, lender, lenderId, feedback, location, distance, flagged, requestable);

            // Need to work out where to position the box now.
            // The charity height is the height of the top line of the box (this could be multiple lines.
            var charityHeight = $('#txt_charity').height();
            if (charityHeight == 0)
                charityHeight = 16; // The height of a single line of text in there...

            if (source == "image") {
                // Now need to adjust the top of the rollover box to the correct position
                var topAdj = parentOffset.top - 22 - charityHeight;
                // Now need to adjust the left hand side of the rollover box to the correct position
                var leftAdj = parentOffset.left - 11; 
            } else {
            // Now need to adjust the top of the rollover box to the correct position
            var topAdj = parentOffset.top - 168 - charityHeight;//was 168 offset
            // Now need to adjust the left hand side of the rollover box to the correct position
            var leftAdj = parentOffset.left - 10;
        }
            

            // This actually sets the box into the correct position on the page
            $('#itemRolloverBox').css({ position: 'absolute', left: leftAdj, top: topAdj, zIndex: 500 });

            // Show the box
            $('#itemRolloverBox').fadeIn('fast');
        });
    }
}


/****
* Puts the item details into the rollover box elements.
* Inputs (all just data to display in the rollover):
*      charity     - string
*      itemID      - integer
*      itemName    - string
*      imageURL    - string
*      cost        - string
*      deposit     - string
*      insurance   - string
*      lender      - string
*      feedback    - integer
*      location    - string
*      distance    - string
****/



function fillPopup(charity, itemID, itemName, imageURL, cost, costPeriod, deposit, insurance, lender, lenderId, feedback, location, distance, flagged, requestable) {


    $('#txt_charity').children().remove();
    if (charity != "") {
        $('#txt_charity').append($(['<p class="title"><span class="lightGreen">proceeds for</span> ' + charity + '</p>', ].join('')));
    } else {
    $('#txt_charity').append($(['<p class="title"><span class="lightGreen">&nbsp;</span></p>', ].join('')));
    }
    
    $('#imgopen').attr({ src: imageURL, alt: itemName });
    $('#hyp_itemName').attr({ href: '#' + itemID }).html(itemName);
    $('#hyp_itemView').attr({ href: '#viewItem' + itemID });
    $('#hyp_itemFlag').attr({ href: '#flagItem' + itemID });
    $('#hyp_itemMap').attr({ href: '#mapItem' + itemID });

    //if (flagged) {
    //    $('#rollover_flag').css({ 'display': 'inline' });
    //} else {
    //    $('#rollover_flag').css({ 'display': 'none' });
    //}
    
    $('#costs').children().remove();
    $('#costs').append($([
        '<li><span>cost ' + costPeriod + '</span> ' + cost + '</li>',
        '<li><span>insurance</span> ' + insurance + '</li>',
        '<li><span>deposit</span> ' + deposit + '</li>'
    ].join('')));
    
    $('#details').children().remove();
    $('#details').append($([
        '<li><span>lender</span><a href="member_profile.aspx?id=' + lenderId + '"> ' + lender + '</a></li>',
        '<li><span>feedback</span> ' + feedback + '</li>',
        '<li><span>location</span> ' + location + '</li>',
        '<li><span id="txt_distance" style="color:#64705B;">' + distance + '</span></li>'
    ].join('')));

    // Re-bind the click event for the hyperlinks at the bottom of the rollover
    $('#hyp_itemView').unbind('click');
    $('#hyp_itemView').click(function() {
        $(this).attr('href', 'item_view.aspx?id=' + itemID);
    });

    $('#hyp_itemMap').unbind('click');
    $('#hyp_itemMap').click(function() {
        $(this).attr('href', 'javascript:openBrowser("popup_viewonmap.aspx?id=' + itemID + '", 525, 600)');
    });

    $('#hyp_request').unbind('click');
    $('#hyp_request').click(function() {
        $(this).attr('href', 'activedl_newrequest.aspx?i=' + itemID);
    });
    
    if (requestable == "true") {
        $('#request').show();
        $('#notrequestable').hide();
    }else if (requestable =="unknown") {
        $('#request').hide();
        $('#notrequestable').hide();
    } else {
        $('#request').hide();
        $('#notrequestable').show();
    }

//    if (flagged) {
//        $('#rollover_flag').css({ 'display': 'inline' });
//    } else {
//        $('#rollover_flag').css({ 'display': 'none' });
//    }
}

/****
* Closes the rollover box.
****/
function closeRolloverBox() {
    $('#itemRolloverBox').fadeOut('fast');
}


