
	/****************************************************************************************************
		
		WEB INTERFACE JQUERY EXTENSION TOOLSET (WIJET)
		wijet.core.js
	
		This script was written by Caleb Rash for Hortus, LTD for http://www.pallensmith.com
		Any unauthorized reproduction or distribution of this script is strictly forbidden.
		This script and all associated files and procedures (c) 2010 Hortus, LTD. All rights reserved.
		
	 ****************************************************************************************************/




var prefs =
{
	init: function()
	{
		this.modules = Array();
			// DEFAULT MODULES
			this.modules[0] = { name: "page", file: "wijet.core.js", isDefault: true};
			this.modules[1] = { name: "user", file: "wijet.core.js", isDefault: true};
			this.modules[2] = { name: "error", file: "wijet.core.js", isDefault: true};
			this.modules[3] = { name: "browser", file: "wijet.core.js", isDefault: true};
			this.modules[4] = { name: "search", file: "wijet.core.js", isDefault: true};
			this.modules[5] = { name: "navigation", file: "wijet.core.js", isDefault: true};
			// NON-DEFAULT MODULES
			this.modules[6] = { name: "ui", file: "wijet.core.js", isDefault: false};
			this.modules[7] = { name: "video", file: "videos.js", isDefault: false};
			this.modules[8] = { name: "social", file: "wijet.core.js", isDefault: false};
		
		this.modMap = Array();
			this.modMap['page'] = page;
			this.modMap['user'] = user;
			this.modMap['error'] = error;
			this.modMap['browser'] = browser;
			this.modMap['search'] = search;
			this.modMap['navigation'] = navigation;
			this.modMap['ui'] = (typeof(ui)=='undefined') ? false : ui;
			this.modMap['video'] = (typeof(video)=='undefined') ? false : video;
			this.modMap['social'] = (typeof(social)=='undefined') ? false : social;
	},
	basename: "http://www.pallensmith.com/",
	transitionSpeed: 500,
	modules: false,
	modMap: false,
	paths:
	{
		scripts: "scripts/",
		php: "php/",
		images: "",
		files: "files/",
		resourceDirectory: "resources/",
		stylesDirectory: "styles/"
	},
	waitInterval: 500,
	errorDisplayLength: 3000,
	iterationLimit: 100,
	css3: "css3.css",
	debug: false
};

var wijet =
{
	init: function(modules, pageid)
	{
		prefs.init();
		this.loadDefaultModules();
		page.set(pageid);
		this.loaded = true;
	},
	version: "1.0.1",
	loadModule: function(moduleName)
	{
		prefs.modMap[moduleName].init();
	},
	loadDefaultModules: function()
	{
		var i = 0;
		while(i < prefs.modules.length)
		{
			if(i > prefs.iterationLimit) { error.fatal("Modules could not finish loading. Maximum iteration limit breached. Please, contact the web site administrator."); break; }
			if(prefs.modules[i].isDefault) { prefs.modMap[prefs.modules[i].name].init(); }
			i++;
		}
	},
	noop: function()
	{
		return true;
	},
	loaded: false
};




var ie7 =
{
	init: function()
	{
//		error.notice('We are currently testing new features that allow us to target and fix Internet Explorer 7 problems. Please be patient while we are working to improve the site.');
	}
};



var page =
{
	init: function()
	{
		$("#header").click( function(e)
		{
			if(search.form.open) { search.form.destroy(); }
			if(user.login.open) { user.login.destroy(); }
			if(user.account.open) { user.account.destroy(); }
		});
		
		// setup article images
		$("div#main div#content div#content-middle div#article img").addClass("article-image");
		if($("div.ad").length > 1) $("div.ad").each( function() { $(this).css("clear", "right") });
	},
	loading: function()
	{
		return true;
	},
	addStylesheet: function(stylesheet)
	{
		$("head").append('<link href="' + stylesheet + '" rel="stylesheet" media="screen" type="text/css" />');
	},
	set: function(pageid)
	{
		this.current = pageid;
	},
	current: false,
	get: function()
	{
		return this.current;
	},
	httpStatus: function(status)
	{
		var s = Array();
			s[100] = "The client should continue with its request.";
			s[101] = "The server is switching protocols.";
			s[200] = "The request was successful.";
			s[201] = "The request was completed and has created a new resource.";
			s[202] = "The request was accepted.";
			s[204] = "No content was found in the destination file.";
			s[300] = "The server found multiple sources when locating the requested file.";
			s[301] = "The requested content has been moved.";
			s[400] = "The request was not properly formatted.";
			s[401] = "The request requested file could not be accessed due to permission requirements.";
			s[403] = "The server has denied access to the requested file.";
			s[404] = "The requested file was not found.";
			s[500] = "There was an error with the server.";
		var si = parseInt(status, 10);
		return s[si];
	}
};


var user =
{
	init: function()
	{
		this.login.init();
	},
	login:
	{
		init: function()
		{
			if($("#login-button").length !== 0)
			{
				$("#login-button").click( function(e)
				{
					if(browser.IE7 === false) e.preventDefault();
					if(search.form.open) { search.form.destroy(); }
					if(user.account.open) { user.login.destroy(); }
					if(user.login.open) { user.login.destroy(); }
					else { user.login.make(); }
				});
			}
			else if($("#account-button").length !== 0)
			{
				$("#account-button").click( function(e)
				{
					e.preventDefault();
					if(search.form.open) { search.form.destroy(); }
					if(user.login.open) { user.login.destroy(); }
					if(user.account.open) { user.login.destroy(); }
					else { user.account.showOptions(); }
				});
			}
			
			if($(".login-elsewhere").length !== 0)
			{
				$(".login-elsewhere").click( function(e) { user.login.elsewhere(); } );
			}
		},
		make: function()
		{
			if(!browser.IE7)
			{
				$("body").append('<div id="login-form"></div>');
				var divs = '<div id="login-form-content"><div id="login-content-left-side"></div><div id="login-content-inside"></div><div id="login-content-right-side"></div></div>';
				divs += '<div id="login-bottom"><div id="login-form-left-side"></div><div id="login-form-middle"></div><div id="login-form-right-side"></div></div>';
				$("#login-form").html(divs);
				$("#login-form").css("left", ($("#top-bar").offset().left + 250) + "px");
				$("#login-form").css("top", "-500px");
				
				$.ajax(
				{
					url: prefs.basename + prefs.paths.resourceDirectory + prefs.paths.php + "user_login_form.php?r=" + window.location,
					success: function(data)
					{
						$("#login-content-inside").html(data);
						$("#login-form").animate({top:"0px"}, prefs.transitionSpeed);
						user.login.open = true;
						$("body").append('<div id="mask"></div>');
						$("#mask").click( function(e)
						{
							$(this).remove();
							user.login.destroy();
						});
					},
					error: function(xhr, textStatus, errorThrown)
					{
						error.warn("Error: " + page.httpStatus(xhr.status));
					}
				});
			}
			else
			{
				window.location = 'http://www.pallensmith.com/about/login';
			}
		},
		destroy: function()
		{
			$("#login-form").animate({top:"-500px"}, prefs.transitionSpeed, function() { $(this).remove(); } );
			this.open = false;
		},
		open: false,
		elsewhere: function()
		{
			$("#login-button").click();
		}
	},
	account:
	{
		showOptions: function()
		{
			$("body").append('<div id="acct-options"></div>');
			var divs = '<div id="login-form-content"><div id="login-content-left-side"></div><div id="login-content-inside"></div><div id="login-content-right-side"></div></div>';
			divs += '<div id="login-bottom"><div id="login-form-left-side"></div><div id="login-form-middle"></div><div id="login-form-right-side"></div></div>';
			$("#acct-options").html(divs);
			$("#acct-options").css("position", "absolute");
			$("#acct-options").css("left", ($("#top-bar").offset().left + 250) + "px");
			$("#acct-options").css("height", "150px");
			$("#acct-options").css("top", "-300px");
			
			$.ajax(
			{
				url: prefs.basename + prefs.paths.resourceDirectory + prefs.paths.php + "user_account_options.php?r=" + window.location,
				success: function(data)
				{
					$("#login-content-inside").html(data);
					$("#acct-options").animate({top:"0px"}, prefs.transitionSpeed);
					user.account.open = true;
					$("body").append('<div id="mask"></div>');
					$("#mask").click( function(e)
					{
						$(this).remove();
						user.account.destroy();
					});
				},
				error: function(xhr, textStatus, errorThrown)
				{
					error.warn("Error: " + page.httpStatus(xhr.status));
				}
			});
		},
		destroy: function()
		{
			$("#acct-options").animate({top:"-500px"}, prefs.transitionSpeed, function() { $(this).remove(); } );
			user.account.open = false;
		},
		open: false
	}
};


var error =
{
	init: function()
	{
		return true;
	},
	sanitize: function(message)
	{
		message = message.replace('iframe', 'span');
		message = message.replace('script', 'span');
		message = message.replace('src', 'title');
		return message;
	},
	notice: function(message)
	{
		var newElement = "div" + Math.round(Math.random()*10000000000);
		message = error.sanitize(message);
		$("body").append('<div id="' + newElement + '" class="error notice"><div>' + message + '</div></div>');
		$("#" + newElement).hide();
		$("#" + newElement).animate({height:"show", opacity:"show"}, prefs.transitionSpeed).delay(5000).animate({height:"hide", opacity:"hide"}, prefs.transitionSpeed);
	},
	warn: function(message)
	{
		message = error.sanitize(message);
		var newElement = "div" + Math.round(Math.random()*10000000000);
		$("body").append('<div id="' + newElement + '" class="error warn"><div>' + message + '</div></div>');
		$("#" + newElement).hide();
		$("#" + newElement).animate({height:"show", opacity:"show"}, prefs.transitionSpeed).delay(5000).animate({height:"hide", opacity:"hide"}, prefs.transitionSpeed);
	},
	fatal: function(message, shouldKillAll)
	{
		message = error.sanitize(message);
		shouldKillAll = (typeof(shouldKillAll) == 'undefined') ? false : shouldKillAll;
		var newElement = "div" + Math.round(Math.random()*10000000000);
		$("body").append('<div id="' + newElement + '" class="error fatal"><div>' + message + '</div><div><a id="close" href="#close">close</a></div></div>');
		$("#" + newElement).hide();
		$("#" + newElement).animate({height:"show", opacity:"show"}, prefs.transitionSpeed);
		$("#close").click(function(e)
		{
			$(this).parent().parent().animate({ height:"hide", opacity:"hide" }, prefs.transitionSpeed, function()
			{
				$(this).remove();
			});
		});
	}
};


var browser =
{
	init: function()
	{
		this.notAcceptable = Array();
			this.notAcceptable[0] = { name: "ie", version: 6 };
		this.extractName(navigator.userAgent);
		this.extractVersion(navigator.userAgent);
		if(!this.acceptable())
		{
			var badBrowserMessage = "This browser is not supported by our system. ";
			badBrowserMessage += "While you will not be restricted from access to our site, please ";
			badBrowserMessage += "be aware that not all features will be guaranteed to function ";
			badBrowserMessage += "properly. We apologize for any inconvenience this may cause, ";
			badBrowserMessage += "however, we must urge you to upgrade your browser to fully ";
			badBrowserMessage += "enjoy www.PAllenSmith.com";
			error.fatal(badBrowserMessage, false);
		}
		

		if((browser.b().name == "ie") && browser.b().version == 7) { this.IE7 = true; ie7.init(); }
		
		if(this.css3()) { page.addStylesheet(prefs.basename + prefs.paths.resourceDirectory + prefs.paths.stylesDirectory + prefs.css3); }
	},
	IE7: false,
	name: function()
	{
		return this.n;
	},
	extractName: function(userAgent)
	{
		if(userAgent.indexOf("opera") >= 0 || userAgent.indexOf("Opera") >= 0) { this.n = "opera"; }
		else if(userAgent.indexOf("MSIE") >= 0) { this.n = "ie"; }
		else if(userAgent.indexOf("Chrome") >= 0) { this.n = "chrome"; }
		else if(userAgent.indexOf("Safari") >= 0) { this.n = "safari"; }
		else if(userAgent.indexOf("Firefox") >= 0) { this.n = "firefox"; }
		else { return false; }
	},
	version: function()
	{
		return this.v;
	},
	extractVersion: function(userAgent)
	{
		if(!this.n) { return false; }
		else if(this.n == "ie") { this.v = parseFloat(userAgent.substr(userAgent.indexOf("MSIE")+5, 3)); }
		else if(this.n == "chrome") { this.v = userAgent.substr(userAgent.indexOf("Chrome/")+7, 3); }
		else if(this.n == "safari") { this.v = userAgent.substr(userAgent.indexOf("Version/")+8, 3); }
		else if(this.n == "opera") { this.v = userAgent.substr(userAgent.indexOf("Version/")+8, 4); }
		else if(this.n == "firefox") { this.v = userAgent.substr(userAgent.lastIndexOf("/")+1,3); }
	},
	b: function()
	{
		return {name: this.n, version: this.v};
	},
	n: false,
	v: false,
	acceptable: function()
	{
		for(var i = 0; i < this.notAcceptable.length; i++)
		{
			if(i > prefs.iterationLimit) { break; }
			else if(this.notAcceptable[i].name == this.n)
			{
				if(this.notAcceptable[i].version >= this.v) { return false; }
			}
		}
		return true;
	},
	notAcceptable: false,
	css3: function()
	{
		return (this.b().name == "chrome") || (this.b().name == "safari") || (this.b().name == "opera") || (this.b().name == "firefox");
	}
};


var search =
{
	init: function()
	{
		$("#site-search-button").click( function(e)
		{
			if(browser.IE7 === false) e.preventDefault();
			if(user.login.open) { user.login.destroy(); }
			if(user.account.open) { user.login.destroy(); }
			if(search.form.open) { search.form.destroy(); }
			else { search.form.site(); }
		});
	},
	form:
	{
		site: function()
		{
			$("body").append('<div id="search-form"></div>');
			var divs = '<div id="search-form-content"><div id="search-content-left-side"></div><div id="search-content-inside"></div><div id="search-content-right-side"></div></div>';
			divs += '<div id="search-bottom"><div id="search-form-left-side"></div><div id="search-form-middle"></div><div id="search-form-right-side"></div></div>';
			$("#search-form").html(divs);
			$("#search-form").css("left", ($("#top-bar").offset().left + 250) + "px");
			$("#search-form").css("top", "-200px");
			$.ajax(
			{
				url: prefs.basename + prefs.paths.resourceDirectory + prefs.paths.php + "search_site_form.php",
				success: function(data)
				{
					$("#search-content-inside").html(data);
					$("#search-form").animate({top:"0px"}, prefs.transitionSpeed);
					if(browser.name() == "ie") { setTimeout(function() { $("input[type=text]").parent().css("margin", "-10px 0px 0px 0px"); }, prefs.transitionSpeed); }
					search.form.open = true;
					$("body").append('<div id="mask"></div>');
					$("#mask").click( function(e)
					{
						$(this).remove();
						search.form.destroy();
					});
				},
				error: function(xhr, textStatus, errorThrown)
				{
					error.warn(page.httpStatus(xhr.status));
				}
			});
		},
		destroy: function()
		{
			$("#search-form").animate({top:"-200px"}, prefs.transitionSpeed, function() { $(this).remove(); } );
			this.open = false;
		},
		open: false
	},
	make: function()
	{
	
	}
};


var navigation =
{
	init: function()
	{
		this.forceOpen(page.get());
		$(".navigation_level_1").hide();
		$(".navigation_level_0 a.level_0").click( function(e)
		{
			
	//		window.alert($(this).parent().next().children().html());
			if($(this).parent().next().children().hasClass("navigation_level_1"))
			{
				e.preventDefault();
				if($(this).parent().next().children().html() == navigation.openName)
				{
					wijet.noop();
				}
				else if(navigation.open !== false)
				{
					(navigation.open).animate({height:"hide"}, prefs.transitionSpeed);
					(navigation.open) = false;
					navigation.openName = false;
				}
				
				
				if(!navigation.open)
				{
					$(this).parent().next().children().animate({height:"show"}, prefs.transitionSpeed);
					navigation.open = $(this).parent().next().children();
					navigation.openName = $(this).parent().next().children().html();
				}
			}
		});
	},
	open: false,
	openName: false,
	forceOpen: function(pageid)
	{
		if(pageid == page.get())
		{
			setTimeout( function()
			{
				var obj = $(".child_of_" + page.get());
				obj.animate({height:"show"}, prefs.transitionSpeed);
				navigation.open = obj;
				navigation.openName = obj.html;
			}, prefs.transitionSpeed*2);
		}
	}
};


















function report(msg)
{
	$("#report").html(msg);
}

























// NON-DEFAULT MODULES

var social =
{
	init: function(f)
	{
		f = (typeof(f) != 'undefined') ? f : false;
		this.loadTemplate();
	},
	template: "social_template.php",
	defaultLocation: "#social-holder",
	contentSection: "#social-content",
	button:
	{
		articles: "#social-articles",
		twitter: "#social-twitter",
		facebook: "#social-facebook",
		youtube: "#social-youtube",
		rss: "#social-rss",
		defaultAction: "#social-articles"
	},
	file:
	{
		articles: "social_articles.php",
		twitter: "social_twitter.php",
		facebook: "social_facebook.php",
		youtube: "social_youtube.php",
		rss: "social_rss.php"
	},
	featured: false,
	extendFiles: function()
	{
		this.file.articles = prefs.basename + prefs.paths.resourceDirectory + prefs.paths.php + this.file.articles + "?f=" + this.featured;
		this.file.twitter = prefs.basename + prefs.paths.resourceDirectory + prefs.paths.php + this.file.twitter;
		this.file.facebook = prefs.basename + prefs.paths.resourceDirectory + prefs.paths.php + this.file.facebook;
		this.file.youtube = prefs.basename + prefs.paths.resourceDirectory + prefs.paths.php + this.file.youtube;
		this.file.rss = prefs.basename + prefs.paths.resourceDirectory + prefs.paths.php + this.file.rss;
	},
	setFeatured: function(f)
	{
		this.featured = f ? f : "false";
		this.extendFiles();
	},
	loadTemplate: function()
	{
		var u = prefs.basename + prefs.paths.resourceDirectory + prefs.paths.php + social.template;
		$.ajax(
		{
			url: u,
			success: function(data)
			{
				$(social.defaultLocation).append(data);
				social.setActions();
			},
			error: function(xhr, textStatus, errorThrown)
			{
				error.warn(page.httpStatus(xhr.status));
				$("#site-search-button").html("Search");
			}
		});
	},
	setActions: function()
	{
		setTimeout(function() { social.doSetActions(); }, prefs.waitInterval);
	},
	doSetActions: function()
	{
		$(social.button.articles).click(function(e)
		{
			$(social.contentSection).html("<div>Loading...</div>");
			e.preventDefault();
			$.ajax(
			{
				url: social.file.articles,
				success: function(data)
				{
					$(social.contentSection).html(data);
				},
				error: function(xhr, textStatus, errorThrown)
				{
					error.warn(page.httpStatus(xhr.status) + ": " + social.file.articles);
				}
			});
		});
		
		$(social.button.twitter).click(function(e)
		{
			$(social.contentSection).html("<div>Loading...</div>");
			e.preventDefault();
			$.ajax(
			{
				url: social.file.twitter,
				success: function(data)
				{
					$(social.contentSection).html(data);
				},
				error: function(xhr, textStatus, errorThrown)
				{
					error.warn(page.httpStatus(xhr.status) + ": " + social.file.articles);
				}
			});
		});
		$(social.button.youtube).click(function(e)
		{
			$(social.contentSection).html("<div>Loading...</div>");
			e.preventDefault();
			$.ajax(
			{
				url: social.file.youtube,
				success: function(data)
				{
					$(social.contentSection).html(data);
				},
				error: function(xhr, textStatus, errorThrown)
				{
					error.warn(page.httpStatus(xhr.status) + ": " + social.file.articles);
				}
			});
		});
		$(social.button.facebook).click(function(e)
		{
			$(social.contentSection).html("<div>Loading...</div>");
			e.preventDefault();
			if(browser.name() == "ie" || browser.name() == "chrome")
			{	
				$(social.contentSection).html('<div id="ie-social"><a href="http://www.facebook.com/GardenHomeRetreat" target="_blank"><img src="/resources/files/ie-facebook.gif" border="0" style="border:none;" /></a></div>');
			}
			else
			{
				$.ajax(
				{
					url: social.file.facebook,
					success: function(data)
					{
						$(social.contentSection).html(data);
					},
					error: function(xhr, textStatus, errorThrown)
					{
						error.warn(page.httpStatus(xhr.status) + ": " + social.file.articles);
					}
				});
			}
		});
		
		$(social.button.rss).click(function(e)
		{
			$(social.contentSection).html("<div>Loading...</div>");
			e.preventDefault();
			$.ajax(
			{
				url: social.file.rss,
				success: function(data)
				{
					$(social.contentSection).html(data);
				},
				error: function(xhr, textStatus, errorThrown)
				{
					error.warn(page.httpStatus(xhr.status) + ": " + social.file.articles);
				}
			});
		});
		
		$(social.button.defaultAction).click();
	}
};











var videoScript = "http://www.pallensmith.com/resources/scripts/videos.js";



function wait(func, fromFile)
{

	var theloop = setTimeout( function()
	{
		if(wijet.loaded === true)
		{
			if(fromFile != 'undefined' && fromFile != "" && typeof(fromFile) != 'undefined')
			{
				$.getScript(fromFile, function(data, textStatus) { func(); });
			}
			else
			{
				func();
			}
			clearTimeout(theloop);
		}
		else
		{
			wait(func, fromFile);
		}
	}, 500);
}




















var mnt =
{
	init: function(id)
	{
/*		HIDE TRACKER BECAUSE NOBODY LIKES IT

		id = (id == 'undefined') ? '' : id;
//		if(browser.b().name == "ie") $(mnt.tracker).css("right", parseInt($(this).css("right"))+300+"px");
		var u = prefs.basename + prefs.paths.resourceDirectory + prefs.paths.php + "mnt.php?id=" + id;
		$.ajax(
		{
			url: u,
			success: function(data)
			{
				$(mnt.tracker).html(data);
//				if(browser.b().name == "ie") window.alert($(mnt.tracker).offset().left); //, parseInt($(this).css("right"))+300+"px");
			},
			error: function(xhr, textStatus, errorThrown) 
			{
				error.warn(page.httpStatus(xhr.status));
			}
		});
		$(mnt.tracker).css({opacity:mnt.startOpacity});
		$(mnt.tracker).click( 
			function()
			{
				$(this).animate({opacity:mnt.endOpacity, marginRight:"0px"}, 300);
			},
			function()
			{
				$(this).animate({opacity:mnt.startOpacity, marginRight:mnt.hiddenMargin}, 300);
			});
*/	},
	tracker: "#newsletter_tracker",
	startOpacity: 0.5,
	endOpacity: 1.0,
	hiddenMargin: "-500px"
};

