


Vudu.widget.Wish = function( divHolder , msgHolder , isWish, directorUrl, contentId, key, userId ) {
    
    this._divHolder = YAHOO.util.Dom.get(divHolder);
	this._msgHolder = YAHOO.util.Dom.get(msgHolder);
    this._directorUrl = directorUrl;
    
    this._contentId = contentId;
    this._key = key;
    this._userId = userId;
    
	this.loading = false;
	
    
	var thisRef = this;
    if (isWish && isWish == "true") {
        this._divHolder.innerHTML = 'Remove Wish';
        YAHOO.util.Event.addListener(this._divHolder, "click", function() {
            thisRef.updateWish(true);
        });
    }
    else {
        this._divHolder.innerHTML = 'Add to Wish';
        YAHOO.util.Event.addListener(this._divHolder, "click", function() {
            thisRef.updateWish(false);
        });
    }
	
    this.onCallback = {
        success: function( oResponse ) {
            
            var results = Vudu.util.parseDirectorJSONResponse(oResponse.responseText);
            var wishWidget = oResponse.argument.wishWidget;
            var thisRef = wishWidget;
            
			if (oResponse.argument.adding) {
				wishWidget._divHolder.innerHTML = 'Remove Wish';
				YAHOO.util.Event.purgeElement(wishWidget._divHolder, false, "click");
				YAHOO.util.Event.addListener(wishWidget._divHolder, "click", function() {
					thisRef.updateWish(true);
				});
			}
			else {
				wishWidget._divHolder.innerHTML = 'Add to Wish';
                YAHOO.util.Event.purgeElement(wishWidget._divHolder, false, "click");
                YAHOO.util.Event.addListener(wishWidget._divHolder, "click", function() {
                    thisRef.updateWish(false);
                });
			}
			
			wishWidget._msgHolder.innerHTML = 'Please reload VUDU on your device';
			
			// hide the text in 2 sec
            window.setTimeout(function() {
				thisRef.loading = false;
                thisRef._msgHolder.innerHTML = '';
            }, 2000);
        },
        
        failure: function( oResponse ) {
            
            var results = Vudu.util.parseDirectorJSONResponse(oResponse.responseText);
            var wishWidget = oResponse.argument.wishWidget;
            
			wishWidget.loading = false;
            
			wishWidget._msgHolder.innerHTML = 'Failed to update wish list';
			
			var thisRef = wishWidget;
            // hide the text in 2 sec
            window.setTimeout(function() {
                thisRef._msgHolder.innerHTML = '';
            }, 2000);
        }
    };
    
    this.updateWish = function(remove)  {
        
		if(this.loading) return;
		
        var req = "_type=wishStore&format=application%2Fjson";
		this.onCallback.argument = { wishWidget : this, adding : true};
		if(remove)    {
			req = "_type=wishRemove&format=application%2Fjson";
			this.onCallback.argument = { wishWidget : this, adding : false};
		}
		
        // set userId
        req += "&userId="+this._userId;
        // set movie Id & type 
        req += "&contentId="+this._contentId;
        // set the auth token
        req += "&sessionKey="+this._key;
        
        this.loading = true;
        
        YAHOO.util.Connect.setDefaultPostHeader(false);
        YAHOO.util.Connect.initHeader("Content-Type", "application/x-vudu-url-note");
		YAHOO.util.Connect.asyncRequest('POST', this._directorUrl , this.onCallback, req );
    };   
}