function SelectAll(id)
{
    document.getElementById(id).focus();
    document.getElementById(id).select();
    copy(document.getElementById(id));
}

function copy(inElement) {
  if (inElement.createTextRange) {
    var range = inElement.value.createTextRange();
    if (range && BodyLoaded==1)
      range.execCommand('Copy');
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(inElement.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
  }
}

function getyScroll()
 {
  yScroll = 0;

  if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX)
   {
    yScroll = window.innerHeight + window.scrollMaxY;
    xScroll = window.innerWidth + window.scrollMaxX;

    var deff = document.documentElement;
    var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
    var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;

    xScroll -= (window.innerWidth - wff);
    yScroll -= (window.innerHeight - hff);
   }
  else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth)
   { // all but Explorer Mac
    yScroll = document.body.scrollHeight;
    xScroll = document.body.scrollWidth;
   }
  else
   { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    yScroll = document.body.offsetHeight;
    xScroll = document.body.offsetWidth;
   }

  return yScroll;
 }
 interval = null;
function ajaxFunction()
    {
    var xmlHttp;
    try
      {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
      }
    catch (e)
      {
      // Internet Explorer
      try
        {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
      catch (e)
        {
        try
          {
          xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        catch (e)
          {
          alert("Your browser does not support AJAX!");
          return false;
          }
        }
      }
      return xmlHttp;
  }

function image_save()
{
    document.getElementById('screen').style.display = "none";
    document.getElementById('screen').style.display = "block";
    document.getElementById('screen').style.width = "100%";
    document.getElementById('screen').style.height = getyScroll() + 'px';
}

function openProgressBar(id) {
    if(document.getElementById(id))
 document.getElementById('screen').style.display = "none";
 document.getElementById('screen').style.display = "block";
 document.getElementById('screen').style.width = "100%";
 document.getElementById('screen').style.height = getyScroll() + 'px';
 document.getElementById('progress').style.visibility = "visible";
 document.getElementById('progress_show').style.display = "block";

 /* generate random progress-id */
 uuid = "";
 for (i = 0; i < 32; i++) {
  uuid += Math.floor(Math.random() * 16).toString(16);
 }

 /* patch the form-action tag to include the progress-id */
 document.getElementById(id).action="http://www.onlinedisk.ru/?action=upload&X-Progress-ID=" + uuid;

 /* call the progress-updater every 1000ms */
 interval = window.setInterval(
   function () {
     fetch(uuid);
   },
   1000
 );
}


//function fetch(uuid) {
// req = ajaxFunction();
// req.open("GET", "/progress", 1);
// req.setRequestHeader("X-Progress-ID", uuid);
// req.onreadystatechange = function () {
//  if (req.readyState == 4) {
//    if (req.status == 200) {
//    /* poor-man JSON parser */
//    var upload = eval(req.responseText);
//
//    document.getElementById('progress_status').innerHTML = upload.state;
//
//    /* change the width if the inner progress-bar */
//    if (upload.state == 'done' || upload.state == 'uploading') {
//     bar = document.getElementById('progress_show');
//     w = 338 * upload.received / upload.size;
//     bar.style.width = w + 'px';
//    }
//    /* we are done, stop the interval */
//    if (upload.state == 'done') {
//     window.clearTimeout(interval);
//    }
//   }
//  }
// }
// req.send(null);
//}


function fetch(uuid) {
 req = ajaxFunction();
 req.open("GET", "/progress", false);
 req.setRequestHeader("X-Progress-ID", uuid);
 req.send(null);
 if (req.status == 200) {
    /* poor-man JSON parser */
    var upload = eval(req.responseText);

    document.getElementById('progress_status').innerHTML = upload.state;

    /* change the width if the inner progress-bar */
    if (upload.state == 'done' || upload.state == 'uploading') {
     bar = document.getElementById('progress_show');
     w = 338 * upload.received / upload.size;
     bar.style.width = w + 'px';
    }
    /* we are done, stop the interval */
    if (upload.state == 'done') {
     window.clearTimeout(interval);
    }
   }
}

function change(id,value)
{
    document.getElementById(id).value = value;
    switch(id)
    {
        case "file_text" : document.getElementById("file_upload_button").disabled = false;break;
        case "image_text" : document.getElementById("image_upload_button").disabled = false;break;
    }
}

//Описание списков
var selectedList;
var backSelect;
function changeBack(id,old)
{
    if(backSelect!=null)
    {
        backSelect.style.background = "#fff";
        backSelect.style.color = "#9a9a9a";
    }
    id.style.background = "#f46401";
    id.style.color ="#fff";
    backSelect = id;
}
function showList(id)
{
    if(selectedList!=id && selectedList!=null)
    {
        document.getElementById(selectedList).style.display = "none";
    }
    obj = document.getElementById(id);
    if(obj.style.display == "none" || obj.style.display == "")
    {
        obj.style.display = "block";
        selectedList = id;
    }
    else
    {
        obj.style.display = "none";
        selectedList = null;
    }
}
function setValue(id,real,hide,value)
{
    if(value == "width") value_text = "в ширину";
    else value_text = "в высоту";
    document.getElementById(id).value = value_text;
    document.getElementById(real).value = value;
    hideList(hide);
}

function check(self,name)
{
    var a = document.getElementsByTagName('input');
    var checkValue = self.checked;
    for (i=1; i<a.length; i++) {
        if (a[i].type == 'checkbox' && a[i].name == name && a[i].style.visibility != "hidden")
            //alert(a[i].name);
            a[i].checked = checkValue;
    }
}

function wrapCheckBox(visible)
{
    var a = document.getElementsByTagName('input');
    var checkValue = self.unchecked;
    for (i=1; i<a.length; i++)
    {

        if (a[i].type == 'checkbox' && a[i].value.match("folder_"))
        {
            //alert(a[i].value);
            a[i].style.visibility = visible;
            if(visible == "hidden") a[i].checked = checkValue;
        }
    }
}
function setList(text_id,real_id,text_value,real_value,list_id)
{
    document.getElementById(text_id).value = text_value;
    document.getElementById(real_id).value = real_value;
    if(real_value != "trash") visible = "hidden";
    else visible = "visible";
    wrapCheckBox(visible);
    hideList(list_id);
}
function hideList(id)
{
    document.getElementById(id).style.display = "none";
}

var lastSwitch;
function switchUpload(id)
{
    if(lastSwitch!=null && lastSwitch!=id)
    {
        document.getElementById(lastSwitch).style.display ="none";
        document.getElementById(id).style.display ="block";
        lastSwitch = id;
        return;
    }
    obj = document.getElementById(id);
    switch(obj.style.display)
    {
        case "block":obj.style.display = "none";lastSwitch = null;break;
        case "none":obj.style.display = "block";lastSwitch = id;break;
        default:obj.style.display = "block";lastSwitch = id;break;
    }
}


function rotate(position)
{
    var corner = document.getElementById('angle').value;
    switch(position)
    {
        case "left":
            $('#image').rotateLeft();
			$('#image').css("margin","10px");
            document.getElementById('angle').value -= -90;
            break;
        case "right":
            $('#image').rotateRight();
			$('#image').css("margin","10px");
            document.getElementById('angle').value -= 90;
            break;
    }
}

var saveWidth = 0;
var saveHeight = 0;
var scaled = false;
var max_width = 900;

function scaleImg(what){
    var width = $('#'+what).width();
    var height = $('#'+what).height();
    if(scaled)
        {
            $('#' + what).height(saveHeight).width(saveWidth);
            scaled = false;
        }
    if (width > max_width)
    {

        if(!scaled)
        {
            saveWidth = width;
            saveHeight = height;
            var ratio = (height / width );
            var new_width = max_width;
            var new_height = (new_width * ratio);
            $('#' + what).height(new_height).width(new_width);
            scaled = true;
        }
    }

}
function loadImage(id)
{
    $('#'+id).show();
    $('#load').hide();
    scaleImg(id);
}

// JS code for torrents
$(document).ready(function() {
	$('.torrent_gen').click( function() {
		$('.torrent_gen').removeClass('torrent_gen').empty().append('<span style="color:red">Идет создание торрент файла...</span>').addClass('loading_torrent');
		$.ajax({
			type: "POST",
			url: "/tgen.php",
			data: "tid="+ $('.tid').val() +"&torrent_gen=yes",
			dataType: "text",
			success: function(msg){
				$('.loading_torrent').remove();
				$('.download_torrent').fadeIn('slow');

			}
		});
	});
});

