﻿// JScript File
var txtBlocks = 1;
var imgBlocks = 1;
var blockToEdit = "";

function SaveTxtBlock()
{
    var txt = tinymce.EditorManager.get('txt_input').getContent();
    
    if (blockToEdit.length == 0)
    {
        var newtxt = document.createElement('div');
        newtxt.id = 'report_txt_content'+txtBlocks.toString();
        newtxt.className = 'textMtop';
        newtxt.onclick = function () {EditTxtBlock(newtxt)};
        newtxt.innerHTML = txt;
        
        getEl("fullrp").appendChild(newtxt);
        txtBlocks++; /*alert(getEl("txtBlockAdd").style.top);*/
    }
    else
    {
        getEl(blockToEdit).innerHTML += txt;
        blockToEdit = '';
    }

    tinymce.EditorManager.get('txt_input').setContent(''); getEl("txtBlockAdd").style.display = 'none';
}

function EditTxtBlock(el)
{
    var len = (tinymce.EditorManager.get('txt_input').getContent()).length;
    if (len > 0) 
        SaveTxtBlock();
        
    tinymce.EditorManager.get('txt_input').setContent(el.innerHTML);
    getEl("txtBlockAdd").style.display = 'block';
    el.innerHTML = '';
    blockToEdit = el.id;
    
//    var fedit = getEl("txtBlockAdd");
//    fedit.id = 'txtBlockAdd_edit';
//    fedit.style.display = 'block';
//    el.appendChild(fedit);
}

function createPhotoBlock(el, path, imgdiv_id)
{
    var newimg = document.createElement('div');
    newimg.id = 'report_img_content'+imgBlocks.toString();
    newimg.className = 'textMtop';

    var inner_html = "";
    inner_html += "<div id=\"toplinediv"+imgBlocks.toString()+"\" style=\"position: relative; cursor: pointer; width:20px; \" onmouseover=\"openToptext('ph"+imgBlocks.toString()+"');\" onmouseout=\"closedToptext('ph"+imgBlocks.toString()+"');\">";
    inner_html += "<div id=\"ph"+imgBlocks.toString()+"\" style=\"display: none;\" class=\"photoLine2\">";
    inner_html += "<div class=\"photoLine\"></div>";
    inner_html += "<div style=\"position: absolute; z-index: 15; right: 0px; top: 25px; _top: 0px; color: rgb(255, 255, 255);\" onclick=\"imageBackToGallery('"+imgdiv_id+"','"+newimg.id+"')\"><div class=\"divTextPhoto\">"+markupList['uspeh23']+"</div> <div class=\"imgupp22\" align=\"center\"></div></div>";
    inner_html += "</div>";
    inner_html += "</div><img onLoad=\"changeDivSize("+imgBlocks+")\" id=\"imgid"+imgBlocks.toString()+"\" src=\""+path+"\" onmouseover=\"openToptext('ph"+imgBlocks.toString()+"');\" onmouseout=\"closedToptext('ph"+imgBlocks.toString()+"');\"></div>";
    inner_html += "<div class=\"bgtextb\" id=\"textardiv"+imgBlocks.toString()+"\"><textarea id=\"report_com_content"+imgBlocks.toString()+"\" style=\"color: rgb(136, 136, 136); height: 28px;\" class=\"textarfocus\" onkeydown=\"resizeTextArea(this);\" onfocus=\"focusArea(this);\" onmouseout=\"mouseOutArea();\" onmouseover=\"mouseOverArea();\"></textarea></div>";
    inner_html += "</div>";

    newimg.innerHTML = inner_html;

    getEl("fullrp").appendChild(newimg);
    getEl('report_com_content'+imgBlocks.toString()).focus();
        
    //changeDivSize(imgBlocks);
    imgBlocks++;
}

function changeDivSize(img_i)
{
    var imgid = "imgid"+img_i;
    var toplinediv = "toplinediv"+img_i;
    var textareaid = "report_com_content"+img_i;
    var textareadiv = "textardiv"+img_i;

    var imgElwidth = getEl(imgid).offsetWidth;
    var txtarWigth = getEl(textareaid).offsetWidth;
    var txtarDivWidth = getEl(textareadiv).offsetWidth;
    
    txtarWigth = imgElwidth - 18;
    txtarDivWidth = imgElwidth - 10;

    if (imgElwidth > 50)
    {
        getEl(toplinediv).style.width = imgElwidth+"px"; 
        getEl(textareaid).style.width = txtarWigth+"px";  
        getEl(textareadiv).style.width = txtarDivWidth+"px";   
    }
    else 
    {
        var func = 'changeDivSize('+img_i+')';
        setTimeout(func, 300);
    }
}

function changeDivSize_frontend(img_i)
{
    var imgid = "imgid" + img_i;
    var imgdescrdiv = "imgdescrdiv" + img_i;
    
    var imgElWidth = getEl(imgid).offsetWidth;
    imgElWidth = imgElWidth - 10;
    
    if (imgElWidth > 0)
    {
        getEl(imgdescrdiv).style.width = imgElWidth+"px";
    }
    else 
    {
        var func = 'changeDivSize_frontend('+img_i+')';
        setTimeout(func, 300);
    }
}

function showHidePhotoBlock()
{
    if (getEl("imgBlockAdd").style.display == 'none') 
    {
        tinymce.EditorManager.get('txt_input').setContent(''); getEl("txtBlockAdd").style.display = 'none';
        getEl("imgBlockAdd").style.display = 'block';
        getEl("imgBlockLoad").style.display = 'none';
    }
}

function showPhotoBlock(istrue)
{
        tinymce.EditorManager.get('txt_input').setContent(''); getEl("txtBlockAdd").style.display = 'none';
        getEl("imgBlockAdd").style.display = istrue?'block':'none';
        getEl("imgBlockLoad").style.display = (!istrue?'block':'none');
}

function showHideTextBlock()
{
    if (getEl("txtBlockAdd").style.display = 'none') 
    {
        getEl("imgBlockAdd").style.display = 'none';
        getEl("txtBlockAdd").style.display = 'block';
    }
}

function showHideUploadBlock()
{
    if (getEl("imgBlockLoad").style.display == 'none')
    {
        getEl("imgBlockLoad").style.display = 'block';
        getEl("imgBlockAdd").style.display = 'none';
        getEl("txtBlockAdd").style.display = 'none';
    }
}

function removeImgFromGallery(el)
{
    var myEl = el.parentNode;
    for (var i=0;i<myEl.childNodes.length;i++)
    {
        if ((myEl.childNodes)[i].type == "hidden")
        {
            imgpath = (myEl.childNodes)[i].value.substring(0,(myEl.childNodes)[i].value.indexOf("."));
            break;  
        }
    }
    myEl.parentNode.removeChild(myEl);
    AjaxRequest("mod/ajax/gallery.aspx?mode=remove&path="+imgpath, fakeFunc1);
}

function imageFromGalleryToPhotoblock(el_id)
{
    var el = getEl(el_id);
//    el.style.display = "none";

    for (var i=0;i<el.childNodes.length;i++)
    {
        if ((el.childNodes)[i].type == "hidden")
        {
            imgpath = (el.childNodes)[i].value.substring(0,(el.childNodes)[i].value.indexOf("."));
            break;  
        }
    }
    
    AjaxRequest("mod/ajax/gallery.aspx?mode=visibility&path="+imgpath+"&vis=false", fakeFunc2);
    
    if (getEl("imgBlockAdd").style.display == 'block')
        getGallery();
 }

function imageBackToGallery(el_id, photoblock_id)
{
    var el = getEl(el_id);
//    el.style.display = "block";

    var pbEl = getEl(photoblock_id);
    pbEl.parentNode.removeChild(pbEl);
    
    for (var i=0;i<el.childNodes.length;i++)
    {
        if ((el.childNodes)[i].type == "hidden")
        {
            imgpath = (el.childNodes)[i].value.substring(0,(el.childNodes)[i].value.indexOf("."));
            break;  
        }
    }

    AjaxRequest("mod/ajax/gallery.aspx?mode=visibility&path="+imgpath+"&vis=true", fakeFunc2);
    
    if (getEl("imgBlockAdd").style.display == 'block')
        getGallery();
}

function getGallery()
{   
    var dt = new Date();
    AjaxRequest ("mod/ajax/gallery.aspx?mode=output&dt=" + dt.getTime(), writeOutGallery);
}

function writeOutGallery(text, xml)
{

    if (getEl("photogal"))
    {    getEl("photogal").innerHTML = text;
        
        showPhotoBlock(true);
        var err = getEl("hiddenimgerror");
        if (err) alert (err.value);
    }
}

function fakeFunc1(text,xml) 
{
    if (text == "0")
        getEl("imgBlockAdd").style.display = 'none';
}

function fakeFunc2(text, xml)
{
    
}

// ------------ ОТПРАВКА ФОТОК --------------------------------
//

function sendpics()
{
    var formpics = document.getElementById("img_upload");
    formpics.submit();

    checkFiles();
}

function bytesConvert(bytes)
{
    var ret = "";
    if (bytes/1024 < 1) ret = bytes + " b";
    else 
    {
        if (bytes/1048576 < 1) ret = Math.round(bytes/1024) + " Kb";
        else ret = Math.round(bytes/1048576) + " Mb";
    }
    return ret; 
}


// -------------- пока будут просто 4 функции с разными именами (по числу инпутов)
var glob_i_0=0;
var glob_el_0;
function go_progresss_0() {
  var pad = -600 + glob_i_0;
  glob_el_0.style.background = "url(content/img/photorep/progbar.gif) "+pad+"px 0px repeat-y";
  glob_i_0++;

  if (glob_i_0<599) setTimeout('go_progresss_0()', 0.001);
}

var glob_i_1=0;
var glob_i_11=0;
var glob_el_1; 
function go_progresss_1() {
  var pad = -600 + glob_i_1;
  glob_el_1.style.background = "url(content/img/photorep/progbar.gif) "+pad+"px 0px repeat-y";
  glob_i_1++;
  glob_i_11++;
  
  if (glob_i_11<20) glob_i_1--;
  
  if (glob_i_1<599) setTimeout('go_progresss_1()', 0.001);
}

var glob_i_2=0;
var glob_i_22=0;
var glob_el_2; 
function go_progresss_2() {
  var pad = -600 + glob_i_2;
  glob_el_2.style.background = "url(content/img/photorep/progbar.gif) "+pad+"px 0px repeat-y";
  glob_i_2++;
  glob_i_22++;
  
  if (glob_i_22<42) glob_i_2--;
  
  if (glob_i_2<599) setTimeout('go_progresss_2()', 0.001);
}

var glob_i_3=0;
var glob_i_33=0;
var glob_el_3; 
function go_progresss_3() {
  var pad = -600 + glob_i_3;
  glob_el_3.style.background = "url(content/img/photorep/progbar.gif) "+pad+"px 0px repeat-y";
  glob_i_3++;
  glob_i_33++;
  
  if (glob_i_33<54) glob_i_3--;
  
  if (glob_i_3<599) setTimeout('go_progresss_3()', 0.001);
}

var glob_i_4=0;
var glob_i_44=0;
var glob_el_4; 
function go_progresss_4() {
  var pad = -600 + glob_i_4;
  glob_el_4.style.background = "url(content/img/photorep/progbar.gif) "+pad+"px 0px repeat-y";
  glob_i_4++;
  glob_i_44++;
  
  if (glob_i_44<65) glob_i_4--;
  
  if (glob_i_4<599) setTimeout('go_progresss_4()', 0.001);
}

var globfull_i=0;
var globfull_i1=0;
var globfull_el; 
function go_progresss_full() {
  var pad = -600 + globfull_i;
  globfull_el.style.background = "url(content/img/photorep/progbar.gif) "+pad+"px 0px repeat-y";
  globfull_i++;
  globfull_i1++;
  
  if (globfull_i1<54) globfull_i--;
  
  if (globfull_i<599) setTimeout('go_progresss_full()', 0.001);
   
  if (globfull_i==599) 
  {
    hideProgressBars();
    showHidePhotoBlock(); 
    getGallery(); 

  }
}

// -------------- 


function draw_prog_bar_header()
{
    el = document.getElementById("progressheader");
    var st = "";
    st += "<div style=\"height:20px; margin: 0 0 10px 25px;\">";
    st += "<div class=\"tdContprog1\"><b>Имя файла</b></div>";
    st += "<div class=\"tdContprog2\"><b>Вес</b></div>";
    st += "<div class=\"tdContprog3\"><b>Размер</b></div>";
    st += "<div class=\"tdContprog4\"></div>";
    st += "<div class=\"clear\"></div>";
    st += "</div>"
    el.innerHTML = st;
}

function checkIfFilesLoaded(text, xml)
{
    var bar = document.getElementById("progressbar");
    var totalimg = xml.getElementsByTagName("totalimg");
    var num = _wGetTxt(totalimg[0]);
    
    var drawdiv = "";
    var drawdivfull = "";
    
    var items = xml.getElementsByTagName("curimage");

    for (var i=0; i<items.length; i++)
    {
        drawdiv += "<div class=\"divdottedline\" id=\"curimg"+i+"\" style=\"background: url(content/img/photorep/progbar.gif) -600px 0px repeat-y\">\n";
        drawdiv += " <div class=\"tdContprog1\"><img src=\"content/img/photorep/photoAp.gif\" class=\"photoAp\">"+_wGetTxt(items[i])+"</div>\n";
        drawdiv += " <div class=\"tdContprog2\">"+bytesConvert(_wGetAttr(items[i], 'size'))+"</div>\n";
        drawdiv += " <div class=\"tdContprog3\">600 х 400 </div>\n";
        drawdiv += " <div class=\"tdContprog4\"><u style=\"color: #dd0000;\"><!-- удалить --></u></div>\n";
        drawdiv += " <div class=\"clear\"></div>\n";
        drawdiv += "</div>\n";
    }

    var barfull = document.getElementById("progressbarfull");
    drawdivfull += "<div class=\"divdottedline\" id=\"totimg\">";
    drawdivfull += "<div class=\"tdContprog1\" style=\"color:#0155d2;\"><b style=\"margin-right:10px; border-bottom:1px dotted #0155d2;\" onclick=\"hideProgressBars(); drawFileInputs();\"><!-- Добавить еще --></b> <span style=\"border-bottom:1px dotted #0155d2;\"><!-- Вставить из интернета --></span></div>";
    drawdivfull += "<div class=\"tdContprog2\"><b>"+items.length+" файлов</b></div>";
    drawdivfull += "<div class=\"tdContprog3\">Всего: "+bytesConvert(_wGetAttr(totalimg[0], 'tsize'))+"</div>";
    drawdivfull += "<div class=\"tdContprog4\"><u style=\"color: #dd0000;\"><!-- удалить --></u></div>";
    drawdivfull += "<div class=\"clear\"></div>";
    drawdivfull += "</div>";
    
    if (items.length > 0) draw_prog_bar_header();
    bar.innerHTML = drawdiv;
    barfull.innerHTML = drawdivfull;

    if (document.getElementById("curimg0")!=null) { glob_el_0 = document.getElementById("curimg0"); go_progresss_0();}
    if (document.getElementById("curimg1")!=null) { glob_el_1 = document.getElementById("curimg1"); go_progresss_1();}
    if (document.getElementById("curimg2")!=null) { glob_el_2 = document.getElementById("curimg2"); go_progresss_2();}
    if (document.getElementById("curimg3")!=null) { glob_el_3 = document.getElementById("curimg3"); go_progresss_3();}
    if (document.getElementById("curimg4")!=null) { glob_el_4 = document.getElementById("curimg4"); go_progresss_4();}
    globfull_el = document.getElementById("totimg");
    go_progresss_full();
    
}

function checkFiles()
{
    AjaxRequest("mod/img/fileloadedcheck.aspx",checkIfFilesLoaded);
  
    //hideProgressBars();
    //showHidePhotoBlock();
    //getGallery();
    
}

function drawFileInputs()
{

    var el = document.getElementById("fileinputs");
    el.style.display = 'block';
    //var str = "";
/*    str += "<div class=\"dottedLine\"></div>";
    str += "<div style=\"font-size:12px; font-weight:bold; padding-left:25; \">Выберите нужные фотографии:</div>";
    str += "<div style=\"float:left; margin-top:12px; width:20px;\">1.</div><input name=\"userfile[]\" type=\"file\" style=\"width:575px; float:right; margin: 10px 0 0 0;\" border=\"0\">";
    str += "<div class=\"clear\"></div>";
    str += "<div style=\"float:left; margin-top:12px; width:20px;\">2.</div><input name=\"userfile[]\" type=\"file\" style=\"width:575px; float:right; margin: 10px 0 0 0;\" border=\"0\">";
    str += "<div class=\"clear\"></div>";
    str += "<div style=\"float:left; margin-top:12px; width:20px;\">3.</div><input name=\"userfile[]\" type=\"file\" style=\"width:575px; float:right; margin: 10px 0 0 0;\" border=\"0\">";
    str += "<div class=\"clear\"></div>";
    str += "<div style=\"float:left; margin-top:12px; width:20px;\">4.</div><input name=\"userfile[]\" type=\"file\" style=\"width:575px; float:right; margin: 10px 0 0 0;\" border=\"0\">";
    str += "<div class=\"clear\"></div>";
    str += "<div style=\"float:left; margin-top:12px; width:20px;\">5.</div><input name=\"userfile[]\" type=\"file\" style=\"width:575px; float:right; margin: 10px 0 0 0;\" border=\"0\">";
    str += "<div class=\"clear\"></div>";

    str += "<a href=\"javascript:void(0);\" class=\"lAdd4\" onclick=\"var dt = new Date(); document.getElementById('img_upload').action='mod/img/filesave.aspx?rnd='+dt.getTime(); sendpics(); hideFileInputs();\">Загрузить в галерею</a>";
*/

    //str += '';
    
    //el.innerHTML = str;
}


var upFiles = new Array();

function juploadOneComplete(e,qid,file,response,data)
{
    var sList = response.split(':');    
    for(i = 0; i < sList.length; i++)
    {   
        //alert(sList[i] + ' ===== ' + sList[i].length);
        if (sList[i].length > 0)
            upFiles.push( sList[i] );
    }
}

function juploadAllComplete()
{
    hideFileInputs();
//    getGallery();
    

    var sxml = '<jfiles>';
    for(i = 0; i < upFiles.length; i++)
        sxml += '<file>'+upFiles[i]+'</file>';
    sxml += '</jfiles>';
    //alert(sxml);
    AjaxXmlRequest (sxml,"mod/ajax/gallery.aspx?mode=output&option=savesession", writeOutGallery);
    upFiles = new Array();
    //AjaxXmlRequest(sxml,'mod/img/core.aspx?option=savesession',saveSession_request);*/
}


function hideFileInputs()
{
    //var el = document.getElementById("fileinputs");
    //el.innerHTML = "";
    var el = document.getElementById("fileinputs");
    el.style.display = 'none';    
}

function hideProgressBars()
{
    el1 = document.getElementById("progressheader");
    el1.innerHTML = "";
    el2 = document.getElementById("progressbar");
    el2.innerHTML = "";
    el3 = document.getElementById("progressbarfull");
    el3.innerHTML = "";
    glob_i_0 = 0;
    glob_i_1 = 0;
    glob_i_2 = 0;
    glob_i_3 = 0;
    glob_i_4 = 0;
    globfull_i = 0;

}

function isImagesInGallery()
{
    var el = getEl("photogal");
    if (el.childNodes.length>2) 
        return true;
    else    
        return false;

}
