var MDS = {};
MDS.feed = {
	source: '',
	queries: [],
	url: function () {
		return [
			MDS.feed.source,
			MDS.feed.queries.join('')
		].join('');
	},
	pipes: function () {
		return [
			//Credit: theezpzway.com
			'http://pipes.yahoo.com/pipes/pipe.run',
			'?_id=NvfW_c9m3hGRjX4hoRWqPg',
			'&_render=json',
			'&_callback=?&feed=',
			MDS.feed.source,
			MDS.feed.queries.join('')
		].join('');
	},
	grab: function (s, q, f, t) {
		/*	s = feed source
		 *	q = feed query string
		 *  f = callback function
		 *	t = feed type ('rss' or 'json')
		 */
		if (s.charAt(s.length - 1) == '/' && q) {
			s = s.substring(0, s.length - 1);
		}
		MDS.feed.source = s;
		MDS.feed.queries = $.isArray(q) ? q : [q];
		var uri = t && t == 'json' ? MDS.feed.url() : MDS.feed.pipes();
		$.ajax({
			url: uri,
			dataType: 'json',
			success: function (json, status) {
				f(json);
			}
		});
	}
};
MDS.weather = {
	classes: [
		'partly',
		'sun',
		'cloud',
		'rain',
		'wintry',
		'snow',
		'fair'
	],
	tasks: function (json) {
		var weather = json.value.items['0']['yweather:condition'],
			temp = weather.temp,
			desc = weather.text.toLowerCase(),
			classes = MDS.weather.classes;
		for (var i = 0, l = classes.length; i < l; i++) {
			if (desc.indexOf(classes[i]) > -1) {
				$(document.body).addClass(classes[i]);
				break;
			}
		}
		if ($(document.body).hasClass('nox') && desc == 'fair') {
			desc = 'clear';
		} else if (desc == 'fair') {
			desc = 'sunny';
		}
		$('#weather span:first').html('<em>' + temp + '<\/em> and <em>' + desc + '<\/em> in Brooklyn');
		Cufon.refresh($('#weather span:first'));
	},
	apply: function () {
		var yweather = 'http://weather.yahooapis.com/forecastrss/',
			brooklyn = '?w=2520841'; //Williamsburg WOEID
		MDS.feed.grab(yweather, brooklyn, MDS.weather.tasks, 'rss');
	}
};
MDS.prspc = {
	aov: 50, // NORMAL LENS
	eff: (1 / 2),	
	distort: function (theta, pageL, pageW) {
		/*	WHERE:
		 * 	theta = angle of view
		 *	pageL = x coordinate of front right corner of grid
		 *	pageW = client width
		 */
		theta = theta || MDS.prspc.aov;
		pageW = pageW || $(window).width();
		var t = theta * Math.PI / 180,
			L = pageL,
			W = pageW,
			a = Math.atan(Math.tan(t / 2) * (2 * L / W - 1));
		return Math.floor(a * 180 / Math.PI);
	},
	flatten: function (coord, theta, pageL, pageW) {
		/*	WHERE:
		 *	coord = point as [height, width, depth]
		 * 	theta = angle swept away from picture plane (degrees)
		 *	pageL = x coordinate of front right corner of grid
		 *	pageW = client width
		 */
		pageW = pageW || $(window).width();
		if (MDS.prspc.aov) {
			theta = theta - MDS.prspc.distort(null, pageL, pageW);
		}
		var h = coord[0],
			w = coord[1],
			d = coord[2],
			t = theta * Math.PI / 180,
			fsw = w * Math.cos(t),
			fsd = d * Math.sin(t),
			L = pageL,
			W = pageW,
			eff = MDS.prspc.eff;
			vpl = eff * (L + W) / Math.tan(t),
			vpr = eff * (2 * W - L) * Math.tan(t),
			x = L + Math.floor(fsd - fsw),
			y = Math.floor(h * (vpl - fsw) * (vpr + fsw - fsd) / vpl / (vpr + fsw));
		return [x, y];
	}
};
MDS.draw = {
	ctx: null,
	init: function (id) {
		var canvas = document.getElementById(id || 'buildings');
		if (canvas.getContext) {
			canvas.width = $(window).width();
			canvas.height = $(window).height();
			MDS.draw.ctx = canvas.getContext('2d');
		}
	},
	buildings: function () {
		var w = $(window).width(),
			h = $(window).height(),
			f = [],
			noxBool = $(document.body).hasClass('nox');
		var windowGrad = MDS.draw.ctx.createLinearGradient(w - 585,h - 460,w,h),
			liteOnGrad = MDS.draw.ctx.createLinearGradient(w - 585,h - 460,w,h),
			foregdGrad = MDS.draw.ctx.createLinearGradient(w - 585,h - 460,w,h),
			backgdGrad = MDS.draw.ctx.createLinearGradient(w,h - 460,0,h),
			darkenGrad = MDS.draw.ctx.createLinearGradient(0,h - 460,0,h),
			nose = 'rgba(104, 84, 84, .75)',
			bands = 'rgba(192, 186, 194, 1)',
			brick = 'rgba(200, 100, 100, 1)';
		if (noxBool) {
			windowGrad.addColorStop(0, 'rgba(0, 0, 0, 1)');
			windowGrad.addColorStop(1, 'rgba(0, 0, 64, .25)');
			liteOnGrad.addColorStop(0, 'rgba(255, 255, 200, 1)');
			liteOnGrad.addColorStop(1, 'rgba(255, 255, 255, .625)');
			foregdGrad.addColorStop(0, 'rgba(0, 0, 64, 1)');
			foregdGrad.addColorStop(1, 'rgba(0, 0, 0, 1)');
			backgdGrad.addColorStop(0, 'rgba(0, 0, 64, 1)');
			backgdGrad.addColorStop(1, 'rgba(0, 0, 0, 1)');
			darkenGrad.addColorStop(0, 'rgba(0, 0, 64, 1)');
			darkenGrad.addColorStop(1, 'rgba(0, 0, 0, 1)');
			nose = 'rgba(0, 0, 32, 1)';
			bands = 'rgba(0, 0, 32, 1)';
			brick = 'rgba(0, 0, 64, 1)';
		} else {
			windowGrad.addColorStop(0, 'rgba(0, 0, 0, .5)');
			windowGrad.addColorStop(1, 'rgba(255, 255, 255, .75)');
			liteOnGrad.addColorStop(0, 'rgba(0, 0, 0, .5)');
			liteOnGrad.addColorStop(1, 'rgba(255, 255, 255, .75)');
			foregdGrad.addColorStop(0, 'rgba(255, 248, 255, .75)');
			foregdGrad.addColorStop(1, 'rgba(192, 186, 194, 1)');
			backgdGrad.addColorStop(0, 'rgba(192, 186, 194, .75)');
			backgdGrad.addColorStop(1, 'rgba(84, 72, 78, .875)');
			darkenGrad.addColorStop(0, 'rgba(112, 96, 104, .5)');
			darkenGrad.addColorStop(1, 'rgba(84, 72, 78, .875)');
		}
		MDS.draw.ctx.clearRect(0, 0, w, h);
		f[0] = MDS.Poly([[0,470,80],[460,470,80],[460,585,80],[0,585,80]], foregdGrad).draw();
		f[1] = MDS.Poly([[0,470,80],[460,470,80],[460,470,220],[0,470,220]], darkenGrad).draw();
		f[2] = MDS.Poly([[0,220,220],[420,220,220],[420,470,220],[0,470,220]], backgdGrad).draw();
			//WINDOWS
			f[3] = MDS.Poly([[5,335,220],[85,335,220],[85,455,220],[5,455,220]], windowGrad).draw();
			f[4] = MDS.Poly([[135,335,220],[215,335,220],[215,455,220],[135,455,220]], liteOnGrad).draw();
			f[5] = MDS.Poly([[265,335,220],[345,335,220],[345,455,220],[265,455,220]], windowGrad).draw();
			//NOSE
			f[6] = MDS.Poly([[345,220,220],[420,220,220],[420,470,220],[345,470,220],[345,415,220],[0,415,220],[0,375,220],[345,375,220],[345,315,220],[0,315,220],[0,275,220],[345,275,220]], nose).draw();
		f[7] = MDS.Poly([[0,40,60],[420,40,60],[420,220,60],[0,220,60]], foregdGrad).draw();
			//WINDOWS
			f[7] = MDS.Poly([[0,150,60],[55,150,60],[55,200,60],[0,200,60]], windowGrad).draw();
			f[8] = MDS.Poly([[105,150,60],[195,150,60],[195,200,60],[105,200,60]], windowGrad).draw();
			f[9] = MDS.Poly([[275,150,60],[335,150,60],[335,190,60],[275,190,60]], liteOnGrad).draw();
			f[10] = MDS.Poly([[245,80,60],[335,80,60],[335,130,60],[245,130,60]], windowGrad).draw();
			//BANDS
			f[11] = MDS.Poly([[55,40,60],[85,40,60],[85,220,60],[55,220,60]], bands).draw();
			f[12] = MDS.Poly([[195,40,60],[225,40,60],[225,220,60],[195,220,60]], bands).draw();
			f[13] = MDS.Poly([[335,40,60],[365,40,60],[365,220,60],[335,220,60]], bands).draw();
		f[14] = MDS.Poly([[0,-210,0],[h-128,-210,0],[h-128,40,0],[0,40,0]], brick).draw();
	}
};
MDS.Poly = function (coords, fill) {
	return {
		fill: fill || 'rgba(192, 186, 194, 1)',
		texture: '',
		stroke: '',
		coords: coords || [],
		theta: 50,
		pageL: $(window).width() - $('#portfolio').width(),
		draw: function () {
			if (!MDS.draw.ctx) {
				MDS.draw.init();
			}
			var ctx = MDS.draw.ctx,
				i,
				c = this.coords,
				l = c.length,
				p = [],
				f = MDS.prspc.flatten;
			//CLEAR EXISTING FOR NEW POLY
			ctx.fillStyle = 'rgb(0, 0, 0)';
			ctx.globalCompositeOperation = 'destination-out';
			ctx.beginPath();
			for (i = 0; i < l; i++) {
				p[i] = f(c[i], this.theta, this.pageL);
				if (i == 0) {
					ctx.moveTo(p[i][0], $(window).height() - p[i][1]);
				} else {
					ctx.lineTo(p[i][0], $(window).height() - p[i][1]);
				}
			}
			ctx.closePath();
			ctx.fill();
			//DRAW NEW POLY
			ctx.fillStyle = this.fill;
			ctx.globalCompositeOperation = 'darker';
			ctx.beginPath();
			for (i = 0; i < l; i++) {
				p[i] = f(c[i], this.theta, this.pageL);
				if (i == 0) {
					ctx.moveTo(p[i][0], $(window).height() - p[i][1]);
				} else {
					ctx.lineTo(p[i][0], $(window).height() - p[i][1]);
				}
			}
			ctx.closePath();
			ctx.fill();
		}	
	};
};
MDS.yiddish = function () {
	var alefbeys = {
		//Alef-initial
		"'i":'אי',"'oy":'אוי',"'ay":'אײַ',"'ey":'איי',
		//Yod dipthongs
		'oy':'אוי','ay':'ײַ','ey':'יי',
		//Semi-vowels
		'yi':'ייִ','vu':'וווּ',
		//Digraphs
		'tz':'צ','sh':'שׂ',
		//The whole megillah
		'a':'א','o':'אָ','b':'ב','g':'ג','d':'ד','h':'ה','v':'וו','u':'ו','z':'ז','H':'ח','t':'ט','y':'י','i':'י','k':'כּ','K':'כ','l':'ל','m':'מ','n':'נ','S':'ס','e':'ע','p':'פּ','f':'פֿ','q':'ק','r':'ר','s':'ש','T':'תּ'
	};
	var s = $('#yiddish h4').text();
	for (yinglish in alefbeys) {
		s = s.replace(yinglish, alefbeys[yinglish]);
	}
	//Repeat to catch UTF errors
	for (yinglish in alefbeys) {
		s = s.replace(yinglish, alefbeys[yinglish]);
	}
	$('#yiddish h4').text(s);
};

MDS.thoughts = function () {
	$('#thoughts p:eq(1)').append('<a id="read-more">read&nbsp;more<\/a>'); 
	$('#thoughts p:last').append('<a id="collapse">collapse<\/a>');
	$('#read-more').data({'recall': $('#thoughts').height()});
	$('#thoughts').width(284);
	$('#thoughts').height($('#thoughts h3').height() + $('#thoughts h4').height() + $('#thoughts p:first').height() + $('#thoughts p:eq(1)').height() + 32);
	$('#collapse').data({'recall': $('#thoughts').height()});
	$('#read-more').click(function () {
		$(this).hide();
		$('#thoughts').animate(
			{
				width: 600,
				height: $('#read-more').data('recall') + 8
			},
			{queue: false, duration: 250, easing: 'linear'}
		);
	});
	$('#collapse').click(function () {
		$('#read-more').show();
		$('#thoughts').animate(
			{
				width: 284,
				height: $('#collapse').data('recall')
			},
			{queue: false, duration: 250, easing: 'linear'}
		);
	});
};

MDS.featured = function () {
	$('#featured p').hide();
	$('#featured img:not(:first)').slideUp(0);
	$('#featured').css({
		height: $('#featured').outerHeight()
	});
	$('#featured img').click(function () {
		$('#desc').hide();
		$('#desc').text($(this).next().attr('alt') || $('#featured img:first').attr('alt'));
		$(this).slideUp(150);
		if ($(this).next().attr('src')) {
			$(this).next().slideDown(150);
		} else {
			$('#featured img:first').slideDown(250);
		}
		$('#desc').show();
	});
};

MDS.cooking = function () {
	$('#cooking img').click(function (e) {
		if (!document.getElementById('enlarge')) {
			var enlarge = document.createElement('img');
			enlarge.src = $(this).attr('src');
			enlarge.id = 'enlarge';
			document.body.appendChild(enlarge);
			$('#enlarge').css({
				opacity: 0,
				bottom: $(window).height() - e.pageY + 16,
				right: $(window).width() - e.pageX + 16
			});
			if ($('#enlarge').position().left - $(window).scrollLeft() < 0) {
				$('#enlarge').animate(
					{width: e.pageX - $(window).scrollLeft() - 32},
					{queue: false, duration: 250}
				);
			} else if ($('#enlarge').position().top - $(window).scrollTop() < 0) {
				$('#enlarge').animate(
					{height: e.pageY - $(window).scrollTop() - 32},
					{queue: false, duration: 250}
				);
			}
			$('#enlarge').fadeTo(250, 1);
		}
	});
	$('#cooking img').mouseout(function () {
		if (document.getElementById('enlarge')) {
			window.setTimeout(function () {
				$('#enlarge').remove();
			}, 150);
		}
	});
};