var cartOpen = false;
$(document).ready(function(){
    $(".tooltip").simpletooltip();
    if(cartQuantity>0){
        loadCart(0, null, null, false);
    }
});
function go(url){
    window.location = url;
}

function goSelectArtist(){
    var artist = $('#selectArtist').val();
    if(artist.length>1){
        var url = baseUrl + '/artist/' + artist;
        go(url);
    }
}
/**
*
*	simpleTooltip jQuery plugin, by Marius ILIE
*	visit http://dev.mariusilie.net for details
*
**/
(function($){
    $.fn.simpletooltip = function(){
        return this.each(function() {
            var text = $(this).attr("title");
            $(this).attr("title", "");
            if(text != undefined) {
                $(this).hover(function(e){
                    var tipX = e.pageX + 12;
                    var tipY = e.pageY + 12;
                    $(this).attr("title", "");
                    $("body").append("<div id='simpleTooltip' style='position: absolute; z-index: 10003; display: none;'>" + text + "</div>");
                    if($.browser.msie) var tipWidth = $("#simpleTooltip").outerWidth(true)
                    else var tipWidth = $("#simpleTooltip").width()
                    $("#simpleTooltip").width(tipWidth);
                    $("#simpleTooltip").css("left", tipX).css("top", tipY).fadeIn("medium");
                }, function(){
                    $("#simpleTooltip").remove();
                    $(this).attr("title", text);
                });
                $(this).mousemove(function(e){
                    var tipX = e.pageX + 12;
                    var tipY = e.pageY + 12;
                    var tipWidth = $("#simpleTooltip").outerWidth(true);
                    var tipHeight = $("#simpleTooltip").outerHeight(true);
                    if(tipX + tipWidth > $(window).scrollLeft() + $(window).width()) tipX = e.pageX - tipWidth;
                    if($(window).height()+$(window).scrollTop() < tipY + tipHeight) tipY = e.pageY - tipHeight;
                    $("#simpleTooltip").css("left", tipX).css("top", tipY).fadeIn("medium");
                });
            }
        });
    }
})(jQuery);
function debug(data){
    console.log(data);
}
function toggleCart(){
    if(cartQuantity>0){
        if(cartOpen){
            cartOpen = false;
            $("#cartBody").slideUp(1000);
        }
        else{
            cartOpen = true;
            $("#cartBody").slideDown(1000);
            $('html, body').animate({scrollTop:0}, 'slow');
        }
    }
}
function purchase(id, type, delivery){
    var postUrl = baseUrl + '/cart/ajaxadd';
    var postData = {
        id : id,
        type : type,
        delivery : delivery
    }
    if(cartOpen){
        cartOpen = false;
        $("#cartBody").slideUp(1000, function(){
            $.ajax({
               type: "POST",
               url: postUrl,
               data: postData,
               success: function(){
                    loadCart(id, type, delivery, true);
                    increaseCartQuantity();
               }
            });
        });
    }
    else{
        $.ajax({
           type: "POST",
           url: postUrl,
           data: postData,
           success: function(){
                loadCart(id, type, delivery, true);
                increaseCartQuantity();
           }
        });
    }
}
function increaseCartQuantity(){
   if(cartQuantity == 0){
       $("#cartQuantity").removeClass('noItems').addClass('withItems');
   }
   cartQuantity++;
   var text = cartQuantity;
   if(cartQuantity>1){
       text += " items in your cart";
   }
   else{
       text += " item in your cart";
   }
   $("#cartQuantity").html(text);
}
function loadCart(id, type, delivery, open){
   var postUrl = baseUrl + '/cart/showmini/id/' + id + '/type/' + type + '/delivery/' + delivery;
   $.ajax({
       type: "GET",
       url: postUrl,
       success: function(html){
            $("#cartBody").html(html);
            if(open){
                if(!cartOpen){
                    toggleCart();
                }
            }
       }
    });
}
function gotoCart(){
    var url = baseUrl + '/cart/show';
    window.location = url;
}