$(document).ready(function() {
    //alert("fly2basket");
    var imageDirectory = "images/fly2basket";

    $(".fly-to-cart a").click(function() {

        // get variables
        var id = $(this).attr("productid");
        var qty = 1;
        var gw = false;

        if ($(this).hasClass("featured-item")) {
            qty = (!isNaN(($('#txtQuantity').val() * 1)) ? $('#txtQuantity').val() : 1);
            gw = $('#cbGiftWrap').is(':checked');
        }

        var indexSplitter = (this.id).split("_");
        var index = indexSplitter[1];

        var productX = $("#productImageWrapID_" + index).offset().left;
        var productY = $("#productImageWrapID_" + index).offset().top;

        if ($("#productID_" + index).length > 0) {
            var basketX = $("#productID_" + index).offset().left;
            var basketY = $("#productID_" + index).offset().top;
        } else {
            var basketX = $("#basketTitleWrap").offset().left;
            var basketY = $("#basketTitleWrap").offset().top;
        }

        var gotoX = basketX - productX;
        var gotoY = basketY - productY;

        //var newImageWidth = $("#productImageWrapID_" + index).width() / 10;
        //var newImageHeight = $("#productImageWrapID_" + index).height() / 10;

        $("#productImageWrapID_" + index + " img")
		.clone()
		.prependTo("#productImageWrapID_" + index)
		.css({ 'position': 'absolute' })
		.animate({ opacity: 0.9 }, 100)
		.animate({ opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: 0, height: 0 }, 1200, function() {
		    $(this).remove();

		});

        CPMethod("addtocart_post", update_count, fail, "id", id, "qty", qty, "gw", gw);

    });

    $("#basketItemsWrap li img").live("click", function(event) {
        var indexSplitter = (this.id).split("_");
        var index = indexSplitter[1];
    });
});

function update_count(data) {
    var idtoUpdate = $(".cart-count");
    $(".cart-count").addClass("pink");
    idtoUpdate.html("(" + data.d + " ITEMS)");
    refreshcart();
}

function fail() {
    alert("Sorry, there has been an error");
}

function CPMethod(methodName, onSuccess, onFail) {
    var args = '';
    var l = arguments.length;
    if (l > 3) {
        for (var i = 3; i < l - 1; i += 2) {
            if (args.length != 0) args += ',';
            args += '"' + arguments[i] + '":"' + arguments[i + 1] + '"';
        }
    }

    $.ajax({
        type: "POST",
        url: "Cart.aspx/" + methodName,
        data: "{" + args + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        async: false,
        success: onSuccess,
        fail: onFail
    });
}

function refreshcart() {

    var myHost = window.location.host;
    var myHref = window.location.href;
    var myPathName = window.location.pathname;
    var myfileName = window.location.href.substring(window.location.href.lastIndexOf("/") + 1, window.location.href.length);

    myfileName = myfileName.toLowerCase();

    if (myfileName == "cart.aspx") {
        try {
            __doPostBack('upnlCartItems', '');
        }
        catch (err) {
            return false;
        }    
    }
}