// remove the "$" namespace from jQuery, avoids conflicts with other libraries
jQuery.noConflict();

// closure, mapping jQuery to $, window, document and undefined - useful for minifing tools
(function($, window, document, undefined){

// document ready method
$(function(){
	//go back links
	$('.goBack').click(function(){
		history.back();
		return false;
	});
	
	//filter by year
	$('#filter-by-year #year').change(function(){
		window.location = $('#filter-by-year #base_url').val() + $('#filter-by-year #year').val();
	});
	
	if($.fn.waitForImages){
		//fix for youtubes tmbs
		$('.video-list').waitForImages(function(){
			var fixed_height_yt = 88;
			$('img.youtube').each(function(){
				var position = Math.round((fixed_height_yt - $(this).height()) / 2);
				$(this).css('top', position + 'px');
			});
		});
		
		//fix for alumni/asudents tmbs
		$('.alumni-list').waitForImages(function() {
			var fixed_width_al_st = 87;
			var fixed_height_al_st = 87;
			$('img.alumni').each(function(){
				var position_y = Math.round((fixed_height_al_st - $(this).height()) / 2);
				$(this).css('top', position_y + 'px');
				var position_x = Math.round((fixed_width_al_st - $(this).width()) / 2);
				$(this).css('left', position_x + 'px');
			});
		});
	}
	
	if (window['Cufon'] !== undefined)
	{
		Cufon.replace('#container h2, .box-2 h3', {fontFamily: 'Archer', hover: true});
	}
	
	if ($.fn.slideshowFade)
	{
		var slideTime = 2000, slider = $('#slideshow-fade1').slideshowFade({
			//speed: 600, // default 600
			auto: slideTime // default 5000
		});
		slider.hover(function(){
			slider.trigger('changeAuto', [0]);
		}, function(){
			slider.trigger('changeAuto', [slideTime]);
		});
	}
	
	if ($.fn.boxLinks)
	{
		$('.boxLink, .alumni-list li').boxLinks();
		$('.video-list li').boxLinks({
			onClick: function(){}
		});
	}
	
	if ($.fn.equalHeights)
	{
		$('.list-1 li').equalHeights();
		$('.box-2').equalHeights();
	}

	if ($.fn.prettyPhoto)
	{
		$("a[rel^='prettyPhoto']").prettyPhoto({
			social_tools: ''
		});
	}

});

// plugins

$.fn.slideshowFade = function(args)
{
	// slideshowFade - jQuery plugin - creates a fading slideshow - by Valentin Agachi - http://agachi.name
	if (!this.length) return this;

	var opts = $.extend({
		slides: '> ul > li',	// slides selector
		buttons: true,			// show buttons
		speed: 600,				// speed of transition
		auto: 5000				// speed of auto transition
	}, args || {});

	return this.each(function(){
		var parent = $(this), slides = parent.find(opts.slides), buttons = $(), timer = null, n = 0, s = '';

		slides.each(function(){
			this.id = parent[0].id + '-' + (++n);
			var cSlide = $(this), img = cSlide.find('img').attr('src');
			s += '<a href="#' + this.id + '"><img src="'+ img +'" width="20" height="20"/></a>';
		});

		if (opts.buttons)
		{
			buttons = parent.append('<div class="buttons">' + s + '</div>').
				find('.buttons a').click(function(){
					var hash = this.hash.substr(1);
					parent.triggerHandler('slideChange', [slides.filter('#' + hash)]);
					return false;
				});
		}

		function change(next)
		{
			var active = slides.filter('.active');
			if (!next || !next.jquery)
				next = active.next();
			if (!next.length)
				next = slides.eq(0);

			var stop = parent.triggerHandler('slideChanging', [parent, slides, opts, active, next]);
			if (!stop)
			{
				next.addClass('next');
				active.fadeOut(opts.speed, function(){
					next.addClass('active').removeClass('next');
					active.removeClass('active').show();
					buttons.removeClass('active').filter('[href$="#' + next[0].id + '"]').addClass('active');
				});
			}
			else
			{
				buttons.removeClass('active').filter('[href$="#' + next[0].id + '"]').addClass('active');
			}

			parent.trigger('slideChanged', [parent, slides, next]);
		}

		function changeNow(next)
		{
			slides.removeClass('active');
			next.addClass('active').show();
			buttons.removeClass('active').filter('[href$="#' + next[0].id + '"]').addClass('active');

			parent.trigger('slideChanged', [parent, slides, next]);
		}

		function auto()
		{
			if (opts.auto)
				timer = setInterval(change, opts.speed + opts.auto);
		}

		parent.bind('slideChange', function(ev, slide, now){
			clearInterval(timer);
			now ? changeNow(slide) : change(slide);
			auto();
		});
		parent.bind('slideAdvance', function(ev, delta){
			var dir = (delta > 0), cnt = Math.abs(delta),
				active = slides.filter('.active'), next = active;
			while (cnt--)
			{
				next = active[(dir ? 'next' : 'prev')]();
				if (!next.length)
					next = slides.eq(dir ? 0 : slides.length - 1);
			}
			parent.trigger('slideChange', [next]);
		});
		parent.bind('changeAuto', function(ev, time){
			clearInterval(timer);
			opts.auto = time;
			auto();
		});

		if ((a = slides.filter('.active')) && !a.length)
			changeNow(slides.eq(0));
		else
			changeNow(a);

		auto();
	});
};

// boxLinks - jQuery plugin - transforms block elements into clickable items - by Valentin Agachi http://agachi.name
$.fn.boxLinks = function(args)
{
	if (!this.length) return this;

	var opts = $.extend({
		trigger: 'a',
		classHover: 'hover',
		classFocus: 'focus',
		onEnter: null,
		onLeave: null,
		onClick: function(){
			if($(this).attr('href').replace(/\s+/g,"").length > 1){
				if($(this).attr('target') == '_blank'){
					window.open($(this).attr('href'));
				}
				else{
					window.location = $(this).attr('href');
				}
			}
			else{
				return false;
			}
		}
	}, args || {});

	this.has(opts.trigger)
		.each(function(){
			var self = $(this), t = self.css('cursor', 'pointer').find(opts.trigger);
			$.data(this, 'trigger', t);		
			$.data(t[0], 'parent', self);
		}).bind('mouseenter', function(){
			var t = $.data(this, 'trigger'), self = $(this).addClass(opts.classHover);
			opts.onEnter && opts.onEnter(self);
			window.status = t.attr('href');
		}).bind('mouseleave', function(){
			var self = $(this).removeClass(opts.classHover);
			opts.onLeave && opts.onLeave(self);
			window.status = '';
		}).bind('click', function(ev){
			var t = $.data(this, 'trigger');
			t.trigger('click');
			ev.stopPropagation();
			return false;
		});

	this.find('a').not(opts.trigger).
		bind('mouseenter', function(){
			$.data(this, 'parent', $(this).parents('.' + opts.classHover).removeClass(opts.classHover));
		}).
		bind('mouseleave', function(){
			$.data(this, 'parent').addClass(opts.classHover);
		});

	this.find(opts.trigger).addClass('trigger')
		.bind('focus', function(){
			var p = $.data(this, 'parent');
			p.addClass(opts.classFocus);
		}).bind('blur', function(){
			var p = $.data(this, 'parent');
			p.removeClass(opts.classFocus);
		}).click(function(ev){
			opts.onClick.apply(this);
			ev.stopPropagation();
			return false;
		});

	this.find('a').not(opts.trigger).click(function(ev){
		ev.stopPropagation();
	});

	return this;
};


// equalHeights - jQuery plugin - forces elements to have the same height (maximum)
$.fn.equalHeights = function(add)
{
	var m = 0;
	this.each(function(){
		m = Math.max(m, $(this).outerHeight());
	});
	return this.each(function(){
		
		var t = $(this), p = 0;
		$.each(['borderTopWidth', 'paddingTop', 'paddingBottom', 'borderBottomWidth'], function(i,n){
			var v = parseInt(t.css(n));
			p += (isNaN(v) ? 0 : v);
		});
		var h = m - p;
		if (add && add[this.id])
			h += add[this.id];
		if ($.browser.msie && $.browser.version <= 6) 
			t.css('height', h);
		t.css('min-height', h); 
		
		//fix for ie 7
		if ($.browser.msie  && parseInt($.browser.version) == 7) {
			t.css('border', '1px solid transparent');
		}
	});
};



})(jQuery, window, document);

