/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

File: 		mfm.js

Abstract:	Defines general Javascript functionality for the website.

Project: 	MyFamilyMemoirs


Copyright (C) 2008 Principle Inc. All Rights Reserved.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


/*
	Launches the Wizard in a centered popup window.
*/
function launchWizard (id) {
	var win = null;
	var url = 'wizard-step-1.jsp?memoirId=' + id;
	var settings;
	var top,
		left,
		w = 960,
		h = 650;

	// Centre the window if we can,
	// otherwise open in the top left corner of screen.
	(screen.height) ? top = (screen.height - h)/2 : 0;
	(screen.width) ? left = (screen.width - w)/2 : 0;

	settings =	'height=' + h + ',' +
				'width=' + w + ',' +
				'top=' + top + ',' +
				'left=' + left +',' +
				'scrollbars=' + 'yes' + ',' +
				'location=' + 'no' + ',' +
				'resizable=' + 'yes' + ',';
	win = window.open(url,'WIZARD',settings);
	if (win) win.focus();
}


/*
	Launches the Wizard and starts a new memoir.
*/
function initializeMemoir() {
	var win = null;
	var url = 'memoirs-start-do.jsp';
	var settings;
	var top,
		left,
		w = 960,
		h = 650;

	// Centre the window if we can,
	// otherwise open in the top left corner of screen.
	(screen.height) ? top = (screen.height - h)/2 : 0;
	(screen.width) ? left = (screen.width - w)/2 : 0;

	settings =	'height=' + h + ',' +
				'width=' + w + ',' +
				'top=' + top + ',' +
				'left=' + left +',' +
				'scrollbars=' + 'yes' + ',' +
				'location=' + 'no' + ',' +
				'resizable=' + 'yes' + ',';
	win = window.open(url,'WIZARD',settings);
	if (win) win.focus();
}


/*
	Launches the Memoir Viewer.
*/
function launchViewer (id) {
	var win = null;
	var url = 'viewer.jsp?memoirId=' + id;
	var settings;
	var top,
		left,
		w = 960,
		h = 650;

	// Centre the window if we can,
	// otherwise open in the top left corner of screen.
	(screen.height) ? top = (screen.height - h)/2 : 0;
	(screen.width) ? left = (screen.width - w)/2 : 0;

	settings =	'height=' + h + ',' +
				'width=' + w + ',' +
				'top=' + top + ',' +
				'left=' + left +',' +
				'scrollbars=' + 'yes' + ',' +
				'location=' + 'no' + ',' +
				'resizable=' + 'yes' + ',';
	win = window.open(url,'VIEWER',settings);
	if (win) win.focus();
}


/*
	Launches the PDF version of a Memoir.
*/
function launchPDF (id) {
	var pdfWin = null;
	var url = 'conduit/make-pdf.jsp?memoirId=' + id;
	var settings;
	var top,
		left,
		w = 960,
		h = 650;

	// Centre the window if we can,
	// otherwise open in the top left corner of screen.
	(screen.height) ? top = (screen.height - h)/2 : 0;
	(screen.width) ? left = (screen.width - w)/2 : 0;

	settings =	'height=' + h + ',' +
				'width=' + w + ',' +
				'top=' + top + ',' +
				'left=' + left +',' +
				'scrollbars=' + 'yes' + ',' +
				'location=' + 'no' + ',' +
				'resizable=' + 'yes' + ',';
	pdfWin = window.open(url,'PDFWIN',settings);
	if (pdfWin) pdfWin.focus();
}


/*
	Handles image-swapping for rollovers.

	Notes:		Deprecated.
*/
function swap(whichImage, whichFile, whichDiv) {
	if (document.images) {
		if( document[whichDiv] ) {
			document[whichDiv].document[whichImage].src = whichFile;
		} else {
			document[whichImage].src = whichFile;
		}
	}
}


/*
	Enables hovering on SIFR elements.
*/
function sfHover() {
	if( document.getElementById("nav") ) {
		var sfEls = document.getElementById("nav").getElementsByTagName("div");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}


/*
	Performs simple client-side validation of form input data.

	Param:		{HTMLElement} the_form		A form element.

	Returns:	{Boolean}					True if the data passes validation, otherwise False.

	Notes:		Looks through the form's children for fields having the attribute "required" with the value "true",
				and checks whether their values are empty (invalid) or not (valid).
*/
function validate (the_form) {
	var o = $(the_form);
	var requiredFields = o.find("[required=true]");
	var valid = true;

	// remove previous validation error msg
	o.find("span.error").remove();

	// loop through
	requiredFields.each(function () {
		var field = this;
		if (field.value.length < 1) {
			$(field).parent().append('<span class="error"><img src="images/form-error.gif"></span>');
			valid = false;
		}
	});

	return valid;
}


/*
	Parses XML "Slide" data into an object.

	Param:		{XML} xmlData		The slide data as XML.

	Returns:	{Object} slide		The slide data as an object.
*/
function parseSlide(xmlData) {
	var slide = {};
	var xml = $(xmlData);

	// default values
	slide.title    = null;
	slide.img      = null;
	slide.caption  = null;
	slide.text     = null;
	slide.video    = null;
	slide.images   = [];
	slide.captions = [];
	slide.type     = null;

	// get the title
	if (xml.find('title').length > 0) {
		slide.title = xml.find('title').text();
	}

	// get the image
	if (xml.find('image').length > 0) {
		
		//-- new, multi-images version
		xml.find('image').each( function() {
			var tmp_img = $(this);
			slide.images.push( tmp_img.attr('url') );
			slide.captions.push( tmp_img.attr('caption') || null );
		});
		
		//-- old version
		var img = xml.find('image');
		slide.img = img.attr('url');

		// get the caption
		if (img.attr('caption')) {
			slide.caption = img.attr('caption');
		}
	}
	
	// get the video
	if (xml.find('video').length > 0) {
		var video = xml.find('video');

		slide.video = video.attr('id');

		// get the caption
		if ( video.attr('caption') ) 
		{
			slide.caption = video.attr('caption');
		}
		// get a type
		if( video.attr('type') )
		{
			slide.type = video.attr('type');
		}
	}

	// get the text as string
	if (xml.find('text').length > 0) {
		var xmlNode = xml.find('text')[0];
		var str = '';

		// convert xml dom into a string
		$.each(xmlNode.childNodes, function () {
			var text;

			text = xml2Str(this);
			if (text) {
				str += text;
			}
		});

		slide.text = str;
	}
	//console.log(slide);
	return slide;
}

