/**
 * Includes js files and runs DOM ready code
 */

 LibMan = {
	// String : The path to libman.js
	path: null,

	/**
	 * Calculates and sets the relative path to libman.js
	 *
	 * @return void  
	 */
	calculatePath: function() {
		// condition : is object var already set?
		if (this.path == null) {
			// get path from LibraryManager javascript include string
			var libman = document.getElementById("libman");
			if (!libman) return false;
			
			// remove the filename before setting the path 
			this.path = libman.src.replace(/libman\.js(\?.*)?$/,'');
		}
	},
	
	/**
	 * Wrapper for requireFile, allows single filename or array
	 * of filenames to be passed in
	 * 
	 * @param mixed libFiles string or array of paths to files 
	 * to include, absolute or relative to libman.js
	 *
	 * @return void  
	 */
	requireFiles: function(libFiles) {
		if (typeof(libFiles) == "string") { // single file to include (called from php)
			LibMan.requireFile(libFiles);
		} else { // array of files to include (called from libman.js)
			for (var i=0; i < libFiles.length; i++) {
				LibMan.requireFile(libFiles[i]);
			};
		}
	},
	
	
	/**
	 * Includes a js file
	 * 
	 * @param string libFile path to file to include, 
	 * absolute or relative to libman.js
	 *
	 * @return void  
	 */
	requireFile: function(libFile){
		// match strings starting with 'http://'
		var pattern = /^http:\/\//;
		
		// if libFile matches pattern, path is absolute, otherwise it's relative
		libFile = (pattern.test(libFile)) ? libFile : this.path + libFile;
		document.write('<script type="text/javascript" src="'+ libFile +'"></script>');
	}
}

/*
 * site-specific file includes
 */
var requiredFiles = [
	 'tonic/devtools/grid/jquery.gridoverlay.js'
	,'tonic/devtools/grid/jquery.cookie.js'
	,'tonic/thickbox.js'
	,'tonic/tooltips.js'
	 ];
	
LibMan.calculatePath();
LibMan.requireFiles(requiredFiles);
	
// on DOM ready
$(function() {
	// grid overlay settings
	gridSettings = {
		imgExt: 'jpg'
	}
	
	// grid overlay
	$.gridOverlay(LibMan.path+"../../../../resources/_templates/", gridSettings);
	
	// if we're on locations page
	if ($('div#locations, div#ourGames').length > 0) {
		// setup thickbox
		var tb_pathToImage = "includes/img/loadingAnimation.gif";

		// call tb_init
		tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
		imgLoader = new Image();// preload image
		imgLoader.src = tb_pathToImage;

		// load tooltips
		tooltip();
	}

});

var Utils = {};

Utils.submitAudience = function(form) {
    var selectGuidelineForm = $(form);
    var audienceGroup = $('articles');
    var value = audienceGroup.options[audienceGroup.options.selectedIndex].value;
    if (value.length>0) {
        selectGuidelineForm.action = value;
        selectGuidelineForm.submit();
    }
    return false;
}



