// Display an image in a new window with a caption, comment (optional), page title, and a Close button.
// Image is specified by a path (absolute or relative) and file name.
// The hr parameter denotes whether to display the high-resolution version of the image (if available).
function showImage(imgPath, imgName, pageTitle, pageCaption, comment) {
  
  // Set default window size (sized for 800 x 600 images)
  var windowWidth = 850;
  var windowHeight = 700;
  
  // Determine whether to display large of small images
  var hr = getRes();
  
  hr = 1;
  
  // Increase window size if displaying hi-res picture
  if(hr != 0) {
    windowWidth = 1100;
    windowHeight = 900;
  }
  
  // Display picture in a new window
  var win = window.open("","","status=yes,resizable=yes,scrollbars=yes,left=0,top=0,width="+windowWidth+",height="+windowHeight);
  
  with(win.document) {
    write("<html><head>");
    write("<title>" + pageTitle + "</title>");
    
    // Add document-level style sheet data
    write("<style type='text/css'>");
    write("li.caption{font-family: verdana,arial,helvetica; ");
    write("font-size: 10pt; ");
    write("font-weight: bold}");
    write("</style></head>");
    
//    alert("Last char = " + imgPath.charAt(imgPath.length - 1));
//    alert("imgPath = " + imgPath);
    
    // Ensure path name has a trailing backslash
    if(imgPath.charAt(imgPath.length - 1) != '/') {
//    	alert("Adding /");
      imgPath = imgPath + '//';
    }
    
    // Append sub-path 'hr/' to imgPath if required
    if(hr == 0) {
      var imgNm = imgName.substring(0, imgName.lastIndexOf('.'));
      imgNm = imgNm + 'r' + imgName.substring(imgName.lastIndexOf('.'), imgName.length);
      imgName = imgNm;
    } else {
      imgPath = imgPath + 'hr//';
    }
    
//    alert("imgPath = " + imgPath);
//    alert("imgName = " + imgName);
    
    // Page body includes picture and caption
    write("<body><img src=" + imgPath + imgName + "><br />");
    if(pageCaption != null) {
      write("<li class='caption'><strong>" + pageCaption + "</strong></li>");
      write("<br />");
    }else{
      write("<p>&nbsp;</p>");
    }
  }
  
  //Display comment if there is one
  if(comment != null){
    win.document.write(comment + "<hr />");
  }
  
  //Add a Close button
  win.document.write("<input type='button' value='Close' id='cmdCloseWindow' name='CloseWindow' title='Close window' onclick='javascript:window.close();'>");
  
  win.document.write("</body></html>");
}

// This function is no longer used
function showPicture(pictureName, pageTitle, pageCaption, comment){
  
  //Set default window size
  var windowWidth = 600;
  var windowHeight = 600;
  
  //Increase window size if displaying hi-res picture
  if(pictureName.indexOf('//hr//') < 0){
    windowWidth = 950;
    windowHeight = 700;
  }
  
  //Display picture in a new window
  var win = window.open("","","status=yes,resizable=yes,scrollbars=yes,left=0,top=0,width="+windowWidth+",height="+windowHeight);
  
  with(win.document){
    write("<html><head>");
    write("<title>" + pageTitle + "</title>");
    
    //Add document-level style sheet data
    write("<style type='text/css'>");
    write("li.caption{font-family: verdana,arial,helvetica; ");
    write("font-size: 10pt; ");
    write("font-weight: bold}");
    write("</style></head>");
    
    //Page body includes picture and caption
    write("<body><img src=" + pictureName + "><br />");
    if(pageCaption.length > 0){
      write("<li class='caption'><strong>" + pageCaption + "</strong></li>");
      write("<br />");
    }else{
      write("<p>&nbsp;</p>");
    }
  }
  
  //Display comment if there is one
  if(comment.length > 0){
    win.document.write(comment + "<hr />");
  }
  
  //Add a Close button
  win.document.write("<input type='button' value='Close' id='cmdCloseWindow' name='CloseWindow' title='Close window' onclick='javascript:window.close();'>");
  
  win.document.write("</body></html>");
}

function openDialog(windowURL, windowName, windowWidth, windowHeight){
  
  //Set default values for Window width and height
  if(windowWidth == 0)
    windowWidth = 300;
  
  if(windowHeight == 0)
    windowHeight = 400;
  
  window.open(windowURL, windowName, "width='" + windowWidth + "',height='" + windowHeight + "',status=yes,menubar=yes,toolbar=yes,resizable=yes,scrollbars=yes", false)
}

function imageDisplayHelp(){
  
  var win = window.open("","imageDisplayHelp","status=no,resizable=yes,scrollbars=yes,left=0,top=0,width=300,height=400");
  
  with(win.document){
    write("<html><head>");
    write("<title>Image Display Settings</title>");
    
    //Add document-level style sheet data
    write("<link href=\'/main.css\' type=\'text/css\' rel=\'stylesheet\'>");
    
    //Close document header
    write("</head>");
    
    //Page body
    write("<body>");
    
    write("<p class=small>You can view either High or Low resolution images. The high resolution images are larger and ");
    write("clearer than the corresponding low resolution images but they take a little longer to download and can be too large ");
    write("to fit on a small or low resolution monitor. The file size of the low resolution images are slightly smaller but the ");
    write("image will not be quite as clear. However the difference in file size is minimal so download time should be little ");
    write("difference between the two. File sizes for the high resolution images is in the order of 100kB on average and image ");
    write("size is approximately 1000 x 700. For low resolution images, file size is approximately 60kB on average and image ");
    write("size is approximately 600 x 400.</p>");
    write("<p class=small>If you have a small monitor, are using a low resolution such as 800 x 600, or you have a very slow ");
    write("internet connection and you find that images take too long to display, then you should use the Low Resolution ");
    write("setting. Otherwise, you should use the High Resolution setting to take advantage of the improved quality.</p>");
    write("<p class=small>You only need to select your desired setting once and it will be remembered when each subsequent ");
    write("image is displayed.</p>");
    
    write("</body>");
  }
}

//===================================================================================

// Constants used to set expiry date for cookies
var oneDay = 1000 * 60 * 60 * 24;	// Number of milliseconds in a day
var oneWeek = oneDay * 7;		// Number of milliseconds in a week
var oneMonth = oneDay * 30;		// Number of milliseconds in a month (actually 30 days)

// Read the document's cookie and determine the last time the user visited this page
function readCookie() {
  // If no cookie is found, write a new one.
  if(document.cookie == "") {
    writeCookie();
  } else {
    var ck = document.cookie;
    
    ck = unescape(ck);
    var ckSplit = ck.split(";");
    
    for(var loop = 0; loop < ckSplit.length; loop++) {
      var splt = ckSplit[loop];
      var nm = splt.indexOf("lv");
      
      if(nm != -1) {
        break;
      }
    }
    
    // If no cookie is found, write a new one.
    if (nm == -1) {
      document.write("No cookie found.");
      writeCookie();
    } else {
      var dateSplit = ckSplit[0].split("=");
      var last = dateSplit[1];
      
      last = fixDate(last);
      alert("Welcome back. Your last visit was on: " + last);
      
      writeCookie();
    }
  }
}

function readValue(nm) {
  
  var ckValue;
  
  // Default to document.cookie
  ck = document.cookie;
  ck = unescape(ck);
  
  if(ck.length == 0) {
    ck = "hr=1";
  }
  
  var ckSplit = ck.split(";");
  
  for(var counter = 0; counter < ckSplit.length; counter++) {
    if(ckSplit[counter].indexOf(nm) > -1) {
      break;
    }
  }
  
  // Get value of cookie
  var str = ckSplit[counter];
  ckValue = str.substring(str.indexOf("=") + 1, str.length);
  
  return ckValue;
}

// Write "Last Visited" date into a cookie
function writeCookie(ckName, ckValue) {
  var dte = new Date("December 31, 2002");
  var ckDate = dte.toGMTString();
  ck = ck + ";expires=" + ckDate;
  
  var ck = ckName + "=" + ckValue;
  ck = ck + ";expires=" + ckDate;
  document.cookie = ck;
}

function setRes(val) {
  writeCookie("hr", val);
}

function getRes() {
  return readValue("hr");
}

function getLastVisit() {
 return readValue("lv");
}

function fixDate(date) {
  var split = date.split(" ");
  var fix_the_time = split[3].split(":");
  var hours = fix_the_time[0];

  if(hours>=12) {
    var ampm = "PM";
  } else {
    var ampm = "AM";
  }

  if(hours > 12) {
    hours = hours-12;
  }

  var new_time = hours + ":" + fix_the_time[1] + " " + ampm;
  var new_date = split[0] + " " + split[1] + ", " + split[2] + " at " + new_time + ", " + split[5];

  return new_date;
}

function getScreenRes() {
  
  var scrWidth = parseInt(screen.width);
  var scrHeight = parseInt(screen.height);
  var scrAvailableWidth = parseInt(screen.availWidth);
  var scrAvailableHeight = parseInt(screen.availHeight);

  return scrWidth + "x" + scrHeight + "," + scrAvailableWidth + "x" + scrAvailableHeight;

}

function calcage(secs, num1, num2) {
	s = ((Math.floor(secs / num1)) % num2).toString();
	if (LeadingZero && s.length < 2)
		s = "0" + s;
	return  s ;
}

function countBack(secs) {
	var xday = calcage(secs, 86400, 100000);
	var xhour =  calcage(secs, 3600, 24);
	var xmin = calcage(secs, 60, 60);
	var xsec =  calcage(secs, 1, 60);
	document.getElementById("xmascounter").innerHTML = "Christmas Countdown:<br />";
	document.getElementById("xday").innerHTML = xday + " d : ";
	document.getElementById("xhour").innerHTML = xhour + " h : ";
	document.getElementById("xmin").innerHTML = xmin + " m : ";
	document.getElementById("xsec").innerHTML = xsec + " s";
	if (CountActive){
		setTimeout("countBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
	}
}

function displayXmasCounter(){
	TargetDate = "12/25/2007 0:00 AM";
	CountActive = true;
	FinishMessage = "";
	CountStepper = -1;
	LeadingZero = true;
	CountStepper = Math.ceil(CountStepper);

	if (CountStepper == 0){
		CountActive = false;
	 }
	var SetTimeOutPeriod = (Math.abs(CountStepper) - 1) * 1000 + 990;

	var dthen = new Date(TargetDate);
	var dnow = new Date();
	if(CountStepper > 0){
		ddiff = new Date(dnow - dthen);
	 }
	else{
		ddiff = new Date(dthen - dnow);
	 }
	gsecs = Math.floor(ddiff.valueOf() / 1000);

	countBack(gsecs);
}
