
//
// Created on: 2011-01-26
// Author: M
//
// Supporting functions for detecting mobile devices and optimizing the
// website content to be displayed on mobile devices.
//
// ***
//
// LAST MODIFICATION:
//
// 2011-09-26: M
//	Added iPad support.
//


// If the DEBUG-Mode is enabled, this script will behave as it would be
// run from an Mobile Device.
// Also, in DEBUG-Mode, notification message are output.
DEBUG = 0;	// Should only be set within the URI


// Check if a DEBUG variable has been passed via GET:
checkURIforDEBUG();


// The following variables are used only in conjunction with slideshows:
imageList = 0;		// Array of slideshow-images
imageIndex = 0;		// Current image index of slideshow


// Notifies the programmer about something by writing the passed message
// to the document.
// This function only writes the message, if the DEBUG-Mode is enabled.
function notify(msg) {
	// If DEBUG-Mode is enabled
	if (DEBUG) {
		// Check if container for debug-messages exists
		if (!document.getElementById("DebugMessages")) {
			// If no container for debug-messages exists,
			// create one:
			var dbgMsgContainer = document.createElement("div");
			var dbgMsgContainerID = document.createAttribute("id");
			dbgMsgContainerID.nodeValue = "DebugMessages";
			dbgMsgContainer.setAttributeNode(dbgMsgContainerID);
			document.body.appendChild(dbgMsgContainer);
		}

		var dbgMsgContainer = document.getElementById("DebugMessages");

		// Write debug-message to container
		dbgMsgContainer.innerHTML =
			dbgMsgContainer.innerHTML +
			"<p class='DEBUG'>" + msg + "</p>";
	}
}


// Detects wether or not the browser is used on an Apple Mobile Device
// like iPhone or iPad and returns either true or false.
function isAppleMobileDevice() {
	return ((navigator.userAgent.match(/(iPhone)|(iPod)|(iPad)/i))
		|| DEBUG)
		? true
		: false;
}


// Gets the HTML-content of the container with the passed ID and writes
// the content to the HTML file. For this to work, the container has to
// get defined BEFORE this function is called.
function displayMobileContent(idOfContainer) {
	notify("ID of container to be displayed: " + idOfContainer);

	// If container exists
	if (document.getElementById(idOfContainer)) {
		notify("FOUND container to be displayed");

		// Get content of container
		var content = document.getElementById(idOfContainer).
			innerHTML;

		// Write content to the document
		document.write(content);
	}
	else {
		notify("DID NOT FOUND container to be displayed");
	}
}


// Creates all HTML tags necessary to run the image transition manager
// slideshow (http://ajaxorized.com/image-transition-manager/).
function displayMobileSlideshow(images, holderId) {
	// Create unordered list of images
	createSlideshowAnchors(images);

	// Create holder for slideshow:
	document.write("<div id='" + holderId + "'><img src='" +
		images[0] + "' /></div>");

	// Initialize slideshow (by creating the Transition) as document
	// on the website of the image transition manager:
	oTransition = new Transition(holderId, images[imageIndex++]);
	notify("Transition for slideshow created");

	// Start slideshow by setting an interval
	var oneSecond = 1000;
	window.setInterval("flipSlideshowImage()", oneSecond * 2);

	// Save images to imageList to provide further access:
	imageList = images;
}


// Creates the anchor-tags of the images used in the slideshow
function createSlideshowAnchors(images) {
	var i = 0;

	// While there are images in the array
	while (images[i]) {
		// Write link to current image
		document.write("<a id='image" + i + "' href='" +
			images[i++] + "' rel='transition[slideright]'></a>");
	}
}


// The content of this function is derived from the image transition
// manager script, which injects instructions in the image-anchor tags
// (onclick).
function flipSlideshowImage() {
	// Flip image:
	g_eTransition.loadImage(document.getElementById("image" + imageIndex++));

	// If imageIndex is out of borders
	if (imageIndex >= imageList.length) {
		// Correct imageIndex
		imageIndex = 0;
	}

	return false;
}


// Checks the URI for the DEBUG variable and sets this variable
// accordingly
function checkURIforDEBUG() {
	var passedVariables = getUrlVars();
	if (	passedVariables['DEBUG'] &&
		passedVariables['DEBUG'] == 1)
	{
		DEBUG = true;
	}
	else {
		DEBUG = false;
	}
}


// The code of this function was copied from
// http://snipplr.com/view/799/
function getUrlVars() {
	var vars = [], hash;
	var hashes = window.location.href.slice(
		window.location.href.indexOf('?') + 1).split('&');

	for(var i = 0; i < hashes.length; i++) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}

	return vars;
}

