/*global $ */

var delmarc = {
	is: "a puerto rican who has come from NYC and always dreamed of doing very big things...", 
	isnt: "a typical person", 
	isThisBig: "thats what she said", 
	likesjQuery: "not so much... as it is I am taking out all jQ from my site soon...", 
	may: "run for mayor one day", 
	cant: "stop", 
	wont: "stop", 
	thinks: "alot", 
	rest: "a little", 
	wonders: "how many will read this and find the hidden stuff", 
	giveMeA: function (putHere, className, varName, domEle, content) {
		if (!domEle) {
			domEle = "div";
		}
		if (!varName) {
			varName = this;
		}
		varName = document.createElement(domEle);
		varName.setAttribute("class", className);
		
		if (typeof putHere == "string") {
			$(putHere).append(varName);
//			putHere = document.getElementByClassName(putHere) 
		} else if (typeof putHere == "function") {
//			alert('popo');
		} else {
			putHere.appendChild(varName);
		}
		
		if (content) {
			$(varName).append(content);
		}
		return varName;
	}, 
	setUp: function (startUp) {
	
		var container = this.giveMeA(document.body, "Container"),
		shifter = this.giveMeA(container, "Shifter"),
		content = this.giveMeA(shifter, "Content"),
		paralox = this.giveMeA(container, "ParaDiv");
//		return false;
		$.getJSON("json/ps.js",  function (pages) {
			for (var i in pages) {
				eval('var ' + i + ' = delmarc.giveMeA(content, "' + i + ' row")');
				for (var j in pages[i]) {
					for (var k in pages[i][j]) {
						delmarc.giveMeA("." + i, k + " diver", k + "section");
//							delmarc.giveMeA("." + i, "c");
						for (var l in pages[i][j][k]) {
							for (var m in pages[i][j][k][l]) {

								switch (m) {
									case "class":
										$("." + k).addClass(pages[i][j][k][l][m]);
										break;
									case "content":
										delmarc.giveMeA("." + k, "", k + "_" + m, "div", pages[i][j][k][l][m]);
										break;
									case "title":
										delmarc.giveMeA("." + k + "", "", k + "_" + m, "h2", pages[i][j][k][l][m]);
										break;
									default:
								}

							}
							delmarc.giveMeA("." + k, "opaquish");
						}						
					}
				}
			}
			
			var topFade = delmarc.giveMeA(container, "top fadeDiv"),
			leftFade = delmarc.giveMeA(container, "left fadeDiv"),
			rightFade = delmarc.giveMeA(container, "right fadeDiv"),
			bottomFade = delmarc.giveMeA(container, "bottom fadeDiv");
		
			var topArrow = delmarc.giveMeA(topFade, "top arrowDiv"),
			leftArrow = delmarc.giveMeA(leftFade, "left arrowDiv"),
			rightArrow = delmarc.giveMeA(rightFade, "right arrowDiv"),
			bottomArrow = delmarc.giveMeA(bottomFade, "bottom arrowDiv");
		
			delmarc.giveMeA(content, "c");
			delmarc.miniMeMap(content);
			$('.diver:first').click();

			delmarc.placeUs();
			
		});
		
	//	this.listen();
		this.clearMeOut(".Container");
		
	}, 
	getJasonPeters: function () {
		//	JSONP
	}, 
	doAregEx: function (whatKind,onWhat) {
	
		switch(whatKind){
			case "title":
				var onWhats = onWhat.split(" ");
				for(var i in onWhats){
					//	lets make it simple cause all I want is the one with the underscore
					if(onWhats[i].search(/_/) != -1){
						return onWhats[i];
					}
				}
				return false;
				break;
				
			default:
		}
		
		
	}, 
	listen: function (when) {

		$(".scaledSection .zoomNavText").click(function () {
			$(".scaledSection").stop().animate({height : "20%", width : "20%"}, 500);
			$(this).parent().children(".row").each(function () {
				var rowNewWidth = $(this).width() * 2;
				$(this).stop().animate({width : rowNewWidth}, 500);
				$(this).children(".diver").stop().animate({height : "0.45em", width : "0.8em"}, 500);
				$(this).parent().addClass("larger");
			});
			$(this).hide().siblings(".unZoomNavText").show();
		});
		
		$(".scaledSection .unZoomNavText").click(function () {
			$(".scaledSection").stop().animate({height : "10%", width : "10%"}, 500);
			$(this).parent().children(".row").each(function () {
				var rowNewWidth = $(this).width() / 2;
				$(this).stop().animate({width : rowNewWidth}, 500);
				$(this).children(".diver").stop().animate({height : "0.225em", width : "0.4em"}, 500);
				$(this).parent().removeClass("larger");
			});
			$(this).hide().siblings(".zoomNavText").show();
		});
		
		$(".diver").click(function () {
		
			if ( $(this).hasClass("focusedCell") ) {
				//	not doing anything... cant believe i didnt see this
			} else {
			
				var cellNum = $(this).parent(".row").children().index(this),
				rowNum = $(this).parents(".Content").children().index( $(this).parent() );

				$(".focusedRow").removeClass("focusedRow").children(".focusedCell").removeClass("focusedCell");

				$(".Content").each(function () {
					$(this).children(".row:eq(" + rowNum + ")").addClass("focusedRow").children(".diver:eq(" + cellNum + ")").addClass("focusedCell");

					var diverHeight = $(this).children(".row:eq(" + rowNum + ")").children(".diver:eq(" + cellNum + ")").height(),
					diverWidth = $(this).children(".row:eq(" + rowNum + ")").children(".diver:eq(" + cellNum + ")").width();
					
					if ( $(this).parent().hasClass("Shifter") ) {
						$(".Shifter").animate({
							left : ((diverWidth * cellNum) + ((diverWidth / 4) * cellNum)) * -1, 
							top : ((diverHeight * rowNum) + ((diverWidth / 4) * rowNum)) * -1
						}, 750);
						$(".ParaDiv").animate({
							left : (((diverWidth * cellNum) + ((diverWidth / 4) * cellNum)) * - 1) / 2, 
							top : (((diverHeight * rowNum) + ((diverWidth / 4) * rowNum)) * - 1) / 2
						}, 750);
					}
				});
				
				delmarc.isfakeTracking();
			
			}

			/*	at some point i will get around to doing this one
				if ($(this).hasClass("loaded")) {
					//	dont load stuff
				} else {
					//	load stuff
					$(this).addClass("loaded")
				}
			*/
		
		});
		
		$(".arrowDiv").click(function () {
			if ($(this).hasClass("bottom")) {
				$(".Container .focusedRow").next("div").children("div:eq(0)").click();
			} else if ($(this).hasClass("left")) {
				$(".Container .focusedCell").prev("div").click();
			} else if ($(this).hasClass("top")) {
				$(".Container .focusedRow").prev("div").children("div:eq(0)").click();
			} else if ($(this).hasClass("right")) {
				$(".Container .focusedCell").next("div").click();
			} else {
				alert("something funny is going on");
			}
		});
		
		$(document).keyup(function (e) {
			if (e.keyCode === 32) {
			//	delmarc.youBeDebuggin();
			} else if (e.keyCode === 13) {
			//	var niggaPalease = $(".dbingInput").val();
			//	delmarc.youBeDebuggin(niggaPalease);
			} else if (e.keyCode === 37) {
				$(".arrowDiv.left").click();
			} else if (e.keyCode === 38) {
				$(".arrowDiv.top").click();
			} else if (e.keyCode === 39) {
				$(".arrowDiv.right").click();
			} else if (e.keyCode === 40) {
				$(".arrowDiv.bottom").click();
			} else if (e.keyCode === 9) {
			}
		});

		$(".threeDMe").click(function () {
			$(this).hide().siblings(".unThreeDMe").show();
			delmarc.seeMeIn3D();
		});
		
		$(".unThreeDMe").click(function () {
			$(this).hide().siblings(".threeDMe").show();
			delmarc.myEyesHurt();
		});
		
		$(window).resize(function () {
			delmarc.placeUs("allOfUs");	
		});
		this.talkToMe();
		
	}, 
	clearMeOut: function (whoNeedsIt) {
		if (whoNeedsIt) {
			var cMe = this.giveMeA(whoNeedsIt, "c");
		} else {
			$(".cMe").each(function () {
				var CMe = this.giveMeA(this, "c");
			});
		}
	}, 
	isfakeTracking: function (causeIWantTo) {
	
		focusedLocation = this.doAregEx("title",$(".focusedCell").attr("class"));

		if(focusedLocation != false){
			var url = document.location + "#" + focusedLocation;
			var trackingPx = document.createElement("img");
			trackingPx.setAttribute("src", "http://c.statcounter.com/t.php?sc_project=2024689&resolution=" + screen.availWidth + "&h=" + screen.availHeight + "&camefrom=&u=" + escape(url) + "&t=UneeK1.com%20-%20Delmarc%20Eric%20Rosario&java=1&security=d48b8ce6&sc_random=" + Math.random(17) + "&sc_snum=1");
		} else {
//			alert("uh oh")
//			NO tracking
		}
		
	}, 
	talkToMe: function (likeLoversDo) {
		$(".contactBody .watermark").focus(function () {
			if (this.value === "subject" || this.value === "name or email") {
				$(this).val("");
			}
		});
		$(".contactBody textarea").focus(function () {
			if (this.value === "whats on your mind") {
				$(this).val("");
			}
		});
		$("form .submitBtn").click(function () {
			var email = $("form").serialize();
			$.post("ItSent.php4",  email,  function (responseText) {
				$("form").replaceWith(responseText);
			});
			return false;
		});
	}, 
	mathIsFun: function (whatKind, withWhat) {

		switch (whatKind) {
			case "rows":
				
				break;
			default:
			alert("what you want me to do with " + whatKind + " and " + withWhat);
		}

	}, 
	youBeDebuggin: function (whoNotMe) {
		
		if ( !$(document.body).children("div").hasClass("dbing") ) {
			var dbingContainer = this.giveMeA("body", "dbing");
			this.giveMeA(dbingContainer, "dbingInput", this, "input");
			$(".dbingInput").fadeIn(500).focus().val("");
			return dbingContainer;
		}
		
		if (whoNotMe) {
			switch (whoNotMe) {
				case "traceMe":
					if ( !traceMeEnabled ) {
//						$.getScript("js/d.js");
//						var traceMeEnabled = true;
					}
					break;
				case "empty":
					break;
				case "bsod":
					this.blueScreenOfDenial();
					break;
				case "callMe":
					alert("oh cool");
					break;
				default:
				alert("what you want me to do with " + whoNotMe + "\nI dont want it");
			}
			$(".dbingInput").val("");
		}
		
	}, 
	placeUs: function (who) {

		if (!who) {
			var dyCSS =	this.giveMeA(document.body, "dyCssDiv", this);
			$(".Container").fadeIn(1000);
			
			var rowNum = 0;
			$(".Content .row").each(function () {
				eval('var rowNumber' + rowNum + ' = this');
				var rowChildWidth = $(this).children().width(),
				rowChildMargin = rowChildWidth / 4,
				rowChildNumber = this.childNodes.length;
				$(this).width((rowChildWidth * rowChildNumber) + (rowChildMargin * rowChildNumber)).children().css("margin", "0px " + Math.floor(rowChildMargin) + "px " + Math.floor(rowChildMargin) + "px 0px");
				rowNum++;
			});
		}
		var contentVariH = $("body").height() * .05,
		contentW = $(".Content").width(),
		contentH = $(".Content").height(),
		containerW = $(".Container").width(),
		containerH = $(".Container").height(),
		bodyW =	$(document.body).width(),
		bodyH =	$(document.body).height(),
		diverW = $(".diver:first").width(),
		diverH = $(".diver:first").height();

		$(".dyCssDiv").empty().append("<style>.scaledSection{right:" + contentVariH + "px;} .ParaDiv{width:" + (containerW + contentW) + "px;height:" + (containerH + contentH) + "px;} .Container{left:" + (bodyW - containerW) / 2 + "px;top:" + (bodyH - containerH) / 2 + "px;} .Container .Content{left:" + ((containerW / 2) - (diverW / 2)) + "px;top:" + ((containerH / 2) - (diverH / 2)) + "px;}</style>");

	}, 
	miniMeMap: function (youCompleteMe) {
	
		var copy = $(youCompleteMe).html(),
		miniMeFull = this.giveMeA(document.body, "scaledSection Content", this, "", copy);
		$(miniMeFull).find(".diver").empty();
		
		var semiOpaque = this.giveMeA(miniMeFull, "semiOpaque"),
		zoomText = this.giveMeA(miniMeFull, "zoomNavText", this, "p", "click to 'zoom'"),
		unzoomText = this.giveMeA(miniMeFull, "unZoomNavText", this, "p", "click to 'unzoom'"),
		threeDMeText = this.giveMeA(miniMeFull, "threeDMe", this, "p", "3D Me Please"),
		unThreeDMeText = this.giveMeA(miniMeFull, "unThreeDMe", this, "p", "Un-3D Me Please");
			
		this.listen();
	}, 
	seeMeIn3D: function () {
		$("body").addClass("threeDeeMode");
		$(".Container .diver .opaquish").each(function () {
			var oRed = delmarc.giveMeA(this, "opaquishR"),
			oBlue = delmarc.giveMeA(this, "opaquishB");
		});
		
		$(".Container .diver h2").each(function () {
			var hTwoText = $(this).text();
			$(this).text("");
			var oRed = delmarc.giveMeA(this, "opaquishHTwoR", this, "span", hTwoText),
			oBlue = delmarc.giveMeA(this, "opaquishHTwoB", this, "span", hTwoText);
		});
	}, 
	myEyesHurt: function () {
		$("body").removeClass("threeDeeMode");
		$(".Container .diver .opaquish").each(function () {
			$(this).children().remove();
		});
		
		$(".Container .diver h2").each(function () {
			var hTwoText = $(this).children(":first").text();
			$(this).text(hTwoText);
		});
	}, 
	whatAreYou: navigator.userAgent, 
	blueScreenOfDenial: function () {
	
		this.blueScreenOfDenial.howWillYouSolveThis = function (yourMethod) {
			
			var browserList = ['firefox', 'chrome', 'msie', 'safari'];
			
			var isItThisBrowser = /firefox/i;
			alert(delmarc.whatAreYou + " and is this here " + isItThisBrowser.exec(delmarc.whatAreYou));
		
			switch (yourMethod) {
				case "download":
					
					break;
				case "LoadTheGameBitch":
					
					break;
			}
		};
	
		if (document.removeEventListener) {
			document.removeEventListener("keyup", delmarc.listen, false);
		} else {
			document.detachEvent("keyup", delmarc.listen);
		}
		
		$(document).unbind("keyup");
		
		var everythingOnThePage = document.getElementsByTagName("body")[0];
		while (everythingOnThePage.hasChildNodes()) {
			everythingOnThePage.removeChild(everythingOnThePage.firstChild);
		}
		
		document.head = document.getElementsByTagName('head')[0];
		
		var looks = document.getElementById("looksCSS").setAttribute("href", "css/bsod.css"),
		errorContainer = this.giveMeA(document.body, "errorContainer"),
		regExedBrowser = this.doAregEx(this.whatAreYou);
		
		$.getJSON("json/bsod.js",  function (sections) {
			for (var i in sections) {
				if (i == "choices") {
					var choicesDiv = delmarc.giveMeA(errorContainer, "choicesContainer", this, "div");
					for (var j in sections[i]) {
						delmarc.giveMeA(choicesDiv, "choices", this, "p", sections[i][j]);
					}
				} else {
					delmarc.giveMeA(errorContainer, "section", this, "div", sections[i]);
				}
			}
			$(".choices").click(function () {
				var choiceText = $(this).children("span").text();
				delmarc.blueScreenOfDenial.howWillYouSolveThis(choiceText);
			});
		});
	}
};

window.onload = function () {
	delmarc.setUp("startUp");
};

