//
// Frequently used javascript functions
//

//
// showImageText
//
// Show the text for the given image
// Parameters:
//		image_month - URL to display
//
function showImageText(image_month) {
	window.open(image_month + '.html','text','location=no,toolbar=no,status=no,directories=no,scrollbars=yes, resizable=yes, width=280, height=180'); 
}

//
// showTshirtShop
//
// Show the tshirt shop
// Sets the focus to the new window
//
// Parameters:
//		page - the page to show
//
function showTshirtShop(page) {
	var page_ref = page + '.html';
	var mywin = window.open(page_ref,'tshirtshop','location=no,toolbar=no,status=no,directories=no,scrollbars=yes, resizable=yes, width=620, height=593'); 
	// mywin.focus();

}

//
// showTshirt
//
// Show the tshirt shop
// Sets the focus to the new window
//
// Parameters:
//		page - the page to show
//
function showTshirt(page) {
	var page_ref = page;
	var mywin = window.open(page_ref,'tshirtshop','location=no,toolbar=no,status=no,directories=no,scrollbars=yes, resizable=yes, width=620, height=593'); 
	// mywin.focus();

}

function changeImage(url)
{
	got_selection=1;
	frames["inset1"].location.href=url;
	return false;
}

  /**
   * checks the browser version to see if fixed positioning works
   **/
  function useFixedPosition()
  {
    var agt=navigator.userAgent.toLowerCase();
    var browser=navigator.appName;
    var appVer=navigator.appVersion.toLowerCase();
    if ((browser=="Microsoft Internet Explorer"))
    {
      var is_minor = 0;
      var is_major = 0;
      var is_mac = (agt.indexOf("mac")!=-1);
      var iePos  = appVer.indexOf('msie');
      if (iePos !=-1) {
        if(is_mac) {
           var iePos = agt.indexOf('msie');
           is_minor = parseFloat(agt.substring(iePos+5,agt.indexOf(';',iePos)));
       }
       else is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)));
       is_major = parseInt(is_minor);
      }

      if (is_major < 7) {
        return false;
      }
    }

    return true;
  }

  function calculatePosition()
  {
    var cell = document.getElementById("cell_full");
    var shirt = document.getElementById("shirtdiv");

    // only do this if we find the shirt and cell
    if (shirt == null || cell == null) {
      return;
    }

    if (!useFixedPosition()) {
      calculatePositionIE(cell, shirt);
      return;
    }

    var scroll = getScrollXY();
    var msg="";
    var cellpos = findPos(cell);
    var pos = (cellpos[0] + cell.offsetWidth - 50 - shirt.offsetWidth - scroll[0]);
    shirt.style.left = pos + "px";


    msg = "Normal<br>Left = " + shirt.style.left;

    setMessage(msg);

  }

  function calculatePositionIE(cell, shirt)
  {
    
    var scroll = getScrollXY();
    var msg="";
    var cellpos = findPos(cell);
    var pos = (cellpos[0] + cell.offsetWidth - 50 - shirt.offsetWidth - scroll[0]);
    shirt.style.left = pos + "px";


    msg = "IE<br>Scroll = " + scroll + "<br>Left = " + shirt.style.left;

    setMessage(msg);


  }

function findPos(obj, id) {
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while ((obj = obj.offsetParent) != null && (id == null || id != obj.id) ) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}


var showing = null;
var timeoutId = null;

function show(imageId, left, top) 
{
  showi(imageId, left, top, "img");
}

function showt(imageId, left, top) 
{
  showi(imageId, left, top, "txt");
}

function showi(imageId, left, top, type)
{
  if (showing != null) {
    var div = document.getElementById("img-preview-div-" + showing);
    var image = document.getElementById("img-preview-" + showing);

    div.className="preview-image";
  }

  if (imageId != null)
  {

    var pic = document.getElementById("pic-" + imageId);
    var txt = document.getElementById("txt-" + imageId);
  
    if (type == "txt")
    {
      pic.style.display = "none";
      txt.style.display = "";
    } else {
      txt.style.display = "none";
      pic.style.display = "";
    }
  
    var div = document.getElementById("img-preview-div-" + imageId);
    //var caption = document.getElementById("caption-" + imageId);
    var image = document.getElementById("img-preview-" + imageId);
    var thumb = document.getElementById("thumb-" + imageId);

    var scroll = getScrollXY();
    var YOffset = scroll[1];
    var XOffset = scroll[0];

    var Xiehack = 0;
    var Yiehack = 0;

    var size = getWindowSize();
    var winWidth = size[0];
    var winHeight = size[1];

    winHeight -= 10;
    winWidth -= 10;
    
    // in IE6 the absolutely positioned pop ups require some fiddling
    if (!useFixedPosition()) {
      div.style.left = (left+5-XOffset) + "px";
      div.style.top = (top-YOffset) + "px";
    } else {
      div.style.left = (left+5) + "px";
      div.style.top = (top) + "px";
    }

    div.className="show-preview-image";

    var divWidth = image.offsetWidth;
    var divHeight = image.offsetHeight;

    if (type == "txt") {
      divWidth = 410;
      divHeight = 330;
    }

    div.className="preview-image";

    if (!useFixedPosition()) {
      // check if it's outside the window
      var x = left + 5 - XOffset;
      var y = top - YOffset;

      if (y + divHeight > winHeight) {
        div.style.top = (winHeight - divHeight - 5) + "px";
      }
      
      if (y < 10) {
        div.style.top = "5px"
      }
     
      if (x + divWidth > winWidth) {
        div.style.left = (x - 90 - divWidth) + "px";
      }


    } else {
      var pos = findPos(document.getElementById("cell_full"));
      pos[0] += left;
      pos[1] += top;

      //caption.innerHTML = "DIVS = " + divWidth + ", " + divHeight;
      //alert("DIVS = " + divWidth + ", " + divHeight);
      //alert("POS = " + pos[0] + ", " + pos[1] + " == " + pos2[0] + ", " + pos2[1] );

      var pageTop = (pos[1] - YOffset);
      var pageBottom = (pageTop + divHeight);
      var pageLeft = (pos[0] - XOffset);
      var pageRight = (pageLeft + divWidth);

      if (pageBottom > winHeight) {
        var diff = pageBottom - winHeight;
        div.style.top = (top - diff - Yiehack) + "px";
      }

      if (pageTop < 0) {
        div.style.top = (top - pageTop) + "px";
      }

      if (pageRight > winWidth) {
        var diff = divWidth + thumb.offsetWidth + 10;
        div.style.left = (left - diff - Xiehack) + "px";
      }

    }
    div.className="show-preview-image";
  }
  
  showing = imageId;
  timeoutId = null;
}

function showLarge(element, imageId)
{
  var image = document.getElementById("thumb-" + imageId);
  var pos = findPos(image, "cell_full");
  var left = (pos[0] + element.offsetWidth);
  var top = (pos[1]);

  timeoutId = setTimeout("show('" + imageId + "', "+left+", "+top+")", 500);
}

function clearLarge(id)
{
  if (timeoutId != null) {
    clearTimeout(timeoutId);
    timeoutId = null;
  }
  //alert("Clearing - " + showing);
  show(null);
}

function showText(element, imageId)
{
  var image = document.getElementById("thumb-" + imageId);
  var pos = findPos(image, "cell_full");
  var left = (pos[0] + image.offsetWidth);
  var top = (pos[1]);
  timeoutId = setTimeout("showt('" + imageId + "', "+left+", "+top+")", 500);
}

function clearTextTimer(id)
{
  if (timeoutId != null) {
    clearTimeout(timeoutId);
    timeoutId = null;
  }
}

function clearText(id)
{
  clearTextTimer(id);
  showt(null);
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
    
  }
  return [ scrOfX, scrOfY ];
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

  return [ myWidth, myHeight ];

}

function showScroll()
{
  var pos = getScrollXY();
  var msg = "Scroll = " + pos;

  setmessage(msg);

}

function setMessage(msg)
{
  //document.getElementById("menu-caption").innerHTML = msg;
}

function validateSubNo(value)
{
  if (value == "") {
    return false;
  }
  
  return true;
}
