﻿window.BT.Common = new function () {
    this.AddFavourite = function (type, id) {
        BT.Ajax.GetJSON("/ajax/favourite/add/", { et: type, eid: id }, Favourite_Callback, null, true);
    };

    function Favourite_Callback(json) {
        if (json.Success == true) {
            window.BT.Common.GetFavouriteCount(json.Type, json.Id);
        }
        else {
            window.BT.Common.GetFavouriteCount(json.Type, json.Id);
        }
    }

    this.GetFavouriteCount = function (type, id) {
        BT.Ajax.GetJSON("/ajax/favourite/GetCount/", { et: type, eid: id }, GetFavouriteCount_Callback, null, true);
    };

    function GetFavouriteCount_Callback(json) {
        $('#FavouriteCount' + json.Id).text(json.Count);
    }

    this.Report = function (entityType, entityId) {
        $('<div><span>Varför vill du raportera?</span><textarea id="txtReportReason"></textarea></div>').dialog({
            modal: true,
            width: 640,
            height: 400,
            buttons: {
                'Ok': function () {
                    var reason = $("#txtReportReason").val();
                    if (reason == null || reason.length == 0) {
                        return;
                    }
                     BT.Ajax.GetJSON("/ajax/report/add/", { et: entityType, eid: entityId, r: reason }, Report_Callback, null, true);
                    $(this).dialog('close');
                },
                'Cancel': function () { $(this).dialog('close'); }
            }
        });
    };

    function Report_Callback() { }

    this.OpenUserProfile = function (userId, eventId) {
        if (eventId != 0) {
            $('#' + eventId).dialog('close');
        }
        window.location = "/Communication/Contacts/ContactProfile/" + userId;
    }
}
//get Querystring
//key queryString Name
//default_ default value if not found
//form url hash or not

window.BT.Common.getQuerystring = function (key, default_, urlVal) {

    var url = urlVal;

    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(url);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

window.BT.Common.OpenAdsLayer = function (width, height, url, groupId) {
    $.FrameDialog.create({ url: url + "?groupId=" + groupId,
        modal: true,
        draggable: false,
        resizable: false,
        autoresize: false,
        width: width,
        closeOnEscape: true,
        height: height
    });
}
window.BT.Common.ImageCropUpload = function (width, height, fileType, fieldName, hasAlt, image, isPortal, unitName) {
    // Portal Images
    if (isPortal) {
        height = $('#Height').val();
        width = $('#Width').val();
        if (unitName == 'imagepanel') {
            width = width - 14;
            height = height - 14;
        }
        else {
            // Single
            if (width == 230) {
                width = 216;
                height = 153;
            }
            // Double
            else if (width == 470) {
                if (height > 300) {
                    width = 456;
                    height = 153;
                }
                else {
                    width = 216;
                    height = 153;
                }
            }
        }
    }
    var query = '?ft=' + fileType + ((width > 0) ? '&ow=' + width : "") + ((height > 0) ? '&oh=' + height : "") + ((image && image.length > 0) ? "&m=edit&f=" + image : "") + "&hasAlt=" + (hasAlt ? "1" : "0");
    var dialogCloseHandler = ImageCrop_Callback;
    if (typeof (arguments[arguments.length - 1]) == "function")
        dialogCloseHandler = arguments[arguments.length - 1];

    $.FrameDialog.create({ url: '/crop/Start' + query,
        modal: true,
        draggable: false,
        resizable: false,
        autoresize: false,
        width: 480,
        closeOnEscape: true,
        height: 600
    })
    .bind('dialogclose', dialogCloseHandler);

    function ImageCrop_Callback(event, ui) {
        if (event.result != null) {
            $("#" + fieldName).val(event.result.file);
            $("#Image_" + fieldName).attr("src", "/upload/" + event.result.file.replace("upload/", "")).parent().show();
        }
        else {
            $("#" + fieldName).val('');
            $("#Image_" + fieldName).attr("src", "");
        }
    }
};
