/* Author: Blaine Jester
 * E-mail: me@blainejester.com
 * For: www.bluinpark.com
 * Date: September 2010
 */

	
(function(){
	var Core, window = this, _Core = window.Core;
	Core = window.Core = function() {
		var Priv = {
			cookies:{
				get: function (cn){var cv=document.cookie; var csa=cv.indexOf(" "+cn+"="); if(csa== -1){csa=cv.indexOf(cn+"=");} if(csa == -1){cv=null;} else{csa=cv.indexOf("=", csa)+1; var cea=cv.indexOf(";", csa); if(cea== -1){cea=cv.length;} cv=unescape(cv.substring(csa, cea));} return cv;},
				set: function(cn, cv, cp, ce){cv=escape(cv); if(ce==""){var nD=new Date(); nD.setMonth(nD.getMonth() + 6); ce=nD.toGMTString();} if(cp!=""){cp=";Path="+cp;} document.cookie=cn+"="+cv+";expires="+ce+cp;}
			},
			cache:{
			},
			form:{
				sent:"",
				_divsub:function(){
					var html = "<span>Sending...</span><br/><img src='images/loading.gif' />";
					$("#form").html(html).css({textAlign:"center"}).addClass("postSubForm");
				},
				_divfail:function(){
					var html = "<span>Submission Failed!</span><br/><a href='javascript:;' onclick='core.form.retry();'>Click Here To Try Again</a>";
					$("#form").html(html).css({textAlign:"center"});
				},
				initSubmission:function(){
					this._divsub();
					var me = this, subd = {}, er = Pub.form._cache;
					this.response = FORM.response;
					for(var a in er) {
						subd[a] = {
							val:er[a].val,
							ques:er[a].ques
						};
					}
					var dd = JSON.stringify(subd);
					$.post("subform.php",{name:Pub.form.name, data:dd},function(d, ts){
						if(d.indexOf("SUCCESS") >= 0) {
							me.sent = subd;
							me.postSub();
						}
						else {
							me._divfail();
						}
					});
				},
				postSub:function(){
					var nD = new Date();
					nD.setHours(nD.getHours() + 6); 
					nD = nD.toGMTString();
					//Priv.cookies.set(Pub.form.name, "Sent", "/",nD);
					this.html = "<div>";
					for(var k in this.sent) {
						this.html += "<span>"+this.sent[k].ques+"</span><ul><li>"+this.sent[k].val+"</li></ul><hr/>";
						this.response = (this.response.split("%"+k+"%")).join(this.sent[k].val);
					}
					this.html += "</div>";
					this.response = (this.response.split("%newline%")).join("<br/>");
					Priv.cookies.set(Pub.form.name, this.response, "/",nD);
					$("#form").html(this.response+'<hr/><a href="javascript:;" onclick="core.form.showSubmission();">Click Here To See Your Submission</a>').css({textAlign:"left"});
				}
			}
		};
		var Pub = {
			setLang:function(A){Priv.cookies.set("lang",A,"/","");},
			show:function(A) {
				var B, EL;
				var cache = Priv.cache;
				if(typeof A == "number") {
					B = cache[cache._keys[A]];
					EL = $("#"+cache._keys[A]);
				}
				else if(typeof A == "string") {
					B = cache[A];
					EL = $("#"+A);
				}
				else {
					return false;
				}
				
				if(typeof B == "undefined" || B == null || !EL.is("*")) {
					return false;
				}
			
				if(!EL.hasClass("showing")) {
					EL.addClass("showing");
					(B.animate) ? (EL.css(B.first).animate(B.show)) :(EL.css(B.first).css(B.show));
					B.first = {};
				}
				else {
					EL.removeClass("showing");
					(B.animate) ? (EL.animate(B.hide)) :(EL.css(B.hide));
				}
				return false;
			},
			init:function() {
				window['show'] = this.show;
				var me = this;
				$(function(){
					me.form.init();
					me.maps.init();
				});
				
			},
			form:{
				name:"",
				_cache:{
					
				},
				init:function(){
					if(!isN(window['FORM'])) {
						this.name = FORM.name;
						var testC = Priv.cookies.get(this.name);
						if(testC != null) {
							$("#form").html(testC).addClass("postSubForm");
							return;
						}
						var a, b, c, me = this;
						$("input, textarea").each(function(){
							a = $(this);
							b = ((FORM.format != "B") ?($(this).closest("tr").find("td:first-child")) :($(this).prev()));
							if(!isN(me._cache[a.attr("id")])) {
								a.attr("id","_"+a.attr("id"));
							}
							c = me._cache[a.attr("id")] = {
								dom: a,
								req: a.hasClass("required"),
								ques: $.trim(b.text()).trimC(),
								spec: (a.attr("class").split("-"))[1] || "NO",
								val: "",
								max:(a.attr("class").split("_"))[1],
								ta:a.is("textarea")
							};
							a.attr("title",c.ques);
						});
						$("#form").append("<button onclick='core.form.submit()'>"+FORM.button+"</button>");
					}
				},
				_err:function(A){
					A.dom.focus().addClass("indicatereq");
					setTimeout(function(){A.dom.removeClass("indicatereq");},3000);
				},
				showSubmission:function(){
					$("#form").html(Priv.form.response+"<hr/>"+Priv.form.html);
				},
				_submitted:false,
				submit:function(){
					var q, test, me = this;
					var reg = /\$|\#|\%|\^|\&|\*|\`|\"|\<|\>|\~|\]|\[|\{|\}/;
					
					for(var p in this._cache) {
						q = this._cache[p];
						q.val = $.trim(q.dom.val());
						test = q.val.match(reg);
						if(test != null && test.length > 0) {
							alert("Please remove all invalid characters!\n < > $ # % ^ & * \" ` ~ [ ] { }");
							this._err(q);
							return;
						}
						else if(!isNaN(q.max) && q.val.length > q.max) {
							alert("Please limit the indicated field to "+q.max+" characters.");
							this._err(q);
							return;
						}
						else if(q.req && q.dom.val() == "") {
							alert("Please fill in the indicated form field.");
							this._err(q);
							return;
						}
						else if(q.spec == "email" && (q.val.indexOf("@") < 0 || q.val.indexOf(".") < 0)) {
							alert("Please enter a valid e-mail address!");
							this._err(q);
							return;
						}
						if(q.ta) { q.val = q.val.nlbr();}
					}
					this._submitted = true;
					Priv.form.initSubmission();
				},
				retry:function(){if(this._submitted){Priv.form.initSubmission();}}
			},
			debug:function(){return Priv;},
			maps: {
				_address:"2189 Highway 78, Ocotillo Wells, CA 92004",
				_map:{},
				_geocoder:{},
				_center:{},
				_options:{
					zoom: 15,
					center: null,
					mapTypeId: null
				},
				_init:function(){
					this._options.center = (new google.maps.LatLng(33.125306, -116.044475));
					this._options.mapTypeId = google.maps.MapTypeId.ROADMAP;
					this._map = new google.maps.Map(document.getElementById(INSERTMAPS), this._options);
					this._marker = new google.maps.Marker({
						map: this._map, 
						position: this._options.center,
						title:"The Law Offices of David Pflaum"
					});
					var tt = this._address.split(", ");
					this._infowindow = new google.maps.InfoWindow({
						content: "<div class='infowindow'>The Blu-In Park<hr/>"+tt[0]+"<br />"+tt[1]+", "+tt[2]+'<br /><a title="Get Directions" href="http://maps.google.com/maps?hl=en&q=33.125306,-116.044475" rel="external">Get Directions</a></div>'
					});
					this._infowindow.open(this._map, this._marker);
				},
				init:function(){
					if(!isN(window["INSERTMAPS"])) {
						var me = this
						this._geocoder = new google.maps.Geocoder();
						this._geocoder.geocode( { 'address': this._address}, function(results, status) {
							if (status == google.maps.GeocoderStatus.OK) {
								me._center = results[0].geometry.location;
								$(function(){
									me._init();
								});
							} 
							else {
								alert("Google Maps Geocode was not successful for the following reason: " + status);
							}
						});
						
					}
				}
			}
		};
		return Pub;
	};
	
})();
	
String.prototype.trimC = function() {
	var me = this;
	return me.substring(0,(this.length - 1));
};
String.prototype.nlbr = function() {
	return this.replace(/\r\n|\n/g,"<br/>");
};
function isN(S) {
	if(typeof S == "undefined" || S == null || S == undefined || S == "" || S == false) {
		return true;
	}
	return false;
}




	
	
	

