﻿/******************************************************************************
 * jPointLoader Class by Will Lawrence, Samir Sijercic, Ken Le 10/10/2009
 *  
 * Below is an example of how to setup a call to this file from a form view web part (content editor web part should not be used as it gets changed in SharePoint 2010) 
 *   
 *          <script type="text/javascript" src="//servername/js/jPointLoader.js?config-variables=are-optional}" ></script>
 *
 ******************************************************************************  
 *  Loading order is determined by local Path variable, URL Paramter, location of this loader file, and then possibly a default
 * 
 *  Loading order left to right ------------------------------->
 *  File      | Path variable   |   URL Parameter   | default			| "myPath" of jPointLoader.js
 *  ----------------------------------------------------------------------------------------
 *  jQueryFile| jQueryPath      |   jQueryPath=path | Google CDN ->		| [assumes jQuery is in the in same directory as jPointLoader.js file]
 *	(Preset in this file)		|	jQueryFile=file | //ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js  
 *
 *  jPointFile| jPointPath      |   jPointPath=path | [assumes same directory]	|  no default because jPoint should always be in same directory as jPointLoader
 *  (Preset in this file)       |   jPointFile=file
 *
 *  List of possible URL Parameters:
 *  jQueryForceLoad = true | false;  //if loading is forced, then the caller instance is demanding a particular version of jQuery to be loaded, even if a jQuery object exists on the page
 *  jQueryPath                       //Example: <script scr="/sharejpoint.googlecode.com/files/jPointLoader.js?jQueryPath=//servername/js/jquery/"></script>
 *  jQueryFile                       //Example: <script scr="/sharejpoint.googlecode.com/files/jPointLoader.js?jQueryFile=jquery.min.js"></script>
 *  jPointForceLoad = true | false;  //if loading is forced, then the caller instance is demanding a particular version of jPoint to be loaded even if a jPoint object exists on the page
 *  jPointPath                       //Example: <script scr="//servername/js/jPointLoader.js?jPointPath=//servername/dev/"></script>
 *  jPointFile                       //Example: <script scr="//servername/js/jPointLoader.js?jPointFile=jPoint-0.7-expanded.js"></script>
 *
 **********************************************************************************************  
 *
 * START OF USER-DEFINED CONFIG
 *
 **********************ATTENTION JQUERY USERS************************/
//if you already use jQuery in your SharePoint environment, set the jQueryPath variable and file name to equal your local jQuery file path and file name
var jQueryPath = "/src/jquery/"; //this variable overrides ALL OTHER PATH SETTINGS. If empty, then URL parameter is used. Else it uses the default publicURL/
var jQueryFile = "jquery.js"; 
var jQueryPublicURL = "//ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js";

var jPointPath = ""; //this variable overrides ALL OTHER PATH SETTINGS. If empty, then URL parameter is used. Else, it is same as "myPath" of this jPointLoader file    
var jPointFile = "jPoint-0.8-expanded.js"; 
/*  There is no need for a jPointPublicURL variable. If jPointLoader.js is referenced from a public url, then we can assume jPoint is in same location and use getMyPath function below.  
 *
 * END OF USER-DEFINED CONFIG - no need to edit anything below this. */

var jLoaderConfig = {
	getjQueryFile : function(currentPath) {
 		var params = jLoaderConfig.URLParams; 
 		var returnVal = (jQueryPath  || params.jQueryPath || currentPath) + (params.jQueryFile || jQueryFile);
 		if (!jQueryPath && !params.jQueryPath) {
 			returnVal = jQueryPublicURL;
 		}
 		return returnVal;	    
	} ,
	getjPointFile : function(currentPath) {
		var params = jLoaderConfig.URLParams; 
		var returnVal = (jPointPath || params.jPointPath || currentPath) + (params.jPointFile || jPointFile);
 		return returnVal;	 
	} ,
	URLParams : "" //variable that holds any URL parameters that are specified. For example, if src='//servername/localPath/jPointLoader.js?jQueryPath=anotherPath&jQueryFileName=jquery.1.2.min.js',
}

//Global jPart class. 
//If another jPart class has been loaded onto the page, then these class properties would already have been defined 
if (typeof jPart === "undefined") jPart = (function () {});
//getMyPath is a function that looks for the jPointLoader <script> tag on the page and returns the path of the src, or in other words, "myPath" of this jPointer.js file
//Example: if page has this: <script src="//servername/js/jPointLoader.js">, then myjPart.getMyPath() returns "//servername/js/"
//getMyPath also discovers any "URL parameters" and populates the jLoaderConfig.URLParams array
if (typeof jPart.prototype.getMyPath === "undefined") jPart.prototype.getMyPath = (function () {
	var scriptElems = document.getElementsByTagName("script");
	var libpath = "";
	for(var i=0; i<scriptElems.length; i++) {
		var src = scriptElems[i].src; 
		if (src.toLowerCase().indexOf("jpointloader")>0) {
			libpath = src.substr(0,src.toLowerCase().indexOf("jpointloader"));
			//save any available URL parameters that could determine path and filenames
			var vars = [], hash;
			var hashes = src.slice(src.indexOf('?') + 1).split('&');				
			for (var i = 0; i < hashes.length; i++)
			{
				hash = hashes[i].split('=');
				vars.push(hash[0]);
				vars[hash[0]] = hash[1];
			}
			jLoaderConfig.URLParams = vars;
			break;
		}
	}
	return libpath;
});

//manualLoad loads a javascript file using the script tag method  
if (typeof jPart.prototype.manualLoad === "undefined") jPart.prototype.manualLoad = (function (file) {
	document.write("<script src='",file,"' type='text/javascript'><\/script>");
});

//setup page's jParts array 
if(typeof myjParts === "undefined") { myjParts = myjWebparts /*myjWebparts is deprecated as of v0.8*/ = new Array() };

//set up the jPart which referenced this jPointLoader file	
myjPart = myjWebpart /*myjWebpart is deprecated as of v0.8*/ = new jPart();

var currentPath = myjPart.getMyPath();
var jQF = jLoaderConfig.getjQueryFile(currentPath);
var jPF = jLoaderConfig.getjPointFile(currentPath);
if (typeof jQuery === "undefined" || jLoaderConfig.URLParams.jQueryForceLoad == "true") myjPart.manualLoad(jQF); //To complete overwritting global path, then you need to pass in override=true (see URLParms in jLoaderConfig at top of page) and for a different version, use jQuery.noConflict() as referenced here, http://stackoverflow.com/questions/528241/how-do-i-run-different-versions-of-jquery-on-the-same-page/528251
if (typeof jPoint === "undefined" || jLoaderConfig.URLParams.jPointForceLoad == "true") myjPart.manualLoad(jPF); //To complete overwritting global path, then you need to pass in override=true (see URLParms in jLoaderConfig at top of page)
if(typeof JpartProcessing === "undefined") var JpartProcessing = 0;

/* jParts page management ****************************************************************/
// This part does jPart page management
// Generic jPart asyncronous accessing, setting up jPart read and show configuration, options
myjPart.Position = myjParts.length;
myjPart.readConfig = function(){
	var jPartOptions = {};
	$(this.ConfigTableDiv).find("input[id^='CfgField']").each(function(idx, item) {
		jPartOptions[item.id.substr(8)] = item.value;
	});
	this.Options.Local = jPartOptions;
};

myjPart.ShowConfigDefaults = null;
myjPart.showConfig = function () {
	var configTable = this.ConfigTableDiv;
	
	this.getOptionDefaults(this.ShowConfigDefaults);
	//Use default options if option has not been updated
	if (this.Options.Local == undefined || this.Options.Local == null)
		var jPartOptions = myOptionDefaults;
	else
		var jPartOptions = this.Options.Local;

	//Remove all previously created config nodes
	$(configTable).find(".configRow").remove();

	configTable.append("<tr class='configRow'>"
			+"<td colspan=2>"
			+"<div id=CONFIGTABLE></div>"
			+"</td>"
			+"</tr>");
	//Append config fields
	var tbl = "<table>";
	$.each(jPartOptions, function(name, val) {
		tbl += ("<tr class='configRow'>"
			+"<td width='100px'>" + name + "</td>"
			+"<td><input type='text' id='CfgField"+name+"' name='" + name + "' value='" + val + "' size='70'/></td>"
			+"<td>" + myOptionDescriptions[name] + "</td>"
			+"</tr>"); 
	});	
	tbl +="</table>";
	$(configTable).find("#CONFIGTABLE").append(tbl);
	jPartOptions=null;
}
//Add myjPart to myjParts collection
myjParts[myjPart.Position] = myjPart;

var myOptionDefaults = {};
var myOptionsDescriptions = {};

//field, description, default value
myjPart.getOptionDefaults = function(myDefinitions) {
	
	myOptionDefaults = {};
	myOptionDescriptions = {};
	
	$.each(myDefinitions, function(idx, itemObj) {
		myOptionDescriptions[itemObj[0]] = itemObj[1];
		myOptionDefaults[itemObj[0]] = itemObj[2];
	});
}
//Setting jPart local options
myjPart.setjPartOptions = function(myjOptions){
	//myjLocalOptions are required from the webpart content editor page
	if(typeof this.myjLocalOptions === "undefined") 
	{
		this.myjLocalOptions = null;
		if(typeof myjLocalOptions === "undefined")
		{
			myjOptions["Local"] = null;
		}
		else
		{
			myjOptions["Local"] = myjLocalOptions;
			myjLocalOptions = null;
		}
		
	}
	else
	{
		myjOptions["Local"] = myjPart.myjLocalOptions;
	}
	//Configure webpart and store info into memory for later access
	jP.config(myjOptions);
}
//load core script libraries
document.write("<div id=myjPart" + myjPart.Position + "><\/div>");
myjPart["MainDivID"] = "myjPart" + myjPart.Position;

/*End jPart Configuration **********************************************************************/
