﻿/******************************************************************************
 * jPointLoader Class by Will Lawrence, Samir Sijercic, Ken Le 10/10/2009
 * 
 * Example of call from form view web part:
 *
 <script type="text/javascript" src="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
 * 
 *  File    |   Path variable   |   URL Parameter   |  "myPath" of jPointLoader    |   default
 *  ----------------------------------------------------------------------------------------
 *  jQuery  |   jQueryPath      |   jQueryPath=path | [assumes jQuery is in the in |   Google CDN -> 
 *									jQueryFile=file | the same directory as jPoint |   //ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
 *
 *  jPoint  |   jPointPath      |   jPointPath=path | [assumes same directory]     |  no default because jPoint should always be in same directory as jPointLoader
 *                                  jPointFile=file
 *
 *  List of possible URL Parameters
 *  jQueryForceLoad = true | false;  //if override is true, then the caller instance is forcing a particular version of jQuery to be loaded even if a jQuery object exists on the pageon the page
 *  jQueryPath                       //Example is //sharejpoint.googlecode.com/files/jPointLoader.js?jQueryPath=//servername/js/jquery/
 *  jQueryFile                       //Example is //sharejpoint.googlecode.com/files/jPointLoader.js?jQueryFile=jquery.min.js
 *  jPointForceLoad = true | false;  //if override is true, then the caller instance is forcing a particular version of jPoint to be loaded even if a jPoint object exists on the page
 *  jPointPath                       //Example is //servername/js/jPoint/jPointLoader.js?jPointPath=//servername/dev/
 *  jPointFile                       //Example is //servername/js/jPoint/jPointLoader.js?jPointFile=jPoint-0.7-expanded.js
 *
 **********************************************************************************************/  

//ATTENTION JQUERY USERS: if you already use jQuery in your SharePoint environment, put it's path in the variable below
var jQueryPath = "/src/jquery/"; //this setting overrides ALL OTHER SETTINGS. If empty, then URL paremter will be used, then same path as jPointLoader if it is not a Google CDN.	    
var jQueryFile = "jquery.js"; 
var jQueryPublicURL = "//ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js";

var jPointPath = "/src/SPSDCDemo/"; //this setting overrides ALL OTHER SETTINGS. If empty, then URL paremter will be used, then same path as jPointLoader if it is not a Google CDN.	    
var jPointFile = "jPoint-0.8-expanded.js"; 

var jLoaderConfig = {
	getjQueryFile : function(currentPath) {
 		var params = jLoaderConfig.URLParams; 
 		var returnVal = (jQueryPath  || params.jQueryPath || currentPath) + (params.jQueryFile || jQueryFile);
 		if (!jQueryPath && currentPath.toLowerCase().indexOf("sharejpoint.googlecode") >= 0) {
 			returnVal = jQueryPublicURL;
 		}
 		return returnVal;	    
	} ,
	getjPointFile : function(currentPath) {
		var params = jLoaderConfig.URLParams; 
		var returnVal = (jPointPath || params.jPointPath || currentPath) + (params.jPointFile || jPointFile);
 		return returnVal;	 
	} ,
	URLParams : "" //Overrides can be sent to this file by referencing: //servername/localPath/jPointLoader.js?jQueryPath=anotherPath&jQueryFileName=jquery.1.2.min.js
}
 
if (typeof jWebpart === "undefined") jWebpart = (function () {});
//global jWebpart property settings.  If another jWebpart has been loaded, then these properties will already have been setup. 
if (typeof jWebpart.prototype.getMyPath === "undefined") jWebpart.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;
	});
if (typeof jWebpart.prototype.manualLoad === "undefined") jWebpart.prototype.manualLoad = (function (file, configDivID) {
		document.write("<script src='",file,"' type='text/javascript'><\/script>");
		//alert("Loaded = " + file)
	});
//Set up once for all webparts
if(typeof myjParts === "undefined") myjParts = new Array();
//setup my webpart	
myjPart = new jWebpart();
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 myOptionDefinition = [ ["ID","Unique identifier for plugin","FeedbackLite"] ];
//myOptionDefinition[0] = ["a", "B" ];//["ID","Unique identifier for plugin","FeedbackLite"] ]
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];
	});
}

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;
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, "myjPart" + myjPart.Position); //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, "myjPart" + myjPart.Position); //To complete overwritting global path, then you need to pass in override=true (see URLParms in jLoaderConfig at top of page) undefined check.
if(typeof JpartProcessing === "undefined") var JpartProcessing = 0;