﻿//var soundsInTheCart = {};
//var downloadableFormats = {};


function playButtonRenderer(value, metadata, record, rowIndex, colIndex, store)
{
    return '<a href="javascript:void();" onclick="playsound({0}, false, this); return false;" class="play" title="Play" id="play{0}"><span>Play</span></a><a href="javascript:void()" onclick="stopsound(); return false;" class="stop" style="display: none" title="Stop" id="stop{0}"><span>Stop</span></a>'.format(record.id);
}

function showDownload(element, id)
{
    var menu = new Ext.menu.Menu({
    });
    //note that we must wrap this in a function to outsmart the closure support in js
    for (property in downloadableFormats) {
        (function (prop) {
            menu.add({ 
                text: downloadableFormats[prop]
                , icon: 'Images/Trex/icons/16x16/music.png'
                , handler: function() {
                        document.location.replace('/getsound/{0}.{1}'.format(id, prop));
                        return false;
                }
            }
        )})(property);
    }
    menu.show(element);
}

function buyButtonRenderer(value, metadata, record, rowIndex, colIndex, store)
{
    if (record.get('Bought') == 'True') {
        return "<a href='#' onClick='try {showDownload(this, {0}); return false;} catch (e){alert(e); return false;}'><img src='Images/download.png'></a>".format(record.id);
//                var dropdownMarkup = '<select class="searchdropdowns" onchange="if (this.options[this.selectedIndex].value != \'-1\') {document.location = \'/getsound/{0}.\' + this.options[this.selectedIndex].value; defaultformat = this.options[this.selectedIndex].value;}"><option value="">Download</option>'.format(record.id);
//                for (property in downloadableFormats) {
//                    dropdownMarkup += '<option value="{0}">{1}</option>'.format(property, downloadableFormats[property]);
//                }
//                dropdownMarkup += '</select>';
//                return dropdownMarkup;
    } else {
        if (soundsInTheCart.indexOf(parseInt(record.id)) == -1) {
            return '<a class="buy" title="Buy" onclick="purchaseSound({0}, this); return false;" href="javascript:void()"><span>Buy</span></a>'.format(record.id);
        } else {
            return '<a class="buy_disabled" title="Buy" onclick="return false;" href="javascript:void()"><span>Buy</span></a>'.format(record.id);
        }
    }
}        

function saveCheckboxRenderer(value, metadata, record, rowIndex, colIndex, store)
{
    if (record.get('Saved') == 'True') {
        return '<input type="checkbox" checked onclick="saveSound(' + "'" + record.id + "'" + ', this.checked ? true : false);"/>';
    } else {
        return '<input type="checkbox" onclick="saveSound(' + "'" + record.id + "'" + ', this.checked ? true : false);"/>';
    }
}    

//Format in the same way as Iteam.Debaser.FormatHelper.FriendlyTimeSpanFormat
function timeRenderer(value, metadata, record, rowIndex, colIndex, store)
{
    var time = record.get('Time');
    if (time != null) {
        time = parseInt(time);
        if (time > 59) {
            var hour = new String(Math.floor(time / 60));
            var sec = time % 60;
            if (sec < 10) {
                return hour + ":0" + sec;
            }
            return hour + ":" + sec;
            
        } else {
            if (time < 10) {
                return ":0" + time;
            }
            return ":" + time;
        }
    }
    return '';
}

function productsRenderer(value, metadata, record, rowIndex, colIndex, store)
{
    var returnString = "";
    var entries = value.split("t:[");
    for (var i = 0; i < entries.length; ++i) {
        if (entries[i] != null && entries[i] != "") {
            var localEntries = entries[i].split(" p:[");
            var type = localEntries[0];
            var product = localEntries[1];
            var replaceRegext = /\ /gi;
            returnString += "<a href='products/{0}/{1}' alt='{2}' title='{2}'>{2}</a>".format(escape(type.replace(replaceRegext, "_").toLowerCase()), escape(product.replace(replaceRegext, "_").toLowerCase()), product);
        }
    }
    return returnString;

}


function titleRenderer(value, metadata, record, rowIndex, colIndex, store, element) {
    return "<span title='" + value + "'>" + value + "</span>";

}