﻿/*
* Shopping Cart Library
* http://www.logomark.com/
*
* Author:		Witt
* Create date:  2009-12-30
* Description:	
*/

var removemsg = "Are you sure you want to remove this item from your shopping cart?";


function rearrangelayout(sessid) {
    $(".removeitem").click(function(e) {

        var answer = confirm(removemsg);
        if (answer) {
            var gid = $(this).siblings(".groupid").val();
            batchremove(gid, function(responce) {
                if ((responce) && (responce > 0)) {
                    $('input[value="' + gid + '"].groupid').parent().parent().fadeOut("normal", function() {
                        $(this).remove();
                    });

                    // set the x and y offset of the poof animation <div> from cursor position (in pixels)
                    var xOffset = 0;
                    var yOffset = 80;

                    // set the absolute postion of the poof animation <div>
                    // uses e.pageX and e.pagY to get the cursor position
                    // and offsets the poof animation from this point based on the xOffset and yOffset values set above
                    $('.poof').css({
                        left: e.pageX - xOffset + 'px',
                        top: e.pageY - yOffset + 'px'
                    }).show(); // display the poof <div>

                    animatePoof(); // run the sprite animation
                }
            });
        }

        return false;
    });

    callgrouping(sessid);
}

function checklogin(userid) {
    // Need to log in
    if ((userid) && (userid != "")) {
        $(".needtologin").hide();
        $(".cartview").show();
    }
    else {
        $(".needtologin").show();
        $(".cartview").hide();
    } 
}

function batchremove(gid, callback) {
    $.ajax({
        type: "GET",
        data: "GroupID=" + gid,
        url: "/Comm/groupremove.aspx",
        success: function(responce) {
            callback(responce);
        }
    });
}

function animatePoof() {
    var bgTop = 0; // initial background-position for the poof sprit is '0 0'
    var frames = 5; // number of frames in the sprite animation
    var frameSize = 32; // size of poof <div> in pixels (32 x 32 px in this example)
    var frameRate = 80; // set length of time each frame in the animation will display (in milliseconds)

    // loop through amination frames
    // and display each frame by resetting the background-position of the poof <div>
    for (i = 1; i < frames; i++) {
        $('.poof').animate({
            backgroundPosition: '0 ' + (bgTop - frameSize) + 'px'
        }, frameRate);
        bgTop -= frameSize; // update bgPosition to reflect the new background-position of our poof <div>
    }
    // wait until the animation completes and then hide the poof <div>
    setTimeout("$('.poof').hide()", frames * frameRate);
}

function callgrouping(sessid) {
    // IE caches ajax call result
    var myRand = parseInt(Math.random() * 99999999);

    $.ajax({
        type: "GET",
        data: "SessID=" + sessid + "&Rand" + myRand,
        url: "/Comm/groupcart.aspx",
        success: function(responce) {

            // this will give us an array of objects
            var info = JSON.parse(responce);

            if ((!info) || (info.ReturnValue < 0) || (!info.GroupInfo) || (info.GroupInfo.length < 1)) {
                $(".removeitem").hide();
                return;
            }

            // group cart
            groupcart(info.GroupInfo);
        }
    });
}

function groupcart(groupinfo) {
    if ((!groupinfo) || (groupinfo.length < 1))
        return;

    var gid = -1;
    for (var i = 0; i < groupinfo.length; i++) {
        var del = $('input[value="' + groupinfo[i].OrderDetail_ID + '"].removeitem');
        if (del) {
            if (gid != groupinfo[i].GroupID) {
                del.show();
                del.parent().parent().children().css("border-top", "2px solid #009045");
            }
            else {
                del.hide();
            }
            gid = groupinfo[i].GroupID;
            del.parent().children(".groupid").val(groupinfo[i].GroupID);
        }
    }
}
