/*
 * ByRei clip-rect
 *
 * Copyright (c) 2009 Markus Bordihn (markusbordihn.de)
 *
 * $Date: 2009-08-09 19:00:00 +0100 (Sun, 09 Aug 2009) $
 * $Rev: 0.3 $
 */

CreateFlash('/swf/loader_0.3.swf',400,120,'','loader');
document.write('      <div id="clip_rect" class="dynDiv_moveDiv">'
             + '       <div class="dynDiv_resizeDiv_tl"><\/div>'
             + '       <div class="dynDiv_resizeDiv_tr"><\/div>'
             + '       <div class="dynDiv_resizeDiv_bl"><\/div>'
             + '       <div class="dynDiv_resizeDiv_br"><\/div>'
             + '      <\/div>');

var 
 isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1,
 flash_image = document.getElementById('flash_image'),
 clip_rect =  document.getElementById('clip_rect'),
 clip_area =  document.getElementById('clip-area'),
 loader = document.getElementById('loader'),
 reseting = document.getElementById('reseting'),
 testing = document.getElementById('testing'),
 result = document.getElementById('result'),
 config = {};

function func_testing() {
 clip_rect.style.visibility = 'hidden';
 flash_image.style.clip = config.result;
}

function func_reseting() {
 flash_image.style.clip = "rect(auto auto auto auto)";
 clip_rect.style.visibility = 'visible';
}

if (flash_image && loader && clip_rect && testing && result && reseting && clip_area && typeof ByRei_dynDiv !== 'undefined') {

          ByRei_dynDiv.api.drop = function () {
           if (ByRei_dynDiv.api.obj) {
               config.clip_width = ByRei_dynDiv.db(13);
               config.clip_height = ByRei_dynDiv.db(14);
               config.clip_top = ByRei_dynDiv.db(5);
               config.clip_left = ByRei_dynDiv.db(4);
               config.result = "rect(" + config.clip_top + "px " + (config.clip_left + config.clip_width + 2) + "px " + (config.clip_top + config.clip_height + 2) + "px " + (config.clip_left) +"px)";
               testing.disabled = false;
               reseting.disabled = false;
               result.disabled = false;
               result.value = config.result;
           }
          }

          config.width = loader.width;
          config.height = loader.height;
          if (isInternetExplorer) {
              loader.attachEvent("FSCommand", loader_DoFSCommand);
          }
          function loader_DoFSCommand(command, args) {
             switch(command) {
              case "set_size" :
               if (args) {
                   if (/\{width:(\d+), height:(\d+)\}/.test(args)) {
                       if (RegExp.$1 > 0 && RegExp.$1 !== config.width) {
                           loader.width = config.width = RegExp.$1;
                           clip_area.style.width = flash_image.style.width = RegExp.$1 + 'px';
                       }
                       if (RegExp.$2 > 0 && RegExp.$2 !== config.height) {
                           loader.height = config.height = flash_image.height = RegExp.$2;
                           clip_area.style.height = flash_image.style.height = RegExp.$2 + 'px';
                       } 
                   }
               }
              break;
              case "set_mode":
               switch (args) {
                case "upload":
                 clip_rect.style.visibility = 'hidden';
                 clip_rect.style.top = "10px";
                 clip_rect.style.left = "10px";
                 clip_rect.style.width = "50px";
                 clip_rect.style.height = "50px";
                 testing.disabled = true;
                 reseting.disabled = true;
                 result.disabled = true;
                 flash_image.style.clip = "rect(auto auto auto auto)";
                break;
                case "preview":
                 clip_rect.style.visibility = 'visible';
                break;
                default:
                 alert(command + ':' + args);
                break;
               }
              break;
             }
          }
      }