Invite.TicketAvailability = {

    // ----------------------------------------------------------------------
    //
    //
    // ----------------------------------------------------------------------
    observe : function() {

    },



    // ----------------------------------------------------------------------
    //
    //
    // ----------------------------------------------------------------------
    refreshModule : function() {
        $('ticket-availability-module-loading').show();
        new Ajax.Updater('ticket-availability-module', '/component-ticket-availability-module.iggli', {
            parameters: { inviteId: _INVITEID },
            onComplete: function() {
                //just a little useless timeout so it seems like a real refresh.
                //remove this soon.
                window.setTimeout("$('ticket-availability-module-loading').hide()", 3000);
            }
        });
    },



    // ----------------------------------------------------------------------
    //
    //
    // ----------------------------------------------------------------------
    expandDiscuss : function(ticketId) {
        new Effect.toggle($('ticket-row-' + ticketId), 'blind', {duration:0.5,afterFinish: function() {
            $('item-text-' + ticketId).activate();
        } });

        return false;
    },



    // ----------------------------------------------------------------------
    //
    //
    // ----------------------------------------------------------------------
    expandDiscussLoginManaged : function(ticketId) {
        Invite.LoginManager.authorize("reload", "Invite.TicketAvailability.expandDiscuss('" + ticketId + "')");

        return false;
    },



    // ----------------------------------------------------------------------
    //
    //
    // ----------------------------------------------------------------------
    popupRedirect : function(width, height, url, inviteId) {
        Invite.Misc.ajaxRecordTicketClick(inviteId);
        var igUrl = "/invite2/open-buy-tix-window.iggli?tixUrl=" + escape(url);
        window.open(igUrl, '_blank', 'location=1,status=1,toolbar=1,scrollbars=1,menubar=1,resizeable=1,width=' + width + ',height=' + height);
    },



    // ----------------------------------------------------------------------
    //
    //
    // ----------------------------------------------------------------------
    handleTicketPost: function(price, num, row, sec, url, ticketBlockId, closeElm, textElmId, providerTicketId) {

        var text = $F('item-text-' + textElmId);


        text = text.stripScripts().stripTags();//remove any scripts and ', "
        new Ajax.Request('/ajax2/eventstream-item-submit.iggli', {
            method:'post',
            parameters: {
                commentText: text,
                type:5,
                inviteId: _INVITEID,
                ticketPrice: price,
                ticketNumSeats: num,
                ticketRow: row,
                ticketSection: sec,
                ticketUrl:url,
                providerTicketId:providerTicketId

            },onSuccess : function() {

                new Ajax.Request('/ajax2/ticket-availability-mark-posted.iggli', {
                    method:'post',
                    parameters: {
                        ticketBlockId: ticketBlockId
                    }
                });  // end inner post
            },onComplete : function() {
                // do an effect to indicate the message was posted
                Effect.Appear('posted-message-' + textElmId, {duration: 0.5});
                Effect.Fade('posted-message-' + textElmId, {duration: 2.0});

                new Effect.toggle(closeElm, 'blind', {duration:0.5,queue:'end'});
            }
        });

    }, // end handleTicketPost

    // ----------------------------------------------------------------------
    //
    //
    // ----------------------------------------------------------------------
    setPricingAlertManaged : function() {
        $('alert-ticket-price-input').removeClassName('input-validation-error');
        var price = $F('alert-ticket-price-input');

        Invite.LoginManager.authorize("reload", "Invite.TicketAvailability.setPricingAlert('" + price + "')");
		//Invite.TicketAvailability.setPricingAlert(price);
        return false;
    },


    // ----------------------------------------------------------------------
    //
    //
    // ----------------------------------------------------------------------
    setPricingAlert : function(price) {

        //alert('setPricingAlert called.');
		
        if (price && price.length > 0) {
        
        	var params = {};
			if (window._IGGLIHASHEVENTID) {
				params.iggliHashEventId = window._IGGLIHASHEVENTID;
			}
			if (window._INVITEID) {
				params.inviteId = window._INVITEID;
			}
			params.ticketPrice = price;

            new Ajax.Request('/ajax2/set-ticket-price-alert.iggli', {
                method:'post',
                evalJSON: "force",
                parameters: params, 
                onSuccess : function(transport) {

                    var json, message;
                    try {

                        json = transport.responseJSON;

                        if (json.responseCode) {

                            if (json.responseCode == 200) {
                                // update the UI
                                $('ticket-price-alert-section-set').style.display = "none";
                                $('ticket-price-alert-section-cancel').style.display = "block";
                                $('alert-section-cancel-label').update(json.message);
                            } else {
                                // display the error message to the user
                                $('alert-ticket-price-input').addClassName('input-validation-error');
                                $('validation-message').update(json.message);
                                $('validation-message').toggle();
                                new Effect.Fade($('validation-message'), { queue: 'end', delay: 2.0 });
                            }

                        } else {
                            $('validation-message').update("Error: json response code not set: " + json.message);
                            $('validation-message').toggle();
                            new Effect.Fade($('validation-message'), { queue: 'end', delay: 2.0 });
                        }


                    } catch(err) {

                        $('validation-message').update("Error: " + json.message);
                        $('validation-message').toggle();
                        new Effect.Fade($('validation-message'), { queue: 'end', delay: 2.0 });
                    }


                }, onError: function(transport) {
                    // this happens when some transport or other infrastructure error occurred
                    $('validation-message').update("An unexpected error occurred - please try again later.");
                    $('validation-message').toggle();
                    new Effect.Fade($('validation-message'), { queue: 'end', delay: 2.0 });
                }
            });

        } // end if price

        return false;
    },


    // ----------------------------------------------------------------------
    //
    //
    // ----------------------------------------------------------------------
    cancelPricingAlert : function() {

        //alert('cancelPricingAlert called.');

        $('alert-ticket-price-input').removeClassName('input-validation-error');
		
		var params = {};
			if (window._IGGLIHASHEVENTID) {
				params.iggliHashEventId = window._IGGLIHASHEVENTID;
			}
			if (window._INVITEID) {
				params.inviteId = window._INVITEID;
			}
        new Ajax.Request('/ajax2/cancel-ticket-price-alert.iggli', {
            method:'post',
            evalJSON: "force",
            parameters: params,
            onSuccess : function(transport) {

                var json, message;
                try {

                    json = transport.responseJSON;

                    if (json.responseCode) {

                        if (json.responseCode == 200) {
                            // update the UI
                            $('alert-ticket-price-input').value = '$';
                            $('ticket-price-alert-section-set').style.display = "block";
                            $('ticket-price-alert-section-cancel').style.display = "none";
                        } else {
                            // display the error message to the user
                            $('validation-message').update(json.message);
                            $('validation-message').toggle();
                            new Effect.Fade($('validation-message'), { queue: 'end', delay: 2.0 });
                        }

                    } else {
                        $('validation-message').update("Error: json response code not set: " + json.message);
                        $('validation-message').toggle();
                        new Effect.Fade($('validation-message'), { queue: 'end', delay: 2.0 });
                    }


                } catch(err) {

                    $('validation-message').update("Error: " + json.message);
                    $('validation-message').toggle();
                    new Effect.Fade($('validation-message'), { queue: 'end', delay: 2.0 });
                }

            }, onError: function(transport) {
                // this happens when some transport or other infrastructure error occurred
                $('validation-message').update("An unexpected error occurred - please try again later.");
                $('validation-message').toggle();
                new Effect.Fade($('validation-message'), { queue: 'end', delay: 2.0 });
            }
        });

        return false;
    }

};
