/*
Script: price.alert.js

Dependancies:
        mootools - <Moo.js>, <Array.js>, <Element.js>, <Function.js>, <Ajax.js>, <Dom.js>
        CNET libraries - <ajax.cnet.js>, <element.cnet.js>, <my.products.js>

Author:
        Bill Krueger, <bill [dot] krueger [at] cnet [dot] com>

Class: PriceAlert
Builds one or more Price Alert (used to be called Product Alert) components that allow a user to subscribe to an alert
from the UserList system to get an email when a product first gets a price.

Arguments:
        options - an object containing options necessary for initializing the PriceAlert object

Options:
        productIdPrefix - prefix to strip from the id of the price alert outer element to be left with the productId
        alertElements - CSS selector that returns one or many price alert elements
        subscribeSelector - CSS selector that returns the subscribe element for a given price alert
        unsubscribeSelector - CSS selector that returns the unsubscribe element for a specific price alert
        subscribeLinkSelector - CSS selector that returns the anchor element used to subscribe to a specific alert
        unsubscribeLinkSelector - CSS selector that returns the anchor element used to unusbscribe from a specific price alert
        waitingSelector - CSS selector that returns the element used to display the waiting icon during the AJAX request
*/
var PriceAlert = new Class({

    initialize: function(options) {
        this.WAITING_STATUS = 1;
        this.COMPLETE_STATUS = 2;
        this.FAILED_STATUS = 3;
        this.options = options;
				dbug.log('alert elements: ', $A(this.options.alertElements));
        $A(this.options.alertElements).each(function(el) {
            var subscribeElement = el.getElement(this.options.subscribeSelector);
            var unsubscribeElement = el.getElement(this.options.unsubscribeSelector);
            el.getElement(this.options.subscribeLinkSelector).addEvent('click', this.subscribe.pass(subscribeElement, this));
            el.getElement(this.options.unsubscribeLinkSelector).addEvent('click', this.unsubscribe.pass(unsubscribeElement, this));
            this.setInitialDisplay(subscribeElement, unsubscribeElement);
        }.bind(this));
    },

    setInitialDisplay: function(subEl, unsubEl) {
        var productId = (subEl) ? subEl.parentNode.id.substr(this.options.productIdPrefix.length) : null;
        if (UserVars.isLoggedIn() || UserVars.isRemembered()) {
            loadProductListJSON('http://reviews.cnet.com/9801-1-0.html?cmd=getChildFolders&viewName=.json-my-products-list');
        }
        if (productId && mpList.list) {
            mpList.list.each(function(listItem) {
                if (listItem.referencedId == productId) {
                    listItem.watchEvents.list.each(function(watchEvent) {
                        if (watchEvent.eventType.value == 3) {
                            subEl.hide();
                            unsubEl.show('block');
                        }
                    }.bind(this));
                }
            }.bind(this));
        }
    },

    subscribe: function(el) {
        var productId = (el) ? el.parentNode.id.substr(this.options.productIdPrefix.length) : null;
        if (productId) {
            this.changeStatus(el, this.WAITING_STATUS);
            var daUrl = '/9800-1-0.xml?referencedId='+productId+'&listItemType=5&referencedType=2&eventTypes=3&cmd=createListItem&component';

            this.subscribeReq = new Request({
            	url: daUrl,
                method: 'get',
                onSuccess: this.changeStatus.pass([el, this.COMPLETE_STATUS], this),
                onFailure: this.handleFailure.pass(el, this)
            }).send();
        }
    },

    unsubscribe: function(el) {
        var productId = (el) ? el.parentNode.id.substr(this.options.productIdPrefix.length) : null;
        var listItemId;
        mpList.list.each(function(listItem) {
            if (listItem.referencedId == productId) listItemId = listItem.id;
        }.bind(this));
        if (listItemId) {
            this.changeStatus(el, this.WAITING_STATUS);
            var daUrl = '/9800-1-0.xml?listItemId='+listItemId+'&eventType=3&cmd=deleteWatchEvent&component';
            this.unsubscribeReq = new Request({
            	url: daUrl,
                method: 'get',
                onSuccess: this.changeStatus.pass([el, this.COMPLETE_STATUS], this),
                onFailure: this.handleFailure.pass(el, this)
            }).send();
        }
    },

    changeStatus: function(el, type) {
				dbug.log('changeStatus: %o, %s', el, type);
        var waitingElement = el.parentNode.getElement(this.options.waitingSelector);
        var subscribeElement = el.parentNode.getElement(this.options.subscribeSelector);
        var unsubscribeElement = el.parentNode.getElement(this.options.unsubscribeSelector);
        switch(type) {
            case this.WAITING_STATUS:
                el.hide();
                waitingElement.show();
                break;
            case this.COMPLETE_STATUS:
                loadProductListJSON('http://reviews.cnet.com/9801-1-0.html?cmd=getChildFolders&viewName=.json-my-products-list');
                waitingElement.hide();
                if (el == subscribeElement) {
                    unsubscribeElement.show();
                } else if (el == unsubscribeElement) {
                    subscribeElement.show();
                }
				var now = new Date();
                var dwUrl = 'http://dw.com.com/redir?ontid='+PageVars.nodeId+'&siteid='+PageVars.siteId+'&pId='+PageVars.pageType+'&edId='+PageVars.editionId+'&useraction=23'+'&tag=pricealert'+'&uniquePingId='+now.getTime();
                new Element('img').setProperty('src', dwUrl).addClass('pingdw');
                break;
            case this.FAILED_STATUS:
                waitingElement.hide();
                el.show();
                break;
        }
    },

    handleFailure: function(el) {
				dbug.log('failure: %o', el);
        var req = (el == el.parentNode.getElement(this.options.subscribeSelector)) ? this.subscribeReq : this.unsubscribeReq;
        if (req.transport.responseText.indexOf("COMPONENT_RESPONSE_CODE=401") >= 0) {
            var url = req.url.replace(/&component(=)?/, '');
            window.location = url+'&viewName=redirect:'+escape(location.href);
        } else {
            alert('There was a problem with the request.  Please try again.');
            this.changeStatus(el, this.FAILED_STATUS);
        }
    }
});
/* Do not edit below this line */
/*

$Source: /cvs/main/flatfile/html/rb/js/commerce/implementations/my.products/price.alert.js,v $
$Log: price.alert.js,v $
Revision 1.6  2007/02/23 23:25:43  newtona
*** empty log message ***

Revision 1.5  2007/02/23 23:19:10  newtona
added some dbug lines

Revision 1.4  2007/02/23 23:17:44  newtona
missing date declaration (var now...)

Revision 1.3  2007/01/22 22:58:39  newtona
removed firenow from ajax calls.

Revision 1.2  2007/01/11 22:46:18  newtona
docs update

Revision 1.1  2007/01/09 02:44:55  newtona
moved files from previous directories in commerce.global.utils and commerce.implementations

Revision 1.1  2006/12/08 23:11:44  kruegerw
initial commit

*/
