
/**************************************************
* Name: im_generic.js
* Author: PJ
* Modified: 20071205
* Overview: Generic Ident Machines functionality
* Copyright:
* NOTE: Requires hijax_generic.js loaded first
**************************************************/

// The contents of the widgets and internal windows
var htmlZoomImage = "<table><tr><td><img id=\"imageplaceholder\" class=\"zoomimagepic\" src=\"/images/hijax_loading.gif\" alt=\"\" title=\"Loading...\" /></td></tr></table>";
var htmlDownloadForm = "<table><tr><td><form action=\"/download.php\" method=\"post\" id=\"downloadform\"><p><strong>Enter your client password to continue:</strong></p><p><input type=\"password\" name=\"password\" title=\"Enter your password\" /><input type=\"hidden\" name=\"f\" id=\"f\" value=\"0\" /><input type=\"submit\" value=\"OK\" /><input type=\"button\" value=\"Cancel\" id=\"btncancel\" /></p><p>If you don\'t have a password, please <a href=\"/contact.php\" title=\"Contact Ident Machines\">contact Ident Machines</a></p></form></td></tr></table>";

// Define some variables for the functionality
var imageDir = "/cms/images/products/"; // The path where all images must reside
var noImage = "no_image.jpg"; // The image to use if there is a problem

// Define some variables for the divwindows
var zoomImageDivWidth = 500;  // The width of the zoomimage divwindow
var zoomImageDivHeight = 395; // The height of the zoomimage divwindow
var fileDownloadDivWidth = 500;  // The width of the filedownload divwindow
var fileDownloadDivHeight = 350; // The height of the filedownload divwindow

// These are global variables set by our viewport size later on
var frameWidth = 0;
var frameHeight = 0;

// Add our load events
winAddLoadEvent (tabTableHijax);
winAddLoadEvent (imageZoomHijax);
winAddLoadEvent (fileDownloadHijax);

// Hijaxed and prepares the in-situ tab table
function tabTableHijax () {
	if (!sanityCheck) return false;
	if (!document.getElementById ("tabheadings")) return false;

	// Find the tab container and go through only the links in there
	var tabSections = document.getElementById ("tabheadings");
	var tabHeadings = tabSections.getElementsByTagName ("a");
	for (var i = 0; i < tabHeadings.length; i++) {
		// Check to see whether the link uses one of our tab classes
		if (tabHeadings[i].className.indexOf ("tab_") > -1) {
			// This is a tab we're interested in Hijaxing
			tabHeadings[i].onclick = function () { return tabToggle (this); };
		}
	}
}

// Hijaxes and prepares the in-situ image zooming
function imageZoomHijax () {
	if (!sanityCheck) return false;

	// Go through all the a elements in the document
	var aElems = document.body.getElementsByTagName ("a");

	for (var i = 0; i < aElems.length; i++) {
		// Check to see if this is a zoom image
		if (aElems[i].className.indexOf ("zoomimage") > -1) {
			// This is a zoom image, let's attach our event
			aElems[i].onclick = function () { return showZoomImage (this); };
		}
	}
}

// Hijaxes and prepares the in-situ file downloading
function fileDownloadHijax () {
	if (!sanityCheck) return false;

	// Go through all the a elements in the document
	var aElems = document.getElementsByTagName ("a");
	for (var i = 0; i < aElems.length; i++) {
		// Check to see if this is a zoom image
		if (aElems[i].className.indexOf ("filedownload") > -1) {
			// This is a file download, let's attach our event
			aElems[i].onclick = function () { return showFileDownload (this); };
		}
	}
}

// Parses a query string
function parseQueryString (url) {
	this.params = new Object ();

	url = url.replace (/\+/g, ' '); // Convert "+" back to space
	url = url.substring (url.indexOf ("?") + 1, url.length); // Remove anything before "?"
	var args = url.split ('&'); // Split the name/value pairs

	// Parse the name/value pairs
	for (var i = 0; i < args.length; i++) {
		var value;
		var pair = args[i].split ('=');
		var name = unescape (pair[0]);

		if (pair.length == 2) {
			value = unescape (pair[1]);
		} else {
			value = name;
		}

		this.params[name] = value;
	}

	return this.params;
}

// Returns the size of the applicable viewport
function getViewportSize () {
	// Should work with all clients
	if (self.innerWidth) {
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight
	} else if (document.body && document.body.clientWidth) {
		frameWidth = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		frameWidth = document.documentElement.clientWidth;
		frameHeight = document.documentElement.clientHeight;
	} else if (self.screen.width) {
		frameWidth = self.screen.width;
		frameHeight = self.screen.height;
	} else {
		frameWidth = this.width;
		frameHeight = this.height;
	}

	// No need to return anything, we are setting global variables
}

// Shows or hides the background tint
function showBackgroundTint (show) {
	// See if the tint already exists
	var tintDiv = document.getElementById ("bgtint");

	// See if we can add or remove it
	if (show == true && tintDiv == null) {
		// It doesn't exist and we want it, so create and add it
		var tintDiv = document.createElement ("div");
		tintDiv.setAttribute ("id", "bgtint");
		tintDiv.setAttribute ("class", "bgtint");
		tintDiv.className = "bgtint";

		document.body.appendChild (tintDiv);

		// Dirty hack for buggy IE
		if (navigator.appVersion.indexOf ("MSIE") > -1) {
			getViewportSize ();
			tintDiv.style.position = "absolute";
			tintDiv.style.left = "0px";
			tintDiv.style.top = "0px";
			tintDiv.style.width = frameWidth + "px";
			tintDiv.style.height = frameHeight + "px";
		}
	} else if (show == false && tintDiv != null) {
		// It does exist and we don't want it, so remove it
		tintDiv.parentNode.removeChild (tintDiv);
	}
}

// Creates a document fragment of a standard divwindow
function createDivWindow () {
	var divWindow = document.createElement ("div");
	divWindow.setAttribute ("id", "divwindow");
	divWindow.setAttribute ("class", "divwindow");
	divWindow.className = "divwindow";

	// Construct the close widget
	var widgetClose = document.createElement ("p");
	var widgetLink = document.createElement ("a");
	widgetLink.setAttribute ("class", "widgetclose");
	widgetLink.className = "widgetclose";
	widgetLink.setAttribute ("href", "#");
	widgetLink.setAttribute ("accesskey", "x");
	widgetLink.onclick = function () { return closeDivWindow (); };
	var widgetCloseText = document.createTextNode ("Close");
	widgetLink.appendChild (widgetCloseText);
	widgetClose.appendChild (widgetLink);
	divWindow.appendChild (widgetClose);

	// Since we're making a window we will need the viewport details shortly so get them here
	getViewportSize ();

	// The basic window has been created, return the document fragment
	return divWindow;
}

// Hides the divwindow
function closeDivWindow () {
	// Firstly find and deconstruct the window
	var divWindow = document.getElementById ("divwindow");
	if (divWindow)
		divWindow.parentNode.removeChild (divWindow);

	// Lastly, remove the background tint
	showBackgroundTint (false);

	// Everything worked
	return false; // Return false to stop href normal loading
}

// Toggles a tab
function tabToggle (what) {
	// Get the tab divs (the actual body)
	var tabSections = document.getElementById ("tabdivs");
	var tabDivs = tabSections.getElementsByTagName ("div");

	// Loop through just the tabs to find those with the display class
	for (var i = 0; i < tabDivs.length; i++) {
		if (tabDivs[i].className.indexOf ("sectiond") > -1) {
			// This is one we're interested in, so hide it
			tabDivs[i].setAttribute ("class", "sectionh");
			tabDivs[i].className = "sectionh";
		}
	}

	// Display the div for the tab we want
	document.getElementById (what.getAttribute ("id") + "_section").setAttribute ("class", "sectiond");
	document.getElementById (what.getAttribute ("id") + "_section").className = "sectiond";

	// Now get the tab headings (the actual tabs)
	var tabSections = document.getElementById ("tabheadings");
	var tabDivs = tabSections.getElementsByTagName ("a");

	// Loop through just the tabs to find those with the visible class
	for (var i = 0; i < tabDivs.length; i++) {
		if (tabDivs[i].className.indexOf ("tab_selected") > -1) {
			// This is one we're interested in, so hide it
			tabDivs[i].setAttribute ("class", "tab_heading");
			tabDivs[i].className = "tab_heading";
		}
	}

	// Highlight the tab we are now on
	what.setAttribute ("class", "tab_selected");
	what.className = "tab_selected";

	// Everything worked
	return false; // Return false to stop href normal loading
}

// Shows the zoom image window
function showZoomImage (what) {
	// First tint the background
	showBackgroundTint (true);

	// Create a basic window, and we will add to it
	var zoomImageDiv = createDivWindow ();
	var appendHTML = document.createElement ("div");
	appendHTML.innerHTML = htmlZoomImage; // This is dirty but gets around lots of browser bugs
	zoomImageDiv.appendChild (appendHTML);

	// Configure the size of our window and position it
	zoomImageDiv.setAttribute ("style", "width: " + zoomImageDivWidth + "px; height: " + zoomImageDivHeight + "px; left: " + (frameWidth / 2 - zoomImageDivWidth / 2) + "px; top: " + (frameHeight / 2 - zoomImageDivHeight / 2) + "px;;");

	// Dirty hack for buggy IE
	if (navigator.appVersion.indexOf ("MSIE") > -1) {
		zoomImageDiv.style.position = "absolute";
		zoomImageDiv.style.left = (frameWidth / 2 - zoomImageDivWidth / 2) + "px";
		zoomImageDiv.style.top = ((frameHeight / 2 - zoomImageDivHeight / 2) - 100) + "px";
		zoomImageDiv.style.width = zoomImageDivWidth + "px";
		zoomImageDiv.style.height = zoomImageDivHeight + "px";
	}

	// Add the window to the body so we can get something shown as quickly as possible
	document.body.appendChild (zoomImageDiv);

	// Now we extract the details of the image to show in the window
	var imageSrc = what.getAttribute ("href");
	imageSrc = parseQueryString (imageSrc);
	imageSrc = imageSrc["img"];
	if (imageSrc) {
		imageSrc = imageDir + imageSrc;
	} else {
		imageSrc = imageDir + noImage;
	}
	var imageTitle = what.getAttribute ("title");

	// Now we manipulate the DOM to display the actual image
	var imagePlaceholder = document.getElementById ("imageplaceholder");
	imagePlaceholder.setAttribute ("src", imageSrc);
	imagePlaceholder.setAttribute ("title", imageTitle);

	// Everything worked
	return false; // Return false to stop href normal loading
}

// Shows the download password window
function showFileDownload (what) {
	// First tint the background
	showBackgroundTint (true);

	// Create a basic window, and we will add to it
	var fileDownloadDiv = createDivWindow ();
	var appendHTML = document.createElement ("div");
	appendHTML.innerHTML = htmlDownloadForm; // This is dirty but gets around lots of browser bugs
	fileDownloadDiv.appendChild (appendHTML);

	// Configure the size of our window and position it
	fileDownloadDiv.setAttribute ("style", "width: " + fileDownloadDivWidth + "px; height: " + fileDownloadDivHeight + "px; left: " + (frameWidth / 2 - fileDownloadDivWidth / 2) + "px; top: " + (frameHeight / 2 - fileDownloadDivHeight / 2) + "px;");

	// Dirty hack for buggy IE
	if (navigator.appVersion.indexOf ("MSIE") > -1) {
		fileDownloadDiv.style.position = "absolute";
		fileDownloadDiv.style.left = (frameWidth / 2 - fileDownloadDivWidth / 2) + "px";
		fileDownloadDiv.style.top = ((frameHeight / 2 - zoomImageDivHeight / 2) - 100) + "px";
		fileDownloadDiv.style.width = fileDownloadDivWidth + "px";
		fileDownloadDiv.style.height = fileDownloadDivHeight + "px";
	}

	// Add the window to the body
	document.body.appendChild (fileDownloadDiv);

	// Now we manipulate the DOM to pass the correct parameters
	var fileIdentifier = what.getAttribute ("href");
	fileIdentifier = parseQueryString (fileIdentifier);
	fileIdentifier = fileIdentifier["f"];
	if (!fileIdentifier)
		fileIdentifier = 0;
	var fileId = document.getElementById ("f");
	fileId.setAttribute ("value", fileIdentifier);

	var cancelButton = document.getElementById ("btncancel");
	cancelButton.onclick = function () { return closeDivWindow (); };

	// Because I crafted the file download script to send the data as an attachment, we can be a bit clever
	// here and have the password prompt close after a few moments.  You may even like to get the password to
	// be recorded so the user doesn't need to be asked again?
	var downloadForm = document.getElementById ("downloadform");
	downloadForm.onsubmit = function () { setTimeout ("closeDivWindow ();", 2000); return true; };

	// Everything worked
	return false; // Return false to stop href normal loading
}
