function noSpamMailtos() {
	var SPAM = /REMOVE_THIS\./, i=0, l;
	while (l=document.links.item(i++)) {
		if (l.href.match(/^mailto:/)) {
			l.href=l.href.replace(SPAM,'');
		}
	}
}
function popup(url, wid, hei) {
	var newwin = window.open(url,'tvPopup','resizable=yes,toolbar=no,status=no,'
	+ 'scrollbars=yes,location=no,menubar=no,directories=no,width='+wid+',height='+hei
	+ ',top=100,left=100');
	newwin.focus();
}
function checkRemoteCover() {
    var MAX_SIZE = 300,
        RESIZE_TO = 300;
    function resize() {
        var ci = this;
        if (ci.width && ci.height) {
            if (ci.width > MAX_SIZE || ci.height > MAX_SIZE) {
                var ratio = ci.width / ci.height;
                if (ci.width > ci.height) {
                    var newWidth = RESIZE_TO,
                        newHeight = parseInt(newWidth * (1 / ratio));
                } else {
                    var newHeight = RESIZE_TO,
                        newWidth = parseInt(newHeight * ratio);
                }
                ci.style.width = newWidth + "px";
                ci.style.height = newHeight + "px";
            }
        }
    }
    $('#view img.viewCover:first').each(function () {
        if (this.width) {
            resize.apply(this);
        } else {
            $(this).load(resize);
        }
    });
}
function ajaxifyAddLinks() {
    $(document.body).on('click', 'a[href^="/cart.php?add"]', function (evt) {
        function getCartNavHtml(numItems) {
            return '<a href="/cart.php" title="view your ' + numItems + ' item(s)">view/edit</a><br class="nn4br"><a href="https://secure.tonevendor.com/checkout.php" title="secure checkout with ' + numItems + ' item(s)">checkout</a><br class="nn4br">';
        }
        var m = this.href.match(/\/cart\.php\?add=(\d+)$/);
        if (! m) {
            return true;
        }
        evt.preventDefault();
        var link = this, loading = 1;
        $.get(m[0] + '&ajax', function (data) {
            loading = 0;
            if (! data) {
                $(link).replaceWith('Sorry this item is unavailable');
                return;
            }
            $(link).html('in your cart')
                .css({background: 'none', paddingLeft: '0'})
                .attr({href: '/cart.php', title: ''});
            var $nc = $('#navCart');
            var numItems = 0;
            $('a:first', $nc[0]).each(function () {
                numItems = parseInt(this.title.match(/\d+/)[0]);
            });
            if (numItems) {
                $nc.html(getCartNavHtml(numItems + 1));
            } else {
                $nc.slideUp('normal', function () {
                    $nc.html(getCartNavHtml(1)).slideDown();
                });
            }
        });
        setTimeout(function () {
            loading && $(link).text('...loading...').css({background: 'none', paddingLeft: '0'});
        }, 500);
    });
}

$(function () {
    noSpamMailtos();
    checkRemoteCover();
    if (! /^\/cart/.test(location.pathname)) {
        ajaxifyAddLinks();
    }
    if (location.pathname == '/') {
        // home
        $('.featuredRelease').each(function () {
            var url = $('h3 a:first', this).attr('href');
            $('.featureCover img', this).wrap('<a href="' + url + '"></a>');
        });
    }
});

