var ContentObjekt = Class.create();
ContentObjekt.prototype = {
  initialize:function(Type, contentID, PreviewFactor, BoxWidth, BoxHeight, PercentWidth, PercentHeight) {
    this.PreviewFactor    = PreviewFactor;
    this.CB               = contentID;
    this.OffsetWidth      = BoxWidth;
    this.OffsetHeight     = BoxHeight;
    this.PercentWidth     = PercentWidth;
    this.PercentHeight    = PercentHeight;
    this.Type             = Type;
    this.Width            = 0;
    this.Height           = 0;
    this.Alpha            = 0;
    this.FitWidth         = 0;
    this.FitHeight        = 0;
    this.previewWidth     = 0;
    this.previewHeight    = 0;
  }
}

var TextObjekt = Class.create();
TextObjekt.prototype = {
  initialize:function(owner, textID, bgColor) {
    this.ID          = textID;
    this.owner      = owner;
    this.Left        = 0;
    this.Top        = 0;
    this.bgColor    = bgColor ? bgColor : "ffffff";
    this.renderText = false;
    var now         = new Date();
    this.fileTime   = now.getTime();
  },

  getImageSrc:function(view) {

    var params       = "";
    var showWidth    = Math.ceil(this.OffsetWidth);
    var showHeight   = Math.ceil(this.OffsetHeight);
    var boxSize      = project.getBoxSizeMM(this.PercentWidth, this.PercentHeight);

    if (view == "Preview") {
      showWidth  = Math.ceil(this.OffsetWidth  * this.PreviewFactor);
      showHeight = Math.ceil(this.OffsetHeight * this.PreviewFactor);
    }

    params += "&resize=true";
    params += "&ftime=" + this.fileTime;
    params += "&color=" + this.bgColor;

    return "/interfaces/project_properties.php?action=getUserTextImage"
                                         + "&text_id="   + this.ID
                                         + "&width="     + showWidth
                                         + "&height="    + showHeight
                                         + "&width_mm="  + boxSize[0]
                                         + "&height_mm=" + boxSize[1]
                                         + params;
  },

  setImageSize:function(Width, Height, newScale) {
    var now       = new Date();
    this.fileTime = now.getTime();
  }
}

var ImageObjekt = Class.create();
ImageObjekt.prototype = {
  initialize:function(owner, imageID, origwidth, origheight) {
    this.bestQuality         = project.bestQuality;
    this.mediumQuality       = project.mediumQuality;
    this.ID                    = imageID;
    this.origwidth           = origwidth;
    this.origheight          = origheight;
    this.ratioOrig            = project.roundValue((origwidth/origheight), 8);
    this.owner               = owner;
    this.quality             = "";
    this.scale               = 1;
    this.Left                 = 0;
    this.Top                 = 0;
    this.srcWidth            = 0;
    this.srcHeight           = 0;
    this.ImagePercentWidth   = 1;
    this.ImagePercentHeight  = 1;
  },

  setImagePosition:function(posLeft, posTop, boxWidth, boxHeight) {
    if (posTop > 0)  { posTop = 0;  }
    if (posLeft > 0) { posLeft = 0; }
    this.Left = project.roundValue((posLeft / boxWidth), 10);
    this.Top   = project.roundValue((posTop   / boxHeight), 10);
  },

  setImageRotation:function(alpha) {
    this.Alpha += alpha;
    if (this.Alpha > 360)  { this.Alpha = 90;  }
    if (this.Alpha < -360) { this.Alpha = -90; }
  },

  setFit:function(FitWidth, FitHeight) {
    this.FitWidth   = FitWidth;
    this.FitHeight  = FitHeight;
  },

  getResolution:function(image, box, orig, factor) {
    var scale      = project.roundValue((image / box), 8);
    var pix        = project.roundValue((1 / scale * orig), 8);
    // @todo: gibts hier einen dreher?
    var resolution = project.roundValue((pix / project.roundValue((box / factor), 8)), 8);

    return resolution;
  },

  checkMaxScale:function(offsetImage) {
    var scale      = project.roundValue((offsetImage / this.OffsetWidth), 8);
    var realOffset = project.roundValue((1 / scale * this.origwidth), 8);
    var maxOffset  = project.roundValue((this.OffsetWidth / project.Pagefactor), 8) * this.mediumQuality;

    if(maxOffset > realOffset) {
      return false;
    } else {
      return true;
    }
  },

  getImageSrc:function(view) {
    // wurde das bild im vergleich zum original gedreht?
    // Achtung hier gibts noch fehler mit der drehung vom original

    // bestimme bildqualität
    var resolution   = this.getResolution(this.Width, this.OffsetWidth, this.origwidth, project.Pagefactor);

    if(resolution > this.bestQuality) {
      this.quality = "best";
    } else {
       if(resolution > this.mediumQuality) {
         this.quality = "medium";
       } else {
         this.quality = "bad";
       }
    }

    var w = this.srcWidth;
    var h = this.srcHeight;

    if (view == "Preview") {
      if(this.srcWidth != 50000) {
         w = this.srcWidth * this.PreviewFactor;
      }
      if(this.srcHeight != 50000) {
         h = this.srcHeight * this.PreviewFactor;
      }
    }

    return ImageTools.getStaticImageURLByOwner(this.owner, FOA.User.id, this.ID, this.Type, w, h, ("&enlarge=true&rotate=" + this.Alpha));
  },

   /**
  * Bestimmt in den gegebenen Wunschausmaßen (= Arbeitsflaeche) die effektive Bildgroeße
  * unter Beruecksichtigung des originalen Seitenverhaeltnisses
  */
  setImageSize:function(Width, Height, newScale) {

    // Seitenverhaeltnis des Originalbilds (Breite/Hoehe)
    var ratioOriginalImage  = this.ratioOrig;
    var rotation            = false;

    // originale Boxgröße
    var boxWidth    = this.PercentWidth   * (project.PageWidth)   / 100;
    var boxHeight   = this.PercentHeight  * (project.PageHeight)  / 100;

    // originale Bildgröße
    var imageWidth  = this.origwidth;
    var imageHeight = this.origheight;

    // wenn das bild gedreht wurde
    if(this.Alpha == 90 || this.Alpha == -90 || this.Alpha == 270 || this.Alpha == -270) {
      rotation    = true;
      var temp    = imageWidth;
      imageWidth  = imageHeight;
      imageHeight = temp;
    }

    // Seitenverhaeltnis der Wunschmaße
    var ratioImage = imageWidth / imageHeight;
    var ratioBox   = boxWidth / boxHeight;

    // Seitenverhaeltnis der Ausgabe kleiner als Bildseitenverhaeltnis
    // => Hoehe anpassen, Breite steht dann ueber
    if (ratioBox < ratioImage) {

      if (typeof newScale == "undefined") {
        this.scale = project.roundValue((Height / this.OffsetHeight), 10);
      }
      else {
        this.scale = newScale;
      }

      // scale darf nicht 0 sein
      if (this.scale == 0) {
        this.scale = 1;
      }

      this.ImagePercentWidth   = ((imageWidth / (imageHeight / boxHeight)) / boxWidth) * this.scale;
      this.ImagePercentHeight  = this.scale;

      if (rotation) {
        this.srcWidth   = 50000;
        this.srcHeight  = this.ImagePercentWidth * this.OffsetWidth;
      }
      else {
        this.srcWidth   = this.ImagePercentWidth * this.OffsetWidth;
        this.srcHeight  = 50000;
      }
    }
    // ansonten: => Breite anpassen, Hoehe steht ueber
    else {

      if (typeof newScale == "undefined") {
        this.scale = project.roundValue((Width / this.OffsetWidth), 10);
      }
      else {
        this.scale = newScale;
      }

      // scale darf nicht 0 sein
      if (this.scale == 0) {
        this.scale = 1;
      }

      this.ImagePercentWidth   = this.scale;
      this.ImagePercentHeight  = ((imageHeight / (imageWidth / boxWidth)) / boxHeight) * this.scale;

      if (rotation) {
        this.srcWidth   = this.ImagePercentHeight * this.OffsetHeight;
        this.srcHeight  = 50000;
      }
      else {
        this.srcWidth   = 50000;
        this.srcHeight  = this.ImagePercentHeight * this.OffsetHeight;
      }
    }

    this.Width  = project.roundValue((this.ImagePercentWidth  * this.OffsetWidth), 8);
    this.Height = project.roundValue((this.ImagePercentHeight * this.OffsetHeight), 8);

    if(this.srcWidth == 50000 && this.srcHeight > 2000) {
      this.srcHeight = 2000;
    }
    if(this.srcHeight == 50000 && this.srcWidth > 2000) {
      this.srcWidth = 2000;
    }
  }
}