﻿var AE_NewAnnouncement = { "Id": 0, "StoreId": 0, "Title": "", "ShortDescription": "", "Intro": "", "ImageId": 0, "SideImageId": 0, "MainImage": { "Id": 0, "FileName": "", "ImagePath": "", "Description": "", "Width": 0, "Height": 0, "ImageType": "", "OwnerType": "", "OwnerId": 0, "DateAdded": "1900-01-01T05:00:00Z", "FileType": "" }, "SideImage": { "Id": 0, "FileName": "", "ImagePath": "", "Description": "", "Width": 0, "Height": 0, "ImageType": "", "OwnerType": "", "OwnerId": 0, "DateAdded": "1900-01-01T05:00:00Z", "FileType": ""} };
var AE_EditedAnnouncement;

var AE_controlId;
var AE_announcementtype;
var AE_showintro;
var AE_editlayer;
var AE_finishaction;
var AE_closeaction;
var AE_previousaction;
var AE_param1;
var AE_param2;
var AE_title;

function AE_ShowAnnouncementEditor(controlId, announcement, announcementtype, title, showintro, editlayer, finishaction, closeaction, previousaction, param1, param2) {
    if (!param1) {
        param1 = '';
    }
    if (!param2) {
        param2 = '';
    }
    if (!announcement) {
        AE_EditedAnnouncement = clone_obj(AE_NewAnnouncement);
    }
    else {
        AE_EditedAnnouncement = clone_obj(announcement);
    }

    AE_controlId = controlId;
    AE_announcementtype = announcementtype;
    AE_showintro = showintro;
    AE_editlayer = editlayer;
    AE_finishaction = finishaction;
    AE_closeaction = closeaction;
    AE_previousaction = previousaction;
    AE_param1 = param1;
    AE_param2 = param2;
    AE_title = title;

    var editorsize = GetMaxEditorSize(0, 140);
    editorsize.width = 850;

    var data = { "announcement": AE_EditedAnnouncement,
        "announcementtype": announcementtype,
        "showintro": showintro,
        "controlId": controlId,
        "editlayer": 1,
        "param1": param1,
        "param2": param2,
        "closeaction": closeaction,
        "finishaction": finishaction,
        "previousaction": previousaction,
        "editorsize": editorsize,
        "title" : title
    };

    var viewresult = TrimPath.parseTemplate(ae_announcement_edit).process(data);
    if (editlayer == 1) {
        document.getElementById('V2editControl').innerHTML = viewresult;
        $('#V2editBackground').show();
        $('#V2editControl').show();
        centerObject('V2editControl');
    }
    else {
        document.getElementById('V2editControl' + editlayer).innerHTML = viewresult;
        $('#V2editBackground' + editlayer).show();
        $('#V2editControl' + editlayer).show();
        centerObject('V2editControl' + editlayer);
    }
}

function AE_AnnouncementEditor_Close(controlId, editlayer, closeaction, param1, param2) {
    if (editlayer == 1) {
        $('#V2editBackground').hide();
        $('#V2editControl').hide();
    }
    else {
        $('#V2editBackground' + editlayer).hide();
        $('#V2editControl' + editlayer).hide();
    }
    var action = closeaction + "('" + AE_controlId + "', '" + AE_param1 + "', '" + AE_param2 + "')";
    eval(action);
}

function AE_AnnouncementEditor_Finish() {
    AE_EditedAnnouncement.MainImage.DateAdded = new Date();
    AE_EditedAnnouncement.SideImage.DateAdded = new Date();
    if (AE_showintro) {
        AE_EditedAnnouncement.Title = $('#annTitle').val();
        AE_EditedAnnouncement.Intro = $('#annIntro').val();
    }
    if (AE_editlayer == 1) {
        $('#V2editBackground').hide();
        $('#V2editControl').hide();
    }
    else {
        $('#V2editBackground' + AE_editlayer).hide();
        $('#V2editControl' + AE_editlayer).hide();
    }
    var json = JSON.stringify(AE_EditedAnnouncement);
    var action = AE_finishaction + "('" + AE_controlId + "', " + json + ",'" + AE_param1 + "', '" + AE_param2 + "')";
    eval(action);
}

function AE_AnnouncementEditor_Previous(controlId, editlayer, previousaction, param1, param2) {
    if (editlayer == 1) {
        $('#V2editBackground').hide();
        $('#V2editControl').hide();
    }
    else {
        $('#V2editBackground' + editlayer).hide();
        $('#V2editControl' + editlayer).hide();
    }
    var action = previousaction + "('" + AE_controlId + "', '" + AE_param1 + "', '" + AE_param2 + "')";
    eval(action);
}


function AE_EditAnnouncementText(controlId) {
    if (AE_showintro) {
        AE_EditedAnnouncement.Title = $('#annTitle').val();
        AE_EditedAnnouncement.Intro = $('#annIntro').val();
    }  
    var editorsize = GetMaxEditorSize(0, 140);
    editorsize.width = 504;
    TE_ShowTextEditor(controlId, '', AE_EditedAnnouncement.ShortDescription, 'AnnouncementText', 'Edit Text', '', editorsize.width, editorsize.height, 2, 'AE_UpdateAnnouncement_Text', 'AE_CloseContentEditor', '', '', '');
}

function AE_UpdateAnnouncement_Text(controlId, content, param1, param2) {
    AE_EditedAnnouncement.ShortDescription = content;
    AE_ShowAnnouncementEditor(AE_controlId, AE_EditedAnnouncement, AE_announcementtype, AE_title, AE_showintro, AE_editlayer, AE_finishaction, AE_closeaction, AE_previousaction, AE_param1, AE_param2);
}

function AE_SelectSideImage() {
    if (AE_showintro) {
        AE_EditedAnnouncement.Title = $('#annTitle').val();
        AE_EditedAnnouncement.Intro = $('#annIntro').val();
    }
    DE_ShowImageEditor(AE_controlId, 'POLAROID', 'Choose image', 1, 'AE_UpdateAnnouncement_SideImage', 'AE_CloseContentEditor', '', '', AE_param1, 'true');
}

function AE_SelectLargeImage() {
    if (AE_showintro) {
        AE_EditedAnnouncement.Title = $('#annTitle').val();
        AE_EditedAnnouncement.Intro = $('#annIntro').val();
    }
    DE_ShowImageEditor(AE_controlId, 'CUSTOM', 'Choose image', 1, 'AE_UpdateAnnouncement_LargeImage', 'AE_CloseContentEditor', '', '', AE_param1, 'true');
}

function AE_UpdateAnnouncement_SideImage(controlId, image, param1, param2) {
    AE_EditedAnnouncement.SideImage = clone_obj(image);
    AE_EditedAnnouncement.SideImageId = image.Id;
    AE_ShowAnnouncementEditor(AE_controlId, AE_EditedAnnouncement, AE_announcementtype, AE_title, AE_showintro, AE_editlayer, AE_finishaction, AE_closeaction, AE_previousaction, AE_param1, AE_param2);
}

function AE_UpdateAnnouncement_LargeImage(controlId, image, param1, param2) {
    AE_EditedAnnouncement.MainImage = clone_obj(image);
    AE_EditedAnnouncement.ImageId = image.Id;
    AE_ShowAnnouncementEditor(AE_controlId, AE_EditedAnnouncement, AE_announcementtype, AE_title, AE_showintro, AE_editlayer, AE_finishaction, AE_closeaction, AE_previousaction, AE_param1, AE_param2);
}

function AE_hideannouncementDetail() {
    $('#V2editBackground').hide();
    $('#V2editControl').hide();
}

function AE_showAnnouncementDetail(controlId, announcement) {
    var controlconfig = GetControlConfigData(controlId);
    var data = { "controlconfig": controlconfig, "announcement": announcement };
    var viewresult = TrimPath.parseTemplate(ae_announcement_detail).process(data);
    document.getElementById('V2editControl').innerHTML = viewresult;
    $('#V2editBackground').show();
    $('#V2editControl').show();
    centerObject('V2editControl');
}

function AE_CloseContentEditor(controlid, param1, param2) {
    AE_ShowAnnouncementEditor(AE_controlId, AE_EditedAnnouncement, AE_announcementtype, AE_title, AE_showintro, AE_editlayer, AE_finishaction, AE_closeaction, AE_previousaction, AE_param1, AE_param2);
}