﻿    var menuFired = false;

	function OnMenuItemClick(s, e){
	    
	    menuFired = true;
	    
	    setTimeout("menuFired = false;",1000);
	    
	    var item = e.get_item();
	    var baseURL = clientScriptPathInfo.ApplicationPath + '/';
	    var attributes = item.get_attributes();
        var itemArg = attributes.getAttribute('arg');
	    
	    switch (attributes.getAttribute('command')){
    
	        case 'friend':
	            return true;
	            break;	        
	        case 'ignore':
	            if (confirm(blockMemberWarn)) { return true; }
	            break;
	        case 'unignore':
                return true;
	            break;
       	            
	        case 'pm':
	            var gotoURL = baseURL + 'pmsend.aspx?toMemId=' + itemArg ;
	            if (currentMemberInfo.popupPosting) {
	                popRadWin(gotoURL + '&pop=true');
	            } else {
	                self.location.href = gotoURL;
	            }
	            
	            break;	 
	        case 'reply':
	            var gotoURL = baseURL + String.format('post.aspx?mq={0}&messageID=', multiQuoteIDs.join()) + itemArg ;
	            if (currentMemberInfo.popupPosting) {
	                popRadWin(gotoURL + '&pop=true');
	            } else {
	                self.location.href = gotoURL;
	            }
	            
	            break;	
	        case 'quote':
	            var gotoURL = baseURL + String.format('post.aspx?quote=true&mq={0}&messageID=', multiQuoteIDs.join()) + itemArg ;
	            if (currentMemberInfo.popupPosting) {
	                popRadWin(gotoURL + '&pop=true');
	            } else {
	                self.location.href = gotoURL;
	            }
	            
	            break;	
	        case 'edit':
	            var gotoURL = baseURL + 'post.aspx?edit=true&messageID=' + itemArg ;
	            if (currentMemberInfo.popupPosting) {
	                popRadWin(gotoURL + '&pop=true');
	            } else {
	                self.location.href = gotoURL;
	            }
	            
	            break;		            

	        case 'ban':
	            var gotoURL = baseURL + 'tempban.aspx?messageID=' + itemArg;
	            popRadWin(gotoURL, 600, 450);

	            break;	            
	            
	        case 'multiquote':
	            AddMultiQuote(itemArg);
	            break;

	        case 'search':
	            self.location.href = item.get_navigateUrl();
	            break;

	    }

	    e.set_cancel(true);
	    hideAllMenus();
	    return false;
	}


	function ShowMenu(e, id, isSelf, login, isFriend, isIgnored, viewerIsGuest, memberPMable, isBanAllowed, isDeleted, msgID, theTarget) {
	    
	    if (menuFired || typeof menu == 'undefined' || !menu) return;

	    menuShowTimeout = setTimeout(function () {

	        hideAllMenus(190);

	        with (menu) {
	            var frienditem = findItemByValue("friend");
	            var pmitem = findItemByValue("pm");
	            var blockitem = findItemByValue("block");
	            var banitem = findItemByValue("ban");
	            var searchpostitem = findItemByValue("searchposts");
	        }

	        var menuImgURL = clientScriptPathInfo.ApplicationPath + '/' + pageThemeImageURL;

	        var oneItemVisible = false;

	        with (frienditem) {
	            set_imageUrl(menuImgURL + 'menuAdd.gif');
	            set_text(String.format(FriendMemText, ''));

	            menu.trackChanges();
	            get_attributes().setAttribute('command', 'friend'); get_attributes().setAttribute('arg', id);
	            if (isFriend || isIgnored || isSelf || viewerIsGuest) hide(); else { show(); oneItemVisible = true; }
	            menu.commitChanges();
	        }

	        with (blockitem) {
	            var blockItemText = isIgnored ? UnBlockMemText : BlockMemText;
	            var blockItemCommand = isIgnored ? 'unignore' : 'ignore';
	            set_imageUrl(menuImgURL + (isIgnored ? 'menuUnblock.gif' : 'menuBlock.gif'));
	            set_text(String.format(blockItemText, ''));

	            menu.trackChanges();
	            get_attributes().setAttribute('command', blockItemCommand); get_attributes().setAttribute('arg', id);
	            if (isSelf || viewerIsGuest) hide(); else { show(); oneItemVisible = true; }
	            menu.commitChanges();
	        }

	        with (pmitem) {
	            set_imageUrl(menuImgURL + 'menuPM.gif');

	            set_text(String.format(PmMemText, login));
	            set_text(String.format(PmMemText, ''));
	            get_attributes().setAttribute('command', 'pm'); get_attributes().setAttribute('arg', id);

	            if (isSelf || viewerIsGuest || !memberPMable) hide(); else { show(); oneItemVisible = true; }

	        }

	        with (searchpostitem) {
	            set_imageUrl(menuImgURL + 'menuSearch.gif');

	            set_text(RecentPostText);

	            set_navigateUrl(clientScriptPathInfo.ApplicationPath + '/search.aspx?author=' + escape(login));
	            get_attributes().setAttribute('command', 'search');

	            if (isIgnored || viewerIsGuest || isDeleted) hide(); else { show(); oneItemVisible = true; }

	        }

	        if (typeof isBanAllowed != "undefined") {

	            with (banitem) {
	                set_text(TempBanText); get_attributes().setAttribute('command', 'ban'); get_attributes().setAttribute('arg', msgID);
	                set_imageUrl(menuImgURL + 'menuBan.gif');
	                if (isBanAllowed && !isDeleted && !isSelf) { show(); oneItemVisible = true; } else { hide(); }
	            }

	        }

	        if (oneItemVisible) menuShow(e, menu, theTarget);

	    }, 200);
    }

    function showOptionMenu(e, id, showReply, showMultiQuote, showEdit, isSelfPost, hasReplyPermission, theTarget) {

        if (menuFired || typeof menu == 'undefined' || !menu) return;

        menuShowTimeout = setTimeout(function () {

            hideAllMenus(110);
            SetMessageID(id);

            var oneItemVisible = false;

            with (menu) {
                var edititem = findItemByValue("edit");
                var quoteitem = findItemByValue("quote");
                var multiquoteitem = findItemByValue("multiquote");
            }

            var menuImgURL = clientScriptPathInfo.ApplicationPath + '/' + pageThemeImageURL;

            if (showReply && !isSelfPost) {
                // this is the click handler for the first menu item "Reply To Message"
                $(theTarget).unbind('click').bind('click.replyclick', function () { replySimulatedClick(id, hasReplyPermission); hideAllMenus(); return false; });
            }
            else if (showEdit && isSelfPost) {
                // this is the click handler for the first menu item "Edit Message"
                $(theTarget).unbind('click').bind('click.editclick', function () { editSimulatedClick(id); hideAllMenus(); return false; });
            }

            with (quoteitem) {
                set_text(QuoteMessageText); get_attributes().setAttribute('command', 'quote'); get_attributes().setAttribute('arg', id);
                set_imageUrl(menuImgURL + 'menuQuote.gif');
                if (showReply && !isSelfPost) { show(); oneItemVisible = true; } else { hide(); }
            }

            with (multiquoteitem) {
                set_text(MultiquoteText); get_attributes().setAttribute('command', 'multiquote'); get_attributes().setAttribute('arg', id);
                set_imageUrl(menuImgURL + 'menuMultiQuote.gif');
                if (showMultiQuote) { show(); oneItemVisible = true; } else { hide(); }
            }

            with (edititem) { //this item works as a Reply when isSelfPost is true, and Edit when showReply is true

                // the reason we check the isSelfPost param is we already have the Reply / Edit button as the first Menu Item 
                // and therefore there is no need to show same sub item again

                if (showReply && isSelfPost) {
                    set_text(ReplyToMessageText); get_attributes().setAttribute('command', 'reply'); get_attributes().setAttribute('arg', id);
                    set_imageUrl(menuImgURL + 'menuReply.gif');
                    show();
                    oneItemVisible = true;                
                }
                else if (showEdit && !isSelfPost) {

                    set_text(EditMessageText); get_attributes().setAttribute('command', 'edit'); get_attributes().setAttribute('arg', id);
                    set_imageUrl(menuImgURL + 'menuEdit.gif');
                    show();
                    oneItemVisible = true;
                } else {
                    hide();
                }
            }

            if (oneItemVisible) menuShow(e, menu, theTarget);
        }, 200);
	}


	function hideAllMenus(widthForImg){
	
	    if (typeof menu == 'undefined' || !menu) return;
        
        setNono(menu, widthForImg);
        
        for (var i = 0; i < menu.get_items().get_count(); i++)
        {
           var theItem = menu.get_items().getItem(i);
           if (theItem.get_value() != 'nono') theItem.hide();
        }

        menu.hide();

    }

