(function(slice){
	Function.prototype.bind = function(){
		var method = this, args = slice.call(arguments), src = args.shift();
		return function() {
			return method.apply(src, args.concat(slice.call(arguments)));
		};
	};
})([].slice);
$.extend({
	classes : {},
	classify : function(name, object, parent){
		function klass(){	
			this.init.apply(this, arguments);
		}
		if (parent && $.classes[parent]) {
			function subklass(){};
			subklass.prototype = $.classes[parent].prototype;
			klass.prototype = new subklass;
		}
		for (var y in object)
			klass.prototype[y] = object[y];
		if (!klass.prototype.init)
			klass.prototype.init = function(){};
		klass.prototype.constructor = klass;
		$.classes[name] = klass;
		$.fn[name] = function(){
			return this.each(function(i, element){
				element.classBehaviors = element.classBehaviors || {};
				if (element.classBehaviors[name]) return;
				function behavior(){};
				function klassBehavior(){
					this.element = element;
					this.init.apply(this, arguments);
					for (var attr in this) {
						if (attr.indexOf("on") === 0) {
							var handler = attr.substr(2);
							if ($.fn[handler])
								$(this.element)[handler](this[attr].bind(this));
						}
					}
				}
				behavior.prototype = $.classes[name].prototype;
				klassBehavior.prototype = new behavior;
				element.classBehaviors[name] = new klassBehavior();
				return element.classBehaviors[name];
			});
		};
		return klass;
	}
});
$.classify("primaryLandingFeatures", {
	init : function(){
		this.features = $(".PrimaryFeature", this.element);
		if (this.features.length < 2) return;
		this.current_feature = this.features[0];
		this.pagination = document.createElement("div");
		this.pagination.id = "primary-feature-pagination";
		$(this.pagination).appendTo(this.element);
		for (var i = 0, feature, page; feature = this.features[i]; ++i) {
			page = document.createElement("a");
			page.className = "FeaturePage";
			page.innerHTML = i + 1;
			$(page).click(this.switchPage.bind(this, i, page)).appendTo(this.pagination);
			if (i === 0)
				this.current_pagination = page;
		}
		$(this.current_pagination).addClass("Active");
		var cf = document.createElement("div");
		cf.className = "ClearFix";
		$(cf).appendTo(this.pagination);
		this.active_pages = "0";
	},
	switchPage : function(i, page) {
		$(this.current_pagination).removeClass("Active");
		$(this.current_feature).hide();
		this.current_feature = this.features[i];
		this.current_pagination = page;
		$(this.current_feature).show();
		$(this.current_pagination).addClass("Active");
		this.swapSifrs();
	},
	swapSifrs : function(){
		if (typeof activateEmersonSifrs === "function")
			activateEmersonSifrs();
	}
});
$.classify("homeAnnouncements", {
	current_announcement : null,
	init : function(){
		$("#announcements").show();
		this.announcement = $("#announcements li", this.element);
		this.current_announcement = null;
		// set inline width based on # of announcements - req to center thumbnails
		announcement_width = ($(this.announcement).size()) * 43;
		$("#announcements").css('width', announcement_width);
		// wait to set display:none until sIFR has run
		setTimeout(function(){
			$("#announcements .messagewrap").css('display', 'none').css('visibility', 'visible');
			$("#announcements .message").css('left', '-120px');
		}.bind(this), 500);
		
		for (var i = 0, announcement; announcement = this.announcement[i]; ++i) {
			this.initAnnouncement(announcement);
		}
		this.is_ie6 = $.browser.msie && $.browser.version < 7;
	},
	initAnnouncement : function(announcement){
		$(announcement).click(this.loadAnnouncement.bind(this, announcement));
		$(announcement).hover(this.hoverAnnouncement.bind(this, announcement, true), this.hoverAnnouncement.bind(this, announcement, false));
		$("#feature-image").click(this.showFeatureText.bind(this, announcement));
	},
	hoverAnnouncement : function(announcement, hovering) {
		$(announcement)[hovering ? 'addClass' : 'removeClass']('Hover');
	},
	loadAnnouncement : function(announcement){
		if (announcement == this.current_announcement) return;
		this.hideFeatureText();
		if (this.current_announcement) { this.unloadAnnouncement(this.current_announcement); }
		this.current_announcement = announcement;
		$(announcement).addClass("Active");
		$("#feature-image img").animate({
			opacity: 0.3
		}, 500, function() {
			$(announcement).children(".messagewrap").fadeIn(500);
		});
	},
	unloadAnnouncement : function(announcement){
		$(".Active", this.element).removeClass("Active");
		$(announcement).children(".messagewrap").fadeOut(500);
	},
	hideFeatureText : function() {
		if ($("#feature-text", this.element).is(':visible')){
			$("#feature-text", this.element).fadeOut(500);
		}
	},
	showFeatureText : function(announcement) {
		if ($("#feature-text", this.element).is(':hidden')){
			$("#feature-text", this.element).fadeIn(500);
			$("#feature-image img").animate({ opacity: 1}, 500);
		}
		this.unloadAnnouncement(announcement);
		this.current_announcement = null;
	}
});
$.classify("carousel", {
	
	initCallback : function(){},
	index : 0,
	width : 609,
	per_index : 7,
	animation_factor : 400,
	init : function(){
		this.list = $("ul", this.element);
		this.items = $("li", this.element);
		$(".left, .right", this.element).show();
		// just avoid IE for the moment. remove the left/right, delete the extras
		if ($.browser.msie) {
			$(".left, .right", this.element).remove();
			for (var i = this.per_index, length = this.items.length; i < length; ++i)
				$(this.items[i]).remove();
			return false;
		}
		this.items_length = Math.ceil(this.items.length / this.per_index);
		$(".left", this.element).click(this.calculateIndex.bind(this, -1));
		$(".right", this.element).click(this.calculateIndex.bind(this, 1));
		this.initCallback();
	},
	calculateIndex : function(change){
		if (this.scrolling) return false;
		var index = this.index + change;
		if (index >= this.items_length)
			index = 0;
		else if (index < 0)
			index = this.items_length - 1;
		this.scrollCarousel(index);
		return false;
	},
	scrollCarousel : function(index){
		this.scrolling = true;
		var diff = Math.abs(this.index - index);
		this.index = index;
		this.list.animate({
			left : "-" + (this.index * this.width) + "px"
		}, diff * (diff > 20 ? 20 : this.animation_factor), function(){
			this.scrolling = false;
		}.bind(this));
	}
});
$.classify("audienceCarousel", {
	width : 190,
	per_index : 1,
	animation_factor : 330,
	// vertical align middle - its a start
	initCallback : function(){
		var height = $(this.element).innerHeight();
		this.items.each(function(outer_height, index, li){
			var inner_height = $(li).innerHeight();
			li.style.marginTop = Math.round((outer_height - inner_height) / 2) + "px";
		}.bind(this, height));
	}
}, "carousel");
$.classify("ContactSliders", {
	init : function(){
		if ($(".DPContact form").size() == 1) { $(".DPContact H3").css({'background' : 'none', 'cursor' : 'default'}); return; }
		current_slider : null,
		$(".SliderWrap").hide();
		this.slider = $(".SliderWrap", this.element);
		this.current_slider = null;
		for (var i = 0, slider; slider = this.slider[i]; ++i) {
			this.initslider(slider);
		}
	},
	initslider : function(slider){
		$(slider).prev("H3").click(this.loadslider.bind(this, slider));
	},
	loadslider : function(slider){
		if (slider == this.current_slider) { this.unloadslider(this.current_slider); return; }
		if (this.current_slider) { this.unloadslider(this.current_slider); }
		this.current_slider = slider;
		$(this.current_slider).parent(".DPContactForm").addClass("Active");
		$(this.current_slider).slideDown("slow");
	},
	unloadslider : function(slider){
		$(this.current_slider).parent(".DPContactForm").removeClass("Active");
		$(this.current_slider).slideUp("slow");
		this.current_slider = null;
	}
});

// dom ready
$(function() { 
	// temporarily hiding flickr slideshows
	if ($('.service-content IMG').length > 0){
		var first_img = $('.service-content IMG')[0];
		$(first_img).css({ display : "block" });
	}
	// Carousels
	$("#FlickrSlides .CarouselWrapper").carousel();
	$("#SecondaryContent .CarouselWrapper").audienceCarousel();
	$("#main-features-carousel").primaryLandingFeatures();
	// tabs
	  if ($('#tabs').length > 0){
	    $("#tabs").tabs();
	  }
	// FCK IE6 image alignment
	if ($.browser.msie && $.browser.version == "6.0")  {
		$('.main .wysiwyg-content img[align]').each(function() {
			var this_img = $(this);
			var align = this_img.attr("align").toLowerCase();
			if (align == 'left') { 
				this_img.addClass('align-left'); 
			} else if (align == 'right') { 
				this_img.addClass('align-right'); 
			}
		});
	}
	// Submit the Events Calendar date picker on date select
	$("#edit-start-value-datepicker-popup-0").bind('change', function(){
		$(this).parents().filter("form")[0].submit();
	});
	// Homepage Main Feature
	$(".Home #MainFeature").homeAnnouncements();
	// Label Overlay
	$('#site-search label').inFieldLabels();
	// Label Overlay
	$('.DPContact').ContactSliders();
	if ($('#site-search LABEL').length > 0){
		$('#site-search LABEL').html('Search');
	}
	// Slideshows
	if ($('#gallery').length > 0){
		// We only want these styles applied when javascript is enabled
		$('div.navigation').css({'width' : '450px', 'float' : 'left'});
		$('div.content').css('display', 'block');
		// Initially set opacity on thumbs and add
		// additional styling for hover effect on thumbs
		var onMouseOutOpacity = 0.67;
		$('#thumbs ul.thumbs li').opacityrollover({
			mouseOutOpacity:   onMouseOutOpacity,
			mouseOverOpacity:  1.0,
			fadeSpeed:         'fast',
			exemptionSelector: '.selected'
		});
		// Initialize Advanced Galleriffic Gallery
		var gallery = $('#thumbs').galleriffic({
			delay:                     2500,
			numThumbs:                 1,
			preloadAhead:              10,
			enableTopPager:            true,
			enableBottomPager:         false,
			maxPagesToShow:            13,
			imageContainerSel:         '#slideshow',
			controlsContainerSel:      '#controls',
			captionContainerSel:       '#caption',
			loadingContainerSel:       '#loading',
			renderSSControls:          true,
			renderNavControls:         false,
			playLinkText:              'Play',
			pauseLinkText:             'Pause',
			nextPageLinkText:          '&rsaquo;',
			prevPageLinkText:          '&lsaquo;',
			enableHistory:             false,
			autoStart:                 false,
			syncTransitions:           true,
			defaultTransitionDuration: 900,
			onSlideChange:             function(prevIndex, nextIndex) {
				// 'this' refers to the gallery, which is an extension of $('#thumbs')
				this.find('ul.thumbs').children()
					.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
					.eq(nextIndex).fadeTo('fast', 1.0);
			}
		});
	}
});
$(document).ready(addClassActiveTrail)
function addClassActiveTrail() { 
	if($('.item-list')) {
		$('.item-list a').each(function() {
			if( window.location.href.indexOf($(this).attr("href")) != -1   ) {
				$(this).addClass("active-trail")
				return false
			}
		})
	}
}
// hides the "More information about formatting options" link on the comment form
$(function(){
	$("a[href=/filter/tips]").hide()
});
// This file contains custom javascript for use on www.emerson.edu
// shows tabs for step-by-step
// this function predicated on the link name being the same as the tab name + _link
function showDivTab(divName, thisDivName) {
	// loop from 1-20
	for (var i = 1; i <= 20; i++) {
		//get the corresponding name of the link container
		var linkP = divName + i + '_link';
		// if there is a matching tab hide it and set its corresponding link bgcolor to white; otherwise break
		if (document.getElementById(divName+i)) { 
			document.getElementById(divName+i).className = 'tabHide';
			document.getElementById(linkP).style.backgroundColor = '#FFFFFF';
			document.getElementById(linkP).style.borderBottom = "thin solid #808080";
			document.getElementById(linkP).style.borderTop = "none";
			document.getElementById(linkP).style.borderLeft = "none";
			document.getElementById(linkP).style.borderRight = "none";
		}	else {
			break;
		}
	}
	//show selected tab and set bgcolor and borders of active tab
	document.getElementById(thisDivName).className = 'tabView';
	document.getElementById(thisDivName + '_link').style.backgroundColor = '#EAEAEA';
	document.getElementById(thisDivName + '_link').style.borderTop = "thin solid #808080";
	document.getElementById(thisDivName + '_link').style.borderLeft = "thin solid #808080";
	document.getElementById(thisDivName + '_link').style.borderRight = "thin solid #808080";
	document.getElementById(thisDivName + '_link').style.borderBottom = "none";
	
}
// shows/hides a simple block of text ~ used in course displays
function showBlock(id) {
	if (document.getElementById) {
		if (document.getElementById(id).style.display == "none"){
			document.getElementById(id).style.display = 'block';	
		} else {
			document.getElementById(id).style.display = 'none';		
		}
	}
}
//panels nav
$(document).ready(function(){
	var clickarray = new Array(".nav-about",".nav-academics",".nav-student",".nav-admission",".nav-news");
		 for (i = 0; i < clickarray.length; i++) {
			 $(clickarray[i]).data('target', clickarray[i].replace( ".", "#" ));
		 }	 
	$(clickarray.toString()).click(function() {
		 var selector = $(this).data('target');	
		 if ($("#PanelFix").hasClass("active") === false) {
			 $(selector).show();
			 $(this).addClass("active");
			 $(this).parent().addClass("active"); 
			 $("#PanelFix").addClass("active");
		 }
		 else { 
			 if ($(this).hasClass("active") == true) {
				$(selector).hide();
				$(this).removeClass("active");
				$(this).parent().removeClass("active"); 
				$("#PanelFix").removeClass("active");
			 }
			 else {
				 $(selector).show();
				 $(this).parent().addClass("active");
				 for (j = 0; j < clickarray.length; j++) {
					 var thediv = clickarray[j].replace( ".", "#" );
					 if (thediv != selector) {
						$(thediv).hide();
						$(clickarray[j]).removeClass("active"); 
						$(clickarray[j]).parent().removeClass("active");
					 }
				 }
			 $(this).addClass("active"); 
			}
		}
	 return false;});
});	

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
