/* --- //

doc: solpass.js
site: SOLPass.org
development: Cobey Potter, Wellfire Interactive
version: q3, 2009

// --- */

$(document).ready(function(){

   NECTAR.init({
      server:["www.solpass.org", "solpass.org"],
      template:"body",
      content:"#content",
      jsReqPath:"/z-site/scripts/"
    });
    NECTAR.analytics({profile:"UA-19383687-1",force:0});
    NECTAR.require("nectar.form", function(){});
    NECTAR.run("tmpl-content","/z-site/scripts/tmpl-content.js");
    NECTAR.run("tmpl-study","/z-site/scripts/tmpl-study.js");  
    NECTAR.run("tmpl-act","/z-site/scripts/tmpl-act.js");
});


//object literal as its a singleton namespaced and is called automatically.
var NECTAR = NECTAR || {
	version: 0.6,
	config: {},
	//*------ REQUIRE SCRIPTS :: Lazy loading, call only extra scripts as needed ------*//
	require: function(s, f){ 
		var ajaxcall = function(script, func){
			$.ajax({
				url: NECTAR.config.jsReqPath + script + ".js",
				async: false, //typically all are async, but we most likely need this for requirements
				cache: true, //typically not cached for scripts, but as these may be "required" in other files, we can cache them in production
				dataType: 'script',
				complete: func,
				error: function(){
					$.ajax({
						url: script,
						dataType: 'script',
						complete: func,
						error: function(){ console.log("script not available"); }
					});
				}
			});
		};
		switch(typeof s){
			case "object":
				script = s.shift();
				if (s.length == 1){ s = s[0]; }
				ajaxcall(script, NECTAR.require(s, f));
			break;
			case "string": ajaxcall(s,f); break;
		}
	},
	//*------ RUN SCRIPTS :: Lazy loading, call only extra scripts as needed ------*//
	/* ----
						TODO:
							[1] load multiple files
							[2] set up css link html creation (for style switcher)
							[3] allow for things to be run, even without template
							[4] Redefine the logic so you aren't repeating yourself
							
	---*/
  run: function(t, s){ 
    var fileAction = {
      js: function(file){ $.getScript(file); },
      html: function(file){ alert(file); },
      css: function(file){ alert(file); }
    };
		
		fileSwitcher = function(s){
		  switch(typeof s){
        case "function": s.call(); break;
        case "string": 
          sArr = s.split(".");
          fileType = sArr.pop();
          fileAction[fileType](s);
        break;
      }
		};
		
    if(!s){
      fileSwitcher(t);  
      return;
    }      
    
		if(t){
		  tArr = t.split(",");
		  for(elm in tArr){
		    if(NECTAR.config.template == tArr[elm]){ 
          fileSwitcher(s);  
		      break;
		    }
		  }
		  return;
		}

	},
	stripSelector: function(elm){ //strips out #  or . from selectors
    if(!elm) return false;
    if(elm.indexOf('.') == 0) return elm.replace(/./i,"");
    if(elm.indexOf('#') == 0) return elm.replace(/#/i,"");
  },
	//*------ parseQuery :: returns query string as an object of all variables, an object of select variables, or a single value ------*//
	parseQuery: function(key){
		if(self.location.search == "") return false;
		if (!NECTAR.config.query){ 
			NECTAR.config.query = {};
			$(decodeURIComponent(self.location.search).substring(1).split("&")).each(function(){
				var q = this.split("=");
				NECTAR.config.query[q[0]] = q[1];
			});	
		}
		switch(typeof key){
			case "undefined": return NECTAR.config.query; break;
			case "string": return NECTAR.config.query[key]; break;
			case "object": 
				var o = {};
				$.map(key, function(a){ $(NECTAR.config.query).each(function(){ if(this[a]) o[a] = this[a]; });	});
				return o;
			break;
		}
	},
	init: function(userConfig){
    NECTAR.config = userConfig;
    NECTAR.client.set();
    NECTAR.config.server = NECTAR.server.set(userConfig.server);
    NECTAR.config.template = $(userConfig.template).attr("id") || $("body").attr("id");
    NECTAR.config.styleDir = userConfig.styleDir || '/media/styles/';
		NECTAR.config.jsReqPath = userConfig.jsReqPath || '/media/scripts/';
  }
}

//revealed module pattern: called immediately and attached to NECTAR
NECTAR.client = function(){
	var IE = ["ie", "ie7", "ie6"];
	var set = function(){ // determine if its an IE or modern browser * TODO: come up with some feature testing for FF3, maybe opera?
    NECTAR.config.browser = "noIE";    
    for(b in this.IE){
      if( $("html").hasClass(this.IE[b]) )
        NECTAR.config.browser = this.IE[b];
    }
  }
	return {
		set: set		
	}
}();

//revealed module pattern: called immediately and attached to NECTAR
NECTAR.server = function(){
	var serverType = ["dev", "stage", "prod"];
	var set = function(serverList){
		if(typeof serverList  == "undefined"){ return serverType.pop(); }
    for(var i in serverList){
      if(!serverList[i].match(/http/))
        serverList[i] = "http://" + serverList[i];
      if(serverList[i] == "http://" + window.location.hostname){ return serverType.pop(); }
      else{ return serverType.shift(); }
    }
	}
	return {
		set: set
	}
}();

//revealed module pattern: called immediately and attached to NECTAR
//*------ FEATURESET LOGGING ------*//
NECTAR.features = function(){ 
	var modules = [];
	var log = function(module){ modules[module] = "running"; };
	var list = function(){ return modules; };
	var has = function(data){
		if(!Modernizr){ return false; }
    if(typeof data == "undefined"){ return Modernizr; }
    else{ return $('html').hasClass(data); }
	}
	return {
		log: log,
		list: list,
		has: has
	}
}();

//revealed module pattern: called immediately and attached to NECTAR
//*------ MODAL WINDOW ------*//
NECTAR.modal = function(){
	var init = function(){ 
		$("<div/>", {
      id: 'nectar-modal',
      css: { display: 'none' }
    }).appendTo('body');
		return true;
  };
	return {
		init: init
	}
}();

//just a function: called immediately and attached to NECTAR
//*------ IMPORT GOOGLE ANALYTICS ------*//
NECTAR.analytics = function(data){
  if(NECTAR.config.server === "prod" || data.force == 1){
    $.getScript( "http://www.google-analytics.com/ga.js", 
    function(){ 
     try { 
			var pageTracker = _gat._getTracker(data.profile); pageTracker._trackPageview(); 
			NECTAR.features.log("analytics");
		 } 
     catch(err) { console.log ( "there was an error: " + err);} 
    });
  }
}
