var MAX_IMAGE_WIDTH;
var MAX_IMAGE_HEIGHT;

function PhotoWeb( pToolkitURL, pDemoImage )
{
  this.contestOn                = true;
  this.lastSavedImage           = null;
  this.previewOn                = false;
  this.currentlySelectedWindow  = null;
  this.baseURL                  = pToolkitURL.substring(0, pToolkitURL.lastIndexOf("/")+1);
  this.toolkitURL               = pToolkitURL;
  this.demoImage                = pDemoImage;
  this.currentTalkBalloon       = null;
  this.doExport                 = false;
  this.freeAddWindows           = new Array("addone", "addtwo", "addthree", "addfour", "addfive", "addsix", "addseven", "addeight", "addnine", "addten", "addeleven", "addtwelve", "addthirteen", "addfourteen", "addfifteen", "addsixteen", "addseventeen", "addeighteen", "addnineteen", "addtwenty", "addtwentyone", "addtwentytwo", "addtwentythree", "addtwentyfour", "addtwentyfive", "addtwentysix", "addtwentyseven", "addtwentyeight", "addtwentynine", "addthirty");
  this.hiddenAdds               = null;
  this.addX                     = 160;
  this.addY                     = 120;
  this.baseImage                = null;
  this.saveIFrame               = "";
  this.openIFrame               = "";
  this.currentSaveData          = "";
  this.loggedIn                 = false;
  
  this.showSaveAsDialog         = pwShowSaveAsDialog;
  this.getBaseImagesText        = pwGetBaseImagesText;
  this.getClipartWindowText     = pwGetClipartWindowText;
  this.setBaseImageTitle        = pwSetBaseImageTitle;
  this.resize                   = pwResize;
  this.login                    = pwLogin;
  this.getLoginDialogText       = pwGetLoginDialogText;
  this.about                    = pwAbout;
  this.getAboutDialogText       = pwGetAboutDialogText;
  this.loadBaseImage            = pwLoadBaseImage;
  this.getWorkingAreaY          = pwGetWorkingAreaY;
  this.getFullWidth             = pwGetFullWidth;
  this.getFullHeight            = pwGetFullWidth;
  this.duplicate                = pwDuplicate;
  this.getWorkingAreaWidth      = pwGetWorkingAreaWidth;
  this.getWorkingAreaHeight     = pwGetWorkingAreaHeight;
  this.setSaveIFrame            = pwSetSaveIFrame;
  this.setBaseImage             = pwSetBaseImage;
  this.toggleBorders            = pwToggleBorders;
  this.addTalkBalloon           = pwAddTalkBalloon;
  this.goSetTalkBalloonText     = pwGoSetTalkBalloonText;
  this.setTalkBalloonText       = pwSetTalkBalloonText;
  this.closeTalkBalloonDialog   = pwCloseTalkBalloonDialog;
  this.addPicture               = pwAddPicture;
  this.openSavedImage           = pwOpenSavedImage;
  this.openImage                = pwOpenImage;
  this.getOpenDialogText        = pwGetOpenDialogText;
  this.getOpenSavedDialogText   = pwGetOpenSavedDialogText;
  this.closeOpenDialog          = pwCloseOpenDialog;
  this.clear                    = pwClear;
  this.demo                     = pwDemo;
  this.save                     = pwSave;
  this.getSaveAsDialogText      = pwGetSaveAsDialogText;
  this.startSave                = pwStartSave;
  this.getTalkBalloonDialogText = pwGetTalkBalloonText;
  this.getFreeAddWindow         = pwGetFreeAddWindow;
  this.releaseAddWindow         = pwReleaseAddWindow;
  this.showSavedDialog          = pwShowSavedDialog;
  this.closeSavedDialog         = pwCloseSavedDialog;
  this.getSavedDialogText       = pwGetSavedDialogText;
  this.getSavedDialogTextFailed = pwGetSavedDialogTextFailed;
  this.gallery                  = pwGallery;
  this.doPicture                = pwDoPicture;
  this.showAdReminder           = pwShowAdReminder;
  this.openLink                 = pwOpenLink;
  this.handleImageMouseDown     = pwHandleImageMouseDown;
  this.lowerWindow              = pwLowerWindow;
  this.dropShadow               = pwDropShadow;
  this.getImageWindows          = pwGetImageWindows;
  this.raiseWindow              = pwRaiseWindow;
  this.findNewSelectedWindow    = pwFindNewSelectedWindow;
  this.setSelectedWindow        = pwSetSelectedWindow;
  this.togglePreview            = pwTogglePreview;
  this.deselectAll              = pwDeselectAll;
  this.contest                  = pwContest;
  this.closeContestDialog       = pwCloseContestDialog;
  this.getContestDialogText     = pwGetContestDialogText;  
  this.showWatermark            = pwShowWatermark;
  this.hideWatermark            = pwHideWatermark;
  this.getExportDialogText      = pwGetExportDialogText;
  this.centerDialog             = pwCenterDialog;

}
function pwDeselectAll()
{
  components = componentManager.GetAllComponents("window");
  for( i=0; i < components.length; i++)
  {
    if( components[i].name.indexOf("add") > -1 )
    {
      components[i].HideExtras();
    }
  }
}

function pwTogglePreview()
{
  if( this.previewOn )
    this.previewOn = false;
  else
    this.previewOn = true;
    
  components = componentManager.GetAllComponents("window");
  for( i=0; i < components.length; i++)
  {
    if( components[i].name.indexOf("add") > -1 
    &&  components[i].IsVisible() )
    {
      if( this.previewOn )
      {
        components[i].HideExtras();
      }
      else
      {
        components[i].ShowExtras();
      }
    }
  }
}
function pwDropShadow()
{
  if( !this.currentlySelectedWindow.isTalkBalloon )
  {
    alert("This feature is only for talk balloons!");
    return;
  }
  this.duplicate(2,2);
  this.lowerWindow();
  var text = this.currentlySelectedWindow.text;
  if( browsertype == IE )
  {
    text = text.replace(/\#.*?/, "#000000");
  }
  else
  {
    text = text.replace(/\#.*?/, "#000000");
    text = text.replace(/\rgb(.*)?/, "rbg(00,00,00)");
  }
  this.currentlySelectedWindow.SetText(text);
}
function pwDuplicate(x,y)
{
  if( !x )
    x = 10;
  if( !y )
    y = 10;
  //this.currentlySelectedWindow
  var w = this.getFreeAddWindow();
  if( !w )
    return;

  w.imageUrl = this.currentlySelectedWindow.imageUrl; 
  
    w.SetText(this.currentlySelectedWindow.text);
  //w.SetText("");
  w.SetIsImage(true);
  w.SetIsTalkBalloon(false);
  if( this.currentlySelectedWindow.isTalkBalloon )
  {
    w.SetIsTalkBalloon(true, w.imageUrl);
    w.SetText(this.currentlySelectedWindow.text);
    w.SetIsImage(false);
  }
  
  w.Resize(this.currentlySelectedWindow.style.width, this.currentlySelectedWindow.style.height);
  w.Move(this.currentlySelectedWindow.style.x+x, this.currentlySelectedWindow.style.y+y);
  w.Show();
  w.ToTop();
  this.setSelectedWindow(w);
}

function pwSetSelectedWindow( win )
{
  if( win.name.indexOf("add") == -1 )
    return; 
  this.deselectAll();
  this.currentlySelectedWindow = win;
  this.currentlySelectedWindow.ShowExtras();
 // this.currentlySelectedWindow.ToTop();
}

function pwSetSelectedWindow1( win )
{
  this.currentlySelectedWindow = win;
  components = componentManager.GetAllComponents("window");
  for( i=0; i < components.length; i++)
  {
    if( components[i].name.indexOf("add") > -1 && components[i].name != this.currentlySelectedWindow.name)
    {
      if( browsertype == IE )
      {
        eval(components[i].name+"lb.style.backgroundColor='"+DEFAULT_TITLECOLOR+"';");
        eval(components[i].name+"rb.style.backgroundColor='"+DEFAULT_TITLECOLOR+"';");
        eval(components[i].name+"tb.style.backgroundColor='"+DEFAULT_TITLECOLOR+"';");
        eval(components[i].name+"bb.style.backgroundColor='"+DEFAULT_TITLECOLOR+"';");
      }
      else
      {
        var w = document.getElementById(components[i].name+"lb");
        w.style.backgroundColor=DEFAULT_TITLECOLOR;
        var w = document.getElementById(components[i].name+"rb");
        w.style.backgroundColor=DEFAULT_TITLECOLOR;
        var w = document.getElementById(components[i].name+"tb");
        w.style.backgroundColor=DEFAULT_TITLECOLOR;
        var w = document.getElementById(components[i].name+"bb");
        w.style.backgroundColor=DEFAULT_TITLECOLOR;
      }
    }
  }
  if( browsertype == IE )
  {
        eval(this.currentlySelectedWindow.name+"lb.style.backgroundColor='"+HILITE_TITLECOLOR+"';");
        eval(this.currentlySelectedWindow.name+"rb.style.backgroundColor='"+HILITE_TITLECOLOR+"';");
        eval(this.currentlySelectedWindow.name+"tb.style.backgroundColor='"+HILITE_TITLECOLOR+"';");
        eval(this.currentlySelectedWindow.name+"bb.style.backgroundColor='"+HILITE_TITLECOLOR+"';");
  }
  else
  {
      var w = document.getElementById(this.currentlySelectedWindow.name + "lb");
      w.style.backgroundColor='gold';
      var w = document.getElementById(this.currentlySelectedWindow.name + "rb");
      w.style.backgroundColor='gold';
      var w = document.getElementById(this.currentlySelectedWindow.name + "tb");
      w.style.backgroundColor='gold';
      var w = document.getElementById(this.currentlySelectedWindow.name + "bb");
      w.style.backgroundColor='gold';
    //var w = document.getElementById(this.currentlySelectedWindow.name + "mover");
    //w.style.backgroundImage="url("+SELECTED_TITLEIMAGE+")";
  }
}
function pwFindNewSelectedWindow()
{
  var newZ = 0;
  components = componentManager.GetAllComponents("window");
  for( i=0; i < components.length; i++)
  {
    if( components[i].name.indexOf("add") > -1 )
    {
      if( components[i].style.zindex > newZ  )
      {
        this.currentlySelectedWindow = components[i];
      }
    }
  }
}

function pwGetImageWindows()
{
  var imageWindows = new Array();
  components = componentManager.GetAllComponents("window");
  for( i=0; i < components.length; i++)
  {
    if( components[i].name.indexOf("add") > -1 && components[i].IsVisible() )
    {
      imageWindows[imageWindows.length] = components[i];
    }
  }
  return imageWindows;
}

function pwRaiseWindow()
{
  var imageWindows = this.getImageWindows();

  imageWindows.sort(zComparator);

  if( imageWindows.length < 2 || imageWindows[imageWindows.length-1].name == this.currentlySelectedWindow.name )
    return;
    
  for( i=0; i < imageWindows.length; i++)
  {
    if( imageWindows[i].name == this.currentlySelectedWindow.name )
    {
      var myPreviousZ = this.currentlySelectedWindow.style.zindex;
      this.currentlySelectedWindow.style.zindex = imageWindows[i+1].style.zindex;
      DivZ(self, this.currentlySelectedWindow.name, this.currentlySelectedWindow.style.zindex);

      imageWindows[i+1].style.zindex = myPreviousZ;
      DivZ(self, imageWindows[i+1].name, imageWindows[i+1].style.zindex);
      
      imageWindows[i+1].Paint();
      this.currentlySelectedWindow.Paint();
      return;
    }
  }
}

function pwLowerWindow()
{
  var imageWindows = this.getImageWindows();

  imageWindows.sort(zComparator);

  if( imageWindows.length < 2 || imageWindows[0].name == this.currentlySelectedWindow.name )
    return;
    
  for( i=0; i < imageWindows.length; i++)
  {
    if( imageWindows[i].name == this.currentlySelectedWindow.name )
    {
      var myPreviousZ = this.currentlySelectedWindow.style.zindex;
      this.currentlySelectedWindow.style.zindex = imageWindows[i-1].style.zindex;
      DivZ(self, this.currentlySelectedWindow.name, this.currentlySelectedWindow.style.zindex);

      imageWindows[i-1].style.zindex = myPreviousZ;
      DivZ(self, imageWindows[i-1].name, imageWindows[i-1].style.zindex);

      imageWindows[i-1].Paint();
      this.currentlySelectedWindow.Paint();
      return;
    }
  }
}

/**
* Sort by Z-index ASCENDING, i.e. lowest to highest
*
*/
function zComparator(a, b)
{ 
  return ( a.style.zindex - b.style.zindex ); 
}

function pwRaiseWindow1( allTheWay )
{
  if( !this.currentlySelectedWindow )
    return;
  var newZ   = 0;
  components = componentManager.GetAllComponents("window");
  if( allTheWay )
  {
    this.currentlySelectedWindow.ToTop();
  }
  else
  {
    newZ = 1000000;
    for( i=0; i < components.length; i++)
    {
      if( components[i].name.indexOf("add") > -1  
      && components[i].IsVisible()
      && components[i].name != this.currentlySelectedWindow.name )
      {
        if( components[i].style.zindex > this.currentlySelectedWindow.style.zindex )
        {
          if( components[i].style.zindex < newZ )
            newZ = components[i].style.zindex;
        }
      }
    }
    this.currentlySelectedWindow.style.zindex = newZ;
    DivZ(self, this.currentlySelectedWindow.name, this.currentlySelectedWindow.style.zindex);
  }
}
function pwLowerWindow1( allTheWay )
{
  if( !this.currentlySelectedWindow )
    return;

  var newZ   = 0;
  components = componentManager.GetAllComponents("window");
  if( allTheWay )
  {
    newZ = this.currentlySelectedWindow.style.zindex;
    for( i=0; i < components.length; i++)
    {
      if( components[i].name.indexOf("add") > -1  
      && components[i].IsVisible()
      && components[i].name != this.currentlySelectedWindow.name)
      {
        if( components[i].style.zindex < newZ  )
        {
          newZ = components[i].style.zindex - 1;
        }
      }
    }
  }
  else
  {
    for( i=0; i < components.length; i++)
    {
      if( components[i].name.indexOf("add") > -1 
      && components[i].IsVisible()
      && components[i].name != this.currentlySelectedWindow.name )
      {
        if( components[i].style.zindex < this.currentlySelectedWindow.style.zindex )
        {
          if( components[i].style.zindex > newZ )
            newZ = components[i].style.zindex -1;
        }
      }
    }
  }
  this.currentlySelectedWindow.style.zindex = newZ;
  DivZ(self, this.currentlySelectedWindow.name, this.currentlySelectedWindow.style.zindex);
}




function pwOpenLink(s)
{
  if( s.indexOf('http') == - 1)
    s = 'http://' + s;
  window.open(s, '', '');
  void(0);
}
function pwGallery()
{
  window.open('gallery.php');
}
function pwGetAboutDialogText()
{
   return '<iframe id="aboutIframe" src="about.php" scrolling=no class="normalText" style="background-color:black;" height=100%  width=100%> </iframe>';
}
function pwLogin()
{
  this.centerDialog(window_logindialog);
  window_logindialog.SetText(this.getLoginDialogText());
  window_logindialog.ToTop();
  window_logindialog.Show();
}
function pwAbout()
{
  this.centerDialog(window_aboutdialog);
  window_aboutdialog.ToTop();
  window_aboutdialog.Show();
}
function pwSetSaveIFrame( pIFrame )
{
  this.saveIFrame = pIFrame;
}

function pwGetFullHeight()
{
  return this.getWorkingAreaHeight();
}
function pwGetFullWidth()
{
  return this.getWorkingAreaWidth() + window_clipart.style.width;
}
function pwGetWorkingAreaHeight()
{
  return window_image.style.height - 20;
}
function pwGetWorkingAreaY()
{
  return window_image.style.y;
}
function pwGetWorkingAreaWidth()
{
  return window_image.style.width;
}

/**
* Save the current image to the database.
*
*/

function sortByZ(a,b) 
{
  if (a.style.zindex < b.style.zindex) 
    return -1;
  if (a.style.zindex > b.style.zindex) 
    return 1;
    
  return 0;
}

function pwShowSaveAsDialog()
{
  if( !this.baseImage )
    return;

  var foundAddOn = false;
  for( i = 0; i< allComponents.length; i++)
  {
    if( allComponents[i].name.indexOf("add") >- 1 && allComponents[i].IsVisible() )
    {
      foundAddOn = true;
    }
  }
  if( !foundAddOn )
  {
    alert("Please modify this image before saving or exporting!");
    return;
  }
  
  if(!this.baseImage.name || this.baseImage.name == "" || this.baseImage.name.indexOf("Untitled") == 0) 
  {
    this.centerDialog( window_saveddialog );
    window_saveddialog.SetText(this.getSaveAsDialogText());
    window_saveddialog.Show();
    window_saveddialog.ToTop();
  }
  else
  {
    this.startSave(false);
  }
}
function pwStartSave(doExport)
{
  this.doExport = doExport;
    
  if( browsertype == MOZILLA || browsertype == SAFARI ) // KRG
  {
    document.getElementById(this.saveIFrame).src = "saveimage.php";
  }
  else
  {
    eval(this.saveIFrame).location="saveimage.php";
  }
}

function pwSave()
{
  // offset by top and left of our image, which is centered in the working
  // area
  if( !this.baseImage || !this.baseImage.width || !this.baseImage.height )
  {
    return;
  }
  
  
  var imageLeft = (this.getWorkingAreaWidth() / 2)  - (this.baseImage.width / 2) + 150;
  var imageTop  = (this.getWorkingAreaHeight() / 2)  - (this.baseImage.height / 2) + this.getWorkingAreaY();
  
  var saveString = "";
  saveString += "baseImageURL=#"    + escape( this.baseImage.url )    + "#";
  saveString += "baseImageWidth=#"  + escape( this.baseImage.width )  + "#";
  saveString += "baseImageHeight=#" + escape( this.baseImage.height ) + "#";
  saveString += "|";
  // now our addons
  allComponents = componentManager.GetAllComponents("window");
  allComponents.sort(sortByZ);

  for( i = 0; i< allComponents.length; i++)
  {
    var xOffset = 0;
    var yOffset = 0;
    if( browsertype == MOZILLA || browsertype == SAFARI ) // KRG
    {
      xOffset = 1;
      yOffset = 0;
    }
    else
    {
      xOffset = 0;
      yOffset = 1;
    }
    if( allComponents[i].name.indexOf("add") >- 1 && allComponents[i].IsVisible() )
    {
      if( this.doExport )
        saveString += "addOnURL=#"    + escape( allComponents[i].GetProcessedImageURL() )     + "#";
      else
        saveString += "addOnURL=#"    + escape( allComponents[i].imageUrl )     + "#";
      saveString += "addOnWidth=#"  + escape( allComponents[i].style.width )  + "#";
      saveString += "addOnHeight=#" + escape( allComponents[i].style.height ) + "#";
      saveString += "addOnX=#"      + escape( allComponents[i].style.x - imageLeft + xOffset )      + "#";
      saveString += "addOnY=#"      + escape( (allComponents[i].style.y  ) - imageTop - TITLE_HEIGHT + yOffset )      + "#";
      saveString += "addOnZ=#"      + escape( allComponents[i].style.zindex )     + "#";
      if( allComponents[i].isTalkBalloon )
      {
        saveString += "addOnTalkBalloonText=#"    + escape( allComponents[i].rawText )     + "#";
      }
      else
      {
        saveString += "addOnTalkBalloonText=#"    + escape( "" )     + "#";
      }
      saveString += "|";
    }
  }

  if( browsertype == MOZILLA || browsertype == SAFARI ) // KRG
  {
    document.getElementById(this.saveIFrame).contentDocument.getElementById("imageData").value = saveString;
    document.getElementById(this.saveIFrame).contentDocument.getElementById("imageID").value = this.baseImage.id;
    document.getElementById(this.saveIFrame).contentDocument.getElementById("imageName").value = this.baseImage.name;
    if( this.doExport )
      document.getElementById(this.saveIFrame).contentDocument.getElementById("export").value = "true";
    else
      document.getElementById(this.saveIFrame).contentDocument.getElementById("export").value = "false";
    document.getElementById(this.saveIFrame).contentDocument.getElementById("saveForm").submit();
  }
  else
  {
    var saveForm = eval(this.saveIFrame + ".document.forms[0]");
    saveForm.elements[0].value = saveString;
    saveForm.elements[1].value = this.baseImage.name;
    saveForm.elements[2].value = this.baseImage.id;
    if( this.doExport )
    {
      saveForm.elements[3].value = "true";
    }
    else
    {
      saveForm.elements[3].value = "false";
    }
    saveForm.submit();
  }
  this.doExport = false;
  // when saveImage.php finishes, it will display the "saved" box
}
function pwCenterDialog(dialog)
{
  var x = (this.getFullWidth() - dialog.style.width) / 2;
  var y = (this.getFullHeight() - dialog.style.height) / 4;
  dialog.Move(x,y); 
}
function pwSetBaseImageTitle(title)
{
  this.baseImage.name = title;
  window_image.MSetText("<font class='window_opendialogm-font'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + this.baseImage.name + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>");
}
function pwSetBaseImage(pBaseImage)
{
  this.baseImage = pBaseImage;
}

function pwToggleBorders( addWindow )
{
  var w = null;
  w = componentManager.FindComponent(addWindow);
  w.ToggleExtras();
}

function pwDoPicture( w, pictureName, width, height )
{
  var maxheight = this.MAX_IMAGE_HEIGHT / 2;
  var maxwidth  = this.MAX_IMAGE_WIDTH / 2;
  
  if( top.gotDrop )
  {
    pictureName = top.currentlyDraggedSrc;
    width       = top.currentlyDraggedWidth;
    height      = top.currentlyDraggedHeight;
  }
  
  // if we are OPENING a saved picture, then we DO NOT resize
  if( this.baseImage.id && this.baseImage.id > 0 )
  {
  }
  // resize if it's too big...
  else
  {
    if( height > width )
    {
      if( height > maxheight  )
      {
        width     = Math.round(width * (maxheight / height));
        height    = maxheight;
      }
    }
    else
    {
      if( width > maxwidth )
      {
        height    = Math.round(height * (maxwidth / width));
        width     = maxwidth;
      }
    }
  }
  
  if( top.gotDrop )
  {
    // assuming they grabbed the exact center of the image, haha
    w.Move( top.currentlyDraggedX - width / 2, top.currentlyDraggedY - height / 2 );
    top.gotDrop    = false;
  }
  else
  {
    w.Move( this.addX, this.addY );
    this.addX += 10;
    this.addY += 10;
  }
  return new PWImage( pictureName, height, width );
}

function pwHandleImageMouseDown(e, name)
{
  var currentWindow = componentManager.FindComponent(name);
  if( currentWindow.extrasShown )
  {
    if( browsertype == MOZILLA )
    {
      currentWindow.mzGoMove(e);
    }
    // KRG BEGIN
    else if (browsertype == SAFARI)
    {
      currentWindow.sfGoMove(e);
    }
    // KRG END
    else
      currentWindow.ieGoMove();
  }
  else
    this.toggleBorders(currentWindow.name);
}

function pwAddPicture(pictureName, width, height)
{
  var w = this.getFreeAddWindow();
  if( !w )
    return;

  var image;
  if( pictureName )
    image = this.doPicture( w, pictureName, width, height );
  else
    image = this.doPicture( w );

  w.imageUrl = image.url;
  w.SetIsImage(true);
  w.SetIsTalkBalloon(false, image.url);
//  w.MSetText("&nbsp;");
  w.Resize(image.width, image.height);
  w.Show();
  w.ToTop();
  var t = "<img  src="+ image.url +" border=0 ";
  w.SetText(t);
  this.setSelectedWindow(w);
  
//  statusword.SetText('Click image to hide the borders.');  
//  document.close();
}

function pwAddTalkBalloon(pictureName, width, height)
{
  var w = this.getFreeAddWindow();

  if( !w )
    return;

  var image;

  if( pictureName )
    image = this.doPicture( w, pictureName, width, height );
  else
    image = this.doPicture( w );
  
  // show talk 
  w.imageUrl = image.url;
  w.SetIsTalkBalloon(true, image.url);
  w.SetIsImage(false);
//  w.MSetText("");
  w.Resize(image.width, image.height);
  w.Show();
  w.ToTop();
  w.SetText("");
  this.setSelectedWindow(w);
}

function pwGoSetTalkBalloonText( windowName )
{
  var w = componentManager.FindComponent(windowName);
  this.currentTalkBalloon = w;
 	  mt = "<table border=0 width=" +window_textballoondialog.mstyle.width+ " height=" + window_textballoondialog.mstyle.height + " cellpadding=" + BORDERSIZE + " cellspacing=0 width=" + window_textballoondialog.mstyle.width + "><tr><td align=center  valign=bottom height=" + window_textballoondialog.mstyle.height + "><b><span style='font-family:Arial;font-size:10px;color:white;'>" + "Edit Talk Bubble" + "</table>";
	DivWrite( eval(window_textballoondialog.name + "mover"), mt);
window_textballoondialog.SetText(this.getTalkBalloonDialogText());
 	  mt = "<table border=0 width=" +window_textballoondialog.mstyle.width+ " height=" + window_textballoondialog.mstyle.height + " cellpadding=" + BORDERSIZE + " cellspacing=0 width=" + window_textballoondialog.mstyle.width + "><tr><td align=center  valign=bottom height=" + window_textballoondialog.mstyle.height + "><b><span style='font-family:Arial;font-size:10px;color:white;' ><font color=white>" + "Edit Talk Bubble" + "</table>";
	DivWrite( eval(window_textballoondialog.name + "mover"),mt);
  this.centerDialog( window_textballoondialog );
  window_textballoondialog.Show();
  window_textballoondialog.ToTop();
//  document.getElementById("tbtext").focus();
}
function pwSetTalkBalloonText(t)
{
 /*
  generateTalkBalloon($_REQUEST['image'], 
										$_REQUEST['addOnImageURL'], 
										$_REQUEST['x'], 
										$_REQUEST['y'], 
										$_REQUEST['height'], 
										$_REQUEST['width'], 
										$_REQUEST['addOnText']);
  */
  this.currentTalkBalloon.SetText(t);
  //this.currentTalkBalloon.Paint();
}
function pwCloseTalkBalloonDialog()
{
  window_textballoondialog.SetText("");
  window_textballoondialog.Hide();
}
function pwGetFreeAddWindow()
{
  if( this.freeAddWindows.length <= 0 )
  {
    alert("Sorry, you can't have more than thirty elements on one page!");
    statusword.SetText("Sorry, you can't have more than thirty elements on one page!");
    return false;
  }
    
  var freeNumber  = this.freeAddWindows[0];
  var w           = componentManager.FindComponent(freeNumber);
  
  // now remove it from our array
  var tempArray = new Array();
  for( var x = 0; x < this.freeAddWindows.length; x++ )
  {
    if( this.freeAddWindows[x] != freeNumber )
      tempArray[tempArray.length] = this.freeAddWindows[x];
  }
  this.freeAddWindows = tempArray;
  
  return w;
}
function pwReleaseAddWindow(name)
{
  this.freeAddWindows[this.freeAddWindows.length] = name;
}


function pwContest()
{
  if( !this.lastSavedImage)
    alert("You must save an image to enter the contest!");
  window_contestdialog.SetText(this.getContestDialogText());
  this.centerDialog( window_contestdialog );
  window_contestdialog.Show();
  window_contestdialog.ToTop();
}
function pwCloseContestDialog()
{
  window_contestdialog.Hide();
  this.centerDialog( window_saveddialog );
  window_saveddialog.Show();
  window_saveddialog.ToTop();
}
function pwGetContestDialogText()
{
  return '<iframe id="contestIframe" src="saveauthor.php?imageKey='+this.lastSavedImage+'" scrolling=no class="normalText" style="background-color:black;" height=100%  width=100%> </iframe>';
}

function pwOpenImage(clipart)
{
/*  hiddenAdds = new Array();
  // hide all add windows
  allComponents = componentManager.GetAllComponents("window");
  for( i = 0; i< allComponents.length; i++)
  {
    if(allComponents[i].name.indexOf("add") >- 1 && allComponents[i].IsVisible())
    {
      allComponents[i].Hide();
      hiddenAdds[hiddenAdds.length]=allComponents[i];
    }
  }
  window_image.Hide();*/
  if( clipart && this.freeAddWindows.length <= 0 )
  {
    alert("Sorry, you can't have more than thirty elements on one page!");
    statusword.SetText("Sorry, you can't have more than thirty elements on one page!");
    return false;
  }
  window_opendialog.SetText(this.getOpenDialogText(clipart));
  //window_opendialog.MSetText("Open");
  this.centerDialog( window_opendialog );
  window_opendialog.Show();
  window_opendialog.ToTop();
}
function pwOpenSavedImage()
{
  window_open_saved.SetText(this.getOpenSavedDialogText());
  this.centerDialog( window_open_saved );
  window_open_saved.Show();
  window_open_saved.ToTop();
}
function pwCloseOpenDialog()
{
  window_opendialog.Hide();
//  this.showAdReminder();
/*  window_image.Show();
  for( i = 0; i< hiddenAdds.length; i++)
  {
    hiddenAdds[i].Show();
  }*/
}
function pwResize(width, height)
{
  MAX_IMAGE_HEIGHT = height;
  MAX_IMAGE_WIDTH  = width;
  
  window_demo.Move(width+CLIPART_WINDOW_WIDTH - 250, (height + MENUBAR_HEIGHT) - DEMO_DIALOG_HEIGHT);
  
  window_image.Resize(width, height+10);
  window_clipart.Resize(CLIPART_WINDOW_WIDTH, height+10);
  window_baseimages.Resize(CLIPART_WINDOW_WIDTH, height+10);

  statusbarlabel.Resize(width + CLIPART_WINDOW_WIDTH, STATUSBAR_HEIGHT);
  graybar.Resize(width + CLIPART_WINDOW_WIDTH, GRAYBAR_HEIGHT);
  statusword.Resize(width + CLIPART_WINDOW_WIDTH, STATUSBAR_HEIGHT);

  statusbarlabel.Move(0, height + MENUBAR_HEIGHT + GRAYBAR_HEIGHT + 10);
  graybar.Move(0, height + MENUBAR_HEIGHT + 10);
  statusword.Move(0, height + MENUBAR_HEIGHT + GRAYBAR_HEIGHT + 10);
  
  
}
function pwLoadBaseImage( imageURL, width, height, iID, iTitle )
{
  this.clear();
  
  if( top.gotDrop )
  {
    imageURL    = top.currentlyDraggedSrc;
    width       = top.currentlyDraggedWidth;
    height      = top.currentlyDraggedHeight;
    top.gotDrop = false;
  }
  
  // saved image
  if( iID )
  {
    if( height > width )
    {
      if( height > this.MAX_IMAGE_HEIGHT )
      {
        this.resize(width+20, height+20);
      }
    }
    else
    {
      if( width > this.MAX_IMAGE_WIDTH )
      {
        this.resize(width+20, height+20);
      }
    }
  }
  else
  {
    if( height > width )
    {
      if( height > this.MAX_IMAGE_HEIGHT )
      {
        width = Math.round(width * (this.MAX_IMAGE_HEIGHT / height));
        height  = this.MAX_IMAGE_HEIGHT;
      }
    }
    else
    {
      if( width > this.MAX_IMAGE_WIDTH )
      {
        height = Math.round(height * (this.MAX_IMAGE_WIDTH / width));
        width  = this.MAX_IMAGE_WIDTH;
      }
    }
  }
  
  if( width == 0 )
  {
    widthstr = "";
    heightstr = "";
  }
  else
  {
    widthstr =  'width=' + width + ' ';
    heightstr = 'height=' + height + ' ';
  }
  
  this.setBaseImage(new PWImage(imageURL, height, width));
  if( iID )
    this.baseImage.id = iID;
  if( iTitle )
    this.setBaseImageTitle(iTitle);
    
  window_image.SetText('<table border=0 cellpadding=0 cellspacing=0 width=100% height=100%><tr><td id="baseimagetd" width=100% height=100% align=center valign=center><img onClick="top.photoWeb.deselectAll();" id=baseimage '  + widthstr + heightstr + ' src="'+imageURL+'"></td></tr></table>');
  window_baseimages.Hide();
  window_clipart.Show();
  window_clipart.SetText(this.getClipartWindowText());
  top.statusword.SetText('Ready.');
  
  this.showWatermark(width,height);  
}

function pwShowWatermark(width, height)
{
  var imageLeft = (this.getWorkingAreaWidth() / 2)  - (this.baseImage.width / 2) + 150;
  var imageTop  = (this.getWorkingAreaHeight() / 2)  - (this.baseImage.height / 2) + this.getWorkingAreaY();
  DivWrite("editorWatermark",  "<img src='images/blobstampsmall.gif'>");
  DivMove("editorWatermark", (imageTop + height + this.getWorkingAreaY()) - WATERMARK_HEIGHT, imageLeft + 5 );
  DivShow(top, "editorWatermark");
  DivResurrectDisplay(top, "editorWatermark");
}
function pwHideWatermark()
{
  DivHide(top, "editorWatermark");
  DivKillDisplay(top, "editorWatermark");
}

function pwClear()
{
  window_image.MSetText("<font class='window_opendialogm-font'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;My Creation&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>");
  this.hideWatermark();  
  this.freeAddWindows           = new Array("addone", "addtwo", "addthree", "addfour", "addfive", "addsix", "addseven", "addeight", "addnine", "addten", "addeleven", "addtwelve", "addthirteen", "addfourteen", "addfifteen", "addsixteen", "addseventeen", "addeighteen", "addnineteen", "addtwenty", "addtwentyone", "addtwentytwo", "addtwentythree", "addtwentyfour", "addtwentyfive", "addtwentysix", "addtwentyseven", "addtwentyeight", "addtwentynine", "addthirty");
  this.setBaseImage(null);
  window_image.SetText("");
  allComponents = componentManager.GetAllComponents("window");
  for( i = 0; i< allComponents.length; i++)
  {
    if( allComponents[i].name.indexOf("add") >- 1  )
    {
      allComponents[i].SetText("");
      allComponents[i].Hide();
    }
  }
  window_baseimages.SetText(this.getBaseImagesText());
  window_baseimages.Show();
  window_clipart.Hide();
}

function pwDemo()
{
  //top.location = location.toString() + "?image=" + escape(this.demoImage);
  if( this.baseImage != null )
  {
    if( !confirm("I'll have to clear your current work to start the demo.  Is that OK?") )
      return;
    
    this.clear();
  }
  top.demoOn = true;
  window_demo.SetText('<iframe id="demoIFrame" src="demo.php" width="100%" height="100%" scrolling="no"></iframe>');
 // window_demo.SetText(this.getOpenDialogText());
  window_demo.Show();
}

function pwShowAdReminder()
{
  window_demo.SetText('<iframe src="demo.php?ad=1" id="demoIFrame" width="100%" height="100%" scrolling=no></iframe>');
  window_demo.Show();
}

function pwShowSavedDialog(pSuccess, pImageURL, height, width)
{
  if( pSuccess )
    window_saveddialog.SetText( this.getSavedDialogText(pImageURL, height, width) );
  else
    window_saveddialog.SetText( this.getSavedDialogTextFailed() );

  if( false && this.contestOn )
  {
    this.contest();
  }
  else
  {
    this.centerDialog( window_saveddialog );
    window_saveddialog.Show();
    window_saveddialog.ToTop();
  }
}

function pwCloseSavedDialog()
{
  window_saveddialog.Hide();
  if( !window_demo.IsVisible() )
    this.showAdReminder();
}

function pwGetSavedDialogTextFailed(pImageURL, height, width)
{
  var imageURL = pImageURL;
  
  var text = "";
  text +=' <form method=POST>';
  text +=' <table border=0 width=100% height=100% cellpadding=5> ';
  text +=' <tr>';
  text +='  <td align=left>';
  text +=' <font class="window_savedialog-font"> Oh no!  Something went wrong while saving your creation.  Likely this is our fault, not yours, but try again anyway!';
  text +='  </td>';
  text +='</tr>';
  text +='<tr>';
  text +='  <td >&nbsp;</td>';
  text +='</tr>';
  text +='<tr>';
  text +='  <td align=center>';
  text +='  <input type="Button" value="Close" onClick="top.photoWeb.closeSavedDialog();" class=button>';
  text +='  </td>';
  text +='</tr>';
  text +='</table></form>';
  
  return text;
}
function pwGetSavedDialogText(pImageURL, height, width)
{
  var imageURL = pImageURL;
  
  var text = "";
  text +=' <form method=POST>';
  text +=' <table border=0 width=100% height=100%  cellpadding=5> ';
  text +=' <tr>';
  text +='  <td align=left><font class="window_saveddialog-font">';
  text +='  You have successfully saved your creation!';
  text +='  </td>';
  text +='</tr>';
  text +='<tr>';
  text +='  <td>';
  text +='  </td>';
  text +='</tr>';
  text +='<tr>';
  text +='  <td align=left><font class="window_saveddialog-font">';
  text +='  To show it to your friends, just send them this URL:<br><br><a style="color:yellow;" target=_new href=' + imageURL + '> ' + imageURL;
  text +='  </a></td>';
  text +='</tr>';
  text +='<tr>';
  text +='  <td align=left><font class="window_saveddialog-font">';
  text +='  To use it on your own web page, just add this code: <br><br><span  style="color:yellow;">';
  text +='  &lt;iframe src="' + imageURL + '&nd=1" scrolling=no height=' + height + ' width='+ width + ' marginwidth=0 marginheight=0 frameborder=0&gt;&lt;/iframe&gt;';
  text +='  </span></td>';
  text +='</tr>';
  text +='<tr>';
  text +='  <td ><font class="window_saveddialog-font">Click <a style="color:yellow;" target=_new href=' + imageURL + '>here</a> to send your creation to a friend!</td>';
  text +='</tr>';
  text +='<tr>';
  text +='  <td ><font class="window_saveddialog-font">Or, <a style="color:yellow;" href="javascript:top.photoWeb.startSave(true);void(0);">export</a> your image to a JPEG!</td>';
  text +='</tr>';
//  text +='<tr>';
//  text +='  <td >&nbsp;</td>';
//  text +='</tr>';
  text +='<tr>';
  text +='  <td align=center>';
  text +='  <input type="Button" class=button value="Close" onClick="top.photoWeb.closeSavedDialog();">';
  text +='  </td>';
  text +='</tr>';
  text +='</table></form>';

  
  return text;
}
function pwGetTalkBalloonText()
{
  var talkBalloonDialogText = '<iframe id="talkBalloonIFrame" src="talkballoondialog.php" width="100%" height="100%" scrolling=no></iframe>';
  return talkBalloonDialogText;
/*  talkBalloonText +=' <form method=POST>';
  talkBalloonText +=' <table border=0 width=100% height=100%> ';
  talkBalloonText +=' <tr>';
  talkBalloonText +='  <td align=center>';
  talkBalloonText +='  What do you want this balloon to say? (Basic HTML is OK)';
  talkBalloonText +='  </td>';
  talkBalloonText +='</tr>';
  talkBalloonText +='<tr>';
  talkBalloonText +='  <td>';
  talkBalloonText +='  </td>';
  talkBalloonText +='</tr>';
  talkBalloonText +='<tr>';
  talkBalloonText +='  <td align=center>';
  var t         = "";
  try
  {
    if( this.currentTalkBalloon && this.currentTalkBalloon.rawText )
    {
      t = this.currentTalkBalloon.rawText;
    }
  }catch(e){}
//  talkBalloonText +='  <textarea style="font-family:arial;font-size:14px;" cols=60 id="tbtext" rows=4>'+t+'</textarea>';
  talkBalloonText +='  </td>';
  talkBalloonText +='</tr>';
  talkBalloonText +='<tr>';
  talkBalloonText +='  <td >&nbsp;</td>';
  talkBalloonText +='</tr>';
  talkBalloonText +='<tr>';
  talkBalloonText +='  <td align=center>';
  talkBalloonText +='  <input  type="Button" onClick="top.photoWeb.setTalkBalloonText(document.getElementById(\'tbtext\').value);top.photoWeb.closeTalkBalloonDialog();" value="&nbsp;&nbsp;OK&nbsp;&nbsp;" width=5>';
  talkBalloonText +='  &nbsp;&nbsp;&nbsp;';
  talkBalloonText +='  <input type="Button" value="Cancel" onClick="top.photoWeb.closeTalkBalloonDialog();">';
  talkBalloonText +='  </td>';
  talkBalloonText +='</tr>';
  talkBalloonText +='</form>';
  
  return talkBalloonText;*/
}
function pwGetOpenDialogText(clipart)
{
  if( clipart )
    return '<iframe id="uploadIframe" src="upload.php?uploadClipart=1" scrolling=no class="normalText" style="background-color:black;" height=100%  width=100%> </iframe>';
  else
    return '<iframe id="uploadIframe" src="upload.php?uploadClipart=0" scrolling=no class="normalText" style="background-color:black;" height=100%  width=100%> </iframe>';
}
function pwGetOpenSavedDialogText()
{
    return '<iframe id="openIframe" src="openimage.php" scrolling=no class="normalText" style="background-color:black;" height=100%  width=100%> </iframe>';
}
function pwGetLoginDialogText()
{
   return '<iframe id="loginIframe" src="login.php" scrolling=no class="normalText" style="background-color:black;" height=100%  width=100%> </iframe>';
}
function pwGetSaveAsDialogText()
{
  return '<iframe id="saveAsIframe" src="saveas.php" scrolling=no class="normalText" style="background-color:black;" height=100%  width=100%> </iframe>';
}
function pwGetExportDialogText()
{
  return '<iframe id="exportIframe" src="exportimage.php" scrolling=no class="normalText" style="background-color:black;" height=100%  width=100%> </iframe>';
}
function pwGetClipartWindowText()
{
  return "<iframe id='clipartIFrame' src='clipart/clipart.php'  scrolling=auto  width="+(CLIPART_WINDOW_WIDTH-2)+" height='100%' marginwidth=0 marginheight=0 frameborder=0 >";
}
function pwGetBaseImagesText()
{
  return "<iframe src='baseimages/baseimages.php' scrolling=auto width="+(CLIPART_WINDOW_WIDTH-5)+" height='100%' marginwidth=0 marginheight=0 frameborder=0 >";
}

