/*	cleaners	*/
w = window, w.d = d = document, w.d.h = d.h = h = document.getElementsByTagName("head")[0], w.d.b = d.b = b = document.body;

var FangJS = fJS = {
	version:"0.99.99.99 rc1",
	DOM : {
		eTargetFix: function(passedEvent){
			var eT = passedEvent.target
			eT.nodeType === 3 && (eT = eT.parentNode);
			return eT;
		},
		Remove: function(DOMEle){
//			this.Remove.fName="Remove";
			if(DOMEle.length != null){
				for(var i = DOMEle.length-1;i >= 0;i--){
					DOMEle[i].parentNode.removeChild(DOMEle[i]);
				}
			} else {
				/*
				removeEvents
				*/
				DOMEle.parentNode.removeChild(DOMEle);
				delete DOMEle;
			}
		},
		giveMeAllYouGot: function(dumpAllBool){
			this.giveMeAllYouGot.fName="gMaYG";
			this.HoldingCell[0].appendChild(this.HoldingCell[1]);
			this.HoldingCell=[null,null];
		},
		giveMeA: function(DOMaction, putHere, IDClassName, objSelf, domEle, content, callMeBack){
			this.giveMeA.fName="gMa";
			!domEle && (domEle = "div");
			
			objSelf = d.createElement(domEle);

			if(IDClassName != false){
				if (typeof IDClassName == "string") {
					objSelf.setAttribute("class", IDClassName);
				} else if (typeof IDClassName == "object"){
					var comAttrLength = IDClassName.length;
					for(var i = comAttrLength;i >= 0;i--){
						if(IDClassName[i]){
							if(i == 0){
								objSelf.setAttribute("id", IDClassName[0]);
							} else if(i == 1){
								objSelf.setAttribute("class", IDClassName[1]);
							} else if(i == 2){
								objSelf.setAttribute("name", IDClassName[2]);
							} else if(i == 3 || i > 3){
								objSelf.setAttribute(IDClassName[i][0], IDClassName[i][1]);
							}
						}
					}
				}
			}

			if(domEle === "img"){
				objSelf.src = content;
			} else {
				content && (objSelf.innerHTML = content);
			}

			if(DOMaction == "Hold"){
				this.HoldingCell = [putHere,objSelf];
			} else if (DOMaction == "Addition" || DOMaction == "Straight"){
				putHere.appendChild(objSelf);
			}
			
			if(callMeBack){
				callMeBack(objSelf);
			}
			return objSelf;
		},
		Empty: function(DOMEle){
			this.Empty.fName="Empty";
			if(typeof DOMEle == "string"){
				DOMEle = d.getElementById(DOMEle);
			} else if(typeof DOMEle == "object"){
				// nothing to do here
			}
			
			while(DOMEle.hasChildNodes()){
				DOMEle.removeChild(DOMEle.firstChild);
			}
		}
	},
	CONSTANTS : {
		orientationChangable: (function(){
			if(typeof w.orientation == true){
				return true;
			} else {
				return false;
			}
		})(),
		touchy: (function(){
			if(typeof w.ontouchstart == "object"){
				return true;
			} else {
				return false;
			}
		})()
	},
	Listen:{
		howeverYouDoIt: function(objEle,whatAction,actionFunction){
			if(d.b.addEventListener){
				this.howeverYouDoIt = function(objEle,whatAction,actionFunction){
					if(!objEle.actions){
						objEle.actions = {};
					}
					objEle.actions[whatAction] = actionFunction;
					objEle.addEventListener(whatAction,objEle.actions[whatAction], false);
				}
			} else {
				this.howeverYouDoIt = function(objEle,whatAction,actionFunction){
					if(!objEle.actions){
						objEle.actions = {};
					}
					objEle.actions[whatAction] = actionFunction;
					objEle.attachEvent("on"+whatAction,objEle.actions[whatAction]);
				}
			}
			this.howeverYouDoIt(objEle,whatAction,actionFunction);
		},
		mouseDown: function(objEle,actionFunction){
			this.howeverYouDoIt(objEle,"mousedown", actionFunction);
		},
		fingerTouchStart: function(objEle,actionFunction){
			objEle.addEventListener("touchstart", actionFunction);
		},
		actionDown: function(objEle,actionFunction){
			if(fJS.CONSTANTS.touchy == true){
				this.actionDown = this.fingerTouchStart;
			} else {
				this.actionDown = this.mouseDown;
			}
			this.actionDown(objEle,actionFunction);
		}
	},
	modalCreater: function(DOMEle){
		var modalOverlay = fJS.DOM.giveMeA("Straight",d.b,["overlay"]);
		var modal = fJS.DOM.giveMeA("Straight",modalOverlay,"",this,"div",DOMEle.innerHTML);
		var modalClose = fJS.DOM.giveMeA("Straight",modal,["modalClose"],this,"div","&#215;");

		modal.style.left = (w.innerWidth - modal.offsetWidth)/2+"px";
		modal.style.top = (w.innerHeight - modal.offsetHeight)/2+"px";
		
	}
}


window.onload = function(){

	if(d.getElementById("indexView")){
		var article = d.getElementsByTagName("article")[0];
		fJS.Listen.actionDown(article, function(e){
			if(e.target === this){
				return false;
			}
			
			if (e.target.nodeType === 3) {
				var eT = e.target.parentNode;
			} else {
				var eT = e.target;
			}
			if(e.target.tagName =="P"){
				eT = eT.parentNode;
			}
			document.location = eT.parentNode.getElementsByTagName("a")[0].href;
		});
		
		var tabletEnable = d.getElementById("tabletInit");
		var htmlTag = d.getElementsByTagName("html")[0];
		fJS.Listen.actionDown(tabletEnable, function(e){
			if (e.target.nodeType === 3) {
				var eT = e.target.parentNode;
			} else {
				var eT = e.target;
			}
		
			if(htmlTag.className == false){
				htmlTag.className = "tablet";
				e.target.innerHTML = "Desktop View";
			} else {
				htmlTag.className = "";
				e.target.innerHTML = "Tablet View";
			}
		});
		tabletEnable.style.display = "block";
		
		var sectionsList = article.getElementsByTagName("section");
		for(var i = 0;i<sectionsList.length;i++){
			if( 3 === i % 4 ){
				sectionsList[i].className = "fourth";
			}
		}
		
	}

	if(d.getElementById("switch")){
		fJS.Listen.actionDown(d.getElementById("switch"), function(e){
			if(e.target.tagName === "IMG"){
/*
				e.target.style.display = "none";
				e.target.parentNode.getElementsByTagName("div")[0].style.display = "block";
				before we wanted to just display this in the place of what was clicked...
				now we still populate it in there we will just populate it into the modal dom
*/
				fJS.modalCreater(e.target.parentNode.getElementsByTagName("div")[0].cloneNode(true));
			}
		});
		
		fJS.Listen.actionDown(w, function(e){
			if(e.target.id === "overlay"){
				fJS.DOM.Remove(e.target);
			} else if(e.target.id === "modalClose"){
				fJS.DOM.Remove(e.target.parentNode.parentNode);
			}
		});
	}

	fJS.Listen.actionDown(d.getElementById("toTop"), function(){
		w.scrollTo(0,0);
		return false;
	});

}

