var UserOptions = new Object();

UserOptions.Storage = new Object();

UserOptions.Settings = {
    FavoritesLinks : {
        Add : 'hotlist_link_add',
        Remove : 'hotlist_link_remove'
    },
    AlertsForm : 'user_options_alerts_subscribe',
    WallForm : 'user_options_wall_post',
    TipForm : 'user_options_tip',
    GiftForm : 'user_options_gift',
    GiftFormContainer : 'user_options_gift_send',
    CantAfford : 'user_options_tip_cant_afford',
    RatingDisplay : 'rating_stretch_bar',
    Voting : 'stars',
    WallpostMessage : 'wallpost_message',
    WallpostPostBox : 'wallpost_post_box',
    WallpostResultBox : 'wallpost_result_box',      
    GiftTipRateContainer : 'op_gift_tip_rate_container_id',
    GiftTipRatingPopUpId : 'op_gift_tip_rate_message_box',
    GiftContainerId : 'op_gifts_container_id',
    GiftStatusMessageId : 'gift_status_message_',
    TipStatusMessageId : 'tip_sent_status_',
    RatingsId : 'op_ratings_id'
};


UserOptions.Rating = new Object();
UserOptions.Alerts = new Object();
UserOptions.Favorites = new Object();
UserOptions.Wallpost = new Object();
UserOptions.Tip = new Object();
UserOptions.Gift = new Object();
UserOptions.Tools = new Object();

UserOptions.View = new Object();

UserOptions.Storage.bIsProcessing = false;

/**
 * **** FAVORITES
 * ****************************************************************
 */

UserOptions.Favorites.Run = function(iLiveCode, bRemove) {
    new Ajax.Request('/ajax/user_options/favorites', {
        parameters : {
            LiveCode : iLiveCode,
            Remove : bRemove
        },
        onLoading : function() {
            AnimatedOverlay.Open();
        },
        onComplete : function(transport) {
            var oResponse = transport.responseJSON;
            if (Object.isUndefined(oResponse.Status) || !oResponse.Status) {
                return;
            }
            UserOptions.View.ActivateFavoriteLink(bRemove);
            AnimatedOverlay.LoadByContent(oResponse.HTML);
            
        }
    });
};

UserOptions.View.ActivateFavoriteLink = function(bRemoved) {
    if (bRemoved) {
        Element.hide(UserOptions.Settings.FavoritesLinks.Remove);
        Element.show(UserOptions.Settings.FavoritesLinks.Add);
    } else {
        Element.hide(UserOptions.Settings.FavoritesLinks.Add);
        Element.show(UserOptions.Settings.FavoritesLinks.Remove);
    }
};

UserOptions.Favorites.Send = function(iLiveCode, bRemove) {
    new Ajax.Request('/ajax/user_options/favorites/send', {
        parameters : {
            LiveCode : iLiveCode,
            Remove : bRemove
        },
        onComplete : function(transport) {
            var oResponse = transport.responseJSON;
            if (Object.isUndefined(oResponse.Status) || !oResponse.Status) {
                return;
            }
            UserOptions.Favorites.switchFavouritesToGifts(bRemove);
            UserOptions.Tools.ShowPopUp(
                UserOptions.Settings.GiftTipRateContainer, 
                UserOptions.Settings.GiftTipRatingPopUpId, 
                oResponse.sStatusMessage
            );
        }
    });
};
UserOptions.Favorites.switchFavouritesToGifts = function(bRemove) {
    if(!bRemove) {
        $('op_favs_btn').hide();
        $('op_alerts_id_hide_div').show();
    } else {
        $('op_favs_btn').show();
        $('op_alerts_id_hide_div').hide();
    }
    
};
/**
 * **** RATING
 * *******************************************************************
 */

// maybe can deprecate as was used for old ajax style ratings
UserOptions.Rating.RollOverChange = function(iVote) {
    $(UserOptions.Settings.Voting).style.width = (20 * iVote) + '%';
};

// maybe can deprecate as was used for old ajax style ratings
UserOptions.Rating.ResetStarts = function(iValue) {
    $(UserOptions.Settings.Voting).style.width = iValue + '%';
};

UserOptions.Rating.Open = function(iLiveCode) {
    new Ajax.Request('/ajax/user_options/rating', {
        parameters : {
            LiveCode : iLiveCode
        },
        onLoading : function() {
            AnimatedOverlay.Open();
        },
        onComplete : function(transport) {
            var oResponse = transport.responseJSON;
            
            AnimatedOverlay.LoadByContent(oResponse.HTML);
        }
    });
};

// Deprecate
UserOptions.Rating.Run = function(iLiveCode, iVote) {
    
    new Ajax.Request('/ajax/user_options/rating/vote', {
        parameters : {
            LiveCode : iLiveCode,
            Vote : iVote
        },
        onLoading : function() {
            AnimatedOverlay.Open();
        },
        onComplete : function(transport) {
            var oResponse = transport.responseJSON;
            if (!Object.isUndefined(oResponse.CurrentRating)) {
                var iRating = oResponse.CurrentRating * 20;
                
                // (max 5 so times 2 to get 10 and times 10 to get 100%);
                
                $(UserOptions.Settings.RatingDisplay).setStyle({
                    width : iRating + '%'
                });
            }
            
            AnimatedOverlay.LoadByContent(oResponse.HTML);
            
        }
    });
};

UserOptions.Rating.Send = function(iLiveCode, iVote) {
    
    if(UserOptions.Storage.bIsProcessing) return;
    UserOptions.Storage.bIsProcessing = true;
    
    UserOptions.Tools.ShowPopUp(
        UserOptions.Settings.GiftTipRateContainer, 
        UserOptions.Settings.GiftTipRatingPopUpId, 
        "Sending..."
    );
    
    new Ajax.Request('/ajax/user_options/rating/vote/send', {
        parameters : {
            LiveCode : iLiveCode,
            Vote : iVote
        },
        onComplete : function(transport) {
            var oResponse = transport.responseJSON;
            if (!Object.isUndefined(oResponse)) {
                UserOptions.Rating.UpdateRatingState(oResponse.CurrentRating, oResponse.CurrentRating);

                UserOptions.Tools.ShowPopUp(
                    UserOptions.Settings.GiftTipRateContainer, 
                    UserOptions.Settings.GiftTipRatingPopUpId, 
                    oResponse.sStatusMessage
                );
                
            }
        }
    });
};

UserOptions.Rating.UpdateRatingState = function(iRate, iCurrentRating) {
    
    iCurrentRating = iCurrentRating || false;
    
    $(UserOptions.Settings.RatingsId).adjacent('li').each(function(oElement){
        if(oElement.readAttribute('class') == 'on' && iRate < oElement.readAttribute('amount')) {
            oElement.writeAttribute('class', 'off');
        } else if(oElement.readAttribute('class') == 'off' && iRate >= oElement.readAttribute('amount')) {
            oElement.writeAttribute('class', 'on');
        }
        if(iCurrentRating) {
            oElement.writeAttribute('onmouseout', 'UserOptions.Rating.UpdateRatingState(' + iCurrentRating + ');'); // Reset mark up and remove existing onmouseout
        }
    });
};

/**
 * **** ALERTS
 * *******************************************************************
 */

UserOptions.Alerts.Run = function(bMyPage) {
    
    oElements = Form.serialize(UserOptions.Settings.AlertsForm, true);
    
    new Ajax.Request('/ajax/user_options/alerts/subscribe', {
        parameters : Form.serialize(UserOptions.Settings.AlertsForm),
        onLoading : function() {
            AnimatedOverlay.ShowLoader();
        },
        onComplete : function(transport) {
            var oResponse = transport.responseJSON;
            if (Object.isUndefined(oResponse.Status) || !oResponse.Status) {
                return;
            }
            
            if (oResponse.Added && bMyPage === false) {
                UserOptions.View.ActivateFavoriteLink(false);
            } else if (bMyPage === true) {
                Element.update(
                    $('AlertsStatus_' + oElements.SubscribeLiveCode), Object
                            .keys(oElements).length > 1 ? 'ON' : 'OFF');
            }
            
            AnimatedOverlay.LoadByContent(oResponse.HTML);
        }
    });
};

UserOptions.Alerts.Open = function(iLiveCode, bMyPage) {
    new Ajax.Request('/ajax/user_options/alerts', {
        parameters : {
            LiveCode : iLiveCode,
            TypeMyPage : new Boolean(bMyPage)
        },
        onLoading : function() {
            AnimatedOverlay.Open();
        },
        onComplete : function(transport) {
            var oResponse = transport.responseJSON;
            if (Object.isUndefined(oResponse.Status) || !oResponse.Status) {
                return;
            }
            AnimatedOverlay.LoadByContent(oResponse.HTML);
        }
    });
};

UserOptions.Alerts.Send = function(iLiveCode) {
    
    if(UserOptions.Storage.bIsProcessing) return;
    UserOptions.Storage.bIsProcessing = true;
    
    UserOptions.Tools.ShowPopUp(
        UserOptions.Settings.GiftTipRateContainer, 
        UserOptions.Settings.GiftTipRatingPopUpId, 
        "Sending..."
    );
    
    new Ajax.Request('/ajax/user_options/alerts/send', {
        parameters : {
            LiveCode : iLiveCode,
            Alerts : Form.serializeElements($('op_alerts_id').adjacent('ul input'))
        },
        onComplete : function(transport) {
            var oResponse = transport.responseJSON;
            if (Object.isUndefined(oResponse.Status)) {
                return;
            }

            UserOptions.Tools.ShowPopUp(
                UserOptions.Settings.GiftTipRateContainer, 
                UserOptions.Settings.GiftTipRatingPopUpId, 
                oResponse.sStatusMessage
            );
        }
    });

};

/**
 * **** WALL POSTS
 * *******************************************************************
 */

UserOptions.Wallpost.iAllowMessage = true;
UserOptions.Wallpost.sMainMessage = 'write your message here';

UserOptions.Wallpost.Clear = function() {
    if($(UserOptions.Settings.WallpostMessage).value == UserOptions.Wallpost.sMainMessage){ 
        $(UserOptions.Settings.WallpostMessage).value = ''; 
    }
};


if(typeof String.prototype.trim !== 'function') {
    String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g, ''); 
    }
}

UserOptions.Wallpost.Post = function(event , iLiveCode) {
    
     if(event.keyCode == 13){ 
         if(UserOptions.Wallpost.iAllowMessage == false){
            sMessage = 'Please wait a little longer before attempting to send another post.';
            UserOptions.Wallpost.ShowMessage(sMessage, 3500);
         }
    
         
    
         
         if(UserOptions.Wallpost.iAllowMessage == true && $(UserOptions.Settings.WallpostMessage).value.trim().replace(/^\s+|\s+$/g,"") != UserOptions.Wallpost.sMainMessage) {
             
            var oParameters = {
                LiveCode : iLiveCode,
                Message : $(UserOptions.Settings.WallpostMessage).value
            };
            
            new Ajax.Request('/ajax/user_options/wall', {
                parameters : oParameters,
                onLoading : function() {
                    UserOptions.Wallpost.iAllowMessage = false;
                },
                onSuccess : function(transport) {
                    var oResponse = transport.responseJSON;
            
                    if(oResponse.Status == 3) {
                        UserOptions.Wallpost.iAllowMessage = true;
                        sMessage = 'Wall Posts must be between 20 and 200 characters long.';
                    } else if(oResponse.Status == 1) {
                        sMessage = 'Your wall post has been sent and is pending approval.'; 
                        $(UserOptions.Settings.WallpostMessage).value = UserOptions.Wallpost.sMainMessage;
                    } else {
                        sMessage = 'Sorry there has been a problem, please try later.';
                    }   
                
                    UserOptions.Wallpost.ShowMessage(sMessage);
                                    
                    var iTimeOut = setTimeout(function(){
                        UserOptions.Wallpost.iAllowMessage = true;
                   }, 30000);
                    
                }
            });
         }
     }
};

UserOptions.Wallpost.ShowMessage = function(sMessage, iTimeOutLength) {
    
    iTimeOutLength = typeof(iTimeOutLength) != 'undefined' ? iTimeOutLength : 4000;
    /* content holder */
    if(!$(UserOptions.Settings.WallpostResultBox)) {
        var oMessageBox = new Element('div', {
            'id' : UserOptions.Settings.WallpostResultBox,
            'style' : 'display: none',
            'class' : 'result_popup'
        });
        
        var oClearBox = new Element('div', {
            'class' : 'clearb'
        });
        
        oMessageBox.update(sMessage);
        oMessageBox.appendChild(oClearBox);
        
        $(UserOptions.Settings.WallpostPostBox).appendChild(oMessageBox);
    } else {
        $(UserOptions.Settings.WallpostResultBox).update(sMessage);
    }
     
    Effect.toggle(UserOptions.Settings.WallpostResultBox, 'appear');
     
    var iTimeOut = setTimeout(function(){
         Effect.toggle(UserOptions.Settings.WallpostResultBox, 'appear');         
         clearTimeout(iTimeOut);
    }, iTimeOutLength); 
}


/**
 * *** TIPS
 * ********************************************************************************
 */

UserOptions.Tip.Open = function(iLiveCode) {
    new Ajax.Request('/ajax/user_options/tip/wallet', {
        parameters : {
            LiveCode : iLiveCode
        },
        onLoading : function() {
            AnimatedOverlay.Open();
        },
        onSuccess : function(transport) {
            var oResponse = transport.responseJSON;
            if (Object.isUndefined(oResponse.Status) || !oResponse.Status) {
                return;
            }
            AnimatedOverlay.LoadByContent(oResponse.HTML);
        }
    });
};

/* Deprecate */
UserOptions.Tip.Post = function(iLiveCode) {
    
    oElements = Form.serialize(UserOptions.Settings.TipForm, true);
    oElements.LiveCode = iLiveCode;
    
    // check the first onload value
    oElementTipValue = document.forms['Tips'].elements["Tips[Tip]"];
    
    UserOptions.Tip.CheckCanAfford(oElementTipValue);
    
    if (UserOptions.Storage.iCanAfford) {
        new Ajax.Request('/ajax/user_options/tip/wallet', {
            parameters : oElements,
            onLoading : function() {
                AnimatedOverlay.Open();
            },
            onSuccess : function(transport) {
                var oResponse = transport.responseJSON;
                if (Object.isUndefined(oResponse.Status) || !oResponse.Status) {
                    return;
                }
                AnimatedOverlay.LoadByContent(oResponse.HTML);
                UserOptions.Tip.NotifyVOCPublic(iLiveCode);
            }
        });
    }
};

// Deprecate
UserOptions.Tip.CheckCanAfford = function(oSelect) {
    
    UserOptions.Storage.iCanAfford = false;
    
    oOption = oSelect.options[oSelect.selectedIndex];
    
    if (Element.readAttribute(oOption, 'available') == 1) {
        UserOptions.Storage.iCanAfford = true;
        Element.hide(UserOptions.Settings.CantAfford);
        Element.show(UserOptions.Settings.TipForm);
    } else {
        Element.show(UserOptions.Settings.CantAfford);
        Element.hide(UserOptions.Settings.TipForm);
        // Element.hide(UserOptions.Settings.AlertsForm);
    }
    // console.log(Element.readAttribute(oOption, 'available'));
};

UserOptions.Tip.Send = function(iLiveCode, bSkip) {
    
    var sNote = bSkip ? null : $('op_comment_id').value;
    
    if(UserOptions.Storage.bIsProcessing) return;
    UserOptions.Storage.bIsProcessing = true;
    
    UserOptions.Tools.ShowPopUp(
        UserOptions.Settings.GiftTipRateContainer, 
        UserOptions.Settings.GiftTipRatingPopUpId, 
        "Sending..."
    );
    
    oElements = Form.serialize(UserOptions.Settings.TipForm, true);
    oElements.LiveCode = iLiveCode;
    oElements.Note = sNote; 
    
    // check the first onload value
    oElementTipValue = document.forms['Tips'].elements["Tips[Tip]"];
    
    UserOptions.Tip.CheckAffordability(oElementTipValue);
    
    if(UserOptions.Storage.iCanAfford) {
        new Ajax.Request('/ajax/user_options/tip/wallet/send', {
            parameters : oElements,
            onComplete : function(transport) {
                var oResponse = transport.responseJSON;
                if (Object.isUndefined(oResponse.Status) || !oResponse.Status) {
                    return;
                }
                UserOptions.Tip.UpdateSelectList(oResponse.NewUserBalance);

                UserOptions.Gift.UpdateGiftList(oResponse.NewUserBalance);
                UserOptions.Tools.NotifyVOCPrivateTip(oResponse.TipAmount);
                
                UserOptions.Tools.ShowPopUp(
                    UserOptions.Settings.GiftTipRateContainer, 
                    UserOptions.Settings.GiftTipRatingPopUpId, 
                    oResponse.sStatusMessage
                );
                
                UserOptions.Tip.NotifyVOCPublic(iLiveCode);
            }
        });
    }
    
};

UserOptions.Tip.PresentComment = function(iLiveCode) {
    $('op_tip_skip').writeAttribute('onclick', "UserOptions.Tip.Send('" + iLiveCode + "', 1)");
    $('op_tip_send').writeAttribute('onclick', "UserOptions.Tip.Send('" + iLiveCode + "')");
    
    UserOptions.Tools.PresentComment($('op_tip_notes_box_id').innerHTML);
};

UserOptions.Tip.UpdateSelectList = function(iUserNewBalance) {
    if($(UserOptions.Settings.TipForm)) {
        $$('#' + UserOptions.Settings.TipForm + ' option').find(function(oOption){
            if(oOption.value > iUserNewBalance) {
                oOption.writeAttribute('available', 0);
            }
        });
    }
};

UserOptions.Tip.CheckAffordability = function(oSelect) {
    oOption = oSelect.options[oSelect.selectedIndex];
    
    UserOptions.Storage.iCanAfford = true;
    
    if(Element.readAttribute(oOption, 'available') == 0) {
        UserOptions.Tools.ShowPopUp(UserOptions.Settings.GiftTipRateContainer, UserOptions.Settings.GiftTipRatingPopUpId, $('tip_insufficient_funds').innerHTML);
        UserOptions.Storage.iCanAfford = false;
    } 
};

UserOptions.Tip.PostSms = function(iLiveCode) {
    
    new Ajax.Request('/ajax/user_options/tip/sms', {
        parameters : {
            LiveCode : iLiveCode
        },
        onLoading : function() {
            AnimatedOverlay.Open();
        },
        onSuccess : function(transport) {
            var oResponse = transport.responseJSON;
            if (Object.isUndefined(oResponse.Status) || !oResponse.Status) {
                return;
            }
            AnimatedOverlay.LoadByContent(oResponse.HTML);
            UserOptions.Tip.NotifyVOCPublic(iLiveCode);
        }
    });
};

UserOptions.Tip.ShowSms = function(oSelect) {
    oOption = oSelect.options[oSelect.selectedIndex];
    Element.show('tip_box');
    Element.update('keyword', oOption.readAttribute('Keyword'));
    Element.update('shortcode', oOption.readAttribute('ShortCode'));
    Element.update('cost', oOption.readAttribute('Cost'));
};

/**
 * *** GIFTS
 * ********************************************************************************
 */

UserOptions.Gift.Open = function(iLiveCode) {
    
    new Ajax.Request('/ajax/user_options/tip/gift', {
        parameters : {
            LiveCode : iLiveCode
        },
        onLoading : function() {
            AnimatedOverlay.Open();
        },
        onSuccess : function(transport) {
            var oResponse = transport.responseJSON;
            if (Object.isUndefined(oResponse.Status) || !oResponse.Status) {
                return;
            }
            AnimatedOverlay.LoadByContent(oResponse.HTML);
            
            if (!oResponse.Images)
                return;
            
            oIMG = new Image();
            oResponse.Images.each(function(image) {
                oIMG.src = image; // preloading
            });
        }
    });
};

UserOptions.Gift.SelectGift = function(iGiftId) {
    
    $('user_options_gift_select').hide();
    
    $('user_options_gift_send').getElementsBySelector('div.gift_container')
            .each(function(element) {
                element.hide();
            });
    
    $('gift_container_' + iGiftId).show();
    $('user_options_gift_send').show();
    
    $('gift_selected_id').value = iGiftId;
    
    AnimatedOverlay.UpdatePosition();
};

UserOptions.Gift.SelectPreviousGift = function(oContainer) {
    
    oContainer = $(oContainer);
    
    oContainer.hide();
    
    if (oContainer.previous()) {
        oElement = oContainer.previous();
    } else {
        oElement = $(oContainer.parentNode.lastChild);
        while (oElement && oElement.nodeType != 1)
            oElement = $(oElement.previousSibling);
    }
    
    oElement.show();
    
    $('gift_selected_id').value = $(oElement.id.split('_')).last();
    
};

UserOptions.Gift.SelectNextGift = function(oContainer) {
    
    oContainer = $(oContainer);
    
    oContainer.hide();
    
    oElement = oContainer.next() ? oContainer.next() : $(oContainer.parentNode)
            .firstDescendant();
    
    oElement.show();
    
    $('gift_selected_id').value = $(oElement.id.split('_')).last();
};

// Deprecate
UserOptions.Gift.Post = function(iLiveCode) {
    
    oElements = Form.serialize(UserOptions.Settings.GiftForm, true);
    oElements.LiveCode = iLiveCode;
    
    oGiftContainer = $('gift_container_'
            + document.forms['Gift'].elements["Gift[GiftId]"].value);
    
    // check the first onload value
    oElementTipValue = oGiftContainer.readAttribute('amount');
    
    UserOptions.Gift.CheckCanAfford(oGiftContainer);
    
    if (UserOptions.Storage.iCanAfford) {
        new Ajax.Request('/ajax/user_options/tip/gift', {
            parameters : oElements,
            onLoading : function() {
                AnimatedOverlay.Open();
            },
            onSuccess : function(transport) {
                var oResponse = transport.responseJSON;
                if (Object.isUndefined(oResponse.Status) || !oResponse.Status) {
                    return;
                }
                AnimatedOverlay.LoadByContent(oResponse.HTML);
                UserOptions.Tip.NotifyVOCPublic(iLiveCode);
            }
        });
    }
};

// Deprecate
UserOptions.Gift.CheckCanAfford = function(oGiftContainer) {
    
    UserOptions.Storage.iCanAfford = false;
    
    if (Element.readAttribute(oGiftContainer, 'available') == 1) {
        UserOptions.Storage.iCanAfford = true;
        Element.hide(UserOptions.Settings.CantAfford);
        Element.show(UserOptions.Settings.GiftFormContainer);
    } else {
        Element.show(UserOptions.Settings.CantAfford);
        Element.hide(UserOptions.Settings.GiftFormContainer);
    }
    // console.log(Element.readAttribute(oOption, 'available'));
};

UserOptions.Gift.Send = function(iLiveCode, iGiftId, bSkip) {

    var sNote = bSkip ? null : $('op_comment_id').value;
    
    if(UserOptions.Storage.bIsProcessing) return;
    UserOptions.Storage.bIsProcessing = true;
    
    UserOptions.Tools.ShowPopUp(
        UserOptions.Settings.GiftTipRateContainer, 
        UserOptions.Settings.GiftTipRatingPopUpId, 
        "Sending..."
    );
    
    new Ajax.Request('/ajax/user_options/tip/gift/send', {
        parameters : {
            LiveCode : iLiveCode,
            GiftId : iGiftId,
            Note : sNote
        },
        onSuccess : function(transport) {
            var oResponse = transport.responseJSON;
            if (Object.isUndefined(oResponse.Status)) {
                oResponse.Status = 0;
            } 
            UserOptions.Gift.UpdateGiftList(oResponse.NewUserBalance);
            UserOptions.Tip.UpdateSelectList(oResponse.NewUserBalance);
            UserOptions.Tools.NotifyVOCPrivateGift(oResponse.GiftDescription);
            UserOptions.Tools.ShowPopUp(
                UserOptions.Settings.GiftTipRateContainer, 
                UserOptions.Settings.GiftTipRatingPopUpId, 
                oResponse.sStatusMessage
            );
            UserOptions.Tip.NotifyVOCPublic(iLiveCode);
        }
    });   

};

UserOptions.Gift.PresentComment = function(iLiveCode, iGiftId) {
    $('op_gift_skip').writeAttribute('onclick', "UserOptions.Gift.Send('" + iLiveCode + "', '" + iGiftId + "', 1)");
    $('op_gift_send').writeAttribute('onclick', "UserOptions.Gift.Send('" + iLiveCode + "', '" + iGiftId + "')");
    
    UserOptions.Tools.PresentComment($('op_gift_notes_box_id').innerHTML);
};

UserOptions.Gift.UpdateGiftList = function(iNewUserBalance) {
    if($(UserOptions.Settings.GiftContainerId)) {
        $(UserOptions.Settings.GiftContainerId).adjacent('span.op_gift_disabled').each(function(oElement){
            oAnchor = oElement.up();
            if(oElement.readAttribute('amount') > iNewUserBalance) {
                oElement.show();
                var sTitle = oAnchor.down('input.logged_in_disabled').readAttribute('title');
                oAnchor.writeAttribute('title', sTitle);
                oAnchor.writeAttribute('href', 'javascript: void(0);');
                oAnchor.observe('click', function() {
                    UserOptions.Tools.ShowPopUp(
                        UserOptions.Settings.GiftTipRateContainer, 
                        UserOptions.Settings.GiftTipRatingPopUpId, 
                        sTitle
                    );
                });
            } else {
                oElement.hide();
                oAnchor.writeAttribute('title', oAnchor.down('input.logged_in_enabled').readAttribute('title'));
                oAnchor.writeAttribute('href', oAnchor.down('input.logged_in_enabled').readAttribute('link'));
            }
        });
    }
};

UserOptions.Tip.NotifyVOCPublic = function(iLiveCode) {
    if (fn_video_operator_chat_free && fn_video_operator_chat_free.notifyUserTip) {
        fn_video_operator_chat_free.notifyUserTip(iLiveCode);
    }
};

UserOptions.Tools.NotifyVOCPrivateTip = function(iTipAmount) {
    if ((oFlash = $('free_video_chat')) || (oFlash = $('VideoOperatorChatViewer2'))) {
        oFlash.displayTipMessage(iTipAmount);
    }
};

UserOptions.Tools.NotifyVOCPrivateGift = function(sGiftMessage) {
    if ((oFlash = $('free_video_chat')) || (oFlash = $('VideoOperatorChatViewer2'))) {
        oFlash.displayGiftMessage(sGiftMessage);
    }
};

UserOptions.Tools.PresentComment = function(sInnerHTML) {
    UserOptions.Tools.ShowPopUp(
        UserOptions.Settings.GiftTipRateContainer, 
        UserOptions.Settings.GiftTipRatingPopUpId, 
        sInnerHTML,
        'op_comment_box'
    );
        
    $$('#op_gift_tip_rate_message_box textarea').each(function(oTextArea) {
        oTextArea.writeAttribute('id', 'op_comment_id');
        oTextArea.insert({after: new Element('div').insert({'top': new Element('span', {'class' : 'bold', 'id' : 'op_message_count'})})});
    });
}

UserOptions.Tools.ShowPopUp = function(sContainerId, sPopId, sInnerHTML, sClassName) {
    
    var sClassName = sClassName || ''; 
    
    var oCloseButton = new Element('div', {id : sPopId + '_close'}).update('X');
    
    var oSPopid = new Element('div', {'id' : sPopId, 'style' : 'display: none', 'class' : sClassName});
  
    if(!$(sPopId)) {
        $(sContainerId).insert({
            bottom: oSPopid
                .insert({
                    top : oCloseButton,
                    bottom : sInnerHTML
                })
        });
    } else {
        if(!sClassName) {
            $(sPopId).classNames().each(function(sOldClassName) {
                $(sPopId).removeClassName(sOldClassName);
            });
        } else {
            $(sPopId).addClassName(sClassName);
        }
        $(sPopId).update();
        $(sPopId).insert({
            top : oCloseButton,
            bottom : sInnerHTML
        });
    }
    
    if($(sPopId).style.display == 'none') {
        Effect.toggle(sPopId, 'appear');
    }

    var iTimeOut = setTimeout(function(){
        UserOptions.Storage.bIsProcessing = false;
        clearTimeout(iTimeOut);
    }, 3000);
    
    oCloseButton.observe('click', function() {
        Effect.toggle(sPopId, 'appear');
        UserOptions.Storage.bIsProcessing = false;
    });
    
};
