﻿/// <reference path="jquery-vsdoc.js" />

//===============================================================================
// For having an appPath in JavaScript
//===============================================================================
var appPath = "/";
function SetAppPath(path)
{
  appPath = path;
}

//===============================================================================
// For automatic replacement of hoverable (and clickable) images
// Usage:
//   Images must be named xxx.ext, xxx-hover.ext, xxx-down.ext (optional)
//   and must be in the same directory
//   eg. submit.png, submit-hover.png, submit-down.png (optional)
// On the .html or .aspx:
//   <img class="imgHoverable" src="<%=AppPath%>images/submit.png"/>
// or
//   <img class="imgHoverable imgPushable" src="<%=AppPath%>images/submit.png"/>
//
// These functions below preloads the mouseover and mousedown images,
// and hook the events to replace images on mouseover/mousedown automatically
//===============================================================================
$(document).ready(function() {
  // Change the image of hoverable images
	$(".imgHoverable").hover( function() {
			var hoverImg = HoverImgOf($(this).attr("src"));
			$(this).attr("src", hoverImg);
		}, function() {
			var normalImg = NormalImgOf($(this).attr("src"));
			$(this).attr("src", normalImg);
		}
	);
	
	// Change the image of pushable images (only works if the image is both pushable and hoverable!!)
	$(".imgPushable").mousedown( function() {
			var hoverImg = PushImgOf($(this).attr("src"));
			$(this).attr("src", hoverImg);
	});
	$(".imgPushable").mouseup( function() {
			var normalImg = HoverImgOf($(this).attr("src"));
			$(this).attr("src", normalImg);
	});
});

// This function is called after page has finished loading
$(window).bind('load', function() {
  // Preload hover and push images
  $(".imgHoverable").each(function() {
    var img = $(document.createElement('img'));
    img.attr("src", HoverImgOf($(this).attr("src")));
  });
  $(".imgPushable").each(function() {
    var img = $(document.createElement('img'));
    img.attr("src", PushImgOf($(this).attr("src")));
  });
});

function HoverImgOf(filename)
{
  if (filename.indexOf("-hover.") > 0)
    return filename;
    
  var re = new RegExp("(.+?)(-down)?\\.(gif|png|jpg)", "g");
	return filename.replace(re, "$1-hover.$3");
}
function PushImgOf(filename)
{
  if (filename.indexOf("-down.") > 0)
    return filename;

  var re = new RegExp("(.+?)(-hover)?\\.(gif|png|jpg)", "g");
	return filename.replace(re, "$1-down.$3");
}
function NormalImgOf(filename)
{
	var re = new RegExp("(.+)-(hover|down)\\.(gif|png|jpg)", "g");
	return filename.replace(re, "$1.$3");
}


//===============================================================================
// Auto-replace transparent PNG with transparent GIF for IE6
// Usage:
//   Have both .png and .gif versions with same filename, eg. submit.png, submit.gif
// In the .html or .aspx:
//   <img class="transparentPNG" src="<%AppPath%>images/submit.png"/>
//===============================================================================
$(document).ready(function() {
  var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
  if (badBrowser) {
    $(".transparentPNG").each(function() {
      this.src = ReplaceFileExtension(this.src, ".gif");
    });
  }
});

function ReplaceFileExtension(filename, newExt) {
  var re = new RegExp("(.+)\\.\\w+", "g");
  return filename.replace(re, "$1"+newExt);
}


/* This is supposed to be better but the runtime filter doesn't seem to work
//===============================================================================
// Fix transparent PNG in IE6
// Usage:
//   Must have blank_pixel.gif on the same directory as the PNG file
//   (May not work if .png file is on root folder)
// In the .html or .aspx:
//   <img class="transparentPNG" src="<%AppPath%>images/submit.png"/>
//===============================================================================

$(window).bind('load', function() {
  var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
  if (badBrowser) {
    $(".transparentPNG").each(function() {
      fixPng(this);
    });
  }
});

function fixPng(png) {
  var blankGif = new Image();
  var re = new RegExp("(.*)/.+\\.png", "g");
  blankGif.src = png.src.replace(re, "$1/blank_pixel.gif");

  // Set width/height if not set yet
  if (!png.style.width) { png.style.width = $(png).width(); }
  if (!png.style.height) { png.style.height = $(png).height(); }

  png.onload = function() { };

  var src = png.src;
  png.src = blankGif.src;
  // Set filter (display original image)
  png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}
*/

// Usage: <input type="text" onkeypress="return InputDigitsOnly(event)" />
// eg. for postcode
function InputDigitsOnly(e) {
  var keycode = e.charCode ? e.charCode : e.keyCode;

  if (keycode == 8 || //backspace
      keycode == 37 || //left
      keycode == 39 || //right
      keycode == 46 || //del
      (keycode >= 48 && keycode <= 57)) {
    return true;
  }
  return false;
}

