// A wrapper around the coinwidget.com script "coin.js"
// coin.js depends on jQuery existing and being accessible as $ (doesn't work in Wordpress)
// This exposes $ as a variable and includes coin.js so as to make it work within Wordpress.
var CoinWidgetCom_Internal
(function($) {
/*
MIT License (MIT)
Copyright (c) 2013 http://coinwidget.com/
Copyright (c) 2013 http://scotty.cc/
Copyright (c) 2014 http://www.alvinhkh.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
if (typeof CoinWidgetComCounter != 'number')
var CoinWidgetComCounter = 0;
if (typeof CoinWidgetCom != 'object')
var CoinWidgetCom = {
source: 'http://www.alvinhkh.com/coinwidget/'
, config: []
, go :function(config, element) {
config = CoinWidgetCom.validate(config);
CoinWidgetCom.config[CoinWidgetComCounter] = config;
CoinWidgetCom.loader.jquery();
var newSpan = document.createElement('span');
newSpan.setAttribute("data-coinwidget-instance", CoinWidgetComCounter);
newSpan.setAttribute("class", "COINWIDGETCOM_CONTAINER");
if( (typeof element === 'object' && (element instanceof HTMLElement || $(element).length > 0)) ||
typeof element === 'string' && (element.charAt(0) === '#' || element.charAt(0) === '.' || element === '#' + element) && element.length > 0
){
if (typeof element === 'object'){
if(element instanceof NodeList)
element = element[0];
else if (typeof jQuery === 'function')
jQuery(element).replaceWith('');
} else if (typeof element === 'string'){
if (element.charAt(0) === '#' && document.getElementById(element.substr(1)) !== null)
element = document.getElementById(element.substr(1));
else if (element.charAt(0) === '.' && document.getElementsByClassName(element.substr(1))[0] !== null)
element = document.getElementsByClassName(element.substr(1))[0];
else
element = document.getElementById('element');
element.parentNode.replaceChild( newSpan, element );
}
} else {
document.getElementsByTagName('body')[0].appendChild(newSpan);
}
CoinWidgetComCounter++;
}
, validate: function(config) {
var $accepted = [];
$accepted['currencies'] = ['bitcoin','litecoin', 'dogecoin'];
$accepted['counters'] = ['count','amount','hide'];
$accepted['amount'] = ['show','hide'];
$accepted['alignment'] = ['al','ac','ar','bl','bc','br'];
if (!config.currency || !CoinWidgetCom.in_array(config.currency,$accepted['currencies']))
config.currency = 'bitcoin';
if (!config.counter || !CoinWidgetCom.in_array(config.counter,$accepted['counters']))
config.counter = 'count';
if (!config.amount || !CoinWidgetCom.in_array(config.amount,$accepted['amount']))
config.amount = 'show';
if (!config.alignment || !CoinWidgetCom.in_array(config.alignment,$accepted['alignment']))
config.alignment = 'bl';
if (typeof config.qrcode != 'boolean')
config.qrcode = true;
if (typeof config.milli != 'boolean')
config.milli = false;
if (typeof config.auto_show != 'boolean')
config.auto_show = false;
if (!config.wallet_address)
config.wallet_address = 'My '+ config.currency +' wallet_address is missing!';
if (!config.lbl_button)
config.lbl_button = 'Donate';
if (!config.lbl_address)
config.lbl_address = 'My Bitcoin Address:';
if (!config.lbl_count)
config.lbl_count = 'Donation';
if (!config.lbl_amount)
config.lbl_amount = 'BTC';
if (typeof config.decimals != 'number' || config.decimals < 0 || config.decimals > 10)
config.decimals = 4;
return config;
}
, init: function(){
CoinWidgetCom.loader.stylesheet();
$(window).resize(function(){
CoinWidgetCom.window_resize();
});
setTimeout(function(){
/* this delayed start gives the page enough time to
render multiple widgets before pinging for counts.
*/
CoinWidgetCom.build();
},800);
}
, build: function(){
$containers = $("span[data-coinwidget-instance]");
$containers.each(function(i,v){
$config = CoinWidgetCom.config[$(this).attr('data-coinwidget-instance')];
$counter = $config.counter == 'hide'?'':('
');
$button = '
'+$config.lbl_button+''+$counter;
$(this).html($button);
$(this).find('> a').unbind('click').click(function(e){
e.preventDefault();
CoinWidgetCom.show(this);
});
});
CoinWidgetCom.counters();
}
, window_resize: function(){
$.each(CoinWidgetCom.config,function(i,v){
CoinWidgetCom.window_position(i);
});
}
, window_position: function($instance){
$config = CoinWidgetCom.config[$instance];
coin_window = "#COINWIDGETCOM_WINDOW_"+$instance;
obj = "span[data-coinwidget-instance='"+$instance+"'] > a";
/* to make alignment relative to the full width of the container instead
of just the button change this occurence of $(obj) to $(obj).parent(),
do the same for the occurences within the switch statement. */
$pos = $(obj).offset();
switch ($config.alignment) {
default:
case 'al': /* above left */
$top = $pos.top - $(coin_window).outerHeight() - 10;
$left = $pos.left;
break;
case 'ac': /* above center */
$top = $pos.top - $(coin_window).outerHeight() - 10;
$left = $pos.left + ($(obj).outerWidth()/2) - ($(coin_window).outerWidth()/2);
break;
case 'ar': /* above right */
$top = $pos.top - $(coin_window).outerHeight() - 10;
$left = $pos.left + $(obj).outerWidth() - $(coin_window).outerWidth();
break;
case 'bl': /* bottom left */
$top = $pos.top + $(obj).outerHeight() + 10;
$left = $pos.left;
break;
case 'bc': /* bottom center */
$top = $pos.top + $(obj).outerHeight() + 10;
$left = $pos.left + ($(obj).outerWidth()/2) - ($(coin_window).outerWidth()/2);
break;
case 'br': /* bottom right */
$top = $pos.top + $(obj).outerHeight() + 10;
$left = $pos.left + $(obj).outerWidth() - $(coin_window).outerWidth();
break;
}
if ($(coin_window).is(':visible')) {
$(coin_window).stop().animate({'z-index':99999999999,'top':$top,'left':$left},150);
} else {
$(coin_window).stop().css({'z-index':99999999998,'top':$top,'left':$left});
}
}
, counter: []
, counters: function(){
$addresses = [];
$.each(CoinWidgetCom.config,function(i,v){
$instance = i;
$config = v;
if ($config.counter != 'hide')
$addresses.push($instance+'_'+$config.currency+'_'+$config.wallet_address);
else {
if ($config.auto_show)
$("span[data-coinwidget-instance='"+i+"']").find('> a').click();
}
});
if ($addresses.length) {
CoinWidgetCom.loader.script({
id: 'COINWIDGETCOM_INFO'+Math.random()
, source: (CoinWidgetCom.source+'lookup.php?data='+$addresses.join('|'))
, callback: function(){
if (typeof COINWIDGETCOM_DATA == 'object') {
CoinWidgetCom.counter = COINWIDGETCOM_DATA;
$.each(CoinWidgetCom.counter,function(i,v){
$config = CoinWidgetCom.config[i];
if (v == null || !v.count){
v = {count:0,amount:0};
}
$("span[data-coinwidget-instance='"+i+"']").find('> span').html($config.counter=='count'?v.count:((v.amount*($config.milli ? 1000 : 1)).toFixed($config.decimals)+' '+($config.milli ? 'm':'')+$config.lbl_amount));
if ($config.auto_show) {
$("span[data-coinwidget-instance='"+i+"']").find('> a').click();
}
});
}
if ($("span[data-coinwidget-instance] > span img").length > 0) {
setTimeout(function(){CoinWidgetCom.counters();},2500);
}
}
});
}
}
, show: function(obj) {
$instance = $(obj).parent().attr('data-coinwidget-instance');
$config = CoinWidgetCom.config[$instance];
coin_window = "#COINWIDGETCOM_WINDOW_"+$instance;
$(".COINWIDGETCOM_WINDOW").css({'z-index':99999999998});
if (!$(coin_window).length) {
$sel = !navigator.userAgent.match(/iPhone/i)?'onclick="this.select();"':'onclick="prompt(\'Select all and copy:\',\''+$config.wallet_address+'\');"';
$html = ''
+ ''
+ ''
+ '
'
+ 'x'
+ '
'
;
if ($config.counter != 'hide') {
$html += '0'+$config.lbl_count+'';
if ($config.amount != 'hide') {
$html += '0.00'+($config.milli ? 'm':'')+$config.lbl_amount+'';
}
}
if ($config.qrcode) {
$html += '
'
+ '
'
;
}
var $div = $('');
$('body').append($div);
$div.attr({
'id': 'COINWIDGETCOM_WINDOW_'+$instance
}).addClass('COINWIDGETCOM_WINDOW COINWIDGETCOM_WINDOW_'+$config.currency.toUpperCase()+' COINWIDGETCOM_WINDOW_'+$config.alignment.toUpperCase()).html($html).unbind('click').bind('click',function(){
$(".COINWIDGETCOM_WINDOW").css({'z-index':99999999998});
$(this).css({'z-index':99999999999});
});
if ($config.qrcode) {
$(coin_window).find('.COINWIDGETCOM_QRCODE').bind('mouseenter click',function(){
$config = CoinWidgetCom.config[$(this).attr('data-coinwidget-instance')];
$lrg = $(this).parent().find('.COINWIDGETCOM_QRCODE_LARGE');
if ($lrg.is(':visible')) {
$lrg.hide();
return;
}
$lrg.attr({
src: CoinWidgetCom.source +'qr/?address='+$config.wallet_address
}).show();
}).bind('mouseleave',function(){
$lrg = $(this).parent().find('.COINWIDGETCOM_QRCODE_LARGE');
$lrg.hide();
});
}
} else {
if ($(coin_window).is(':visible')) {
CoinWidgetCom.hide($instance);
return;
}
}
CoinWidgetCom.window_position($instance);
$(coin_window).show();
$pos = $(coin_window).find('input').position();
$(coin_window).find('img.COINWIDGET_INPUT_ICON').css({'top':$pos.top+3,'left':$pos.left+3});
$(coin_window).find('.COINWIDGETCOM_WALLETURI').css({'top':$pos.top+3,'left':$pos.left+$(coin_window).find('input').outerWidth()+3});
if ($config.counter != 'hide') {
$counters = CoinWidgetCom.counter[$instance];
if ($counters == null) {
$counters = {
count: 0,
amount: 0
};
}
if ($counters.count == null) $counters.count = 0;
if ($counters.amount == null) $counters.amount = 0;
$(coin_window).find('.COINWIDGETCOM_COUNT').html($counters.count+ ''+$config.lbl_count+'');
if ($config.amount != 'hide') {
$(coin_window).find('.COINWIDGETCOM_AMOUNT').html(($counters.amount*($config.milli ? 1000 : 1)).toFixed($config.decimals)+ ''+($config.milli ? 'm':'')+$config.lbl_amount+'');
}
}
if (typeof $config.onShow == 'function')
$config.onShow();
}
, hide: function($instance) {
$config = CoinWidgetCom.config[$instance];
coin_window = "#COINWIDGETCOM_WINDOW_"+$instance;
$(coin_window).fadeOut();
if (typeof $config.onHide == 'function') {
$config.onHide();
}
}
, in_array: function(needle,haystack) {
for (i=0;i');
$("head").append($link);
$link.attr({
id : 'COINWIDGETCOM_STYLESHEET'
, rel : 'stylesheet'
, type : 'text/css'
, href : CoinWidgetCom.source+'coin.css'
});
}
}
, jquery: function(){
if (!window.jQuery && !CoinWidgetCom.loader.loading_jquery) {
$prefix = window.location.protocol=='file:'?'http:':'';
CoinWidgetCom.loader.script({
id : 'COINWIDGETCOM_JQUERY'
, source : $prefix + '//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'
, callback : function(){
CoinWidgetCom.init();
}
});
return;
}
CoinWidgetCom.init();
}
}
};
CoinWidgetCom_Internal = CoinWidgetCom
})(jQuery);
CoinWidgetCom = CoinWidgetCom_Internal