﻿/// <reference name="MicrosoftAjax.js"/>

function SetupFacebook() {
    FB_RequireFeatures(["XFBML"], function() {
        FB.Facebook.init("f94a68900b6319e52e1ab799adca9551", "channel/xd_receiver.htm");
    });
}

function setupgrid() {
    jQuery("#list2").jqGrid({
    url: '/Home/TopEarners/',
    height: 'auto',
        width: '600',
        datatype: "json",
        hiddengrid : true,        
        colNames: ['DonationId', 'Name', 'Pic', 'Date', 'Earned'],
        colModel: [
        { name: 'DonationId', index: 'DonationId', key: true, width: 100, hidden: true },
   		{ name: 'Name', index: 'Name', jsonmap: 'FacebookId', formatter:fbNameFormatter, width: 140 },
   		{ name: 'FacebookId', index: 'FacebookId', formatter:fbPhotoFormatter, width:50 },
   		{ name: 'DonationDate', index: 'DonationDate', formatter:datetimeFormatter, width: 130, align: "right" },
   		{ name: 'DonationAmount', index: 'DonationAmount', width: 40, align: "right", formatter:'currency', formatoptions:{decimalSeparator:".", thousandsSeparator: ",", decimalPlaces: 2, prefix: "$"}  }
   	],
   		jsonReader: {
   		    repeatitems: false
   		},
        rowNum: 10,
        rowList: [10, 20, 30],
        //pager: jQuery('#pager2'),
        sortname: 'DonationDate',
        gridComplete : SetupFacebook,
        viewrecords: true,
        sortorder: "desc",
        caption: "Most Recent Earners"
    });
    //.navGrid('#pager2', { edit: false, add: false, del: false });
    
    //$("#earn2donate").click(function() {document.location = "/Home/EarnToDonate/";});
}

function dateFormatter(cellval, opts, rwdat, _act) {
    var time = cellval.replace(/\/Date\(([0-9]*)\)\//, '$1');
    var d = new Date();
    d.setTime(time);
    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    curr_month++;
    var curr_year = d.getFullYear();
    return (curr_month + "/" + curr_date + "/" + curr_year);
}

function fbPhotoFormatter(cellval, opts, rwdat, _act) {
    return '<fb:profile-pic uid="' + cellval + '"></fb:profile-pic>';   
}

function fbNameFormatter(cellval, opts, rwdat, _act) {
    return '<fb:name uid="' + cellval + '"></fb:name>';
}

function datetimeFormatter(cellval, opts, rwdat, _act) {
    var time = cellval.replace(/\/Date\(([0-9]*)\)\//, '$1');
    var d = new Date();
    d.setTime(time);
    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    curr_month++;
    var curr_year = d.getFullYear();
    var curr_minute = d.getMinutes();
    var curr_hour = d.getHours();
    var ampm;    
    if (curr_hour > 12) {
        curr_hour = curr_hour - 12;
        ampm = "pm";
    } else {
        ampm = "am";
    }   
    var str_hour = new String(curr_hour);
    var str_minute = new String(curr_minute);  
    
    if (str_hour.length < 2) str_hour = "&nbsp;&nbsp;" + str_hour;
    if (str_minute.length < 2) str_minute = "0" + str_minute;
    
    return (curr_month + "/" + curr_date + "/" + curr_year + "&nbsp;" + str_hour + ":" + str_minute + ampm);
}


$(document).ready(function() {
    SetupFacebook();
    setupgrid();
});



