﻿function getPrice(url) { location.href = url };
function initDialog(idDialog) {
    $(function() {
        $("#" + idDialog).dialog
        ({
            autoOpen: false,
            modal: true,
            stack: true/*,

            buttons:
            {
                'Ok': function () {
                    $(this).dialog('close');
                }
            }   */
        });
    });
};
    var dataSrc;
    var currIndex = -1;
    function openDlgExt(idCat, number, idDialog, idImg) {
        var handlerOK =

        $.ajax({
            url: "/Catalog/General/PathImage",
            type: "GET",
            cache: false,
            data: {
                idCatalog: idCat,
                number: number
            },
            error: function (msg) {
                $(this).dialog("close");
            },
            success: function (data) {
                dataSrc = data;
                for (var i = 0; i < data.length; i++) {
                    if (data.length > 1) {
                        initButtonOkNext(idDialog, idImg);
                    }
                    else {
                        $("#" + idDialog).dialog(/*"option", "buttons", {
                            'Ok': function () {
                                    $("#" + idImg).attr("src", "");
                                    $(this).dialog('close');
                                 }
                        }*/);
                    }
                    currIndex = i;
                    var handler = function () {
                        $("#" + idDialog).dialog("open");
                        $("#" + idDialog).dialog("option", { "width": this.width + 50, "height": 'auto' });
                        $("#" + idImg).unbind("load");
                    };
                    $("#" + idImg).bind("load", handler);
                    $("#" + idImg).attr("src", GrlogalAction + data[i]);
                    break;
                }
            }
        });
    };

    var GrlogalAction = "/Watermark/GetImage?filename=";
    function initButtonOkNext(idDialog, idImg) {
        $("#" + idDialog).bind("dialogclose", function (event, ui) {
            $('.ui-dialog-buttonpane').remove();
        });
        $("#" + idDialog).dialog("option", "buttons", {
            'Ok': function () {
                $("#" + idImg).attr("src", "");
                $(this).dialog('close');
            },
            '>>': function () {
                if (currIndex < dataSrc.length - 1) {
                    currIndex++;
                    $("#" + idImg).attr("src", GrlogalAction + dataSrc[currIndex]);
                    if (currIndex == dataSrc.length - 1) {
                        initButtonOkPrev(idDialog, idImg);
                    }
                    else {
                        initButtonOkPrevNext(idDialog, idImg);
                    }
                }
            }
        });
    }


    function initButtonOkPrevNext(idDialog, idImg) {
        $("#" + idDialog).dialog("option", "buttons", {
            'Ok': function () {
                $("#" + idImg).attr("src", "");
                $(this).dialog('close');
            },
            '<<': function () {
                if (currIndex > 0) {
                    currIndex--;
                    $("#" + idImg).attr("src",GrlogalAction + dataSrc[currIndex]);
                    if (currIndex == 0) {
                        initButtonOkNext(idDialog, idImg);
                    }
                    else {
                        initButtonOkPrevNext(idDialog, idImg);
                    }
                }
            },
            '>>': function () {
                if (currIndex < dataSrc.length - 1) {
                    currIndex++;
                    $("#" + idImg).attr("src", GrlogalAction + dataSrc[currIndex]);
                    if (currIndex == dataSrc.length - 1) {
                        initButtonOkPrev(idDialog, idImg);
                    }
                    else {
                        initButtonOkPrevNext(idDialog, idImg);
                    }
                }
            }
        });            
    }
    function initButtonOkPrev(idDialog, idImg)
   {
        $("#" + idDialog).dialog( "option", "buttons", {
            'Ok': function () {
                $("#" + idImg).attr("src", "");
                    $(this).dialog('close');
                },
                '<<': function () {
                    if (currIndex > 0) {
                        currIndex--;
                        $("#" + idImg).attr("src", GrlogalAction + dataSrc[currIndex]);
                        if (currIndex == 0) {
                            initButtonOkNext(idDialog, idImg);
                        }
                        else {
                            initButtonOkPrevNext(idDialog, idImg);
                        }
                    }
                }
            });    
   }

    function openDialog(path, idDialog, idImg) {
        $("#" + idImg).attr("src", GrlogalAction+ path);
        $("#" + idDialog).dialog({ disabled: false });
        $("#" + idDialog).dialog("open");
    };

    var handlerNext = function () {
        if (currIndex < dataSrc.length - 1) {
            currIndex++;
            $("#" + idImg).attr("src", GrlogalAction + dataSrc[currIndex]);
            if (currIndex == dataSrc.length - 1) {
                initButtonOkPrev(idDialog, idImg);
            }
            else {
                initButtonOkPrevNext(idDialog, idImg);
            }
        }
    };
