var dataChanged = false;
window.onunload = function() {
    if (dataChanged) {
        if (!confirm("Are you sure to quit without saving ?")) return false;
    }
};
if (document.getElementById('admin_banner')) {
    alert(document.body.style.marginTop);
    window.onload = function() {
        var TextToEdit;
        var textToSet;
        var editorIdToRemove;
        var textToSave;
        var imageToUpload;
        var videoToUpload;
        var fileToUpload;
        var currentMaxChar;

        document.body.style.marginTop = '20px';
        alert(document.body.style.marginTop);
        document.getElementById('editor').style.width = document.body.clientWidth;
        document.getElementById('editor').style.height = document.body.clientHeight;
        document.getElementById('admin_banner').style.left = (document.body.clientWidth < 930) ? 150 + 'px' : (((document.body.clientWidth - 930) / 2) + 150) + 'px';
    };

    window.onresize = function() {
        alert(document.body.style.marginTop);
        document.getElementById('editor').style.width = document.body.clientWidth;
        document.getElementById('editor').style.height = document.body.clientHeight;
        document.getElementById('admin_banner').style.left = (document.body.clientWidth < 930) ? 150 + 'px' : (((document.body.clientWidth - 930) / 2) + 150) + 'px';
    };
}

function rightClick_(slot, textField, name, tools, maxChar) {

    var editorDiv = document.getElementById('editor');

    if (document.getElementById('upload_photo') != null || document.getElementById('upload_video') != null || document.getElementById('FCKtext') != null) {
        editorDiv.removeChild(editorIdToRemove);
    }

    TextToEdit = textField;
    textToSet = textField.innerHTML;
    currentMaxChar = maxChar;

    textToSave = document.getElementById(name + "_TOSAVE");

    var textdivContent = '<table align="center" width="417">';
    textdivContent += '   <tr>';
    textdivContent += '		<td style="height:343px;padding-left:26px;padding-right:26px;text-align:left;background-image: url(png/textEditorBG.jpg);background-repeat:no-repeat;">';
    textdivContent += '			<br/><br/><br/><br/><br/><br/>';
    textdivContent += '			<textarea style="display:none;" id="FCKtext" name="FCKtext">.</textarea>';
    textdivContent += '			<input style="margin-top:8px;" class="inputbutton" type="button" value="Submit" onclick="GetContent_(' + slot + ');"/>';
    textdivContent += '			<input class="inputbutton" type="button" value="Cancel" onclick="document.getElementById(\'editor\').style.visibility = \'hidden\'" style=" margin-left:3px;" />';
    textdivContent += '		</td>';
    textdivContent += '	</tr>';
    textdivContent += '</table>';

    var textdiv = document.createElement('div');
    var divIdName = name + '_editor';
    textdiv.setAttribute('id', divIdName);
    textdiv.innerHTML = textdivContent;
    editorIdToRemove = textdiv;
    editorDiv.appendChild(textdiv);

    var oFCKeditor = new FCKeditor('FCKtext');
    sBasePath = 'FCKeditor/';
    oFCKeditor.BasePath = sBasePath;
    oFCKeditor.Config['CustomConfigurationsPath'] = '../cmsconfig.js';

    if (tools != "") {
        oFCKeditor.Config['DynamicToolbar_buttons'] = tools;
        oFCKeditor.ToolbarSet = 'DynamicToolbar';
    }

    sSkinPath = '../editor/skins/silver/';
    oFCKeditor.Config['SkinPath'] = sSkinPath;
    oFCKeditor.ReplaceTextarea();

    editorDiv.style.visibility = "visible";

}

function FCKeditor_OnComplete(s) {
    var oEditor = FCKeditorAPI.GetInstance('FCKtext');
    oEditor.SetData(textToSet);
}

function GetContent_(slot) {
    var oEditor = FCKeditorAPI.GetInstance('FCKtext');
    TextToEdit.innerHTML = oEditor.GetXHTML(false);
    if (TextToEdit.innerHTML.length > 3) {
        if (TextToEdit.innerHTML.substring(TextToEdit.innerHTML.length - 4) == '<br>') {
            TextToEdit.innerHTML = TextToEdit.innerHTML.substring(0, TextToEdit.innerHTML.length - 4);
        }
    }
    if (TextToEdit.innerHTML.length < currentMaxChar || currentMaxChar == 0) {
        textToSave.value = TextToEdit.innerHTML;
        document.getElementById('editor').style.visibility = "hidden";
    } else {
        alert('Le texte saisi est trop long\nVeuillez retirer ' + (TextToEdit.innerHTML.length - currentMaxChar) + ' caractères');
    }
}

function clickImage_(slot, moduleInstanceId, image, image_key, data_id) {
    imageToUpload = image_key;
    var editorDiv = document.getElementById('editor');

    if (document.getElementById('upload_photo') != null || document.getElementById('upload_video') != null || document.getElementById('FCKtext') != null) {
        editorDiv.removeChild(editorIdToRemove);
    }

    var uploadDiv = document.createElement('div');
    var divIdName = image_key + '_editor';
    uploadDiv.setAttribute('id', divIdName);

    uploadDivContent = '<form id="upload_photo" method="post" enctype="multipart/form-data" target="upload_target_' + slot + '">';
    uploadDivContent += '<input type="hidden" name="version" value="xml"/>';
    uploadDivContent += '<table align="center" width="417px">';
    uploadDivContent += '<tr>';
    uploadDivContent += '<td style="height:198px;padding:26px;text-align:left;background-image: url(png/imageEditorBG.jpg);background-repeat:no-repeat;">';
    uploadDivContent += '<input style="width:300px;margin-top:60px;"  name="Filedata" id="Filedata" type="file" onchange="this.form.upload.disabled = false" />';
    uploadDivContent += '<input name="IMAGE_KEY" value="' + image_key + '" type="hidden" />';
    uploadDivContent += '<input name="DATA_ID" value="' + data_id + '" type="hidden" />';
    uploadDivContent += '<br />';
    uploadDivContent += '<input style="width:300px;margin-top:5px" onFocus="this.form.upload.disabled = false;" name="imageUrl" type="text" value="http://"/>';
    uploadDivContent += '<br />';
    uploadDivContent += '<input name="action_' + slot + '" type="hidden" value="UPLOAD_PHOTO" />';
    uploadDivContent += '<input style="margin-top:5px;" class="inputbutton" id="upload" type="submit" value="Upload" onclick="GetImageContent_(' + slot + ', ' + moduleInstanceId + ', ' + data_id + ');" disabled="" />';
    uploadDivContent += '<input class="inputbutton" style=" margin-left:3px;" type="button" value="Cancel" onclick="document.getElementById(\'editor\').style.visibility = \'hidden\'" />';
    uploadDivContent += '</td>';
    uploadDivContent += '</tr>';
    uploadDivContent += '</table>';
    uploadDivContent += '</form>';

    uploadDiv.innerHTML = uploadDivContent;

    editorIdToRemove = uploadDiv;
    editorDiv.appendChild(uploadDiv);
    editorDiv.style.visibility = "visible";
}

function GetImageContent_(slot, moduleInstanceId, dataId) {
    var iFRAME = document.getElementById("upload_target_" + slot);
    if (!iFRAME.hasEvent) {
        eventPush(iFRAME, 'load', function() { displayImage_(slot); });
    }
    function eventPush(obj, event, handler) {
        if (obj.addEventListener) {
            obj.addEventListener(event, handler, false);
        } else if (obj.attachEvent) {
            obj.attachEvent('on' + event, handler);
        }
        obj.hasEvent = true;
    }

    function displayImage_(slot) {

        var isGecko = navigator.userAgent.indexOf('Gecko') > -1;
        var content = isGecko ? iFRAME.contentWindow.document : iFRAME.contentWindow.document.XMLDocument;
        var imageName = isGecko ? content.getElementsByTagName('UploadedImage')[0].textContent : content.selectSingleNode("//UploadedImage").text;
        var imageUrl = isGecko ? content.getElementsByTagName('ImageUrl')[0].textContent : content.selectSingleNode("//ImageUrl").text;

        if (imageName.length > 0) { // user uploaded an image

            var currentPath = "files/CMS/" + moduleInstanceId + "/" + imageToUpload + "/temp/";
            var newPath = currentPath + imageName;
            document.getElementById('data_id_' + dataId + '_' + slot).setAttribute('src', newPath);
            document.getElementById('data_id_' + dataId + "_" + slot + "_TOSAVE").value = imageName;

        }

        if (imageUrl.length > 0) {

            // link for image already exists. safe to change its href value
            if (document.getElementById('data_id_' + dataId + "_" + slot + "_ANCHOR")) {
                document.getElementById('data_id_' + dataId + "_" + slot + "_ANCHOR").setAttribute('href', imageUrl);
            }

            document.getElementById('data_id_' + dataId + "_" + slot + "_URL_TOSAVE").value = imageUrl;

            // User might want to erase href value
        } else {

            document.getElementById('data_id_' + dataId + "_" + slot + "_URL_TOSAVE").value = "";

        }

        document.getElementById('editor').style.visibility = "hidden";

    }

}

function clickVideo_(slot, moduleInstanceId, video, data_key, data_id) {
    videoToUpload = data_key;
    var editorDiv = document.getElementById('editor');

    if (document.getElementById('upload_photo') != null || document.getElementById('upload_video') != null || document.getElementById('FCKtext') != null) {
        editorDiv.removeChild(editorIdToRemove);
    }

    var uploadDiv = document.createElement('div');
    var divIdName = data_key + '_editor';
    uploadDiv.setAttribute('id', divIdName);

    uploadDivContent = '<form id="upload_video" method="post" enctype="multipart/form-data" target="upload_target_' + slot + '">';
    uploadDivContent += '<input type="hidden" name="version" value="xml"/>';
    uploadDivContent += '<table align="center" width="417px">';
    uploadDivContent += '<tr>';
    uploadDivContent += '<td style="color:#999;height:198px;padding:26px;text-align:left;background-image: url(png/cmsEditorBG.jpg);background-repeat:no-repeat;">';
    uploadDivContent += '<h2>Edition de vidéo</h2>';
    uploadDivContent += '<input style="width:300px;margin-top:20px;" name="Filedata" id="Filedata" type="file" onchange="this.form.upload.disabled = false" />';
    uploadDivContent += '<input name="DATA_KEY" value="' + data_key + '" type="hidden" />';
    uploadDivContent += '<input name="DATA_ID" value="' + data_id + '" type="hidden" />';
    uploadDivContent += '<br />';
    uploadDivContent += '<input name="action_' + slot + '" type="hidden" value="UPLOAD_VIDEO" />';
    uploadDivContent += '<input style="margin-top:5px;" class="inputbutton" id="upload" type="submit" value="Upload" onclick="GetVideoContent_(' + slot + ', ' + moduleInstanceId + ', ' + data_id + ');" disabled="" />';
    //uploadDivContent += '<input style="margin-top:5px;" class="inputbutton" id="upload" type="submit" value="Upload" disabled="" />';
    uploadDivContent += '<input class="inputbutton" style=" margin-left:3px;" type="button" value="Cancel" onclick="document.getElementById(\'editor\').style.visibility = \'hidden\'" />';
    uploadDivContent += '</td>';
    uploadDivContent += '</tr>';
    uploadDivContent += '</table>';
    uploadDivContent += '</form>';

    uploadDiv.innerHTML = uploadDivContent;

    editorIdToRemove = uploadDiv;
    editorDiv.appendChild(uploadDiv);
    editorDiv.style.visibility = "visible";
}

function GetVideoContent_(slot, moduleInstanceId, dataId) {
    var iFRAME = document.getElementById("upload_target_" + slot);
    if (!iFRAME.hasEvent) {
        eventPush(iFRAME, 'load', function() { displayVideo_(slot); });
    }
    function eventPush(obj, event, handler) {
        if (obj.addEventListener) {
            obj.addEventListener(event, handler, false);
        } else if (obj.attachEvent) {
            obj.attachEvent('on' + event, handler);
        }
        obj.hasEvent = true;
    }

    function displayVideo_(slot) {

        var isGecko = navigator.userAgent.indexOf('Gecko') > -1;
        var content = isGecko ? iFRAME.contentWindow.document : iFRAME.contentWindow.document.XMLDocument;
        var videoName = isGecko ? content.getElementsByTagName('UploadedVideo')[0].textContent : content.selectSingleNode("//UploadedVideo").text;

        if (videoName.length > 0) { // user uploaded a video

            var currentPath = "files/CMS/" + moduleInstanceId + "/" + videoToUpload + "/temp/";
            var newPath = currentPath + videoName;
            //            document.getElementById('data_id_' + dataId + '_' + slot).setAttribute('value', newPath);
            document.getElementById('data_id_' + dataId + "_" + slot + "_TOSAVE").value = videoName;

        }

        document.getElementById('editor').style.visibility = "hidden";

    }

}

function EditFile(DATA_ID, DATA_KEY, oldFileName) {
    //alert('edit js');
    fileToUpload = DATA_KEY;
    document.getElementById('editFile').element_data_id.value = DATA_ID;
    //document.getElementById('editFile').DATA_KEY.value = DATA_KEY;
    document.getElementById('editFile').style.display = 'block';
    document.getElementById('oldValue').innerHTML = oldFileName;
}

function GetFileContent_(slot, moduleInstanceId, dataId) {
    var iFRAME = document.getElementById("upload_target_" + slot);
    if (!iFRAME.hasEvent) {
        eventPush(iFRAME, 'load', function() { displayFile_(slot); });
    }
    function eventPush(obj, event, handler) {
        if (obj.addEventListener) {
            obj.addEventListener(event, handler, false);
        } else if (obj.attachEvent) {
            obj.attachEvent('on' + event, handler);
        }
        obj.hasEvent = true;
    }

    function displayFile_(slot) {

        var isGecko = navigator.userAgent.indexOf('Gecko') > -1;
        var content = isGecko ? iFRAME.contentWindow.document : iFRAME.contentWindow.document.XMLDocument;
        var fileName = isGecko ? content.getElementsByTagName('UploadedFile')[0].textContent : content.selectSingleNode("//UploadedFile").text;

        if (fileName.length > 0) { // user uploaded a file

            var currentPath = "files/CMS/" + moduleInstanceId + "/" + fileToUpload + "/temp/";
            var newPath = currentPath + fileName;
            //            document.getElementById('data_id_' + dataId + '_' + slot).setAttribute('value', newPath);
            document.getElementById('data_id_' + dataId + "_" + slot + "_TOSAVE").value = fileName;

        }

        document.getElementById('editFile').style.display = "none";

    }

}

