﻿var c, d, svgCode = new Array(), svgCode2 = new Array(), counter, draw = false, drawx, drawy;

google.load("jquery", "1");

google.setOnLoadCallback(function() 
{
	convertJargonLinks();
	if($("#twitterStatus p").innerHTML == "")
		$("#twitterStatus").hide();
	
	//$.getScript("css-gradients-via-canvas.js");
	$.getScript("jquery.easing.1.3.js");
	$.getScript("raphael.js", function()
	{
		$(window).resize(function()
		{
			console.log("window resized");
			var topSep = $('#seperator1');
			var leftV = topSep.offset().left - drawx;
			var topV = topSep.offset().top + 2 - drawy;
			console.log("Left: " + leftV + "; Top: " + topV);
			document.getElementById('drawingTag').style.marginLeft = leftV + "px";
			document.getElementById('drawingTag').style.marginTop = topV + "px";
		});
		doRaphaelStuff();
	});
	
});

function convertJargonLinks()
{
	$("#jargonDiv a[href]").each(function()
    {
		this.href = "javascript:loadJargon('" + this.href.substr(this.href.indexOf("?") + 1) + "');";
	});
}

function loadJargon(url)
{
	var jargonDiv = $("#jargonDiv");
	
	$('html,body').animate({scrollTop: 0}, 1000, "easeOutQuad");
	jargonDiv.load("index.xmlhttp.php", "action=getNewJargon&" + url, function()
	{
		convertJargonLinks();
		$('html,body').animate({scrollTop: jargonDiv.offset().top}, 1000, "easeOutQuad");
	}); 
}

function doRaphaelStuff()
{
	var r = 10;
	var R = .5522 * r;
	var topSep = $('#seperator1');
	var paper = Raphael("drawingTag", document.documentElement.clientWidth, document.documentElement.clientHeight);
	drawx = topSep.offset().left;
	drawy = topSep.offset().top + 2;
	
	var currentDirection = "l";
	var currentDirection2 = "l";
	
	var directions = new Array();
	directions = {
		'u': new Array('r', 'l'),
		'l': new Array('u', 'd'),
		'd': new Array('l', 'r'),
		'r': new Array('d', 'u')
	};
	var directions2 = new Array();
	directions2 = {
		'u': new Array('l', 'r'),
		'l': new Array('d', 'u'),
		'd': new Array('r', 'l'),
		'r': new Array('u', 'd')
	};
	
	var actions = new Array();
	var actions = {
		lu: ' c' + (-R) + " 0 " + (-r) + " " + (-(r - R)) + " " + (-r) + " " + (-r),
		ld: ' c' + (-R) + " 0 " + (-r) + " " + (r - R) + " " + (-r) + " " + (r),
		ru: ' c' + (R) + " 0 " + (r) + " " + (-(r - R)) + " " + (r) + " " + (-r),
		rd: ' c' + (R) + " 0 " + (r) + " " + (r - R) + " " + (r) + " " + (r),
		ur: ' c 0 ' + (-R) + " " + (-(R - r)) + " " + (-r) + " " + (r) + " " + (-r), 
		ul: ' c 0 ' + (-R) + " " + (R - r) + " " + (-r) + " " + (-r) + " " + (-r), 
		dr: ' c 0 ' + (R) + " " + (-(R - r)) + " " + (r) + " " + (r) + " " + (r), 
		dl: ' c 0 ' + (R) + " " + (R - r) + " " + (r) + " " + (-r) + " " + (r), 
	};
	
	d = paper.path({stroke: "#999", "stroke-width": 4}, "M"+drawx+" "+drawy+"L"+drawx+" "+drawy+"");
	c = paper.path({stroke: "#333", "stroke-width": 4}, "M"+drawx+" "+drawy+"L"+drawx+" "+drawy+"");
	for(i = 0; i < 50; i++)
	{
		s = Math.floor(Math.random()*2);
		
		svgCode[i] = actions[currentDirection + directions[currentDirection][s]];
		svgCode2[i] = actions[currentDirection2 + directions2[currentDirection2][s]];
		currentDirection = directions[currentDirection][s];
		currentDirection2 = directions2[currentDirection2][s];
	}
	
	for(i = 0; i < 4; i++)
	{
		svgCode[svgCode.length] = actions[currentDirection + directions[currentDirection][0]];
		svgCode2[svgCode2.length] = actions[currentDirection2 + directions2[currentDirection2][0]];
		currentDirection = directions[currentDirection][0];
		currentDirection2 = directions2[currentDirection2][0];
	}
	svgCode[svgCode.length] = "";
	counter = 0
	animateLine();
}

function animateLine()
{
	draw = !draw;
	if(draw)
	{
		if(svgCode[counter] != "")
		{
			d.animate({path: d.attrs.path + svgCode2[counter]}, 100, function() {
				animateLine();
			});
			c.animate({path: c.attrs.path + svgCode[counter]}, 100, function() {
				animateLine();
			});
			counter++;
		}
	}
}
