﻿function renderPhotoMenu(photoID, encMediaID, shortDate, albumType) {

    var oMenu;

    if (YAHOO.widget.MenuManager.getMenu("basicmenu") !== undefined) {
        oMenu = YAHOO.widget.MenuManager.getMenu("basicmenu");
        try {
            oMenu.destroy();
            oMenu = null;
            YAHOO.util.Event.purgeElement($get('photo_info_' + photoID), true);
        } catch (e) { }
    }

    oMenu = new YAHOO.widget.Menu("basicmenu", { zindex: 21000, position: 'dynamic', shadow: true, clicktohide: false, lazyload: true, iframe: false });

    var oOuterContainer = document.getElementById('photo_scrollbox');  // Important!  Used for Webkit based browser scroll fix.
    
    function Browse(p_sType, p_aArgs, p_oValue) {
        location.href = ("../album/photos_view.aspx?q=" + p_oValue);
    }

    function AddToCart(p_sType, p_aArgs, p_oValue) {
        var cartService = new ICartService();
        cartService.OrderEntireAlbum(p_oValue, Redirect, gotError);
    }

    function ShowSlideShow(p_sType, p_aArgs, p_oValue) {
        location.href = "../album/slideshow_preview.aspx?q=" + p_oValue;
    }

    function EditAlbum(p_sType, p_aArgs, p_oValue) {
        location.href = "../album/edit_album.aspx?q=" + p_oValue;
    }

    function AddMorePhotos(p_sType, p_aArgs, p_oValue) {
        location.href = "../upload/copyright.aspx?q=" + p_oValue;
    }

    function Redirect(result) {
        if (result) {
            location.href = RootUrl + result;
        }
    }

    function gotError() {

    }
    
    /* ----- Photo Specific Methods ---- */

    function OrderPrints(p_sType, p_aArgs, p_oValue) {
        showBusyPopup('processing', 'adding to cart...');
        albumService.AddToShoppingCart(p_oValue, UpdateCartCount, gotError);
    }

    function UpdateCartCount(result) {
        if (result) {
            UpdateCartItemLabel(result);
        }
        if (hideBusyPopup) {
            hideBusyPopup();
        }
    }
    
    
       
    
    function EditPrint(p_sType, p_aArgs, p_oValue) {
        GoToMediaEdit(p_oValue)
    }

    function AddToFavorites(p_sType, p_aArgs, p_oValue) {
        AddToFavsByMediaID(p_oValue);
    }

    function AddBorders(p_sType, p_aArgs, p_oValue) {
        EditBorderByMediaID(p_oValue)
    }

    function RotateAndFlip(p_sType, p_aArgs, p_oValue) {
        RotateMedia(p_oValue);
    }

    function MoveTo(p_sType, p_aArgs, p_oValue) {
        CopyMoveMedia(p_oValue, false);
    }

    function CopyTo(p_sType, p_aArgs, p_oValue) {
        CopyMoveMedia(p_oValue, true);
    }

    function DeletePrint(p_sType, p_aArgs, p_oValue) {
        DeleteMediaByMediaID(p_oValue, false);
    }

    function RemoveFromFavorites(p_sType, p_aArgs, p_oValue) {
        DeleteMediaByMediaID(p_oValue, true);
    }

    function RenamePhotos(p_sType, p_aArgs, p_oValue) {
        RenameMedia(p_oValue);
    }

    function SetChecked(mediaID, checked) {
        var media_input = $get("sel_" + mediaID);

        if (media_input.checked == checked) {
            return;
        } else {
            CheckMedia(mediaID);
        }
        return;
    }
    
   

    /*
    Add items to the Menu instance by passing an array of object literals 
    (each of which represents a set of YAHOO.widget.MenuItem 
    configuration properties) to the "addItems" method.
    */

    var year = (String)(shortDate).slice(0, 4);

    oMenu.clearContent();

    // Add menu items
    switch (albumType){
        case "albums": 
            {
                oMenu.addItems([
                { text: "order print", onclick: { fn: OrderPrints, obj: photoID }, selected: true },
                { text: "view & edit", onclick: { fn: EditPrint, obj: photoID} },
                { text: "rename", onclick: { fn: RenamePhotos, obj: photoID} },
                { text: "add to favorites", onclick: { fn: AddToFavorites, obj: photoID} },
                { text: "add borders", onclick: { fn: AddBorders, obj: photoID} },
                { text: "rotate & flip", onclick: { fn: RotateAndFlip, obj: photoID} },
                { text: "move to...", onclick: { fn: MoveTo, obj: photoID} },
                { text: "copy to...", onclick: { fn: CopyTo, obj: photoID} },
                { text: "delete", onclick: { fn: DeletePrint, obj: photoID} }

            ]);
            break;
            }
        case "friends":
            {
                oMenu.addItems([
                { text: "order print", onclick: { fn: OrderPrints, obj: photoID }, selected: true },
                { text: "copy to...", onclick: { fn: CopyTo, obj: photoID} },
                { text: "delete", onclick: { fn: DeletePrint, obj: photoID} }

            ]);
                break;
            }
        case "groups":
            {
                oMenu.addItems([
                { text: "order print", onclick: { fn: OrderPrints, obj: photoID }, selected: true },
                { text: "copy to...", onclick: { fn: CopyTo, obj: photoID} }
            ]);
                break;
            }
        case "favorites":
            {
                oMenu.addItems([
                { text: "order print", onclick: { fn: OrderPrints, obj: photoID }, selected: true },
                { text: "view & edit", onclick: { fn: EditPrint, obj: photoID} },
                { text: "rename", onclick: { fn: RenamePhotos, obj: photoID} },
                { text: "remove from favorites", onclick: { fn: RemoveFromFavorites, obj: photoID} },
                { text: "add borders", onclick: { fn: AddBorders, obj: photoID} },
                { text: "rotate & flip", onclick: { fn: RotateAndFlip, obj: photoID} }
            ]);
                break;
            }
    }

    function onShow(p_sType, p_aArgs) {
        $get("photo_thumb_" + photoID).onmouseout = function() { photoMouseOver(this) };
        $get("photo_thumb_" + photoID).onmouseover = function() { photoMouseOver(this) };
    }

    function onHide(p_sType, p_aArgs) {
        $get("photo_thumb_" + photoID).onmouseout = function() { photoMouseOut(this) };
        $get("photo_thumb_" + photoID).onmouseover = function() { photoMouseOver(this) };
        $get("img_photo_menu_" + photoID).style.display = 'none';
    }

    function photoMouseOut(e) {
        $get("img_photo_menu_" + photoID).style.display = 'none';
    }

    function photoMouseOver(e) {
        $get("img_photo_menu_" + photoID).style.display = 'block';
    }


    oMenu.subscribe("show", onShow);
    oMenu.subscribe("hide", onHide);



    YAHOO.util.Event.addListener("photo_info_" + photoID, "click",
                             function(e) {
                                 aXY = YAHOO.util.Event.getXY(e);
                                 aXY[0] += 1;
                                 aXY[1] += 1;
                                 this.cfg.setProperty('XY', aXY);
                                 this.cfg.setProperty('hidedelay', 100);
                                 YAHOO.widget.MenuManager.hideVisible();
                                 this.show();
                             },
                            null,
                            oMenu);
                            

    function getScrollAmount(p_oElement, p_sProperty, p_nTotalScrollAmount) {
        var nTotalScrollAmount = p_nTotalScrollAmount || 0,
        nElScrollAmount = p_oElement[p_sProperty],
        oParentNode;

        if (nElScrollAmount && YAHOO.util.Dom.getStyle(p_oElement, "position") == "static") {
            nTotalScrollAmount += nElScrollAmount;
        }

        oParentNode = p_oElement.parentNode;

        if (oParentNode && oParentNode != document.body) {
            return getScrollAmount(oParentNode, p_sProperty, nTotalScrollAmount);
        } else {
            return nTotalScrollAmount;
        }
    }

    function onYChange(p_sType, p_aArgs, p_oButton) {

        var nY = this.cfg.getProperty("y"),
        oIFrame;

        if (nY) {
            nY = (nY - getScrollAmount(oOuterContainer, "scrollTop"));
            YAHOO.util.Dom.setY(this.element, nY);

            // Sync the position of the iframe shim if it is enabled
            oIFrame = this.iframe;

            if (oIFrame) {
                YAHOO.util.Dom.setY(oIFrame, nY);
            }

            // Sync the value of the "y" property to the new, correct value
            this.cfg.setProperty("y", nY, true);
        }

    }

    if ((!YAHOO.env.ua.ie) && (!(YAHOO.env.ua.gecko && YAHOO.env.ua.gecko >= 1.9))) {
        oMenu.cfg.subscribeToConfigEvent("y", onYChange, this);
    }

    oMenu.render("photo_info_" + photoID);

    $get("photo_info_" + photoID).onclick;
}

