﻿$ldc(function() {
    $ldc('#ldcWriteReviewDiv').dialog({ stretchIFrame: true, resizable: false, modal: true, width: 600, autoOpen: false });
    $ldc('#ldcReadReviewsDiv').dialog({ stretchIFrame: true, resizable: true, modal: true, width: '75%', height: 500, autoOpen: false,
        buttons: {
            "Close": function() {
                $ldc(this).dialog('close');
                if ($ldc(this).attr('reloadId') != null) ldc_ReloadReviewControl(null, null, null, $ldc(this).attr('reloadId'));
            }
        }
    });

    // Localize the close button text
    try {
        if (rvw_js_CloseText != undefined) $ldc($ldc("button", $ldc("#ldcReadReviewsDiv").parent())[0]).text(rvw_js_CloseText);
    } catch (x) {
        // Do nothing
    }
});

function ldc_ShowWriteReview(sSessionId, itemtypeid, itemid) {
    var objFrame = $ldc('#ldcWriteReviewDiv').dialog('open').find('iframe').get(0);
    objFrame.src = '/LCNet/ControlPanel/Social/WriteReview.aspx?sessionid=' + sSessionId + '&itemid=' + 
                        itemid + '&itemtype=' + itemtypeid + '&id=0&DCT=1&overlay=ldcWriteReviewDiv' +
                        (arguments.length > 3 ? ((''+arguments[3]).length > 0 ? '&reloadonsave='+arguments[3] : '') : '') +
                        (arguments.length > 4 ? ((''+arguments[4]).length > 0 ? '&v='+arguments[4] : '') : '') +
                        (arguments.length > 5 ? ((''+arguments[5]).length > 0 ? '&vs='+arguments[5] : '') : '') +
                        (arguments.length > 6 ? ((''+arguments[6]).length > 0 ? '&it='+arguments[6] : '') : '') +
                        (arguments.length > 7 ? (('' + arguments[7]).length > 0 ? '&rsid=' + arguments[7] : '') : '') +
                        (arguments.length > 8 ? (('' + arguments[8]).length > 0 ? '&readmodal=' + arguments[8] : '') : '') +
                        (arguments.length > 9 ? (('' + arguments[9]).length > 0 ? '&cfip=' + arguments[9] : '') : '');
}

function ldc_ReloadReviewControl(sSessionId, itemtypeid, itemid, sFrameId) {
    var objFrame = null;

    if (sFrameId != null) {
        // Check if we are to reference this element in an iframe
        if (sFrameId.indexOf('$') >= 0) {
            // 0 index = iframe Id, 1 index = element id or class name
            var idSplit = sFrameId.split('$');
            objFrame = $ldc('#' + idSplit[0]).contents().find('#' + idSplit[1]);
            if (objFrame.length <= 0) objFrame = $ldc('#' + idSplit[0]).contents().find('.' + idSplit[1]);
        }
        else {
            // Get reference to element by its Id, otherwise, by the class name
            objFrame = $ldc('#' + sFrameId);
            if (objFrame.length <= 0) objFrame = $ldc('.' + sFrameId);
        }

        // Make sure we have an object to work with at least.
        if (objFrame.length > 0) {
            objFrame.each(function() {
                // Object to reload is in iframe, simply reload it by resetting the source again
                if ($ldc(this).attr('src') != null)
                    $ldc(this).attr('src', $ldc(this).attr('src'));  // reload the frame
                else {
                    // Object to reload is a container element, use Ajax to load contents and inject into container
                    $ldc(this).load('/LCNet/Services/Social/ReviewWidget.aspx?' + $ldc(this).attr('reviewparams'));
                }
            });
        }
    }
}

function ldc_ShowReadReviews(sSessionId, nItemType, sItemId) {
    var objFrame = $ldc('#ldcReadReviewsDiv').dialog('open').find('iframe').get(0);
    var strSrc = '/LCNet/ControlPanel/Social/ReadReviews.aspx?sessionid=' + sSessionId + '&itemid=' + sItemId + '&type=' + nItemType + '&id=0';//&DCT=1&overlay=ldcReadReviewsDiv';
    if (arguments.length > 3) $ldc('#ldcReadReviewsDiv').attr('reloadId', arguments[3]); //strSrc += '&reloadonsave=' + arguments[3];
    objFrame.src = strSrc;

    // Localize the close button text
    try {
        if (rvw_js_CloseText != undefined) $ldc($ldc("button", $ldc("#ldcReadReviewsDiv").parent())[0]).text(rvw_js_CloseText);
    } catch (x) {
        // Do nothing
    }
}
