
  $(document).ready(function()
        {
        $("#loading").ajaxStart(function(){$('#buttonSend').hide();});         
        $("#loading").ajaxStop(function(){$('#buttonSend').show();});
        });

var mainCont, paintPaperHeight, windowWidth, windowHeight, yScroll;
var shadowOptions = {left:1, top:1, blur:1, color:'#969697'};
var zInd = 1;
var Ymax = 0;

function checkY(){
  var thisY;
  Ymax = 0;
  $('div.voxel').each(function(){
    thisY = parseInt($(this).css('top'));
    Ymax = thisY > Ymax ? thisY : Ymax;
  });
}

//screen preparation
function screenPreparation(){
  var paintPaper = $('#paintPaper');
  windowWidth = $(window).width(); //$(document).width();    $(window).width();
  checkY();
  windowHeight = $.browser.msie ? document.documentElement.clientHeight : window.innerHeight;//$(document).height();
  var windowHeightFull = windowHeight > Ymax ? windowHeight : Ymax + 250;
  var panelHeight = $('#colorToolbox').height();
  var headerHeight = $('#header').innerHeight();
  
  var paintPaperHeight = windowHeightFull > 500 ? (windowHeightFull - panelHeight - headerHeight) : 500;
  paintPaperHeight = (paintPaperHeight - (paintPaperHeight % 76)) - 10;
  $(paintPaper).css('height',paintPaperHeight+'px');
  
  
  var toolsLeft = ($(window).width() - 997) / 2;
  toolsLeft = toolsLeft - (toolsLeft % 66);

  //alert(windowWidth+' - '+);
  //alert(windowWidth+' - '+$(window).width());

  if(windowWidth > 1024){
    $('#colorToolbox').css('margin-left',toolsLeft +'px');
  }else{
    $('#colorToolbox').css('margin-left','0');
  }
};

//cubes action
$.fn.setCubeAction = function(){
  $(this)
  .draggable({
    grid: [33,19],
    start: function(event, ui){
      $(this).css('z-index',zInd+1);
      zInd++;
    },
    stop: function(event, ui){
      screenPreparation();
    }
  })
  .click(function(){
    $(this).fadeOut(300,function(){
      $(this).remove();
      screenPreparation();
    });
  });
  return this;
};



//cube field
$.fn.cubeField = function(){
  var posLeft,posTop;
  $(this).click(function(e){
    var posX = e.pageX - (e.pageX % 66);
    var posY = (e.pageY - (e.pageY % 38)) - 5;
    $(mainCont).append('<div class="voxel color13" style="left:'+posX+'px; top:'+posY+'px; z-index:'+(zInd+1)+';"></div>');
    $('div:last',mainCont).setCubeAction();
    zInd++;
  });
  return this;
  
};



//color tool
$.fn.colorTool = function(){
  var panel = $(this).parent();
  $(this).each(function(){
    var cube = $(this);
    var thisStyle;
    $(cube)
    .draggable({
      grid: [33,19],
      helper: 'clone',
      stop: function(event, ui){
        var thisClass = this.className;
        $(mainCont).append('<div class="voxel '+thisClass+'" style="left:'+ui.offset.left+'px; top:'+ui.offset.top+'px; z-index:'+(zInd+1)+';"></div>');
        $('div:last',mainCont).setCubeAction();
        zInd++;
        screenPreparation();
      }
    });
  });
};


//save form window
function showSaveForm(){
  $('#fill_the_form_box').hide();
  $('#saveImgBox').removeShadow();
  $('#saveImgBox').show().dropShadow(shadowOptions);
  return false;
}

//save complete window
function showSaveComplete(){
  $('#saveImgCompleteBox').show().dropShadow(shadowOptions);
  return false;
}

//save
function savePicture(){
  //скрипт сохнанения картинки
  $('#buttonSend').hide();
  $('#saveImgBox').removeShadow();
  $('#saveImgBox').dropShadow(shadowOptions);
  
  var cubes = Array();
  for(i=0;i<$('div.voxel').size();i++)
    {
    var left = parseInt($('div.voxel:eq('+i+')').css('left'));
    var top = parseInt($('div.voxel:eq('+i+')').css('top'));
    var color = $('div.voxel:eq('+i+')').attr('class').split(' ')[1].split('color')[1];
    var zindex = parseInt($('div.voxel:eq('+i+')').css('z-index'));
    cubes[i] = left+','+top+','+color+','+zindex;
    }
  
  var cubes_str = cubes.join(' ');
  var name = $('#user_name').val();
  var email = $('#user_email').val();
  var code = $('#user_code').val();
  var desc = $('#user_desc').val();
  var capd  = $('#capd').val();
  $.ajax({
      type: "POST",
      cache: false,
      url: 'save.php',
      data: {cubes:cubes_str,
             name:name,
             email:email,
             code:code,
             desc:desc,
             capd:capd},
      success: function(data){
        var str = '';
        $.each(data,function(i,val){str = str + val});
        
        if(str == "")
          {
          $('#saveImgBox').hide().removeShadow(); // убираем окошко
          $('#buttonSend').show();
          showSaveComplete();
          }
        else
          {
          $('#buttonSend').show();
          
          if(str == "Please fill the form")
            {
            $('#fill_the_form_box').show();
            $('#saveImgBox').removeShadow();
            $('#saveImgBox').dropShadow(shadowOptions);
            }
          else
            {
            $('#fill_the_form_box').hide();
            $('#saveImgBox').removeShadow();
            $('#saveImgBox').dropShadow(shadowOptions);
            alert(str);
            }
          
          }
        
        $.ajax({
            type: "POST",
            cache: false,
            url: '/cap',
            success: function(data){
              var str = '';
              $.each(data,function(i,val){str = str + val});
              str = '<input class="textfield" type="text" name="code"  id="user_code" style="width:90px" />' + str;
              $('#captcha-block').html(str);
              $('#user_code').html('');
            }
          });
        }
    });
  
  
  
  return false;
};


$(document).ready(function(){
    mainCont = $('body');
    $('div.dropshadow').dropShadow(shadowOptions);
});

