/*************************************************
**
**		File: common.js
**		Author: Ben Belcourt
**		Created: 08.02.2008
**		Last modified: 08.02.2008
**
**		Description: This file contains the common
**		functions for the NHBEA site.
**
*************************************************/

/* Initialize functions */
$(document).ready(function () {
	links.init();
	fileList();
	//forms.init();
});

/* Global configuration object for common values */
var config = {
	classes : {
		error:		'error',
		noPrint:	'noPrint',
		print:		'print'
	}
}


/* Link function */
var links = {

	init : function () {
		this.download();
		this.external();
		this.print();
	},
	
	// Set up functionality for download links
	download : function () {
		var fileName;
		var downloadLinks = $('.downloads ul li a').each(function(e) {
			var ext = this.href.match(/\.(.*)/);
			if (ext != null) { $(this).addClass(ext[1]); }
		});
		
		for (var x=0, len=downloadLinks.length; x<len; x++) {
			fileName = downloadLinks[x].href.match(/temp/);
			if (fileName != null) {
				$(downloadLinks[x]).addClass('disabled').click(function(e){
					return false;
				});
			} else {
				$(downloadLinks[x]).attr('rel', 'external');
			}
		}
	},
	
	// Set up functionality for external links
	external : function () {
		var externalLinks = $('a[rel=external]').click(function(e){
			e.preventDefault();
			window.open($(this).attr('href'), '_blank');
		}); 
    },
    
    // Set up functionality for "print" links
    print : function () {
    	var printLinks = $('a.'+config.classes.print).click(function(e){
    		e.preventDefault();
    		window.print();
    	});
    }

};

/* Meeting file functionality */
function fileList (){
	var lists = $('div.fileList').hide();
	var links = $('h4.reportYear a').text('Show files...').click(function(e){
		e.preventDefault();
		($(this).hasClass('open')) ? hideLists() : showList(this);
		updateLink(this);
		
	});
	
	function showList(link){
		hideLists();
		$(link.hash).show();
	}
	
	function hideLists(){
		lists.hide();
	}
	
	function updateLink(link){
		link = link || links;
		if ($(link).hasClass('open')){
			$(link).text('Show files...');
			$(link).removeClass('open');
		} else {
			$(link).text('Hide files...');
			$(link).addClass('open');
		};
	}
};

/* Registration form behaviors */
var forms = {
	init : function () {
		var form = $('form').submit(function (e) {
			forms.printForm()
		});
		
		var radios = $('form#registrationForm input[type="radio"]').click(function (e) {
			forms.hideOptions(this, $(this).attr('name'));
		});
		
		/*var printButton = $('form#registrationForm input[type="submit"]').click(function (e) {
			console.log(this);
			e.preventDefault();
			var valid = validation.radios(radios);
			if (valid) { forms.printForm(); }
		});*/
	},
	
	hideOptions : function (elem, name) {
		var options = $('input[name="'+ name +'"]').addClass(config.classes.noPrint);
		var listItem = $(elem).parent('li').removeClass(config.classes.noPrint);
		
		for (var x=0; x<options.length; x++) {
			if (options[x] !== elem) {
				$(options[x]).parent('li').addClass(config.classes.noPrint);
			}
		}
	},
	
	printForm : function () {
		window.print();
	}
};

/* Form validation */
var validation = {
	radios : function (elems) {
		var lists = $('ul.radios').removeClass(config.classes.error);
		var radios = $('input[type="radio"]');
		var names = [];
		var validGroups = [];
		var temp;
		var valid = false;
		
		// Split the radios array into it's separate groups
		for (var i=0; i<radios.length; i++) {
			
			if ($(radios[i]).attr('name') !== temp) {
				names.push($(radios[i]).attr('name'));
			}
		}
		
		var validCount = names.length;
		var checkedCount = 0;
		
		// Loop through each group
		for (var j=0; j<names.length; j++) {
			var items = $('input[name="'+ names[j] +'"]');
			
			// Check each item to see if it is checked
			for (var y=0; y<items.length; y++) {
				if($(items[y]).attr('checked')) {
					validGroups.push(names[j]);
					checkedCount++ // Increment checkedCount for each checked item found
				} 
			}
		}
		
		// If every group doesn't have a selected option then...
		if (checkedCount !== validCount) {
			alert('Please select an option for each session.');
			
			for (var g=0; g<names.length; g++) {
				if (!(names[g] in validation.objConvert(validGroups))) {
					// ... highlight the empty groups
					$('ul.'+ names[g]).addClass(config.classes.error)
				}
			}
		}else{
			valid = true;
		}
		
		return valid;
	},
	
	objConvert : function (a) {
  		var o = {};
  		for(var i=0;i<a.length;i++) {
    		o[a[i]]='';
  		}
  		return o;
	}
}