﻿var DE_NewImage = { "Id": 0, "FileName": "", "Description": "", "ImagePath": "", "Width": 0, "Height": 0, "ImageType": "", "LinkType": "", "Link": "", "OwnerType": "", "OwnerId": 0, "DateAdded": "1900-01-01T05:00:00Z", "FileType": "" };
var images;
var SMALLBANNERIMAGES = null;
var BIGBANNERIMAGES = null;
var POLAROIDIMAGES = null;
var CUSTOMIMAGES = null;
var STOREIMAGES = null;
var DE_SelectedImage = null;

var DE_controlId;
var DE_imagetype;
var DE_title;
var DE_editlayer;
var DE_finishaction;
var DE_closeaction;
var DE_previousaction;
var DE_nextaction;
var DE_param1;
var DE_param2;

function DE_ShowImageEditor(controlId, imagetype, title, editlayer, finishaction, closeaction, previousaction, nextaction, param1, param2) {
    var editorwidth = 800;
    var editorheight = 450;
    DE_SelectedImage = null;
    var controlconfig = GetControlConfigData("DAMEDITOR");
    if (!param1) {
        param1 = '';
    }
    if (!param2) {
        param2 = '';
    }
    DE_controlId = controlId;
    DE_imagetype = imagetype;
    DE_title = title;
    DE_editlayer = editlayer;
    DE_finishaction = finishaction;
    DE_closeaction = closeaction;
    DE_previousaction = previousaction;
    DE_nextaction = nextaction;
    DE_param1 = param1;
    DE_param2 = param2;
    var data0;
    var view;
    
    var refreshimages = false;
    switch(imagetype)
    {
        case 'SMALLBANNER':
            editorwidth = 885;
            if (SMALLBANNERIMAGES == null) {
                data0 = { 'imageType': 'SMALLBANNER', 'storeId': controlconfig.StoreId };
                refreshimages = true;
            }
            else {
                images = SMALLBANNERIMAGES;
            }
            view = de_smallbanner_picker;
            break;
        case 'BIGBANNER':
            editorwidth = 885;
            if (BIGBANNERIMAGES == null) {
                data0 = { 'imageType': 'BIGBANNER', 'storeId': controlconfig.StoreId };
                refreshimages = true;
            }
            else {
                images = BIGBANNERIMAGES;
            }
            view = de_bigbanner_picker;
            break;
        case 'CUSTOM':
        if(CUSTOMIMAGES == null)
        {
            data0 = { 'imageType': 'CUSTOM', 'storeId': controlconfig.StoreId };
            refreshimages = true;
        }
        else {
            images = CUSTOMIMAGES;
        }
        view = de_custom_picker;
        break;
        case 'POLAROID':
        if(POLAROIDIMAGES == null)
        {
            data0 = { 'imageType': 'POLAROID', 'storeId': controlconfig.StoreId };
            refreshimages = true;
        } 
        else {
            images = POLAROIDIMAGES;
        }
        view = de_polaroid_picker;
        break;
    case 'STOREPHOTO':
        editorwidth = 885;
        if (STOREIMAGES == null) {
            data0 = { 'imageType': 'STOREPHOTO', 'storeId': controlconfig.StoreId };
            refreshimages = true;
        }
        else {
            images = STOREIMAGES;
        }
        view = de_storephoto_picker;
        break;
    default:
        data0 = '';
        break;
}
if (refreshimages == true) {
    var json = JSON.stringify(data0);

    $.ajax({
        type: "POST",
        url: "/Model/DAM/model.asmx/GetImagesByImageTypeForStore",
        data: json,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        dataFilter: function(data, type) {
            return data.replace(/"\\\/(Date\([0-9-]+\))\\\/"/gi, 'new $1');
        },
        success: function(msg) {
            switch (imagetype) {
                case 'SMALLBANNER':
                    SMALLBANNERIMAGES = clone_obj(msg.d);
                    images = SMALLBANNERIMAGES;
                    break;
                case 'BIGBANNER':
                    BIGBANNERIMAGES = clone_obj(msg.d);
                    images = BIGBANNERIMAGES;
                    break;
                case 'CUSTOM':
                    CUSTOMIMAGES = clone_obj(msg.d);
                    images = CUSTOMIMAGES;
                    break;
                case 'POLAROID':
                    POLAROIDIMAGES = clone_obj(msg.d);
                    images = POLAROIDIMAGES;
                    break;
                case 'STOREPHOTO':
                    STOREIMAGES = clone_obj(msg.d);
                    images = STOREIMAGES;
                    break;
                default:
                    break;
            }
            if (DE_param2 != '') {
                images = DE_CheckAddBlank(clone_obj(images))
            }
            var data = { "images": images,
                "editorwidth": editorwidth,
                "editorheight": editorheight,
                "title": title,
                "finishaction": finishaction,
                "closeaction": closeaction,
                "nextaction": nextaction,
                "previousaction": previousaction
            };
            var result = TrimPath.parseTemplate(view).process(data);
            if (editlayer == 1) {
                document.getElementById('V2editControl').innerHTML = result;
                $('#V2editBackground').show();
                $('#V2editControl').show();
                if (DE_imagetype == 'SMALLBANNER' || DE_imagetype == 'BIGBANNER') {
                    initBannerTabs();
                }
                centerObject('V2editControl');
            }
            else {
                document.getElementById('V2editControl').innerHTML = result;
                $('#V2editBackground' + editlayer).show();
                $('#V2editControl' + editlayer).show();
                if (DE_imagetype == 'SMALLBANNER' || DE_imagetype == 'BIGBANNER') {
                    initBannerTabs();
                }
                centerObject('V2editControl' + editlayer);
            }
        }
    });
}
else {
    if (DE_param2 != '') {
        images = DE_CheckAddBlank(clone_obj(images))
    }
    var data = { "images": images,
    "editorwidth": editorwidth,
    "editorheight": editorheight,
    "title": title,
    "finishaction": finishaction,
    "closeaction": closeaction,
    "nextaction": nextaction,
    "previousaction": previousaction
    };
    var result = TrimPath.parseTemplate(view).process(data);
    if (editlayer == 1) {
        document.getElementById('V2editControl').innerHTML = result;
        $('#V2editBackground').show();
        $('#V2editControl').show();
        if (DE_imagetype == 'SMALLBANNER' || DE_imagetype == 'BIGBANNER') {
            initBannerTabs();
        }
        centerObject('V2editControl');
    }
    else {
        document.getElementById('V2editControl').innerHTML = result;
        $('#V2editBackground' + editlayer).show();
        $('#V2editControl' + editlayer).show();
        if (DE_imagetype == 'SMALLBANNER' || DE_imagetype == 'BIGBANNER') {
            initBannerTabs();
        }
        centerObject('V2editControl' + editlayer);
    }
}

}
function DE_CheckAddBlank(imgs) {
    var found = false;
    for (var img in imgs) {
        if (img.Id == 0) {
            found = true;
            break;
        }
    }
    if (!found) {
        var newimgs = new Array();
        newimgs.push(clone_obj(DE_NewImage));
        for (var k = 0; k < imgs.length;  k++) {
            newimgs.push(clone_obj(imgs[k]));
        }
        return newimgs;
    }
    else {
        return imgs;
    }
}

function initBannerTabs() {
    $('.admin_lightboxes_bgcolor > ul > li > a').click(function() {
        if ($(this).attr('id') == 'StaticBanners') {
            $(this).removeClass().addClass('animated_banner_hover');
            $('#AnimatedBanners,#CreatedBanners').removeClass().addClass('animated_banner');
            $('#StaticBannersDiv').show();
            $('#AnimatedBannersDiv , #CreatedBannersDiv').hide();
        }
        else if ($(this).attr('id') == 'AnimatedBanners') {
            $(this).removeClass().addClass('animated_banner_hover');
            $('#StaticBanners,#CreatedBanners').removeClass().addClass('animated_banner');
            $('#AnimatedBannersDiv').show();
            $('#StaticBannersDiv,#CreatedBannersDiv').hide();
        }
        else if ($(this).attr('id') == 'CreatedBanners') {
            $(this).removeClass().addClass('animated_banner_hover');
            $('#StaticBanners,#AnimatedBanners').removeClass().addClass('animated_banner');
            $('#CreatedBannersDiv').show();
            $('#StaticBannersDiv,#AnimatedBannersDiv').hide();
        }
    });

}

function DE_DeleteImage() {
    var data0 = { 'id': DE_SelectedImage.Id };
    var json = JSON.stringify(data0);
    $.ajax({
        type: "POST",
        url: "/Model/DAM/Admin/model.asmx/DeleteImage",
        data: json,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        dataFilter: function(data, type) {
            return data.replace(/"\\\/(Date\([0-9-]+\))\\\/"/gi, 'new $1');
        },
        success: function(msg) {
            switch (DE_imagetype) {
                case 'SMALLBANNER':
                    SMALLBANNERIMAGES = null;
                    break;
                case 'BIGBANNER':
                    BIGBANNERIMAGES = null;
                    break;
                case 'CUSTOM':
                    CUSTOMIMAGES = null;
                    break;
                case 'POLAROID':
                    POLAROIDIMAGES = null;
                    break;
                case 'STOREPHOTO':
                    STOREIMAGES = null;
                    break;
                default:
                    break;
            }
            DE_ShowImageEditor(DE_controlId, DE_imagetype, DE_title, DE_editlayer, DE_finishaction, DE_closeaction, DE_previousaction, DE_nextaction, DE_param1, DE_param2);
        }
    });
}

function DE_ImageEditor_Close() {

    if (DE_editlayer == 1) {
        $('#V2editBackground').hide();
        $('#V2editControl').hide();
    }
    else {
        $('#V2editBackground' + editlayer).hide();
        $('#V2editControl' + editlayer).hide();
    }
    if(DE_closeaction != '') {
        var action = DE_closeaction + "('" + DE_controlId + "', '" + DE_param1 + "', '" + DE_param2 + "')";
        eval(action);
    }
}

function DE_ImageEditor_Finish() {
    if (DE_SelectedImage != null) {
        if (DE_editlayer == 1) {
            $('#V2editBackground').hide();
            $('#V2editControl').hide();
        }
        else {
            $('#V2editBackground' + editlayer).hide();
            $('#V2editControl' + editlayer).hide();
        }
        var json = JSON.stringify(DE_SelectedImage);
        var action = DE_finishaction + "('" + DE_controlId + "', " + json + ", '" + DE_param1 + "', '" + DE_param2 + "')";
        eval(action);
    }
    else {
        alert('Please select an image');
    }
}

function DE_ImageEditor_Next() {
    if (DE_SelectedImage != null) {
        if (DE_editlayer == 1) {
            $('#V2editBackground').hide();
            $('#V2editControl').hide();
        }
        else {
            $('#V2editBackground' + editlayer).hide();
            $('#V2editControl' + editlayer).hide();
        }
        var json = JSON.stringify(DE_SelectedImage);
        var action = DE_nextaction + "('" + DE_controlId + "', " + json + ", '" + DE_param1 + "', '" + DE_param2 + "')";
        eval(action);
    }
    else {
        alert('Please select an image');
    }
}


function DE_ImageUpload_Show() {
    var controlconfig = GetControlConfigData(DE_controlId);
    var editorsize = GetMaxEditorSize(0, 0);
    editorsize.width = 600;
    editorsize.height = 350;
    var data = { "controlId": DE_controlId,
        "imageType": DE_imagetype,
        "storeId": controlconfig.StoreId,
        "editorwidth": editorsize.width,
        "editorheight": editorsize.height
    };
    var viewresult = TrimPath.parseTemplate(de_upload).process(data);
    document.getElementById('V2editControl2').innerHTML = viewresult;
    $('#V2editBackground2').show();
    $('#V2editControl2').show();
    centerObject('V2editControl2');
}

function DE_ImageUpload_Finish() {
    $('#V2editBackground2').hide();
    $('#V2editControl2').hide();
    switch(DE_imagetype)
    {
            case 'SMALLBANNER':
                    SMALLBANNERIMAGES = null;
                    images = null;
                    break;
                case 'BIGBANNER':
                    BIGBANNERIMAGES = null;
                    images = null;
                    break;
                case 'CUSTOM':
                    CUSTOMIMAGES = null;
                    images = null;
                    break;
                case 'POLAROID':
                    POLAROIDIMAGES = null;
                    images = null;
                    break;
                case 'STOREPHOTO':
                    STOREIMAGES = null;
                    images = null;
                    break;
                default:
                    break;
            }
            DE_ShowImageEditor(DE_controlId, DE_imagetype, DE_title, DE_editlayer, DE_finishaction, DE_closeaction, DE_previousaction, DE_nextaction, DE_param1, DE_param2);
}

function DE_ImageUpload_Close() {
    switch (DE_imagetype) {
        case 'SMALLBANNER':
            SMALLBANNERIMAGES = null;
            images = null;
            break;
        case 'BIGBANNER':
            BIGBANNERIMAGES = null;
            images = null;
            break;
        case 'CUSTOM':
            CUSTOMIMAGES = null;
            images = null;
            break;
        case 'POLAROID':
            POLAROIDIMAGES = null;
            images = null;
            break;
        case 'STOREPHOTO':
            STOREIMAGES = null;
            images = null;
            break;
        default:
            break;
    }
    $('#V2editBackground2').hide();
    $('#V2editControl2').hide();
    DE_ShowImageEditor(DE_controlId, DE_imagetype, DE_title, DE_editlayer, DE_finishaction, DE_closeaction, DE_previousaction, DE_nextaction, DE_param1, DE_param2);
}


function DE_ImageEditor_Previous(controlId, editlayer, previousaction, param1, param2) {
    if (editlayer == 1) {
        $('#V2editBackground').hide();
        $('#V2editControl').hide();
    }
    else {
        $('#V2editBackground' + editlayer).hide();
        $('#V2editControl' + editlayer).hide();
    }
    var action = previousaction + "('" + controlId + "', '" + param1 + "', '" + param2 + "')";
    eval(action);
}

function DE_SelectImage(imageId) {
    if (imageId != 0) {
        for (var i = images.length - 1; i >= 0; i--) {
            if (images[i].Id == imageId) {
                DE_SelectedImage = clone_obj(images[i]);
                break;
            }
        }
    }
    else {
        DE_SelectedImage = clone_obj(DE_NewImage);
        DE_SelectedImage.DateAdded = new Date();
    }
    if (DE_SelectedImage.OwnerType == 'S') {
        $("#divremove").show();
    }
    else {
        $("#divremove").hide();
    }
    $("[damtype='image']").each(function(index, item) {
        $(this).css({ 'border': '0' });
    });
    $("[id='DAMImage_" + imageId + "']").css({ 'border': '2px solid black' });
}