$(function() {
    $('#deathMatchLink, #deathMatchContentLink').click(function() {
        $("#deatchMatchDetail").load("/product/deathmatch", function(responseText, textStatus, XMLHttpRequest) {
            var enableVote = true;
            var cookie = $.cookie('vr_dm');
            if (cookie) {
                var voted = $('a#vote' + cookie);
                if (voted) {
                    voted.addClass('active').text('Your Selection');
                    $('a.vote:not(.active), a.voteimg:not(.active)').css('cursor', 'default');
                    enableVote = false;
                }
            }

            if (enableVote) {
                $('a.vote').click(function() {
                    $this = $(this);
                    var pId = this.id.replace('vote', '');

                    $.ajax({
                        async: false,
                        type: "POST",
                        url: "/product/deathmatch/vote/" + pId,
                        data: "",
                        success: function(data, textStatus) {
                            $.cookie('vr_dm', pId, { expires: 1 });

                            $('a.vote').removeClass('active').text('Vote for this Tee');
                            $this.addClass('active').text('Your Selection');
                            $('a.vote, a.voteimg').unbind('click');
                            $('a.vote:not(.active), a.voteimg:not(.active)').css('cursor', 'default');
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            debugger;
                        }
                    });
                    return false;
                });

                $('a.voteimg').click(function() {
                    $(this).addClass('active');
                    $('#' + this.id.replace('img', '')).click();
                    return false;
                });
            }

            enlarge('vote');
        });
        $.pageTracker._trackPageview('/deathmatch');

        return false;
    });

    $('.contactLink').click(function() {
        enlarge('popup1');
        $.pageTracker._trackPageview('/contact');
        return false;
    });

    $.gaTracker('UA-2677029-3');
});

function doAjaxSubmitWithValidation(form, e, loadingElement, updateTarget, validationSelector) {
    if (validateAjaxForm(validationSelector)) {
        if (e == null) return false;

        Sys.Mvc.AsyncForm.handleSubmit(form, new Sys.UI.DomEvent(e),
        {
            insertionMode: Sys.Mvc.InsertionMode.replace,
            loadingElementId: loadingElement,
            updateTargetId: updateTarget
        });
    }
    else {
        return false;
    }
}

function validateAjaxForm(validationSelector) {
    var isValid = true;
    $(validationSelector).removeClass('invalid').each(function() {
        var val = $(this).val();
        if (val.length == 0 || ($(this).hasClass('email') && !validateEmail(val))) {
            $(this).addClass('invalid');
            isValid = false;
        }
    });
    return isValid;
}

function validateEmail(value) {
    return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
}

function updateCartDetails(pieces, total) {
    $("#cartPieces a").text(pieces + " shirts");
    $("#cartTotal").text(total);

    var cart = $(".cart");
    cart.fadeTo('fast', 0);
    cart.fadeTo('normal', .6);
    cart.fadeTo('normal', .1);
    cart.fadeTo('slow', 1);
}

function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt * 100)) / 100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00'
                      : ((mnt * 10 == Math.floor(mnt * 10)) ?
                               mnt + '0' : mnt);
}

function enlarge(popup_id)
{
    var de = document.documentElement;

	$('#fader').css({ width: de.scrollWidth + 'px', height: de.scrollHeight + 'px' }).show();
	var popup = $('#' + popup_id)[0];
	centerEl(popup);
	$(popup).show();
}

function hide(popup_id)
{
    $('#' + popup_id).hide();
	$('#fader').hide();		
}

function getScroll(e) {
    var t = 0, l = 0, w = 0, h = 0, iw = 0, ih = 0;
    if (e && e.nodeName.toLowerCase() != 'body') {
        t = e.scrollTop;
        l = e.scrollLeft;
        w = e.scrollWidth;
        h = e.scrollHeight;
        iw = 0;
        ih = 0;
    } else {
        if (document.documentElement) {
            t = document.documentElement.scrollTop;
            l = document.documentElement.scrollLeft;
            w = document.documentElement.scrollWidth;
            h = document.documentElement.scrollHeight;
        } else if (document.body) {
            t = document.body.scrollTop;
            l = document.body.scrollLeft;
            w = document.body.scrollWidth;
            h = document.body.scrollHeight;
        }
        iw = self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0;
        ih = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0;
    }
    return { t: t, l: l, w: w, h: h, iw: iw, ih: ih };
}

function getSize(e) {
    var w = jQuery.css(e, 'width');
    var h = jQuery.css(e, 'height');
    var wb = 0;
    var hb = 0;
    var es = e.style;
    if (jQuery(e).css('display') != 'none') {
        wb = e.offsetWidth;
        hb = e.offsetHeight;
    } else {
        var oldVisibility = es.visibility;
        var oldPosition = es.position;
        es.visibility = 'hidden';
        es.display = 'block';
        es.position = 'absolute';
        wb = e.offsetWidth;
        hb = e.offsetHeight;
        es.display = 'none';
        es.position = oldPosition;
        es.visibility = oldVisibility;
    }
    return { w: w, h: h, wb: wb, hb: hb };
}

function centerEl(el, axis) {
    var clientScroll = getScroll();
    var windowSize = getSize(el);
    if (!axis || axis == 'vertically')
        jQuery(el).css(
				{
				    top: Math.max(clientScroll.t + ((Math.min(clientScroll.h, clientScroll.ih) - clientScroll.t - windowSize.hb) / 2), 0) + 'px'
				}
			);
    if (!axis || axis == 'horizontally')
        jQuery(el).css(
				{
				    left: Math.max(clientScroll.l + ((Math.min(clientScroll.w, clientScroll.iw) - clientScroll.l - windowSize.wb) / 2), 0) + 'px'
				}
			);
}