var W3CDOM = (document.createElement && document.getElementsByTagName);
var y = 0, yy = 0;
var tmtmy, pr_barInterval, ch;
var graphFile = '';

function init() {

	if (y != 0) {  y.relatedElement.value = '';   }

	if (!W3CDOM) return;
	var fakeFileUpload = document.createElement('div');
	fakeFileUpload.className = 'fakefile';
	fakeFileUpload.appendChild(document.createElement('input'));
	var x = document.getElementsByTagName('input');

	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].getAttribute('noscript')) continue;
		if (x[i].parentNode.className != 'fileinputs') continue;
		x[i].className = 'file hidden';
		var clone = fakeFileUpload.cloneNode(true);
		y = x[i];

		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];
		if (x[i].value)
			x[i].onchange();
		x[i].onchange = x[i].onclick = function () {

          if (filename != this.value ) { yy = 0; }
          if (extend(this.value) || y == 0 || this.value == '')
          {
            yy = 0;
		    this.relatedElement.value = this.value;
		    filename = this.value;
		    graphFile = this.value;
		  }
		  else
		    {
		      if (yy == 0 && filename != this.value) { alert('Это не графический файл!'); this.relatedElement.value = ''; filename = this.value; yy = 1; graphFile = this.relatedElement.value; }
		    }
		}
	}
}

function extend(filePath)
{
  fileP = new String(filePath);
  var ind = fileP.lastIndexOf('.');
  var s = '';
  for(i=ind+1; i<=fileP.length; i++)  { s += fileP.charAt(i); }

  if (s != 'bmp'  && s != 'dib' && s != 'jpg'  &&
      s != 'jpeg' && s != 'jpe' && s != 'jfif' &&
      s != 'gif'  && s != 'tif' && s != 'tiff' &&
      s != 'png'  && s != 'BMP' && s != 'DIB'  &&
      s != 'JPG'  && s != 'JPEG'&& s != 'JPE'  &&
      s != 'JFIF' && s != 'GIF' && s != 'TIF'  &&
      s != 'TIFF' && s != 'PNG') { return false; }

  return true;
}


function selectEffect(num)
{
  if (zagr == 1)
    {
      if (byID('box1_img_id'))
      {
        //alert(zagr);
        clearTimeout(tmtmy);
        clearTimeout(pr_barInterval);
        clearTimeout(ch);

        hide('process_request');
        hide('box3_otstup');
        hide('box3_link');
        hide('box3_botton_id');
        show('box3_img_id');

        hide('progress');
        hide('stopLoad_id');
        show('box2_load');
        byID('box2_id').style.backgroundImage = 'url(' + HTTP_IMG_PATH + '/images/at_smart/goodfotka/greenbox.jpg' + ')';

        step = 1;

        hide('box1_id');

        var image = byID(num).innerHTML;
        byID('box1_img_id').innerHTML = image;

        byID('box1_title').innerHTML = 'Вы выбрали эффект:';
        show('box1_bottom');
      } else { alert('Дождитесь полной загрузки страницы!'); return false; }
    } else { alert('Дождитесь полной загрузки страницы!'); return false; }
}


function fakeLoad()
{
  if (step !== 1) { alert('Вы должны выбрать эффект!'); }
    else { startProgress(); }
}


var startProgress = function()
{
    if (graphFile == '') { alert('Вы должны выбрать файл!'); }
      else
      {
        //alert('url(' + HTTP_IMG_PATH + '/images/at_smart/goodfotka/greenbox2.jpg' + ')');
        byID('box2_id').style.backgroundImage = 'url(' + HTTP_IMG_PATH + '/images/at_smart/goodfotka/greenbox2.jpg' + ')';
	    hide('box2_load');
	    show('progress');

	    var bar_data = $('#bar-content').html();

	    $('#progress_id').show();
	    $('#bar-data').html(bar_data);

	    setTimeout('setPrInterv(null)', 200);
      }
}

var setPrInterv = function (delay)
{
	pr_barInterval = setInterval('drawBar(' + delay + ')', pr_showprogr_time);
}

var drawBar = function (stop)
{
	var cur_width = parseInt($('#pr_bar').css('width'));
	var cur_perc = parseInt($('#pr_val').html());
	var plus = parseInt(pr_bar_max_width / 100);

	if (cur_perc == 100)
	{
	    LoadEnd();

	}
	else
	{
		var perc = cur_width < pr_bar_max_width ? cur_width + plus : pr_bar_max_width;
		var cur_c = parseInt(100 / -1);

		$('#pr_bar').css('width', perc + 'px');
		$('#pr_val').html((cur_perc + 1) + ' %');
	}
}


function LoadEnd()
{
  clearInterval(pr_barInterval);
  hide('progress');

  hide('box3_img_id');

  show('box3_otstup');
  show('box3_link');
  ch_color('box3_link');

  show('stopLoad_id');
  step = 2;
}


function fakePreobraz()
{
   byID('box3_caption').style.fontSize = "13px";
   byID('box3_caption').innerHTML = 'Подождите. Идет преобразование фото';
   hide('box3_otstup');
   hide('box3_link');
   show('process_request');
   doProg(8,1);
}


////////////////////////////////////

function tem(max,cur)
{
  if (cur <= max)
  {
    show('sus' + cur);
    el = byID('susp' + cur);
    if (el)
    {
      el.className = "gray";
      incr = rand(1,10);
      if (cur >= 10) { incr = rand(1,3); }
      if (incr < 1) { incr = 1; }
      cnt = parseInt(el.innerHTML) + incr;
      if (cnt >= 100) { el.className = "green"; cnt=100; cur++; byID('process_request').scrollTop = byID('process_request').scrollTop + 100; }
      byID('process_request').scrollTop = byID('process_request').scrollTop + 400;
      el.innerHTML = cnt + '%';
    }
    var curSpedTmt = rand(50,120);
    tmtmy = setTimeout('tem('+max+','+cur+')',curSpedTmt);
  } else { showRes(); }
}

function doProg(max,cur)
{
  for (i=cur;i<=max;i++) { byID('susp' + i).innerHTML = '0'; hide('sus' + i); }
  clearTimeout(tmtmy);
  show('process_request');
  tem(max, cur);
}

function showRes()
{
  byID('box3_caption').style.fontSize = "22px";
  byID('box3_caption').innerHTML = 'Получите результат!';
  hide('process_request');
  show('box3_otstup');
  show('box3_botton_id');

}

function selPage(num)
{
  hide('page_eff_1');
  hide('page_eff_2');
  show('page_eff_'+num);
}

