﻿$(function() {
    //Ratings
    $('input[@type=radio].rating').rating({
        starWidth: 14,
        required: true,
        callback: function(value, link) {
            var pId = this.name.replace('rating', '');

            var allowVote = true;
            var rated = $.cookie('vr_rate');
            if (rated) {
                var ratedIds = rated.split(',');
                if (ratedIds && ratedIds.length > 0) {
                    $.each(ratedIds, function() {
                        if (this == pId) {
                            allowVote = false;
                            return false;
                        }
                    });
                }
            } else rated = '';

            if (allowVote) {
                $.ajax({
                    type: "POST",
                    url: "/product/rate/" + pId + "/" + value,
                    data: "",
                    success: function(data, textStatus) {
                        rated += rated.length > 0 ? ',' + pId : pId;
                        $.cookie('vr_rate', rated);
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        debugger;
                    }
                });
            } else alert("You've already rated this design.");
        }
    });

    //Email to friend
    $('ul.send a.email').click(function() {
        var pId = $(this).attr('href').substring(7);
        var pName = $('.item-box a[href=#' + pId + ']').attr('title');
        $('#etProductName').attr('value', pName);
        $('#etProductId').attr('value', pId);
        $('#emailTeeName').text(pName);

        enlarge('emailTee');
        $.pageTracker._trackPageview('/product/' + pId + '/email');
        return false;
    });

    //Fix up links
    $("a.fixlinks").each(function() {
        var href = $(this).attr("href");
        $(this)
            .attr("href", href.replace("/product/detail/", "#"))
            .click(function() {
                try {
                    $.pageTracker._trackPageview(href);
                } catch(ex) { };
             });
    });

    //History
    $("a.close-popup.history[href='#']").history(function() {
        hide('shirt-detail');
        return false;
    });
    $(".items a.history").history(showProductDetails);

    $.ajaxHistory.initialize(function() {
        hide('shirt-detail');
    });

    //Preload for zoom rollovers
    var preload = new Array();
    $('.item-box img').each(function() {
        $this = $(this);
        if ($this.metadata().rollId) {
            $this.hover(
                        function() {
                            this.src = '/image/get/' + $(this).metadata().withId + '.jpg';
                        },
                        function() {
                            this.src = '/image/get/' + $(this).metadata().rollId + '.jpg';
                        }
                    );
            preload.push('/image/get/' + $(this).metadata().withId + '.jpg');
        }
    });
    if (preload.length > 0) $.preloadImages(preload);
});

function showProductDetails() {
    var hash = $(this).attr('href').substring(1);

    $("#productDetail").load("/product/popupdetail/" + hash + "?rand=" + Math.floor(Math.random() * 1000000), function(responseText, textStatus, XMLHttpRequest) {
        //setup image gallery
        $('#sirvice-imgs a').click(function() {
            $('#sirvice-img').attr('src', $(this).find('span img').attr('src'));
            $('#sirvice-imgs li').removeClass('active');
            $(this).parent().addClass('active');
            return false;
        });

        //setup quantity drop-downs to calc subtotal
        $(".selItemQuantity").change(function() {
            sel = $(this);

            var price = parseFloat(sel.parent().prev().text().substring(1));
            var quan = parseInt(sel.val());

            sel.parent().next().text("$" + formatAsMoney(price * quan));
        });
        $(".selItemQuantity").change();

        //show popup
        enlarge('shirt-detail');
    });

    return false;
}

function addToCartOnSuccess(resultObj) {
    var pieces = 0;
    var total = "$0.00";
    var msg = "&nbsp;";
    if (resultObj.indexOf("|") != -1) {
        var info = resultObj.split("|");
        pieces = info[0];
        total = info[1];
        if (info.length == 3 && info[2]) msg = info[2];
    }

    $.pageTracker._trackPageview('/order/addtoorder/' + location.hash.substring(1) + '/' + pieces);
    updateCartDetails(pieces, total);

    $('#cart-msg').html(msg);
    if (msg != "&nbsp;") {
        $(this).oneTime(8000, function() {
            var cartMsg = $('#cart-msg');
            cartMsg.fadeTo('slow', 0, function() {
                cartMsg.html("&nbsp;");
                cartMsg.css('opacity', '1');
            });
        });
    }
    
    hide('shirt-detail');
    history.back(1);
}

function addToCartOnFailure(resultObj) {
    debugger;
}