/* A library of Javascript functions for moving images and responding to
 * mouse events in the browser window.
 *
 * Copyright 2008, Akkana Peck, http://shallowsky.com/javascript/
 * This code is licensed under the terms of the GNU Public License (GPL) v.2.
 * Please feel free to share it or modify it.
 */

/*******************************************************
 * General useful graphics functions
 */
function screenWidth() {
  if (window.innerWidth != null)
    return window.innerWidth;
  if (document.documentElement.clientWidth)
    return document.documentElement.clientWidth;
  return 800;
}

function screenHeight() {
  if (window.innerHeight != null)
    return window.innerHeight;
  if (document.documentElement.clientHeight)
    return document.documentElement.clientHeight;
  return 600;
}

// A couple of other possible ways to get size:
//  var width = document.documentElement.clientWidth;
//  var height = document.documentElement.clientHeight;
//  var width = window.innerWidth - 160;
//  var height = window.innerHeight - 160;

/*******************************************************
 * More specific functions for the flower exercises:
 */
function moveToClick(event)
{
  // IE doesn't see the event argument passed in, so get it this way:
  if (window.event) event = window.event;

  var img=document.getElementById("flower");
  img.style.left = event.clientX - 50;
  img.style.top = event.clientY - 50;
}

function moveAndChangeOnClick(event)
{
  // IE doesn't see the event argument passed in, so get it this way:
  if (window.event) event = window.event;

  var img=document.getElementById("flower");
  var whichflower = parseInt(Math.random() * 10 + 1);
  var flowerimg = "images/flower" + whichflower + ".gif";
  img.setAttribute("src", flowerimg);
  img.style.left = event.clientX - 50;
  img.style.top = event.clientY - 50;
}

function addNewImage(imgurl, parent, x, y, w, h)
{
  var img = document.createElement("img");
  img.setAttribute("src", imgurl);
  img.style.position = "absolute";

  if (typeof w != 'undefined') {
      img.setAttribute("width", w);
  }
  else
      w = 140;
  if (typeof h != 'undefined') {
      img.setAttribute("height", h);
  }
  else
      h = 140;

  img.style.left = x - w/2;
  img.style.top = y - h/2;

  parent.appendChild(img);
  return img;
}

