var QuickViewPopUp = Class.create({
    name: '',
    skus: null,
    product: null,
    selectedSku: null,
    selectedImageName: '',
    swatchCarousel: null,
    tabs: null,
    isAddAndSave: false,
    parentFamilyId: '',
    promoTypeId: '',
    promoId: '',
    url: '',
    absolutePosition: '',
    iframe: null,
    selectedAvailabilityStatus: 1,
    initialize: function(tname) {

        this.name = tname;
        this.getPopDimensions();

        Event.observe(this.name + "ColorDropDown", 'change', this.ColorDropDownChange.bindAsEventListener(this));
        Event.observe(this.name + "SizeDropDown", 'change', this.SizeDropDownChange.bindAsEventListener(this));
        Event.observe(this.name + "ProgramDropDown", 'change', this.ProgramDropDownChange.bindAsEventListener(this));


        Event.observe(this.name + '_accept', 'click', this.AcceptAddAndSave.bindAsEventListener(this));
        Event.observe(this.name + '_decline', 'click', this.DeclineAddAndSave.bindAsEventListener(this));

        Event.observe(this.name + '_addToCart', 'click', this.AddProductToCart.bindAsEventListener(this));
        Event.observe(this.name + '_quickBuy', 'click', this.QuickBuyProduct.bindAsEventListener(this));


        Control.Window.baseZIndex = 110000;
        this.iframe = new Control.Window($(this.name + 'Wrapper'), Object.extend({
            draggable: $(this.name + '_header')
        }));
        this.iframe.options.fade = true;

        if (navigator.appName == 'Netscape')
            this.iframe.iFrameShim.destroy();


    },
    SetUpAddAndSave: function() {

        if ($(this.name + '_promoMessage')) {
            $(this.name + '_promoMessage').show();
            $(this.name + '_promoMessage').innerHTML = this.promoText;
        }
        //             if($(this.name+ '_closeDiv'))
        //                 $(this.name+ '_closeDiv').hide();
        if ($(this.name + '_buyButton'))
            $(this.name + '_buyButton').hide();
        if ($(this.name + '_addAndSaveButtons'))
            $(this.name + '_addAndSaveButtons').show();

    },
    SetUpQuickView: function() {
        if ($(this.name + '_promoMessage'))
            $(this.name + '_promoMessage').hide();
        if ($(this.name + '_closeDiv'))
            $(this.name + '_closeDiv').show();
        if ($(this.name + '_buyButton'))
            $(this.name + '_buyButton').show();
        if ($(this.name + '_addAndSaveButtons'))
            $(this.name + '_addAndSaveButtons').hide();



    },

    popWidth: 150,
    popHeight: 300,
    popPadding: 5,
    isVisable: false,
    xLoc: 0,
    yLoc: 0,
    promoPrice: 0,
    promoPriceType: -1,
    promoText: "",


    getPopDimensions: function() {

        this.popHeight = $(this.name + 'Wrapper').getDimensions().height;
        this.popWidth = $(this.name + 'Wrapper').getDimensions().width;
    },

    //Load the popup info, and then displayes it next to the item
    ShowPopUp: function() {

        if (!this.isVisable) {
            //Some position logic that I just made up
            if (!this.absolutePosition) {
                var windowPos = document.viewport.getScrollOffsets();
                var windowSize = document.viewport.getDimensions();

                this.xLoc = this.xLoc - (.5 * this.popWidth) + 75;
                this.yLoc = this.yLoc - 20;

                if (this.yLoc < windowPos.top + this.popPadding)
                    this.yLoc = windowPos.top + this.popPadding;

                if (this.yLoc + this.popHeight > windowPos.top + windowSize.height - this.popPadding) {
                    this.yLoc = windowPos.top + windowSize.height - this.popPadding - this.popHeight;
                    this.xLoc = this.xLoc + .5 * this.popWidth + 75;
                }

                if (this.xLoc < windowPos.left + this.popPadding)
                    this.xLoc = windowPos.left + this.popPadding;

                if (this.xLoc + this.popWidth > windowPos.left + windowSize.width - this.popPadding)
                    this.xLoc = windowPos.left + windowSize.width - this.popPadding - this.popWidth
            }

            this.iframe.options.position = new Array();
            this.iframe.options.position[0] = this.xLoc;
            this.iframe.options.position[1] = this.yLoc;

            this.isVisable = this.iframe.open();
            setTimeout(this.name + ".SetCarousel()", 850);
            setTimeout("$('quickViewWrapper').style.top = " + this.name + ".yLoc + \"px\";", 5);

            //tracking
            dcsMultiTrack("dcsuri", "/Quickview.aspx", "WT.ti", "QuickView", "WT.si_x", "1", "WT.tx_u", "1", "WT.si_n", "purchase;quickbuy", "WT.tx_e", "v", "WT.pn_sku", this.familyId, "WT.cg_n", "QuickView", "DCSext.VideoView", "", "DCSext.ProductVideoView", "", "DCSext.tabs", "");


        }
    },

    closePopup: function(overRideAddAndSave) { //overRideAddAndSave is only used when you want to overide the addand savecheck, since this call occures in the declineAddAndSave function
        if (!overRideAddAndSave && this.isAddAndSave) //otherwise you can simply call closePopup();
        {
            this.DeclineAddAndSave();
        }
        else if (this.isVisable) {
            this.StopVideo();
            this.isVisable = !this.iframe.close();
        }
    },

    /************ END OF POP UP DISPLAY PROPERTIES **************/

    // parentFamilyId,promoId, promoTypeId,promoPrice,promoText,promoPriceType are only set on a add and save window 
    // For a normal quickview  you can simply call LoadQuickView(familyId, event) or even just LoadQuickView(familyId)
    LoadQuickView: function(familyId, event, parentFamilyId, promoId, promoTypeId, promoPrice, promoText, promoPriceType) {

        if (this.isVisable && this.familyId == familyId && parentFamilyId == null) //It's a reclick so ignore 
            return;

        this.familyId = familyId;
        if (parentFamilyId) //Is AddAndSave
        {
            this.isAddAndSave = true;
            this.promoId = promoId;
            this.promoPrice = promoPrice;
            this.parentFamilyId = parentFamilyId;
            this.promoTypeId = promoTypeId;
            this.promoText = promoText;
            this.promoPriceType = promoPriceType;
        }
        else
            this.isAddAndSave = false;

        //productServiceUrl is set in the quickview controller to get the productservice url from the config.  
        this.url = productServiceUrl + '/GetProductData/' + familyId + "/SetUpQuickViewData/";

        try {

            if (event != null) {
                if (event.qvx != null && event.qvy != null) {
                    this.xLoc = event.qvx;
                    this.yLoc = event.qvy;

                    var windowSize = document.viewport.getDimensions();
                    var offset = (windowSize.width - 966) / 2;
                    if (offset > 0)
                        this.xLoc = this.xLoc + offset;

                    this.absolutePosition = true;

                }
                else {
                    this.xLoc = Event.pointerX(event);
                    this.yLoc = Event.pointerY(event);
                    this.absolutePosition = false;
                }
            }
            else {
                if (this.xLoc == 0 && this.yLoc == 0)//set location if first time, else we default to the previous location
                {
                    this.xLoc = 10;
                    this.yLoc = 200;

                    var windowSize = document.viewport.getDimensions();
                    var offset = (windowSize.width - 966) / 2;
                    if (offset > 0)
                        this.xLoc = this.xLoc + offset;
                }
                else {

                    this.xLoc = parseInt($(this.name + 'Wrapper').offsetLeft);
                    this.yLoc = parseInt($(this.name + 'Wrapper').offsetTop);

                    if (this.xLoc == 0 && this.yLoc == 0) {
                        this.xLoc = parseInt($(this.name + 'Wrapper').style.left);
                        this.yLoc = parseInt($(this.name + 'Wrapper').style.top);
                    }
                }
                this.absolutePosition = true;
            }
        }
        catch (e) {


        }

        if (!this.isVisable)
            setTimeout(this.name + ".makecall()", 1); //Need this for a fix to show the QV on the OnAir pages.  Not sure why.
        else {
            this.closePopup(true);
            setTimeout(this.name + ".makecall()", 950);
        }

    },
    makecall: function() {
        makeCall(this.url);
    },
    /***  USED TO SET UP THE QUICK VIEW****/

    SetUpQuickViewData: function(product) {

        this.product = product;
        this.skus = product._skus;
        this.selectedAvailabilityStatus = product._availabilityStatus;

        if (this.isAddAndSave)
            this.SetUpAddAndSave();
        else
            this.SetUpQuickView();

        this.SetButtonImage();

        this.SetProductInfo();
        this.DisplayImage('default');
        this.SetMorePhotos();
        this.SetSkuData();
        this.SetupTabs();
        this.tabs = new Control.Tabs(this.name + 'Block_tabs');

        this.UpdateColorText('');
        this.UpdateSizeText('');
        this.ClearMessages();

        this.ShowPopUp();

    },

    SetProductInfo: function() {
        if (this.product != null) {
            if ($(this.name + '_moreInfoPhoto'))
                $(this.name + '_moreInfoPhoto').innerHTML = this.GetLearnMore();
            if ($(this.name + '_moreInfoDesc'))
                $(this.name + '_moreInfoDesc').innerHTML = this.GetLearnMore();

            if ($(this.name + 'ProductNamePhoto'))
                $(this.name + 'ProductNamePhoto').innerHTML = "<a href=\"" + this.GetLearnMoreLink() + "\">" + this.product._familyID + " - " + this.product._familyName + "</a>";
            if ($(this.name + 'ProductNameDesc'))
                $(this.name + 'ProductNameDesc').innerHTML = "<a href=\"" + this.GetLearnMoreLink() + "\">" + this.product._familyID + " - " + this.product._familyName + "</a>";

            if ($(this.name + 'PriceLabelPhoto'))
                $(this.name + 'PriceLabelPhoto').innerHTML = this.product._priceLabel + ": ";
            if ($(this.name + 'PriceLabelDesc'))
                $(this.name + 'PriceLabelDesc').innerHTML = this.product._priceLabel + ": ";

            if ($(this.name + 'PriceBlockPhoto'))
                if (this.isAddAndSave) {
                if (this.promoPrice < 0)
                    $(this.name + 'PriceBlockPhoto').innerHTML = "FREE";
                else if (this.promoPriceType == 1)
                    $(this.name + 'PriceBlockPhoto').innerHTML = "$" + ParseToMoneyString(this.product._currentPrice - ((this.promoPrice / 100) * this.product._currentPrice));
                else if (this.promoPriceType == 0)
                    $(this.name + 'PriceBlockPhoto').innerHTML = "$" + ParseToMoneyString(this.promoPrice);
                else
                    $(this.name + 'PriceBlockPhoto').innerHTML = "$" + ParseToMoneyString(this.product._currentPrice - this.promoPrice);
            }
            else
                $(this.name + 'PriceBlockPhoto').innerHTML = "$" + ParseToMoneyString(this.product._currentPrice);
            if ($(this.name + 'PriceBlockDesc'))
                if (this.isAddAndSave) {
                if (this.promoPrice < 0)
                    $(this.name + 'PriceBlockDesc').innerHTML = "FREE";
                else if (this.promoPriceType == 1)
                    $(this.name + 'PriceBlockDesc').innerHTML = "$" + ParseToMoneyString(this.product._currentPrice - ((this.promoPrice / 100) * this.product._currentPrice));
                else if (this.promoPriceType == 0)
                    $(this.name + 'PriceBlockDesc').innerHTML = "$" + ParseToMoneyString(this.promoPrice);
                else
                    $(this.name + 'PriceBlockDesc').innerHTML = "$" + ParseToMoneyString(this.product._currentPrice - this.promoPrice);
            }
            else
                $(this.name + 'PriceBlockDesc').innerHTML = "$" + ParseToMoneyString(this.product._currentPrice);


            if ($(this.name + 'Description')) {
                var desc = this.product._shortDescription + "<br>" + this.product._longDescription;
                desc = desc.substring(0, 650);
                var len = desc.lastIndexOf('. ');
                if (len > 0)
                    desc = desc.substring(0, len);
                else
                    desc = desc.substring(0, desc.lastIndexOf(' '));
                desc = desc + '...<br>' + this.GetLearnMore(); ;
                $(this.name + 'Description').innerHTML = desc;

            }
        }
    },

    GetLearnMoreLink: function() {
        return "/product/?familyid=" + this.product._familyID + "&track=-10102&ciid=11281"; //&cm_re=" + cmre;
    },
    GetLearnMore: function() {
        var u = this.GetLearnMoreLink();
        var s = "<p><a href=\"" + u + "\" class=\"moreInfo\" >Learn More</a> &nbsp;" + "<img src='/images/global/home/blueCaratRight.gif' />"; +"</p>";
        return s;
    },

    SetMorePhotos: function() {
        if (this.product != null) {
            var photos = this.product._photos;
            if (photos.length > 1) {
                if ($(this.name + 'MorePhotosDiv'))
                    $(this.name + 'MorePhotosDiv').show();

                if ($(this.name + 'MorePhotos')) {
                    $(this.name + 'MorePhotos').innerHTML = "";
                    for (var i = 0; i < photos.length; i++) {
                        var imageName = this.product._familyID.toLowerCase();
                        if (photos[i]._type == 2)
                            if (photos[i]._sequence < 10)
                            imageName = imageName + "_0" + photos[i]._sequence + "_detail";
                        else
                            imageName = imageName + "_" + photos[i]._sequence + "_detail";
                        $(this.name + 'MorePhotos').innerHTML +=
								            "<a href=\"Javascript:" + this.name + ".DisplayImage('"
								            + imageName + "');\"><img src=\"http://images.shopnbc.com/is/image/ShopNBC/"
								            + imageName + "?DefaultImage=1&$80x80_jpg$\" width=\"80\" height=\"80\" alt=\""
								            + ((photos[i]._caption) ? photos[i]._caption : '') + "\" title=\""
								            + ((photos[i]._caption) ? photos[i]._caption : '')
								            + "\" /></a>";
                    }
                }
            }
            else {
                if ($(this.name + 'MorePhotosDiv'))
                    $(this.name + 'MorePhotosDiv').hide();
            }
        }
    },
    hasCarousel: false,
    SetCarousel: function() {
        if (this.hasCarousel) {
            if (this.swatchCarousel == null) {
                this.swatchCarousel = new UI.Carousel(this.name + "_carousel");
                this.swatchCarousel.elements = new Array();
                var swatchImagesLi = $(this.name + '_SwatchImages').childElements();
                for (var i = 0; i < swatchImagesLi.length; i++) {
                    if (swatchImagesLi[i].innerHTML == "")
                        break;
                    this.swatchCarousel.elements[i] = swatchImagesLi[i];
                }
                if (i < 4)
                    this.swatchCarousel.updateButtons()
            }
            else {
                if (this.swatchCarousel.currentIndex() > 0)
                    this.swatchCarousel.scrollTo(0);
                else
                    this.swatchCarousel.updateButtons()
            }
        }
    },

    AddDropDownOption: function(selectbox, text, value) {
        var optn = document.createElement("OPTION");
        optn.text = text;
        optn.value = value;
        selectbox.options.add(optn);
    },

    SetSkuData: function() {

        var colorDropDown = this.GetColorDropDown();
        var sizeDropDown = this.GetSizeDropDown();
        var programDropDown = this.GetProgramDropDown();

        colorDropDown.innerHTML = "";
        this.AddDropDownOption(colorDropDown, "Color", "-1")
        sizeDropDown.innerHTML = "";
        this.AddDropDownOption(sizeDropDown, "Size", "-1")
        programDropDown.innerHTML = "";
        this.AddDropDownOption(programDropDown, "Select Delivery Program", "-2");
        this.AddDropDownOption(programDropDown, "Single Delivery", "-1");

        var sku;
        var addElemnt;
        var swatchImageIndex = 0;
        var swatchImagesLi = $(this.name + '_SwatchImages').childElements();
        var swatchImageArray = new Array();
        var programsAvailable = false;
        for (var i = 0; i < this.skus.length; i++) {
            sku = this.skus[i];

            addElemnt = true;

            for (var j = 1; j < colorDropDown.options.length; j++) {
                if (colorDropDown.options[j].value == sku._colorCode)
                    addElemnt = false;
            }
            if (addElemnt) {
                var status = this.GetColorAvailability(sku._colorCode);
                if (status == 0)
                    this.AddDropDownOption(colorDropDown, sku._colorValue, sku._colorCode);
                else if (status == 1)
                    this.AddDropDownOption(colorDropDown, sku._colorValue + " - Sold Out", sku._colorCode);
                else
                    this.AddDropDownOption(colorDropDown, sku._colorValue + " -Coming Soon", sku._colorCode);

                if (sku._isPhotoActive) {
                    this.selectedSku = sku;

                    var imageSrc = this.GetImageURL('swatch', 40);

                    //if we directly edit the list the carousel script wont work, so we need to edit the objects 
                    //in the list so this script error won't happen.  
                    if (swatchImagesLi.length > swatchImageIndex) {
                        if (this.selectedImageName != '') {
                            swatchImagesLi[swatchImageIndex].innerHTML = "<a href=\"Javascript:"
						                            + this.name + ".SwatchSelect('"
						                            + sku._colorCode
						                            + "');\"><img src=\""
						                            + imageSrc
						                            + "\" id=\""
						                            + this.name + "_swatch"
						                            + this.selectedSku._colorCode
						                            + "\" width=\"40\" height=\"40\" alt=\""
						                            + this.selectedSku._colorValue + "\" title=\""
						                            + this.selectedSku._colorValue + "\" /></a>";
                            swatchImageArray[swatchImageIndex] = swatchImagesLi[swatchImageIndex]
                            swatchImageIndex++;
                        }
                    }
                }
            }

            addElemnt = true;
            for (var j = 1; j < sizeDropDown.options.length; j++) {
                if (sizeDropDown.options[j].value == sku._sizeCode)
                    addElemnt = false;
            }
            if (addElemnt) {
                var status = this.GetSizeAvailability(sku._sizeCode);
                if (status == 0)
                    this.AddDropDownOption(sizeDropDown, sku._sizeValue, sku._sizeCode);
                else if (status == 1)
                    this.AddDropDownOption(sizeDropDown, sku._sizeValue + " - Sold Out", sku._sizeCode);
                else
                    this.AddDropDownOption(sizeDropDown, sku._sizeValue + " -Coming Soon", sku._sizeCode);

            }

            // need to determine if the dropdown box should be displayed or not
            if (sku._autoDeliveryPrograms != null && !this.isAddAndSave) {
                for (var j = 0; j < sku._autoDeliveryPrograms.length; j++) {
                    if (sku._autoDeliveryPrograms[j] != null) {
                        programsAvailable = true;
                        break;
                    }
                }
            }
        }


        if (swatchImageIndex < 1) {
            this.hasCarousel = false;
            $(this.name + "_carousel").hide();
        }
        else {

            this.hasCarousel = true;
            $(this.name + "_carousel").show();


            if (swatchImageIndex > 3) {
                $(this.name + "CarouselButtons").show();
            }
            else {
                $(this.name + "CarouselButtons").hide();
            }
        }

        //we clear out the rest of the carousel from the previous quickview
        while (swatchImagesLi.length > swatchImageIndex && swatchImagesLi[swatchImageIndex].innerHTML != "") {
            swatchImagesLi[swatchImageIndex].innerHTML = "";
            swatchImageIndex++;
        }

        //we need to reset the carousel object with a list of active list items 
        if (this.swatchCarousel)
            this.swatchCarousel.elements = swatchImageArray;

        //make sure we have skus to work with.  It is possible (although highly unlikely) that the familyid is web approved but none
        //of the skus are web avail or have a status with a Do Not Display (DND) outcome, in which case the product object is not null but 
        //no skus are returned.
        if (this.skus.length > 0) {
            if (sizeDropDown != null) {
                if (sizeDropDown.options.length < 3) {

                    sizeDropDown.options[1].selected = true;
                    sizeDropDown.style.display = "none"
                    document.getElementById(this.name + "SizeText").style.display = "none"
                }
                else {
                    sizeDropDown.options[0].selected = true;
                    sizeDropDown.style.display = ""
                    document.getElementById(this.name + "SizeText").style.display = ""
                }
            }
            if (colorDropDown != null) {
                if (colorDropDown.options.length < 3) {
                    colorDropDown.options[1].selected = true;
                    colorDropDown.style.display = "none"
                    document.getElementById(this.name + "ColorText").style.display = "none"
                }
                else {
                    colorDropDown.options[0].selected = true;
                    colorDropDown.style.display = ""
                    document.getElementById(this.name + "ColorText").style.display = ""
                }
            }
        }
        else {
            sizeDropDown.style.display = "none"
            document.getElementById(this.name + "SizeText").style.display = "none"
            colorDropDown.style.display = "none"
            document.getElementById(this.name + "ColorText").style.display = "none"
        }

        if (programDropDown != null) {
            this.RegenorateProgramDropDown();
        }


    },

    SetButtonImage: function() {
        if ($(this.name + '_addToCartBtn'))
            if (this.product._availabilityStatus != 0) {
            $(this.name + '_addToCartBtn').src = '/images/stylesheetimgs/btn_addToCartGrey.jpg';
            $(this.name + '_quickBuyBtn').hide();

        }
        else {
            $(this.name + '_addToCartBtn').src = '/images/stylesheetimgs/btn_addToCart.gif';
            $(this.name + '_quickBuyBtn').show();
        }
    },



    GetColorDropDown: function() {
        return $(this.name + "ColorDropDown");
    },

    GetSizeDropDown: function() {
        return $(this.name + "SizeDropDown");
    },
    GetProgramDropDown: function() {
        return $(this.name + "ProgramDropDown");
    },
     GetProgramText: function() {
        return $(this.name + "ProgramText");
    },

    DoesProductHaveAutoDeliveryPrograms: function() {
        var hasPrograms = false;

        for (var i = 0; i < this.skus.length; i++) {
            var sku = this.skus[i];

            if (sku != null && sku._autoDeliveryPrograms != null) {
                for (var j = 0; j < sku._autoDeliveryPrograms.length; j++) {
                    if (sku._autoDeliveryPrograms[j] != null) {
                        hasPrograms = true;
                        break;
                    }
                }
            }
        }

        return hasPrograms;
    },

    overlayStatus: '',
    GetImageURL: function(type, size) {
        //displays ths swatch  of the selected color
        //looks at selectedAvailabilityStatus to determan what sash to put on the product
        //returns the imageURL;  

        var imageType = '';
        var imageURL;
        var rootURL = "http://images.shopnbc.com/is/image/ShopNBC/";
        var newSelectedImageName = '';
        var familyId = this.product._familyID.toLowerCase();
        //this.overlayStatus = this.product._availabilityStatus; 

        if (type == "default") {
            newSelectedImageName = familyId;
            imageType = 'default'
        }
        else if (type == "swatch") {
            if (this.selectedSku) {
                if (this.selectedSku._isPhotoActive && this.selectedSku._isPhotoActive != 'False') {
                    if (this.selectedSku._photoSequence < 10)
                        newSelectedImageName = familyId + "_0" + this.selectedSku._photoSequence + "_swatch";
                    else
                        newSelectedImageName = familyId + "_" + this.selectedSku._photoSequence + "_swatch";
                }
                imageType = "swatch"
            }

        }
        else if (type == "details") {
            if (selectedSku._photoSequence < 10)
                newSelectedImageName = familyId + "_0" + this.selectedSku._photoSequence + "_detail";

            else
                newSelectedImageName = familyId + "_" + this.selectedSku._photoSequence + "_detail";

            imageType = "details";


        }
        else //then type is the imagename
        {
            newSelectedImageName = type;
        }


        if (newSelectedImageName != '') {
            this.selectedImageName = newSelectedImageName
        }
        if (size != 200) // we are in zoom 
            imageURL = rootURL + this.selectedImageName + "?hei=" + size + "&wid=" + size + "&op_sharpen=1";
        else if (this.selectedAvailabilityStatus == 2)
            imageURL = rootURL + "comingsoon_2008_overlay_1?$product=ShopNBC/" + this.selectedImageName + "&$" + size + "x" + size + "_jpg$";
        else if (this.selectedAvailabilityStatus == 1 && !this.isAddAndSave)
            imageURL = rootURL + "soldout_2008_overlay_1?$product=ShopNBC/" + this.selectedImageName + "&$" + size + "x" + size + "_jpg$";
        else {
            if (this.product._isNewItem == true)
                imageURL = rootURL + "newtoshop_2008_overlay_1?$product=ShopNBC/" + this.selectedImageName + "&$" + size + "x" + size + "_jpg$";
            else
                imageURL = rootURL + this.selectedImageName + "?DefaultImage=1&$" + size + "x" + size + "_jpg$";
        }
        return imageURL
    },

    DisplayImage: function(imageName)//passes in the image name or the type of the object to get off of the selectedSku
    {

        var imageUrl = this.GetImageURL(imageName, 200);

        if (imageUrl && imageUrl.length > 0) {
            var img = document.getElementById(this.name + 'ProductImagePhoto');
            if (img) {

                img.src = imageUrl;
                img.alt = this.product._familyID + " - " + this.product._familyName;
                img.title = this.product._familyID + " - " + this.product._familyName;
            }

            img = document.getElementById(this.name + 'ProductImageDesc');
            if (img) {
                img.src = imageUrl;
                img.alt = this.product._familyID + " - " + this.product._familyName;
                img.title = this.product._familyID + " - " + this.product._familyName;
            }
        }
    },


    /*** Begin event handling***/
    ColorDropDownChange: function() {
        var sizeDropDown = this.GetSizeDropDown();
        var colorDropDown = this.GetColorDropDown();

        if (sizeDropDown.value != -1 || colorDropDown.value != -1) {
            var colorCode = colorDropDown.value;
            this.ClearMessages();

            this.UpdateSelectedSwatch(colorCode);

            this.RegenorateSizeDropDown(colorCode);

            this.UpdateSkuMessage();
            if (colorCode != -1)
                this.DisplayImage('swatch');
        }
        else {
            this.ResetProdduct();
        }

    },
    SetupTabs: function() {
        this.SetVideo();
        Event.observe(this.name + "PhotoTab", 'click', this.PhotoTabClick.bindAsEventListener(this));
        Event.observe(this.name + "DescTab", 'click', this.DescTabClick.bindAsEventListener(this));
        Event.observe(this.name + "VideoTab", 'click', this.VideoTabClick.bindAsEventListener(this));
    },

    PhotoTabClick: function() {
        this.SetVideo();
        dcsMultiTrack("dcsuri", "/Quickview.aspx", "WT.ti", "QuickView", "WT.si_x", "1", "WT.tx_u", "1", "WT.si_n", "purchase;quickbuy", "WT.tx_e", "v", "WT.pn_sku", this.familyId, "WT.cg_n", "QuickView", "DCSext.VideoView", "", "DCSext.ProductVideoView", "", "DCSext.tabs", "QuickView Photos");
    },
    DescTabClick: function() {
        this.SetVideo();
        dcsMultiTrack("dcsuri", "/Quickview.aspx", "WT.ti", "QuickView", "WT.si_x", "1", "WT.tx_u", "1", "WT.si_n", "purchase;quickbuy", "WT.tx_e", "v", "WT.pn_sku", this.familyId, "WT.cg_n", "QuickView", "DCSext.VideoView", "", "DCSext.ProductVideoView", "", "DCSext.tabs", "QuickView Description");
    },
    VideoTabClick: function() {
        dcsMultiTrack("dcsuri", "/QuickviewVideo.aspx", "WT.ti", "QuickViewVideo", "WT.si_x", "1", "WT.tx_u", "1", "WT.si_n", "purchase;quickbuy", "WT.tx_e", "v", "WT.pn_sku", this.familyId, "WT.cg_n", "QuickView Video Player", "DCSext.VideoView", "1", "DCSext.ProductVideoView", "1", "DCSext.tabs", "QuickView Video");
    },

    StopVideo: function() {
        var dTag = $(this.name + 'VideoFlash');
        if (dTag)
            dTag.innerHTML = '';

    },
    SetVideo: function() {
        var dTag = $(this.name + 'VideoFlash');
        if (dTag) {
            dTag.innerHTML = '';
            if (this.product._videos && this.product._videos.length > 0) {
                var videoTab = $(this.name + 'VideoTab');
                videoTab.show();
                var url = this.product._videos[0]._videoURL; // this.product._videos[0].<VideoURL>k__BackingField;

                // need to clean up the video url so that just the video file name is the video url.  Full URL will be built in the flash player.
                if (url != null && url.length > 0) {
                    var replaceText = '';

                    var index = url.lastIndexOf('/');
                    if (index > -1)
                        replaceText = url.substring(0, index + 1);
                    url = url.replace(replaceText, "");

                    index = url.indexOf('.flv');
                    if (index > -1)
                        replaceText = url.substring(index, url.length);
                    url = url.replace(replaceText, "");
                }

                var str = '';
                str = AC_FL_RunContent(
		        'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
		        'width', '403',
		        'height', '335',
		        'src', 'FlashVideoPlayer7',
		        'quality', 'best',
		        'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
		        'align', 'middle',
		        'play', 'true',
		        'loop', 'false',
		        'scale', 'showall',
		        'wmode', 'transparent',
		        'devicefont', 'false',
		        'id', 'FlashVideoPlayer7',
		        'bgcolor', '#ffffff',
		        'name', 'FlashVideoPlayer7',
		        'menu', 'false',
		        'allowFullScreen', 'false',
		        'allowScriptAccess', 'sameDomain',
		        'movie', '/flash/store1/videoplayers/productVideo_str_IE6?movie=' + url + '',
		        'salign', ''
		        ); //end AC code
                //  alert(str);
                dTag.innerHTML = str;

            }
            else {
                var videoTab = $(this.name + 'VideoTab');
                videoTab.hide();
                //Set Video display to none
            }
        }

    },


    SizeDropDownChange: function() {
        var sizeDropDown = this.GetSizeDropDown();
        var colorDropDown = this.GetColorDropDown();
        if (sizeDropDown.value != -1 || colorDropDown.value != -1) {
            var sizeCode = sizeDropDown.value;
            this.ClearMessages();
            this.RegenorateColorDropDown(sizeCode);

            this.UpdateSkuMessage();
            this.DisplayImage('swatch');
        }
        else {
            this.ResetProdduct();
        }

    },

    ProgramDropDownChange: function() {
        var autoDeliveryDropDown = this.GetProgramDropDown();

        if (this.selectedSku && autoDeliveryDropDown.value != '-2' && autoDeliveryDropDown.value != '-1') {
            for (var j = 0; j < this.selectedSku._autoDeliveryPrograms.length; j++) {
                var program = this.selectedSku._autoDeliveryPrograms[j];

                if (autoDeliveryDropDown.value == program.ProgramId) {
                    if ($(this.name + 'PriceBlockDesc')) {
                        $(this.name + 'PriceBlockDesc').innerHTML = "$" + ParseToMoneyString(program.Price);
                    }

                    if ($(this.name + 'PriceBlockPhoto')) {
                        $(this.name + 'PriceBlockPhoto').innerHTML = "$" + ParseToMoneyString(program.Price);
                    }
                }
            }
        } else {
            // reset price label to product level price
            if ($(this.name + 'PriceBlockDesc')) {
                $(this.name + 'PriceBlockDesc').innerHTML = "$" + ParseToMoneyString(this.product._currentPrice);
            }

            if ($(this.name + 'PriceBlockPhoto')) {
                $(this.name + 'PriceBlockPhoto').innerHTML = "$" + ParseToMoneyString(this.product._currentPrice);
            }
        }
    },

    RegenorateProgramDropDown: function() {
        var programDropDown = this.GetProgramDropDown();
        var sizeDropDown = this.GetSizeDropDown();
        var colorDropDown = this.GetColorDropDown();
        var programText = this.GetProgramText();

        if (programDropDown) {
            this.SetSelectedSku();
            // clear all options
            for (var o = programDropDown.options.length; o >= 0; o--)
                programDropDown[o] = null;

            // add back default options
            this.AddDropDownOption(programDropDown, 'Select Delivery Program', '-2');
            this.AddDropDownOption(programDropDown, 'Single Delivery', '-1');

            var isSizeSelected = sizeDropDown == null ? true : sizeDropDown.value > -1 ? true : false;
            var isColorSelected = colorDropDown == null ? true : colorDropDown.value > -1 ? true : false;

            // add the sku's programs if available
            if (this.selectedSku != null && isSizeSelected && isColorSelected) {
                if (this.selectedSku._autoDeliveryPrograms != null) {
                    for (var j = 0; j < this.selectedSku._autoDeliveryPrograms.length; j++) {
                        this.AddDropDownOption(programDropDown, this.selectedSku._autoDeliveryPrograms[j].ProgramName,
                            this.selectedSku._autoDeliveryPrograms[j].ProgramId);
                    }
                }
            }

            // set default selected program
            if (programDropDown.options.length > 2 && !this.isAddAndSave) {
                programDropDown.show();
                programDropDown.options[0].selected = 'selected';
                programText.show();
            } else {
                programDropDown.hide();
                programDropDown.options[programDropDown.options.length - 1].selected = 'selected';
                programText.hide();
            }
        }
    },

    SwatchSelect: function(colorCode) {
        this.ClearMessages();
        this.UpdateColorDropDown(colorCode);
        this.UpdateSelectedSwatch(colorCode);

        this.RegenorateSizeDropDown(colorCode);

        this.UpdateSkuMessage();
        this.DisplayImage('swatch');

    },

    ResetProdduct: function() {
        this.ClearMessages();
        //var quantityMessage = document.getElementById('ProductQuantity');
        // quantityMessage.innerHTML = FamilyQuantityStatus; 
        this.UpdateSelectedSwatch(-1);
        this.DisplayImage("default");
        this.RegenorateColorDropDown(-1);
        this.RegenorateSizeDropDown(-1);
        this.UpdateColorText('');
        this.UpdateSizeText('');
        this.RegenorateProgramDropDown();

    },

    //genorates the color drop down - listing items as slod out based on the sizeCode; 
    RegenorateSizeDropDown: function(colorCode) {
        this.selectedSku = null;
        colorCode = this.GetColorDropDown().value;

        var skuIndex = 0;
        var colorValue = '';
        var foundSku = false;
        for (var skuIndex = 0; skuIndex < this.skus.length; skuIndex++) {
            var sku = this.skus[skuIndex];

            if (sku._colorCode == colorCode || colorCode == -1) {
                if (this.selectedSku == null)
                    this.selectedSku = sku;
                colorValue = sku._colorValue;
                for (var sizeIndex = 0; sizeIndex < this.GetSizeDropDown().options.length; sizeIndex++) {
                    var sizeOption = this.GetSizeDropDown().options[sizeIndex];

                    if (sizeOption.value == sku._sizeCode) {
                        var status = this.GetSizeAvailability(sku._sizeCode);
                        if (status == 0)
                            sizeOption.innerHTML = sku._sizeValue;
                        else if (status == 1)
                            sizeOption.innerHTML = sku._sizeValue + " - Sold Out";
                        else
                            sizeOption.innerHTML = sku._sizeValue + " -Coming Soon";

                        if (sizeOption.selected) {
                            this.selectedAvailabilityStatus = status;
                            if (colorCode != -1) {
                                foundSku = true;
                                this.selectedSku = sku;
                            }
                        }
                        break;
                    }
                }
            }
        }

        if (colorCode != -1)
            this.UpdateColorText(colorValue)
        else
            this.UpdateColorText('')

        this.RegenorateProgramDropDown();

        return;
    },

    GetColorAvailability: function(colorCode) {//IsColorSoldOut
        var sizeCode = this.GetSizeDropDown().value;
        var availabilityStatus = 1;
        var skuIndex = 0;
        for (var skuIndex = 0; skuIndex < this.skus.length; skuIndex++) {
            if (this.skus[skuIndex]._colorCode == colorCode) {
                if (this.skus[skuIndex]._sizeCode == sizeCode || sizeCode == -1) {
                    if (this.skus[skuIndex]._availabilityStatus == 0) {
                        availabilityStatus = 0; //available 
                        break;
                    }
                    else if (this.skus[skuIndex]._availabilityStatus == 2) {
                        availabilityStatus = 2; //commming Soon
                    }
                }
            }
        }
        return availabilityStatus;
    },
    GetSizeAvailability: function(sizeCode) { //IsSizeSoldOut
        var colorCode = this.GetColorDropDown().value;

        var availabilityStatus = 1;
        var skuIndex = 0;
        for (var skuIndex = 0; skuIndex < this.skus.length; skuIndex++) {
            if (this.skus[skuIndex]._sizeCode == sizeCode) {
                if (this.skus[skuIndex]._colorCode == colorCode || colorCode == -1) {

                    if (this.skus[skuIndex]._availabilityStatus == 0) {
                        availabilityStatus = 0; //available 
                        break;
                    }
                    else if (this.skus[skuIndex]._availabilityStatus == 2) {
                        availabilityStatus = 2; //commming Soon
                    }
                }
            }
        }

        return availabilityStatus;
    },

    //genorates the size dropdown - listing items as sold out when they are sold out
    //returns the total number of the selected combination
    RegenorateColorDropDown: function(sizeCode) {
        this.selectedSku = null;
        sizeCode = this.GetSizeDropDown().value;

        var skuIndex = 0;
        var sizeValue = '';
        var foundSku = false;
        for (var skuIndex = 0; skuIndex < this.skus.length; skuIndex++) {
            sku = this.skus[skuIndex];

            if (sku._sizeCode == sizeCode || sizeCode == -1) {
                if (this.selectedSku == null)
                    this.selectedSku = sku;
                sizeValue = sku._sizeValue;

                for (var colorIndex = 0; colorIndex < this.GetColorDropDown().options.length; colorIndex++) {
                    var colorOption = this.GetColorDropDown().options[colorIndex];

                    if (colorOption.value == sku._colorCode) {
                        var status = this.GetColorAvailability(sku._colorCode);
                        if (status == 0)
                            colorOption.innerHTML = sku._colorValue;
                        else if (status == 1)
                            colorOption.innerHTML = sku._colorValue + " - Sold Out";
                        else
                            colorOption.innerHTML = sku._colorValue + " -Coming Soon";


                        if (colorOption.selected) {
                            this.selectedAvailabilityStatus = status;
                            if (sizeCode != -1) {
                                foundSku = true;
                                this.selectedSku = sku;
                            }
                        }
                        break;
                    }
                }
            }
        }
        if (sizeCode != -1)
            this.UpdateSizeText(sizeValue);
        else
            this.UpdateSizeText('');

        this.RegenorateProgramDropDown();

        return;
    },

    //displays the correcct in stock message
    //sets the isInStock variable 
    UpdateSkuMessage: function() {
        var messageArea = $(this.name + '_skuMessage');

        if (messageArea) {
            if (this.selectedAvailabilityStatus == 0) {
                messageArea.hide();
                messageArea.innerHTML = '';
            }
            else {
                messageArea.show();
                if (this.GetSizeDropDown().options.length > 1 && this.GetColorDropDown().options.length > 1) {
                    messageArea.innerHTML = 'This combination is not available';
                }
                else {
                    messageArea.innerHTML = 'This option is not available';
                }
            }
        }

    },

    UpdateSelectedSwatch: function() {
        //updates the selected swatch with the drop down selection

        var colorCode = this.GetColorDropDown().value;
        var activeSwatch = $$('img.' + this.name + 'activeSwatch');
        if (activeSwatch.length > 0 && activeSwatch[0] != null) {
            activeSwatch[0].removeClassName('activeSwatch');
            activeSwatch[0].removeClassName(this.name + 'activeSwatch');
        }

        //highlight the correct image 
        if (colorCode != -1 && $(this.name + '_swatch' + colorCode)) {
            $(this.name + '_swatch' + colorCode).addClassName('activeSwatch');
            $(this.name + '_swatch' + colorCode).addClassName(this.name + 'activeSwatch');

            if (this.swatchCarousel) //update the carousel if we have it 
            {
                var swatchIndex = 0; // colorDropDown.selectedIndex; 
                var sName = this.name + '_swatch' + colorCode;
                var existing = this.swatchCarousel.container.childElements();

                if ((existing) && existing.length > 3) {
                    for (var i = 0; i < (existing.length); i++) {
                        var imgElement = existing[i].down(1);
                        if ((imgElement) && (imgElement.id) && imgElement.id == sName) {
                            swatchIndex = i;
                            break;
                        }
                    }
                }
                if (this.swatchCarousel.currentIndex() + 3 <= swatchIndex || this.swatchCarousel.currentIndex() + 1 > swatchIndex) //if we can't see it 
                {
                    this.swatchCarousel.scrollTo(swatchIndex);
                }
            }
        }
    },

    //sets the colordropdown to the color code 
    UpdateColorDropDown: function(colorCode) {
        for (var colorIndex = 0; colorIndex < this.GetColorDropDown().options.length; colorIndex++) {
            if (this.GetColorDropDown().options[colorIndex].value) {
                if (this.GetColorDropDown().options[colorIndex].value == colorCode)
                    this.GetColorDropDown().options[colorIndex].selected = true;
                else
                    this.GetColorDropDown().options[colorIndex].selected = false;
            }
        }
    },

    //use the selectedSku object to update text 
    UpdateColorText: function(colorValue) {
        var textarea = document.getElementById(this.name + 'SelectedColor');
        if (textarea)
            textarea.innerHTML = colorValue;
    },

    //use the selectedSku object to update text 
    UpdateSizeText: function(sizeValue) {
        var textarea = document.getElementById(this.name + 'SelectedSize');
        if (textarea)
            textarea.innerHTML = sizeValue;
    },

    ClearMessages: function() {
        var messageArea = document.getElementById(this.name + '_skuMessage');
        if (messageArea) {
            messageArea.style.display = 'none';
            messageArea.innerHTML = '';
        }
    },

    SetSelectedSku: function() {
        var success = false;
        if (this.product != null) {
            var colorSelect = this.GetColorDropDown().value;
            var sizeSelect = this.GetSizeDropDown().value;

            selectedSku = null;
            var skuMessage = document.getElementById(this.name + '_skuMessage');
            if (skuMessage)
                skuMessage.style.display = "";
            if (sizeSelect > -1 && colorSelect > -1) {
                if (skuMessage) {
                    skuMessage.style.display = "none";
                    skuMessage.innerHTML = '';
                }

                for (var i = 0; i < this.skus.length; i++) {
                    if (this.skus[i]._colorCode == colorSelect && this.skus[i]._sizeCode == sizeSelect) {
                        this.selectedSku = this.skus[i];
                        if (this.skus[i]._availabilityStatus == 0)
                            success = true;
                        else
                            this.UpdateSkuMessage();
                        break;
                    }
                }
            }
            else if (!(sizeSelect > -1) && (colorSelect > -1)) {
                if (skuMessage)
                    skuMessage.innerHTML = "Please select size";
            }
            else if ((sizeSelect > -1) && !(colorSelect > -1)) {
                if (skuMessage)
                    skuMessage.innerHTML = "Please select color";
            }
            else if (!(sizeSelect > -1) && !(colorSelect > -1)) {
                if (skuMessage)
                    skuMessage.innerHTML = "Please select color and size";
            }
        }
        return success;
    },

    AddAndSaveType: function(typeId) {
        if (typeId == null)
            typeId = this.promoTypeId;
        if (typeId == 3)
            return 'GWP';
        else if (typeId == 4)
            return 'PWP';
        else
            return '';
    },

    MakeAjaxCall: function(url) {
        if (typeof XMLHttpRequest != 'undefined')
            req = new XMLHttpRequest();
        else if (window.ActiveXObject)
            req = new ActiveXObject("Microsoft.XMLHTTP"); 	// code for IE6, IE5
        if (req != null) {
            this.closePopup(true);
            req.open("GET", url, true);
            req.onreadystatechange = CartCallBack;
            req.send(null);
            return true;
        }
        else
            return false;
    },

    /********* Add/Accept/Decline Functions *************/

    AddProductToCart: function() {
        if (this.SetSelectedSku() && this.selectedSku) {
            if (this.ValidateProgramDelivery()) {
                var programString = "";
                if (this.GetProgramDropDown() != null && this.GetProgramDropDown().value != '-2' && this.GetProgramDropDown().value != '-1') {
                    programString = "&programcode=" + this.GetProgramDropDown().value;
                }


                var url = '/product/?action=addtocart&familyid=' + this.product._familyID + '&selectedsku=' + this.selectedSku._id + '&qty=1' + programString + '&rand=' + (new Date()).getMilliseconds();


                if (this.MakeAjaxCall(url)) {
                    adjustQuantity = true;
                    showCartFlyout = true;
                    dcsMultiTrack("dcsuri", "/Quickview.aspx", "WT.ti", "QuickView", "WT.si_x", "1", "WT.tx_u", "1", "WT.si_n", "purchase", "WT.tx_e", "a", "WT.pn_sku", this.familyId, "WT.cg_n", "QuickView", "DCSext.VideoView", "", "DCSext.ProductVideoView", "", "DCSext.tabs", "");
                }
            }
        }
    },

    ValidateProgramDelivery: function() {
        //assume it is valid.
        var valid = false;
        var noDeliverySeleced = false;
        //Check to make sure the autoDelviery was selected on this item
        if (this.GetProgramDropDown() != null && this.GetProgramDropDown().value == -2) {
            noDeliverySeleced = true;
        }
        else if (this.GetProgramDropDown() != null && this.GetProgramDropDown().value > -1) {
            //check to make sure we have an auto delivery on this item
            if (this.selectedSku._autoDeliveryPrograms != null) {

                for (var i = 0; i < this.selectedSku._autoDeliveryPrograms.length; i++) {

                    if (this.selectedSku._autoDeliveryPrograms[i].ProgramId == this.GetProgramDropDown().value) {
                        //we found our program
                        valid = true;
                        break;
                    }
                }
            }

        }
        else {
            valid = true;
        }

        if (!valid) {
            if (noDeliverySeleced)
                alert('Please select a Delivery Type');
            else
                alert("invalid program selected");
        }

        return valid;
    },


    QuickBuyProduct: function() {
        if (this.SetSelectedSku() && this.selectedSku) {
            isQuickBuy = true;

            var url = '/product/?action=quickbuy&familyid=' + this.product._familyID + '&selectedsku=' + this.selectedSku._id + '&qty=1' + '&rand=' + (new Date()).getMilliseconds();

            if (this.MakeAjaxCall(url)) {
                adjustQuantity = false;
                showCartFlyout = false;
                //dcsMultiTrack("dcsuri","/Quickview.aspx","WT.ti","QuickView","WT.si_x","1","WT.tx_u","1","WT.si_n","purchase;quickview","WT.tx_e","a","WT.pn_sku",this.familyId,"WT.cg_n","QuickView","DCSext.VideoView","","DCSext.ProductVideoView","","DCSext.tabs","")
            }
        }
    },

    AcceptAddAndSave: function() {
        if (this.SetSelectedSku() && this.isAddAndSave) {
            var url;
            if (isQuickBuy) {
                url = '/product/?action=ViewAddAndSaveAcceptQuickBuy&familyid=' + quickView.parentFamilyId + '&addandsavefamilyid=' + quickView.familyId + '&selectedsku=' + quickView.selectedSku._id + '&qty=1&addandsavepromoid=' + this.promoId + '&addandsavetype=' + this.AddAndSaveType() + '&rand=' + (new Date()).getMilliseconds();
            }
            else {
                url = '/product/?action=ViewAddAndSaveAccept&familyid=' + quickView.parentFamilyId + '&addandsavefamilyid=' + quickView.familyId + '&selectedsku=' + quickView.selectedSku._id + '&qty=1&addandsavepromoid=' + this.promoId + '&addandsavetype=' + this.AddAndSaveType() + '&rand=' + (new Date()).getMilliseconds();
                adjustQuantity = true;
                showCartFlyout = true;
            }
            this.MakeAjaxCall(url)
        }
    },

    DeclineAddAndSave: function() {
        if (this.isAddAndSave) {

            if (isQuickBuy) {
                url = '/product/?action=ViewAddAndSaveDeclineQuickBuy&familyid=' + quickView.parentFamilyId + '&addandsavefamilyid=' + quickView.familyId + '&selectedsku=' + quickView.skus[0]._id + '&qty=1&addandsavepromoid=' + this.promoId + '&addandsavetype=' + this.AddAndSaveType() + '&rand=' + (new Date()).getMilliseconds();
            }
            else {
                url = '/product/?action=ViewAddAndSaveDecline&familyid=' + quickView.parentFamilyId + '&addandsavefamilyid=' + quickView.familyId + '&selectedsku=' + quickView.skus[0]._id + '&qty=1&addandsavepromoid=' + this.promoId + '&addandsavetype=' + this.AddAndSaveType() + '&rand=' + (new Date()).getMilliseconds();
                showCartFlyout = true;
                adjustQuantity = false;

            }
            this.MakeAjaxCall(url)
        }
    }


});


var isQuickBuy = false; 

function SetUpQuickViewData(product)
{
    quickView.SetUpQuickViewData(product)
}

    
    function ParseToMoneyString(n)
    {
	    var t = parseInt(n / 1000); 
		var h = parseInt(n - t *1000); 
		var d = Math.round((n - (t*1000) - h)*100);

		if(d<1)
			d = '00'; 
		else if(d<10)
			d = '0' + d; 

		if(t>0)
		{
			t = t + ','; 
			if (h <1 )
				h = '00' + h ; 
			else if (h < 100)
				h = '0' + h ; 
		}
		else
			t = ''; 

		return t + h + '.' + d;
    }

var browser = GetBrowserInfo();

var quickviewBigURL = '';
var quickviewSmlURL = '';

if(browser.objIE.IsIE && (browser.objIE.IsIE4 || browser.objIE.IsIE5 || browser.objIE.IsIE5_5 || browser.objIE.IsIE6)) {
    // does not support png images, use gif
    quickviewBigURL = '/images/global/listing/btn_quickView_ie6.gif';
    quickviewSmlURL = '/images/global/listing/btn_OTQuickView_listing.gif';
} else {
    // supports png images
    quickviewBigURL = '/images/global/listing/btn_quickView.png';
    quickviewSmlURL = '/images/global/listing/btn_quickView_sml.png';
}

var activeImage = ''; 
var image =  new Class.create({
	initialize: function(id) {
		this.id = id;
		this.isListingButton = false; 
		this.familyId=id.substring(5);
		if($(id))
		{
			Event.observe(id,'mouseover', this.OverlayShow);
			Event.observe(id,'mouseout', this.OverlayHide);
		}
	},
	OverlayShow: function(event){
	    var imageoffset = 39; 
	    var cImg = $(this.id);
	    var imageXoffSet = 0;
	    
	    if (cImg.width == 140 && cImg.height == 140)
	    {
	        // Large product image (e.g. gallery)
	        imageXoffSet = -30;
	    } 
	    else if (cImg.width == 120 && cImg.height == 120)
	    {
	        // Medium size image (e.g. Last items aired)
	        imageXoffSet = 0
	    }
	    else
	    {
	        imageXoffSet = 0;    
	    } 
	    
	    if(cImg.width > 100 )
	    {
	        //alert(quickviewBigURL);
            $(quickViewButton.id+'Img').src = quickviewBigURL;
            this.isListingButton = false;
            imageoffset = 42             
        }
	    else
	    {
	        //alert(quickviewSmlURL);
	        $(quickViewButton.id+'Img').src = quickviewSmlURL; 
	        this.isListingButton = true; 
	    }

	    
		var imagepos = $(this.id).viewportOffset();
		var windowPos = document.viewport.getScrollOffsets();
		var x = ((imagepos.top+windowPos.top+$(this.id).height*.70)+imageXoffSet  );
		var y = imagepos.left+windowPos.left+$(this.id).width*.50 - imageoffset;
		$(quickViewButton.id).style.top = x + 'px';
		$(quickViewButton.id).style.left= y + 'px';
	
		$(quickViewButton.id).show();
		activeImage = this.id; 
		},
	OverlayHide: function (event){
		$(quickViewButton.id).hide();
	 
	}
});
var underlay = null; 

function SetOverlayForImages(){

		underlay = new image('underlay');
		var images = $$('img');
		for(var i = 0; i<images.length; i++)
		{
			if(images[i].name == 'imgProduct')
			{
			new image(images[i].id);
			}
		}
	}
	Event.observe(window,'load',SetOverlayForImages);


var button =  new Class.create({

	initialize: function(id) {
		this.id = id; 
		Event.observe(this.id,'mouseover', this.highlight);
		Event.observe(this.id,'mouseout', this.unhighlight);
		Event.observe(this.id,'click', this.click);
		$(this.id).hide();
	},
	highlight: function(event){
		//$('activeImage').stopObserving('mouseover', underlay.OverlayShow);	
		$(activeImage).stopObserving('mouseout', underlay.OverlayHide);
		
		$(this.id).show()
		//$(this.id + 'Img').src = "/images/stylesheetimgs/btn_quickViewOver.gif"; 
		//$(this.id + 'Img').src = "/images/global/listing/btn_OTQuickView.gif"; 
		
		},
	unhighlight: function (event){
		//$(this.id + 'Img').src = "/images/stylesheetimgs/btn_quickView.gif";
		//$(this.id + 'Img').src = "/images/global/listing/btn_OTQuickView.gif";
		$(activeImage).observe('mouseout', underlay.OverlayHide);
		//$(activeImage).observe('mouseout', underlay.OverlayShow); 
	},
	click: function(event){
		var title = $(activeImage).title; 
		if (title.length == 0)
		{
		   title = $(activeImage).alt; 
		}
		var familyId = title.substring(0,title.indexOf(' ')); 
		quickView.LoadQuickView(familyId, event);
	}
});

// script.aculo.us dragdrop.js v1.8.0, Tue Nov 06 15:01:40 +0300 2007

// Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
//           (c) 2005-2007 Sammi Williams (http://www.oriontransfer.co.nz, sammi@oriontransfer.co.nz)
// 
// script.aculo.us is freely distributable under the terms of an MIT-style license.
// For details, see the script.aculo.us web site: http://script.aculo.us/

if(Object.isUndefined(Effect))
  throw("dragdrop.js requires including script.aculo.us' effects.js library");

var Droppables = {
  drops: [],

  remove: function(element) {
    this.drops = this.drops.reject(function(d) { return d.element==$(element) });
  },

  add: function(element) {
    element = $(element);
    var options = Object.extend({
      greedy:     true,
      hoverclass: null,
      tree:       false
    }, arguments[1] || { });

    // cache containers
    if(options.containment) {
      options._containers = [];
      var containment = options.containment;
      if(Object.isArray(containment)) {
        containment.each( function(c) { options._containers.push($(c)) });
      } else {
        options._containers.push($(containment));
      }
    }
    
    if(options.accept) options.accept = [options.accept].flatten();

    Element.makePositioned(element); // fix IE
    options.element = element;

    this.drops.push(options);
  },
  
  findDeepestChild: function(drops) {
    deepest = drops[0];
      
    for (i = 1; i < drops.length; ++i)
      if (Element.isParent(drops[i].element, deepest.element))
        deepest = drops[i];
    
    return deepest;
  },

  isContained: function(element, drop) {
    var containmentNode;
    if(drop.tree) {
      containmentNode = element.treeNode; 
    } else {
      containmentNode = element.parentNode;
    }
    return drop._containers.detect(function(c) { return containmentNode == c });
  },
  
  isAffected: function(point, element, drop) {
    return (
      (drop.element!=element) &&
      ((!drop._containers) ||
        this.isContained(element, drop)) &&
      ((!drop.accept) ||
        (Element.classNames(element).detect( 
          function(v) { return drop.accept.include(v) } ) )) &&
      Position.within(drop.element, point[0], point[1]) );
  },

  deactivate: function(drop) {
    if(drop.hoverclass)
      Element.removeClassName(drop.element, drop.hoverclass);
    this.last_active = null;
  },

  activate: function(drop) {
    if(drop.hoverclass)
      Element.addClassName(drop.element, drop.hoverclass);
    this.last_active = drop;
  },

  show: function(point, element) {
    if(!this.drops.length) return;
    var drop, affected = [];
    
    this.drops.each( function(drop) {
      if(Droppables.isAffected(point, element, drop))
        affected.push(drop);
    });
        
    if(affected.length>0)
      drop = Droppables.findDeepestChild(affected);

    if(this.last_active && this.last_active != drop) this.deactivate(this.last_active);
    if (drop) {
      Position.within(drop.element, point[0], point[1]);
      if(drop.onHover)
        drop.onHover(element, drop.element, Position.overlap(drop.overlap, drop.element));
      
      if (drop != this.last_active) Droppables.activate(drop);
    }
  },

  fire: function(event, element) {
    if(!this.last_active) return;
    Position.prepare();

    if (this.isAffected([Event.pointerX(event), Event.pointerY(event)], element, this.last_active))
      if (this.last_active.onDrop) {
        this.last_active.onDrop(element, this.last_active.element, event); 
        return true; 
      }
  },

  reset: function() {
    if(this.last_active)
      this.deactivate(this.last_active);
  }
}

var Draggables = {
  drags: [],
  observers: [],
  
  register: function(draggable) {
    if(this.drags.length == 0) {
      this.eventMouseUp   = this.endDrag.bindAsEventListener(this);
      this.eventMouseMove = this.updateDrag.bindAsEventListener(this);
      this.eventKeypress  = this.keyPress.bindAsEventListener(this);
      
      Event.observe(document, "mouseup", this.eventMouseUp);
      Event.observe(document, "mousemove", this.eventMouseMove);
      Event.observe(document, "keypress", this.eventKeypress);
    }
    this.drags.push(draggable);
  },
  
  unregister: function(draggable) {
    this.drags = this.drags.reject(function(d) { return d==draggable });
    if(this.drags.length == 0) {
      Event.stopObserving(document, "mouseup", this.eventMouseUp);
      Event.stopObserving(document, "mousemove", this.eventMouseMove);
      Event.stopObserving(document, "keypress", this.eventKeypress);
    }
  },
  
  activate: function(draggable) {
    if(draggable.options.delay) { 
      this._timeout = setTimeout(function() { 
        Draggables._timeout = null; 
        window.focus(); 
        Draggables.activeDraggable = draggable; 
      }.bind(this), draggable.options.delay); 
    } else {
      window.focus(); // allows keypress events if window isn't currently focused, fails for Safari
      this.activeDraggable = draggable;
    }
  },
  
  deactivate: function() {
    this.activeDraggable = null;
  },
  
  updateDrag: function(event) {
    if(!this.activeDraggable) return;
    var pointer = [Event.pointerX(event), Event.pointerY(event)];
    // Mozilla-based browsers fire successive mousemove events with
    // the same coordinates, prevent needless redrawing (moz bug?)
    if(this._lastPointer && (this._lastPointer.inspect() == pointer.inspect())) return;
    this._lastPointer = pointer;
    
    this.activeDraggable.updateDrag(event, pointer);
  },
  
  endDrag: function(event) {
    if(this._timeout) { 
      clearTimeout(this._timeout); 
      this._timeout = null; 
    }
    if(!this.activeDraggable) return;
    this._lastPointer = null;
    this.activeDraggable.endDrag(event);
    this.activeDraggable = null;
  },
  
  keyPress: function(event) {
    if(this.activeDraggable)
      this.activeDraggable.keyPress(event);
  },
  
  addObserver: function(observer) {
    this.observers.push(observer);
    this._cacheObserverCallbacks();
  },
  
  removeObserver: function(element) {  // element instead of observer fixes mem leaks
    this.observers = this.observers.reject( function(o) { return o.element==element });
    this._cacheObserverCallbacks();
  },
  
  notify: function(eventName, draggable, event) {  // 'onStart', 'onEnd', 'onDrag'
    if(this[eventName+'Count'] > 0)
      this.observers.each( function(o) {
        if(o[eventName]) o[eventName](eventName, draggable, event);
      });
    if(draggable.options[eventName]) draggable.options[eventName](draggable, event);
  },
  
  _cacheObserverCallbacks: function() {
    ['onStart','onEnd','onDrag'].each( function(eventName) {
      Draggables[eventName+'Count'] = Draggables.observers.select(
        function(o) { return o[eventName]; }
      ).length;
    });
  }
}

/*--------------------------------------------------------------------------*/

var Draggable = Class.create({
  initialize: function(element) {
    var defaults = {
      handle: false,
      reverteffect: function(element, top_offset, left_offset) {
        var dur = Math.sqrt(Math.abs(top_offset^2)+Math.abs(left_offset^2))*0.02;
        new Effect.Move(element, { x: -left_offset, y: -top_offset, duration: dur,
          queue: {scope:'_draggable', position:'end'}
        });
      },
      endeffect: function(element) {
        var toOpacity = Object.isNumber(element._opacity) ? element._opacity : 1.0;
        new Effect.Opacity(element, {duration:0.2, from:0.7, to:toOpacity, 
          queue: {scope:'_draggable', position:'end'},
          afterFinish: function(){ 
            Draggable._dragging[element] = false 
          }
        }); 
      },
      zindex: 1000,
      revert: false,
      quiet: false,
      scroll: false,
      scrollSensitivity: 20,
      scrollSpeed: 15,
      snap: false,  // false, or xy or [x,y] or function(x,y){ return [x,y] }
      delay: 0
    };
    
    if(!arguments[1] || Object.isUndefined(arguments[1].endeffect))
      Object.extend(defaults, {
        starteffect: function(element) {
          element._opacity = Element.getOpacity(element);
          Draggable._dragging[element] = true;
          new Effect.Opacity(element, {duration:0.2, from:element._opacity, to:0.7}); 
        }
      });
    
    var options = Object.extend(defaults, arguments[1] || { });

    this.element = $(element);
    
    if(options.handle && Object.isString(options.handle))
      this.handle = this.element.down('.'+options.handle, 0);
    
    if(!this.handle) this.handle = $(options.handle);
    if(!this.handle) this.handle = this.element;
    
    if(options.scroll && !options.scroll.scrollTo && !options.scroll.outerHTML) {
      options.scroll = $(options.scroll);
      this._isScrollChild = Element.childOf(this.element, options.scroll);
    }

    Element.makePositioned(this.element); // fix IE    

    this.options  = options;
    this.dragging = false;   

    this.eventMouseDown = this.initDrag.bindAsEventListener(this);
    Event.observe(this.handle, "mousedown", this.eventMouseDown);
    
    Draggables.register(this);
  },
  
  destroy: function() {
    Event.stopObserving(this.handle, "mousedown", this.eventMouseDown);
    Draggables.unregister(this);
  },
  
  currentDelta: function() {
    return([
      parseInt(Element.getStyle(this.element,'left') || '0'),
      parseInt(Element.getStyle(this.element,'top') || '0')]);
  },
  
  initDrag: function(event) {
    if(!Object.isUndefined(Draggable._dragging[this.element]) &&
      Draggable._dragging[this.element]) return;
    if(Event.isLeftClick(event)) {    
      // abort on form elements, fixes a Firefox issue
      var src = Event.element(event);
      if((tag_name = src.tagName.toUpperCase()) && (
        tag_name=='INPUT' ||
        tag_name=='SELECT' ||
        tag_name=='OPTION' ||
        tag_name=='BUTTON' ||
        tag_name=='TEXTAREA')) return;
        
      var pointer = [Event.pointerX(event), Event.pointerY(event)];
      var pos     = Position.cumulativeOffset(this.element);
      this.offset = [0,1].map( function(i) { return (pointer[i] - pos[i]) });
      
      Draggables.activate(this);
      Event.stop(event);
    }
  },
  
  startDrag: function(event) {
    this.dragging = true;
    if(!this.delta)
      this.delta = this.currentDelta();
    
    if(this.options.zindex) {
      this.originalZ = parseInt(Element.getStyle(this.element,'z-index') || 0);
      this.element.style.zIndex = this.options.zindex;
    }
    
    if(this.options.ghosting) {
      this._clone = this.element.cloneNode(true);
      this.element._originallyAbsolute = (this.element.getStyle('position') == 'absolute');
      if (!this.element._originallyAbsolute)
        Position.absolutize(this.element);
      this.element.parentNode.insertBefore(this._clone, this.element);
    }
    
    if(this.options.scroll) {
      if (this.options.scroll == window) {
        var where = this._getWindowScroll(this.options.scroll);
        this.originalScrollLeft = where.left;
        this.originalScrollTop = where.top;
      } else {
        this.originalScrollLeft = this.options.scroll.scrollLeft;
        this.originalScrollTop = this.options.scroll.scrollTop;
      }
    }
    
    Draggables.notify('onStart', this, event);
        
    if(this.options.starteffect) this.options.starteffect(this.element);
  },
  
  updateDrag: function(event, pointer) {
    if(!this.dragging) this.startDrag(event);
    
    if(!this.options.quiet){
      Position.prepare();
      Droppables.show(pointer, this.element);
    }
    
    Draggables.notify('onDrag', this, event);
    
    this.draw(pointer);
    if(this.options.change) this.options.change(this);
    
    if(this.options.scroll) {
      this.stopScrolling();
      
      var p;
      if (this.options.scroll == window) {
        with(this._getWindowScroll(this.options.scroll)) { p = [ left, top, left+width, top+height ]; }
      } else {
        p = Position.page(this.options.scroll);
        p[0] += this.options.scroll.scrollLeft + Position.deltaX;
        p[1] += this.options.scroll.scrollTop + Position.deltaY;
        p.push(p[0]+this.options.scroll.offsetWidth);
        p.push(p[1]+this.options.scroll.offsetHeight);
      }
      var speed = [0,0];
      if(pointer[0] < (p[0]+this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[0]+this.options.scrollSensitivity);
      if(pointer[1] < (p[1]+this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[1]+this.options.scrollSensitivity);
      if(pointer[0] > (p[2]-this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[2]-this.options.scrollSensitivity);
      if(pointer[1] > (p[3]-this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[3]-this.options.scrollSensitivity);
      this.startScrolling(speed);
    }
    
    // fix AppleWebKit rendering
    if(Prototype.Browser.WebKit) window.scrollBy(0,0);
    
    Event.stop(event);
  },
  
  finishDrag: function(event, success) {
    this.dragging = false;
    
    if(this.options.quiet){
      Position.prepare();
      var pointer = [Event.pointerX(event), Event.pointerY(event)];
      Droppables.show(pointer, this.element);
    }

    if(this.options.ghosting) {
      if (!this.element._originallyAbsolute)
        Position.relativize(this.element);
      delete this.element._originallyAbsolute;
      Element.remove(this._clone);
      this._clone = null;
    }

    var dropped = false; 
    if(success) { 
      dropped = Droppables.fire(event, this.element); 
      if (!dropped) dropped = false; 
    }
    if(dropped && this.options.onDropped) this.options.onDropped(this.element);
    Draggables.notify('onEnd', this, event);

    var revert = this.options.revert;
    if(revert && Object.isFunction(revert)) revert = revert(this.element);
    
    var d = this.currentDelta();
    if(revert && this.options.reverteffect) {
      if (dropped == 0 || revert != 'failure')
        this.options.reverteffect(this.element,
          d[1]-this.delta[1], d[0]-this.delta[0]);
    } else {
      this.delta = d;
    }

    if(this.options.zindex)
      this.element.style.zIndex = this.originalZ;

    if(this.options.endeffect) 
      this.options.endeffect(this.element);
      
    Draggables.deactivate(this);
    Droppables.reset();
  },
  
  keyPress: function(event) {
    if(event.keyCode!=Event.KEY_ESC) return;
    this.finishDrag(event, false);
    Event.stop(event);
  },
  
  endDrag: function(event) {
    if(!this.dragging) return;
    this.stopScrolling();
    this.finishDrag(event, true);
    Event.stop(event);
  },
  
  draw: function(point) {
    var pos = Position.cumulativeOffset(this.element);
    if(this.options.ghosting) {
      var r   = Position.realOffset(this.element);
      pos[0] += r[0] - Position.deltaX; pos[1] += r[1] - Position.deltaY;
    }
    
    var d = this.currentDelta();
    pos[0] -= d[0]; pos[1] -= d[1];
    
    if(this.options.scroll && (this.options.scroll != window && this._isScrollChild)) {
      pos[0] -= this.options.scroll.scrollLeft-this.originalScrollLeft;
      pos[1] -= this.options.scroll.scrollTop-this.originalScrollTop;
    }
    
    var p = [0,1].map(function(i){ 
      return (point[i]-pos[i]-this.offset[i]) 
    }.bind(this));
    
    if(this.options.snap) {
      if(Object.isFunction(this.options.snap)) {
        p = this.options.snap(p[0],p[1],this);
      } else {
      if(Object.isArray(this.options.snap)) {
        p = p.map( function(v, i) {
          return (v/this.options.snap[i]).round()*this.options.snap[i] }.bind(this))
      } else {
        p = p.map( function(v) {
          return (v/this.options.snap).round()*this.options.snap }.bind(this))
      }
    }}
    
    var style = this.element.style;
    if((!this.options.constraint) || (this.options.constraint=='horizontal'))
      style.left = p[0] + "px";
    if((!this.options.constraint) || (this.options.constraint=='vertical'))
      style.top  = p[1] + "px";
    
    if(style.visibility=="hidden") style.visibility = ""; // fix gecko rendering
  },
  
  stopScrolling: function() {
    if(this.scrollInterval) {
      clearInterval(this.scrollInterval);
      this.scrollInterval = null;
      Draggables._lastScrollPointer = null;
    }
  },
  
  startScrolling: function(speed) {
    if(!(speed[0] || speed[1])) return;
    this.scrollSpeed = [speed[0]*this.options.scrollSpeed,speed[1]*this.options.scrollSpeed];
    this.lastScrolled = new Date();
    this.scrollInterval = setInterval(this.scroll.bind(this), 10);
  },
  
  scroll: function() {
    var current = new Date();
    var delta = current - this.lastScrolled;
    this.lastScrolled = current;
    if(this.options.scroll == window) {
      with (this._getWindowScroll(this.options.scroll)) {
        if (this.scrollSpeed[0] || this.scrollSpeed[1]) {
          var d = delta / 1000;
          this.options.scroll.scrollTo( left + d*this.scrollSpeed[0], top + d*this.scrollSpeed[1] );
        }
      }
    } else {
      this.options.scroll.scrollLeft += this.scrollSpeed[0] * delta / 1000;
      this.options.scroll.scrollTop  += this.scrollSpeed[1] * delta / 1000;
    }
    
    Position.prepare();
    Droppables.show(Draggables._lastPointer, this.element);
    Draggables.notify('onDrag', this);
    if (this._isScrollChild) {
      Draggables._lastScrollPointer = Draggables._lastScrollPointer || $A(Draggables._lastPointer);
      Draggables._lastScrollPointer[0] += this.scrollSpeed[0] * delta / 1000;
      Draggables._lastScrollPointer[1] += this.scrollSpeed[1] * delta / 1000;
      if (Draggables._lastScrollPointer[0] < 0)
        Draggables._lastScrollPointer[0] = 0;
      if (Draggables._lastScrollPointer[1] < 0)
        Draggables._lastScrollPointer[1] = 0;
      this.draw(Draggables._lastScrollPointer);
    }
    
    if(this.options.change) this.options.change(this);
  },
  
  _getWindowScroll: function(w) {
    var T, L, W, H;
    with (w.document) {
      if (w.document.documentElement && documentElement.scrollTop) {
        T = documentElement.scrollTop;
        L = documentElement.scrollLeft;
      } else if (w.document.body) {
        T = body.scrollTop;
        L = body.scrollLeft;
      }
      if (w.innerWidth) {
        W = w.innerWidth;
        H = w.innerHeight;
      } else if (w.document.documentElement && documentElement.clientWidth) {
        W = documentElement.clientWidth;
        H = documentElement.clientHeight;
      } else {
        W = body.offsetWidth;
        H = body.offsetHeight
      }
    }
    return { top: T, left: L, width: W, height: H };
  }
});

Draggable._dragging = { };

/*--------------------------------------------------------------------------*/

var SortableObserver = Class.create({
  initialize: function(element, observer) {
    this.element   = $(element);
    this.observer  = observer;
    this.lastValue = Sortable.serialize(this.element);
  },
  
  onStart: function() {
    this.lastValue = Sortable.serialize(this.element);
  },
  
  onEnd: function() {
    Sortable.unmark();
    if(this.lastValue != Sortable.serialize(this.element))
      this.observer(this.element)
  }
});

var Sortable = {
  SERIALIZE_RULE: /^[^_\-](?:[A-Za-z0-9\-\_]*)[_](.*)$/,
  
  sortables: { },
  
  _findRootElement: function(element) {
    while (element.tagName.toUpperCase() != "BODY") {  
      if(element.id && Sortable.sortables[element.id]) return element;
      element = element.parentNode;
    }
  },

  options: function(element) {
    element = Sortable._findRootElement($(element));
    if(!element) return;
    return Sortable.sortables[element.id];
  },
  
  destroy: function(element){
    var s = Sortable.options(element);
    
    if(s) {
      Draggables.removeObserver(s.element);
      s.droppables.each(function(d){ Droppables.remove(d) });
      s.draggables.invoke('destroy');
      
      delete Sortable.sortables[s.element.id];
    }
  },

  create: function(element) {
    element = $(element);
    var options = Object.extend({ 
      element:     element,
      tag:         'li',       // assumes li children, override with tag: 'tagname'
      dropOnEmpty: false,
      tree:        false,
      treeTag:     'ul',
      overlap:     'vertical', // one of 'vertical', 'horizontal'
      constraint:  'vertical', // one of 'vertical', 'horizontal', false
      containment: element,    // also takes array of elements (or id's); or false
      handle:      false,      // or a CSS class
      only:        false,
      delay:       0,
      hoverclass:  null,
      ghosting:    false,
      quiet:       false, 
      scroll:      false,
      scrollSensitivity: 20,
      scrollSpeed: 15,
      format:      this.SERIALIZE_RULE,
      
      // these take arrays of elements or ids and can be 
      // used for better initialization performance
      elements:    false,
      handles:     false,
      
      onChange:    Prototype.emptyFunction,
      onUpdate:    Prototype.emptyFunction
    }, arguments[1] || { });

    // clear any old sortable with same element
    this.destroy(element);

    // build options for the draggables
    var options_for_draggable = {
      revert:      true,
      quiet:       options.quiet,
      scroll:      options.scroll,
      scrollSpeed: options.scrollSpeed,
      scrollSensitivity: options.scrollSensitivity,
      delay:       options.delay,
      ghosting:    options.ghosting,
      constraint:  options.constraint,
      handle:      options.handle };

    if(options.starteffect)
      options_for_draggable.starteffect = options.starteffect;

    if(options.reverteffect)
      options_for_draggable.reverteffect = options.reverteffect;
    else
      if(options.ghosting) options_for_draggable.reverteffect = function(element) {
        element.style.top  = 0;
        element.style.left = 0;
      };

    if(options.endeffect)
      options_for_draggable.endeffect = options.endeffect;

    if(options.zindex)
      options_for_draggable.zindex = options.zindex;

    // build options for the droppables  
    var options_for_droppable = {
      overlap:     options.overlap,
      containment: options.containment,
      tree:        options.tree,
      hoverclass:  options.hoverclass,
      onHover:     Sortable.onHover
    }
    
    var options_for_tree = {
      onHover:      Sortable.onEmptyHover,
      overlap:      options.overlap,
      containment:  options.containment,
      hoverclass:   options.hoverclass
    }

    // fix for gecko engine
    Element.cleanWhitespace(element); 

    options.draggables = [];
    options.droppables = [];

    // drop on empty handling
    if(options.dropOnEmpty || options.tree) {
      Droppables.add(element, options_for_tree);
      options.droppables.push(element);
    }

    (options.elements || this.findElements(element, options) || []).each( function(e,i) {
      var handle = options.handles ? $(options.handles[i]) :
        (options.handle ? $(e).select('.' + options.handle)[0] : e); 
      options.draggables.push(
        new Draggable(e, Object.extend(options_for_draggable, { handle: handle })));
      Droppables.add(e, options_for_droppable);
      if(options.tree) e.treeNode = element;
      options.droppables.push(e);      
    });
    
    if(options.tree) {
      (Sortable.findTreeElements(element, options) || []).each( function(e) {
        Droppables.add(e, options_for_tree);
        e.treeNode = element;
        options.droppables.push(e);
      });
    }

    // keep reference
    this.sortables[element.id] = options;

    // for onupdate
    Draggables.addObserver(new SortableObserver(element, options.onUpdate));

  },

  // return all suitable-for-sortable elements in a guaranteed order
  findElements: function(element, options) {
    return Element.findChildren(
      element, options.only, options.tree ? true : false, options.tag);
  },
  
  findTreeElements: function(element, options) {
    return Element.findChildren(
      element, options.only, options.tree ? true : false, options.treeTag);
  },

  onHover: function(element, dropon, overlap) {
    if(Element.isParent(dropon, element)) return;

    if(overlap > .33 && overlap < .66 && Sortable.options(dropon).tree) {
      return;
    } else if(overlap>0.5) {
      Sortable.mark(dropon, 'before');
      if(dropon.previousSibling != element) {
        var oldParentNode = element.parentNode;
        element.style.visibility = "hidden"; // fix gecko rendering
        dropon.parentNode.insertBefore(element, dropon);
        if(dropon.parentNode!=oldParentNode) 
          Sortable.options(oldParentNode).onChange(element);
        Sortable.options(dropon.parentNode).onChange(element);
      }
    } else {
      Sortable.mark(dropon, 'after');
      var nextElement = dropon.nextSibling || null;
      if(nextElement != element) {
        var oldParentNode = element.parentNode;
        element.style.visibility = "hidden"; // fix gecko rendering
        dropon.parentNode.insertBefore(element, nextElement);
        if(dropon.parentNode!=oldParentNode) 
          Sortable.options(oldParentNode).onChange(element);
        Sortable.options(dropon.parentNode).onChange(element);
      }
    }
  },
  
  onEmptyHover: function(element, dropon, overlap) {
    var oldParentNode = element.parentNode;
    var droponOptions = Sortable.options(dropon);
        
    if(!Element.isParent(dropon, element)) {
      var index;
      
      var children = Sortable.findElements(dropon, {tag: droponOptions.tag, only: droponOptions.only});
      var child = null;
            
      if(children) {
        var offset = Element.offsetSize(dropon, droponOptions.overlap) * (1.0 - overlap);
        
        for (index = 0; index < children.length; index += 1) {
          if (offset - Element.offsetSize (children[index], droponOptions.overlap) >= 0) {
            offset -= Element.offsetSize (children[index], droponOptions.overlap);
          } else if (offset - (Element.offsetSize (children[index], droponOptions.overlap) / 2) >= 0) {
            child = index + 1 < children.length ? children[index + 1] : null;
            break;
          } else {
            child = children[index];
            break;
          }
        }
      }
      
      dropon.insertBefore(element, child);
      
      Sortable.options(oldParentNode).onChange(element);
      droponOptions.onChange(element);
    }
  },

  unmark: function() {
    if(Sortable._marker) Sortable._marker.hide();
  },

  mark: function(dropon, position) {
    // mark on ghosting only
    var sortable = Sortable.options(dropon.parentNode);
    if(sortable && !sortable.ghosting) return; 

    if(!Sortable._marker) {
      Sortable._marker = 
        ($('dropmarker') || Element.extend(document.createElement('DIV'))).
          hide().addClassName('dropmarker').setStyle({position:'absolute'});
      document.getElementsByTagName("body").item(0).appendChild(Sortable._marker);
    }    
    var offsets = Position.cumulativeOffset(dropon);
    Sortable._marker.setStyle({left: offsets[0]+'px', top: offsets[1] + 'px'});
    
    if(position=='after')
      if(sortable.overlap == 'horizontal') 
        Sortable._marker.setStyle({left: (offsets[0]+dropon.clientWidth) + 'px'});
      else
        Sortable._marker.setStyle({top: (offsets[1]+dropon.clientHeight) + 'px'});
    
    Sortable._marker.show();
  },
  
  _tree: function(element, options, parent) {
    var children = Sortable.findElements(element, options) || [];
  
    for (var i = 0; i < children.length; ++i) {
      var match = children[i].id.match(options.format);

      if (!match) continue;
      
      var child = {
        id: encodeURIComponent(match ? match[1] : null),
        element: element,
        parent: parent,
        children: [],
        position: parent.children.length,
        container: $(children[i]).down(options.treeTag)
      }
      
      /* Get the element containing the children and recurse over it */
      if (child.container)
        this._tree(child.container, options, child)
      
      parent.children.push (child);
    }

    return parent; 
  },

  tree: function(element) {
    element = $(element);
    var sortableOptions = this.options(element);
    var options = Object.extend({
      tag: sortableOptions.tag,
      treeTag: sortableOptions.treeTag,
      only: sortableOptions.only,
      name: element.id,
      format: sortableOptions.format
    }, arguments[1] || { });
    
    var root = {
      id: null,
      parent: null,
      children: [],
      container: element,
      position: 0
    }
    
    return Sortable._tree(element, options, root);
  },

  /* Construct a [i] index for a particular node */
  _constructIndex: function(node) {
    var index = '';
    do {
      if (node.id) index = '[' + node.position + ']' + index;
    } while ((node = node.parent) != null);
    return index;
  },

  sequence: function(element) {
    element = $(element);
    var options = Object.extend(this.options(element), arguments[1] || { });
    
    return $(this.findElements(element, options) || []).map( function(item) {
      return item.id.match(options.format) ? item.id.match(options.format)[1] : '';
    });
  },

  setSequence: function(element, new_sequence) {
    element = $(element);
    var options = Object.extend(this.options(element), arguments[2] || { });
    
    var nodeMap = { };
    this.findElements(element, options).each( function(n) {
        if (n.id.match(options.format))
            nodeMap[n.id.match(options.format)[1]] = [n, n.parentNode];
        n.parentNode.removeChild(n);
    });
   
    new_sequence.each(function(ident) {
      var n = nodeMap[ident];
      if (n) {
        n[1].appendChild(n[0]);
        delete nodeMap[ident];
      }
    });
  },
  
  serialize: function(element) {
    element = $(element);
    var options = Object.extend(Sortable.options(element), arguments[1] || { });
    var name = encodeURIComponent(
      (arguments[1] && arguments[1].name) ? arguments[1].name : element.id);
    
    if (options.tree) {
      return Sortable.tree(element, arguments[1]).children.map( function (item) {
        return [name + Sortable._constructIndex(item) + "[id]=" + 
                encodeURIComponent(item.id)].concat(item.children.map(arguments.callee));
      }).flatten().join('&');
    } else {
      return Sortable.sequence(element, arguments[1]).map( function(item) {
        return name + "[]=" + encodeURIComponent(item);
      }).join('&');
    }
  }
}

// Returns true if child is contained within element
Element.isParent = function(child, element) {
  if (!child.parentNode || child == element) return false;
  if (child.parentNode == element) return true;
  return Element.isParent(child.parentNode, element);
}

Element.findChildren = function(element, only, recursive, tagName) {   
  if(!element.hasChildNodes()) return null;
  tagName = tagName.toUpperCase();
  if(only) only = [only].flatten();
  var elements = [];
  $A(element.childNodes).each( function(e) {
    if(e.tagName && e.tagName.toUpperCase()==tagName &&
      (!only || (Element.classNames(e).detect(function(v) { return only.include(v) }))))
        elements.push(e);
    if(recursive) {
      var grandchildren = Element.findChildren(e, only, recursive, tagName);
      if(grandchildren) elements.push(grandchildren);
    }
  });

  return (elements.length>0 ? elements.flatten() : []);
}

Element.offsetSize = function (element, type) {
  return element['offset' + ((type=='vertical' || type=='height') ? 'Height' : 'Width')];
}
//v1.7
// Flash Player Version Detection
// Detect Client Browser type
// Copyright 2005-2007 Adobe Systems Incorporated.  All rights reserved.
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

function ControlVersion()
{
	var version;
	var axo;
	var e;

	// NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry

	try {
		// version will be set for 7.X or greater players
		axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		version = axo.GetVariable("$version");
	} catch (e) {
	}

	if (!version)
	{
		try {
			// version will be set for 6.X players only
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
			
			// installed player is some revision of 6.0
			// GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,
			// so we have to be careful. 
			
			// default to the first public version
			version = "WIN 6,0,21,0";

			// throws if AllowScripAccess does not exist (introduced in 6.0r47)		
			axo.AllowScriptAccess = "always";

			// safe to call for 6.0r47 or greater
			version = axo.GetVariable("$version");

		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 4.X or 5.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = axo.GetVariable("$version");
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 3.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = "WIN 3,0,18,0";
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 2.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			version = "WIN 2,0,0,11";
		} catch (e) {
			version = -1;
		}
	}
	
	return version;
}

// JavaScript helper required to detect Flash Player PlugIn version information
function GetSwfVer(){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	var flashVer = -1;
	
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			var descArray = flashDescription.split(" ");
			var tempArrayMajor = descArray[2].split(".");			
			var versionMajor = tempArrayMajor[0];
			var versionMinor = tempArrayMajor[1];
			var versionRevision = descArray[3];
			if (versionRevision == "") {
				versionRevision = descArray[4];
			}
			if (versionRevision[0] == "d") {
				versionRevision = versionRevision.substring(1);
			} else if (versionRevision[0] == "r") {
				versionRevision = versionRevision.substring(1);
				if (versionRevision.indexOf("d") > 0) {
					versionRevision = versionRevision.substring(0, versionRevision.indexOf("d"));
				}
			}
			var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	else if ( isIE && isWin && !isOpera ) {
		flashVer = ControlVersion();
	}	
	return flashVer;
}

// When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
{
	versionStr = GetSwfVer();
	if (versionStr == -1 ) {
		return false;
	} else if (versionStr != 0) {
		if(isIE && isWin && !isOpera) {
			// Given "WIN 2,0,0,11"
			tempArray         = versionStr.split(" "); 	// ["WIN", "2,0,0,11"]
			tempString        = tempArray[1];			// "2,0,0,11"
			versionArray      = tempString.split(",");	// ['2', '0', '0', '11']
		} else {
			versionArray      = versionStr.split(".");
		}
		var versionMajor      = versionArray[0];
		var versionMinor      = versionArray[1];
		var versionRevision   = versionArray[2];

        	// is the major.revision >= requested major.revision AND the minor version >= requested minor
		if (versionMajor > parseFloat(reqMajorVer)) {
			return true;
		} else if (versionMajor == parseFloat(reqMajorVer)) {
			if (versionMinor > parseFloat(reqMinorVer))
				return true;
			else if (versionMinor == parseFloat(reqMinorVer)) {
				if (versionRevision >= parseFloat(reqRevision))
					return true;
			}
		}
		return false;
	}
}

function AC_AddExtension(src, ext)
{
  if (src.indexOf('?') != -1)
    return src.replace(/\?/, ext+'?'); 
  else
    return src + ext;
}

function AC_Generateobj(objAttrs, params, embedAttrs) 
{ 
  var str = '';
  if (isIE && isWin && !isOpera)
  {
    str += '<object ';
    for (var i in objAttrs)
    {
      str += i + '="' + objAttrs[i] + '" ';
    }
    str += '>';
    for (var i in params)
    {
      str += '<param name="' + i + '" value="' + params[i] + '" /> ';
    }
    str += '</object>';
  }
  else
  {
    str += '<embed ';
    for (var i in embedAttrs)
    {
      str += i + '="' + embedAttrs[i] + '" ';
    }
    str += '> </embed>';
  }
return str;
  //document.write(str);
}

function AC_FL_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     , "application/x-shockwave-flash"
    );
   return AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_SW_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".dcr", "src", "clsid:166B1BCA-3F9C-11CF-8075-444553540000"
     , null
    );
  return AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_GetArgs(args, ext, srcParamName, classid, mimeType){
  var ret = new Object();
  ret.embedAttrs = new Object();
  ret.params = new Object();
  ret.objAttrs = new Object();
  for (var i=0; i < args.length; i=i+2){
    var currArg = args[i].toLowerCase();    

    switch (currArg){	
      case "classid":
        break;
      case "pluginspage":
        ret.embedAttrs[args[i]] = args[i+1];
        break;
      case "src":
      case "movie":	
        args[i+1] = AC_AddExtension(args[i+1], ext);
        ret.embedAttrs["src"] = args[i+1];
        ret.params[srcParamName] = args[i+1];
        break;
      case "onafterupdate":
      case "onbeforeupdate":
      case "onblur":
      case "oncellchange":
      case "onclick":
      case "ondblclick":
      case "ondrag":
      case "ondragend":
      case "ondragenter":
      case "ondragleave":
      case "ondragover":
      case "ondrop":
      case "onfinish":
      case "onfocus":
      case "onhelp":
      case "onmousedown":
      case "onmouseup":
      case "onmouseover":
      case "onmousemove":
      case "onmouseout":
      case "onkeypress":
      case "onkeydown":
      case "onkeyup":
      case "onload":
      case "onlosecapture":
      case "onpropertychange":
      case "onreadystatechange":
      case "onrowsdelete":
      case "onrowenter":
      case "onrowexit":
      case "onrowsinserted":
      case "onstart":
      case "onscroll":
      case "onbeforeeditfocus":
      case "onactivate":
      case "onbeforedeactivate":
      case "ondeactivate":
      case "type":
      case "codebase":
      case "id":
        ret.objAttrs[args[i]] = args[i+1];
        break;
      case "width":
      case "height":
      case "align":
      case "vspace": 
      case "hspace":
      case "class":
      case "title":
      case "accesskey":
      case "name":
      case "tabindex":
        ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
        break;
      default:
        ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
    }
  }
  ret.objAttrs["classid"] = classid;
  if (mimeType) ret.embedAttrs["type"] = mimeType;
  return ret;
}
