window.addEvent('domready', function()
{
	// decorate outbound links //
	$$('a').each(function(el)
	{
		if (el.hasClass('stbutton')) return;
		
		el.addEvent('click', (function()
		{
			pageTracker._trackEvent('outbound', 'click', this.href);
			window.open(this.href);
			return false;
		}).bind(el));
	});

	
	addVideoEmbed();
	new FlickrFeed('willyouCU', $('photostream'));
	new FacebookFeed('53021875278', $('comments'));
	//new TwitterFeed('#willyou', $('comments'));
	new DesktopCreator();
	
	$('caplink').addEvent('mouseenter', function(event){ event.target.src = 'img/bar_futurestudentinfo_over.gif'; });
	$('caplink').addEvent('mouseleave', function(event){ event.target.src = 'img/bar_futurestudentinfo.gif'; });
	
	$('caplink2').addEvent('mouseenter', function(event){ event.target.src = 'img/bar_applynow_over.gif'; });
	$('caplink2').addEvent('mouseleave', function(event){ event.target.src = 'img/bar_applynow.gif'; });
	
	$('logo').addEvent('mouseenter', function(event){ event.target.src = 'img/header_logo_over.gif'; });
	$('logo').addEvent('mouseleave', function(event){ event.target.src = 'img/header_logo.gif'; });
});


/*
*	Add the video via mootools
*/
function addVideoEmbed()
{
	var el = $('video-box');
	//el.getElement('a').setStyle('display','none');
	//el.getLast().setStyle('visibility','visible');
	
	// flash //
//	var swf = new Swiff('http://vimeo.com/moogaloop.swf',
//	{
//		id:'VideoPlayer',
//		width:640,
//		height:360,
//		params:{quality:'high', allowScriptAccess:'always'},
//		vars:{clip_id:'3545384',server:'vimeo.com',show_title:'0',show_byline:'0',show_portrait:'0',color:'342a86',fullscreen:'0'},
//		callBacks:{}
//	}); 
//	el.empty();
//	el.adopt(swf); 
	//3301284
	//3398752
	//3543228
	
	
	var swf = new Swiff('http://www.youtube.com/v/fEDuddGsIaY&hl=en&fs=1&hd=1',
	{
		id:'VideoPlayer',
		width:640,
		height:360,
		params:{quality:'high', allowScriptAccess:'always', allowFullScreen:'true'},
		vars:{},
		callBacks:{}
	}); 
	el.empty();
	el.adopt(swf); 
	//<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/lGgjitx0mHg&hl=en&fs=1&hd=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/lGgjitx0mHg&hl=en&fs=1&hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>
	
}


var DesktopCreator = new Class(
{
	colors	: ['purple', 'orange', 'blue', 'aqua', 'green', 'yellow', 'brown', 'gray'],
	
	initialize: function()
	{
		this.buttonbox = $('right-column').getElement('.showoff .colorbuttons');
		this.desktop   = $('right-column').getElement('.showoff .dotted');

		this.colors.each(this.bindButton.bind(this));
		
		this.form = $('form1');
		this.form.setProperty('target', '_new');
		this.form.addEvent('submit', this.onSubmit.bind(this));
		
		var txt = $(this.form.txt);

		txt.store('text', txt.value);
		txt.addEvent('focus', (function(){ if (this.value == this.retrieve('text')) this.value = ''; }).bind(txt));
		txt.addEvent('blur', (function(){ if (this.value == '') this.value = this.retrieve('text'); }).bind(txt));
	},
	
	onSubmit: function()
	{
		var url = '/desktop/' + this.form.bg.value + '/' + this.form.size.value + '/' + escape(this.form.txt.value);
		pageTracker._trackPageview(url);
		return true;
	},
	
	bindButton :function(color, index)
	{
		var button = new Element('a',{'class': color, href:'#', title:''});
		button.store('color', color);
		
		if (color == 'orange') button.addClass('selected');
		
		button.addEvent('click', this.onButtonClick.bind(this));
		
		this.buttonbox.grab(button);
	},
	
	onButtonClick: function(event)
	{
		var el = $(event.target);
		
		this.buttonbox.getChildren().removeClass('selected');
		el.addClass('selected');
		
		var color = el.retrieve('color');
		this.desktop.setProperty('class', 'dotted ' + color + 'bg');
		this.form.bg.value = color;
		
		if (color == 'yellow' || color == 'gray')
		{
			this.form.txt.setStyles({'color':'#342a86','border-color':'#342a86'});
		}
		else
		{
			this.form.txt.setStyles({'color':'#FFF','border-color':'#FFF'});
		}
		
		return false;
	}
});

var TwitterFeed = new Class(
{
	initialize: function(qry, el)
	{
		this.qry = qry;
		this.el = el;
		
		this.request = new Request.JSON({url:'search.json', onComplete: this.onComplete.bind(this)});
		this.request.get({'rpp':'10', 'callback':'this.parseJSON', 'ands': qry});
	},
	
	onComplete: function(obj, txt)
	{
		$('comments-loader').setStyle('display','none');
		this.data = eval(txt);
		this.data.results.each(this.addTweet.bind(this));
		
		// sorting hack //
		feedCount++;
		if (feedCount > 1) doSort();
	},
	
	addTweet: function(tweet, index)
	{
		// only the first five //
		if (index > 10) return;
		
		if (tweet.from_user == 'bvaeth') return;
		
		var div = new Element('div',{'class':'cropped fbdiv i' + this.el.getChildren().length});
		
		var a = new Element('a',{href:'http://twitter.com/' + tweet.from_user, 'class':'profile_pic'});
		a.addEvent('click', function(){pageTracker._trackEvent('outbound', 'click', 'twitter'); return true;});
		a.setProperty('target', '_new');
		div.grab(a);
		
		div.getFirst().grab(new Element('img',{src:tweet.profile_image_url, width:'36'}));
		
		
		var rcol = new Element('div',{style:'float:left;width:231px'});
		div.grab(rcol)
		
		rcol.addEvent('mouseenter', (function(){ this.removeClass('cropped'); }).bind(div));
		rcol.addEvent('mouseleave', (function(){ this.addClass('cropped'); }).bind(div));
		
		a = new Element('a',{href:'http://twitter.com/' + tweet.from_user, 'class':'profile_link', text:tweet.from_user});
		a.addEvent('click', function(){pageTracker._trackEvent('outbound', 'click', 'twitter'); return true;});
		a.setProperty('target', '_new');
		rcol.grab(a);
		
		var date = new Date(tweet.created_at);
		div.store('date', date);
		var format = date.format('h:MMtt') + ' on ' + date.format('longDate');
		rcol.grab(new Element('div',{text:'Twitter at ' + format, 'class':'byline'}));

		rcol.grab(new Element('div',{text:tweet.text, 'class':'walltext'}));
		
		div.grab(new Element('div',{'class':'clear',html:'&nbsp;'}));
		this.el.grab(div);
	},
	
	parseJSON: function(obj)
	{
		return obj;
	}
	
});

var FacebookFeed = new Class(
{
	initialize: function(id, el)
	{
		this.id = id;
		this.el = el;
		this.url = 'wall.php';
		this.request = new Request.HTML({url: this.url, onSuccess: this.onSuccess.bind(this), evalScripts: false});
		this.request.get();
	},
	
	onSuccess: function(el, els, responseHTML, responseJavaScript)
	{
		$('comments-loader').setStyle('display','none');
		
		var payload = eval('(' + responseHTML.substr(9) + ')');
		//payload.payload.stream_html = payload.payload.stream_html.replace(/value="&euro;,&acute;,........."/g, 'value=""');	
		var div = new Element('div',{'html': payload.payload.stream_html});
		this.wallposts = div.getElements('.UIStory');
		this.wallposts.each(this.addPost.bind(this));
		
		// sorting hack //
		//feedCount++;
		//if (feedCount > 1) doSort();
	},
	
	addPost: function(post, index)
	{

		if (this.el.getElements('div.fbdiv').length > 14) return;
		
//		var tt = post.getElement('.UIIntentionalStory_Names a');
//		if (tt)
//		{
//			if (tt.get('text') == 'WillYouCapital') return;
//		}
		
		
		var div = new Element('div',{'class':'cropped fbdiv i' + this.el.getChildren().length});
		var el = post.getElement('a.UIIntentionalStory_Pic');
		if (el == null) return;
		/*
		var a = new Element('a', {href:el.get('href'),'target':'_new'});
		a.grab(el.getElement('span.UIRoundedImage img'));
		div.grab(a);
		
		a.addClass('profile_pic');
		a.addEvent('click', function(){pageTracker._trackEvent('outbound', 'click', 'facebook'); return true;});
		
		
		// size the profile pic //
		a.getFirst().setProperty('width','36');
		*/
		
		var rcol = new Element('div',{style:'float:left;width:231px'});
		div.grab(rcol)
		
		rcol.addEvent('mouseenter', (function(){ this.removeClass('cropped'); }).bind(div));
		rcol.addEvent('mouseleave', (function(){ this.addClass('cropped'); }).bind(div));
		
		el = post.getElement('.UIIntentionalStory_Names a');
		if (el == null) 
		{
			el = post.getElement('.UIIntentionalStory_Names');
		}
		else
		{
			el.addEvent('click', function(){pageTracker._trackEvent('outbound', 'click', 'facebook'); return true;});
			el.setProperty('target', '_new');
		}
		rcol.grab(el);
		el.addClass('profile_link');
		
		el = post.getElement('.UIIntentionalStory_Time');
		if (el == null) return;
		rcol.grab(new Element('div',{text:'Facebook ' + el.get('text'), 'class':'byline'}));
		
		el = post.getElement('h3');
		if (el == null) return;
		rcol.grab(new Element('div',{'text': el.get('text')}));
		
		div.grab(new Element('div',{'class':'clear',html:'&nbsp;'}));
		this.el.grab(div);
	}

});

var FlickrFeed = new Class(
{
	initialize: function(tags, el)
	{
		this.el = el;
		this.url = 'photos_public.gne?lang=en-us&format=json&tags=' + tags;
		this.json = new Request.JSON({url: this.url, onComplete: this.onComplete.bind(this)});
		this.json.get({});
	},
	
	onComplete: function(obj, txt)
	{
		$('flickr-loader').setStyle('display','none');
		this.data = eval('this.' + txt);		
		this.data.items.each(this.addImage.bind(this));
		
		// add the final thing //
		var a = new Element('a',{href:'http://www.flickr.com/search/?q=willyouCU', title:'see em all', target: '_new', 'class':'i8'});
		a.grab(new Element('img', {src:'img/seeallphotos.gif',width:'100',height:'80'}));
		this.el.grab(a);
		
		a.addEvent('click', function(){pageTracker._trackEvent('outbound', 'click', 'flickr'); return true;});
	},
	
	jsonFlickrFeed: function(obj)
	{
		return obj;
	},
	
	addImage: function(item, index)
	{
		// only the first eight //
		if (index > 7) return;
		
		// figure out the aspect ratio //
		var z = item.description.match(/.*width=.([0-9]*)/,'g');
		var width = z[1].toInt();
		z = item.description.match(/.*height=.([0-9]*)/,'g');
		var height = z[1].toInt();
			
		var img = new Element('img', {src: item.media.m});
		var pct;
		if (height > width)
		{
			pct = (100 / width);
		}
		else
		{
			pct = (80 / height);
		}
		
		img.setProperty('width', Math.ceil(width * pct));
		img.setProperty('height', Math.ceil(height * pct));
		
		var a = new Element('a', { href: item.link, title: item.title, target: '_new', 'class': 'i' + index});
		
		a.grab(img);
		this.el.grab(a);
	}
});

/**
 * DATE FORMAT THINGY
 */
var dateFormat = function () 
{
	var	token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
		timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
		timezoneClip = /[^-+\dA-Z]/g,
		pad = function (val, len) 
		{
			val = String(val);
			len = len || 2;
			while (val.length < len) val = "0" + val;
			return val;
		};

	// Regexes and supporting functions are cached through closure
	return function (date, mask, utc) 
	{
		var dF = dateFormat;

		// You can't provide utc if you skip other args (use the "UTC:" mask prefix)
		if (arguments.length == 1 && (typeof date == "string" || date instanceof String) && !/\d/.test(date)) 
		{
			mask = date;
			date = undefined;
		}

		// Passing date through Date applies Date.parse, if necessary
		date = date ? new Date(date) : new Date();
		if (isNaN(date)) throw new SyntaxError("invalid date");

		mask = String(dF.masks[mask] || mask || dF.masks["default"]);

		// Allow setting the utc argument via the mask
		if (mask.slice(0, 4) == "UTC:") 
		{
			mask = mask.slice(4);
			utc = true;
		}

		var	_ = utc ? "getUTC" : "get",
			d = date[_ + "Date"](),
			D = date[_ + "Day"](),
			m = date[_ + "Month"](),
			y = date[_ + "FullYear"](),
			H = date[_ + "Hours"](),
			M = date[_ + "Minutes"](),
			s = date[_ + "Seconds"](),
			L = date[_ + "Milliseconds"](),
			o = utc ? 0 : date.getTimezoneOffset(),
			flags = {
				d:    d,
				dd:   pad(d),
				ddd:  dF.i18n.dayNames[D],
				dddd: dF.i18n.dayNames[D + 7],
				m:    m + 1,
				mm:   pad(m + 1),
				mmm:  dF.i18n.monthNames[m],
				mmmm: dF.i18n.monthNames[m + 12],
				yy:   String(y).slice(2),
				yyyy: y,
				h:    H % 12 || 12,
				hh:   pad(H % 12 || 12),
				H:    H,
				HH:   pad(H),
				M:    M,
				MM:   pad(M),
				s:    s,
				ss:   pad(s),
				l:    pad(L, 3),
				L:    pad(L > 99 ? Math.round(L / 10) : L),
				t:    H < 12 ? "a"  : "p",
				tt:   H < 12 ? "am" : "pm",
				T:    H < 12 ? "A"  : "P",
				TT:   H < 12 ? "AM" : "PM",
				Z:    utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
				o:    (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
				S:    ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
			};

		return mask.replace(token, function ($0) 
		{
			return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
		});
	};
}();

// Some common format strings
dateFormat.masks = {
	"default":      "ddd mmm dd yyyy HH:MM:ss",
	shortDate:      "m/d/yy",
	mediumDate:     "mmm d, yyyy",
	longDate:       "mmmm d, yyyy",
	fullDate:       "dddd, mmmm d, yyyy",
	shortTime:      "h:MM TT",
	mediumTime:     "h:MM:ss TT",
	longTime:       "h:MM:ss TT Z",
	isoDate:        "yyyy-mm-dd",
	isoTime:        "HH:MM:ss",
	isoDateTime:    "yyyy-mm-dd'T'HH:MM:ss",
	isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};

// Internationalization strings
dateFormat.i18n = {
	dayNames: [
		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
		"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
	],
	monthNames: [
		"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
		"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
	]
};

// For convenience...
Date.prototype.format = function (mask, utc) {
	return dateFormat(this, mask, utc);
};

var feedCount = 0;

function doSort()
{
	//console.log('jsjsjshjshjh');
}

