﻿(function($){
    
    var neSortFields = {
    	date: "date",
    	region: "region"
    }
	
    Behaviors.register("NewsEventsList", ".news-events", {
    	init: function(){
    		this.$itemsUl = this.$el.children("ul");
    		this.$sortDate = this.find("#sortby-date");
    		this.$sortRegion = this.find("#sortby-region");
    		
    		// bugfix: sortdate comes from server with wrong default sort states
    		this.$sortDate.removeClass("desc").addClass("asc");
    		this.isSortedBy = neSortFields.date;
    	}
    	,initEvents: function(){
    		this.$sortDate.children("a").click(this.sortByDate.bind(this));
    				
    		this.$sortRegion.children("a").click(this.sortByRegion.bind(this));

    	}
    	,sortByDate: function(e){
    	
    		this.sort(e, neSortFields.date,
    			function(a, b){
					return getDate(a) - getDate(b);	   			    			
				}
    		);
			this.$itemsUl.children().removeClass('first');
			this.$itemsUl.children(":first").addClass('first');
    		
    		function getDate(el){
				return new Date($(el).text().replace("|",""));
			}
    	}
    	,sortByRegion: function(e){
    		this.sort(e, neSortFields.region,
    			function(a, b){
					var x = $(a).text(),
						y = $(b).text();
						
					return ((x < y) ? -1 : ((x > y) ? 1 : 0));	   			    			
				}
    		);
			this.$itemsUl.children().removeClass('first');
			this.$itemsUl.children(":first").addClass('first');
    	}
    	,sort: function(e, field, sortFn)
    	{
    		if (e)
    		{
    			e.stopPropagation();
    			e.preventDefault();
    		}
    		
    		var $sortField = field == neSortFields.date ? this.$sortDate : this.$sortRegion;
    		var $nonSortField = field == neSortFields.date ? this.$sortRegion : this.$sortDate;
    		
    		var dir;
    		if (this.isSortedBy == field)
    			dir = $sortField.is(".asc") ? "desc" : "asc";
    		else
    		{
    			dir = this.$sortDate.is(".asc") ? "asc" : "desc";
    			$nonSortField.removeClass("selected");
    		}
    		
    		$sortField
    			.removeClass("asc desc")
    			.addClass(dir + " selected");
    			
    		this.isSortedBy = field;
    		
    		// push elements into a basic element array
    		var elems = [];
    		this.$itemsUl.find( field == neSortFields.date ? "h4" : "var" )
    			.each(function(){
	    			elems.push(this);
	    		});
    		
    		elems.sort(sortFn);
 
    		if (dir == "desc")
    			elems = elems.reverse();
    			
    		elems.each(function(el){
			    $(el).parent().appendTo(this.$itemsUl);
			}.bind(this));
    	}
    });
    
    
    /* Carousel Behavior */
    function getMediaInfo(el)
    {
    	if (!el) return {
    		href: "",
    		className: "",
    		src: ""
    	}
    	
    	var $el = $(el);
    	var className = $el.attr('class');
    	return {
    		href: $el.find("a:first").attr("href"),
   			className: className && className.contains("video") ? "video" : "image",
   			src:$el.find("img:first").attr("src")
   		}
    }

    Behaviors.register("Carousel", "div.carousel", {
    	reqScripts: [SCRIPTS.JUI]
    	,visibleNdx: 0
    	
    	,init: function(){
    		this.$ul = this.find("ul");
    		this.$li = this.$ul.children();
    		this.$gallery = this.$ul.parent();
			this.$slider = this.find(".slider");
			this.$handle = this.find(".handle");
			this.$player = this.find("p:first");
			this.playerId = this.$player.attr("id") + "parsonsplayer";

			var mediaInfo = getMediaInfo(this.$li.get(0));
			
			swfobject.embedSWF("/Flash/mediaplayer/parsons_media_player.swf", this.$player.attr("id"), "432", "276", "8.0.0", false, 
				{	//flashvars
					mediaPath: mediaInfo.href,
					mediaPreview: mediaInfo.src.replace("_sm", "_lg"),
					mediaType: mediaInfo.className,
					autoPlay: "false"
				},{ //params
					menu: "false",
					allowfullscreen: "true",
					allowscriptaccess: "always",
					wmode: "transparent"				
				},{ // attributes
					id: this.playerId	
				});
			
			this.setItemSelected(0);
    	}
    	
    	,initLayout: function(){
    		this.step = this.$li.width();
    		if (this.step) this.$handle.css("width", this.step  + "px");
    		
    		$.ui.disableSelection(this.$ul.get(0));
			
    		// if there are more then 3 items then initialize the slider
    		if (this.$li.length > 3)
				{
					var stepOffset = ((this.$li.eq(1).offset().left - this.$li.eq(0).offset().left) * 3);
					if (this.$li.css("display") == "inline"){
						this.$ul.css("width",this.$li.length * 159 + "px");
    			}
    			function refreshUI(e, ui){
    				this.$ul.css("left", stepOffset * (ui.value / this.step) * -1);
					//alert(ui.value);
				}	    
			
    			
				this.slider = new $.ui.slider(this.$slider.children(".handleSlider").get(0), {
					min: 0,
					max: this.step * (Math.ceil(this.$li.length / 3) - 1),
					//max: this.step * (this.$li.length - 3),
					handle: this.$handle.get(0),
					stepping: this.step,
					startValue: 0,
					slide: refreshUI.bind(this),
					change: refreshUI.bind(this)
				});
			}			
			// else just hide it
			else {
				this.$slider.hide();
				this.$gallery.css("height","106px");
			}
			this.find("li p").hide();
			
			this.$el.css("background-color", "transparent");		
			if (this.$li.length > 1) {	
				this.$gallery.show();
				this.$li.show();
			} else
				this.$gallery.hide();
    	}
    	,initEvents: function(){
    	
			this.find("ul a").preventDefault();    	
			
			this.$li.click(this.selectMediaItem.bindWith(this));
			
			if (this.slider)
			{
				this.find(".right").click(this.stepRight.bind(this));
				this.find(".left").click(this.stepLeft.bind(this));
				
				this.find("ul, li, li *").keypress(function(e){
					this.slider.keydown(e.keyCode);
					e.stopPropagation();
				}.bind(this));
			}
    	}
    	,stepRight: function(e){
    		
    		this.slider.keydown(39 /*right key*/);    		
    		if (e)
				e.stopPropagation();
    	}
    	
    	,stepLeft: function(e){
    		this.slider.keydown(37 /*left key*/);
    		if (e)
				e.stopPropagation();

    	}
    	,selectMediaItem: function(srcEl, e){
    	
    		this.$li.removeClass("selected");
    		
    		this.currentIndex = this.$li.index(srcEl);
   			
   			if (this.currentIndex < 0)
   				return false;
   			
   			this.setItemSelected(this.currentIndex);
   			
			var info = getMediaInfo(srcEl);		
   			this.getPlayer().loadMedia(info.href, info.className, info.src.replace("_sm", "_lg"), info.className == "video");
   			
   			if (e)
   				e.stopPropagation();
   				   				
   			return true;
    	}
    	,setItemSelected: function(index)
    	{
    		this.$li.eq(index).addClass("selected");
    	}
    	,getPlayer: function(){
    		return swfobject.getObjectById(this.playerId);
    	}
    });
    
    /* Pagination Behavior */
    Behaviors.register("Pagination", null, {

        init: function() {
            this.$next = this.find("li.next");
            this.$prev = this.find("li.prev");
            this.$viewAll = this.find("p a");
            this.$pageInput = this.find("input");
            this.$pageCount = this.$pageInput.next("span");
			this.$pagingControls = this.find("h4,ul");
            this.update();
        }
	    ,initEvents: function() {
	        this.$next
	            .children("a")
	                .click(this.nextPage.bind(this));
	        this.$prev
	            .children("a")
	                .click(this.prevPage.bind(this));
	
	        this.$viewAll.click(this.viewAll.bind(this));
	
	        this.$pageInput
	        	.blur(function(el){
	        		var val = el.value;
	        		this.gotoPage(val.toInt());
		        }.bindWith(this))
	        	.keypress(function(el, e) {
	            	var val = el.value;
		            var i = val.toInt();
		
		            switch (e.keyCode) 
		            {
		                case 38: // up
		                    if (i < this.totalPages)
		                        this.$pageInput.val(i + 1);
		                    break;
		
		                case 40: // down
		                    if (i > 1)
		                        this.$pageInput.val(i - 1);
		                    break;
		
		                case 8: //backspace
		                case 46: //delete
		                    break;
		
		                case 13: // return
		                case 9:  // tab
		
		                    this.gotoPage(i);
		                    break;
		
		                default:
		                    (function() {
		                        i = el.value.toInt();
		
		                        if ($NaN(i))
		                            i = this.currentPageNum;
		                        else if (i > this.totalPages)
		                            i = this.totalPages;
		                        else if (i < 1)
		                            i = 1;
		
		                        this.$pageInput.val(i);
		
		                    }).delay(10, this);
		            }					
		        } .bindWith(this));
	    }
	    ,update: function() {
	    	
	        // if the behavior is still being initialized then get the values directly from the controls
	        if (!this.isInitialized) {
	            this.currentPageNum = this.$pageInput.val().toInt();
	            this.totalPages = this.$pageCount.eq(0).text().toInt();
	        }
	        
	        else if (!this.options || !this.options.target)
	            return false;
	        // otherwise get the values from the var elements returned by the ajax call
	        else {
	        
	            this.currentPageNum = this.options.target.find("#pageNumber").text().toInt();
	            
	            this.totalPages = this.options.target.find("#pageTotal").text().toInt();
	
	            this.$pageInput.val(this.currentPageNum > 0 ? this.currentPageNum : 1);
	            this.$pageCount.text(this.totalPages);
	        }
	
	        if (this.totalPages == 1 && this.$viewAll.text().contains("View All")) $(".pagination").hide();
	        this.$pageInput.attr("disabled", this.totalPages == 1);
	        this.currentPageNum > 1 ? this.$prev.removeClass("disabled") : this.$prev.addClass("disabled");
	        this.currentPageNum < this.totalPages && this.$viewAll.text().contains("View All") ? this.$next.removeClass("disabled") : this.$next.addClass("disabled");
	        
	        this.fireEvent("update");
	        return true;
	    }
	    ,nextPage: function(e) {
	        if (e) e.preventDefault();
	        if (this.$next.hasClass("disabled"))
	            return false;
	        this.gotoPage(this.currentPageNum+1);
	    }
	    ,prevPage: function(e) {
	        if (e) e.preventDefault();
	        if (this.$prev.hasClass("disabled"))
	            return false;
	        this.gotoPage(this.currentPageNum-1);
	    }
	    ,gotoPage: function(i) {
	    	if (this.currentPageNum == i)
	    		return false;
	    		
	    	this.$viewAll.text(i > 0 ? "View All" : "Paged View")
	    	//i > 0 ? this.$pagingControls.supaFlyIn() : this.$pagingControls.supaFlyOut();
	    	this.currentPageNum = i;
	    	this.$pageInput.val(i > 0 ? i : "");
	    	
	        this.fireEvent("pagechange", [i]);
	        return true;
	    }
	    ,viewAll: function(e) {
	        if (e) e.preventDefault();
	        
			this.gotoPage(this.currentPageNum == 0 ? 1 : 0);
	    }
    });
    
    /* Tooltip Behavior */
    Behaviors.register("Tooltip", ".ms-WPBody a[id][title]:not([href])", {
    	viewOffset: 10
 		,init: function()
 		{
 			this.title = this.find("h3").text();
 			this.$anchor = this.el.tagName == "A" ? this.$el : this.find("a[title]");
 			this.description = this.$anchor.replaceAttr("title", "");
 									
 			// if title wasnt set through a h3 element then check to see if the description has a title defined within in
 			if (this.description.contains("::") || (!this.title && this.description))
 			{
 				// :: is used to seperate the title from description i.e.: "Jobs::This is description for jobs"
 				var split = this.description.split("::");
 				if (split.length > 1)
 				{
 					this.title = split[0];
 					this.description = split[1];
 				}
 			}
 			
 			this.$tooltip = $("<div class='tooltip'></div>")
				 				.hide()
				 				.appendTo(document.body);
 				
 			if (this.title)
 				this.$tooltip.append("<h3>" + this.title + "</h3>");
 				
 			if (this.description)
 				this.$tooltip.append("<p>" + this.description+ "</p>");
 					
 			var $imgVar = this.find("var");
 			if ($imgVar.length == 1)
 			{
 				this.$tooltip
 					.find("h3")
 						.prepend("<img src='{0}' alt='{1}' height='112' width='222'/>".format(
 							$imgVar.text(),
 							this.title));
 			}
 			else
 				this.$tooltip.addClass("no-image");
 				
 			if (this.$anchor.is("[href]"))
 				this.$tooltip.css("cursor", "pointer");
 		}
 		,initEvents: function()
 		{
 			this.$el
 				.bind("mouseenter", function(){
 					this.hideActive();	
					if (!this.timeout)
 						this.timeout = this.show.delay(500, this);
 						
 					this.closeTimeout = $clear(this.closeTimeout);
 				}.bind(this))
	 			//.bind("mouseleave", this.hide.bind(this))
	 			.bind("mouseleave", function(){
	 				this.timeout = $clear(this.timeout);
	 				this.closeTimeout = this.hide.delay(500, this);
	 			}.bind(this))
	 			.mousemove(function(e){
	 				if (this.timeout && this.$tooltip.not(":visible"))
		 				this.$tooltip.css({
		 					left: (e.pageX + this.viewOffset) + "px",
		 					top: (e.pageY + this.viewOffset) + "px"
		 				});
	 			}.bind(this));
	 			
	 		this.$tooltip
	 			.bind("mouseenter", function(){
	 				this.closeTimeout = $clear(this.closeTimeout);
	 			}.bind(this))
	 			.bind("mouseleave", this.hide.bind(this))
	 			.click(function(){
	 				if (this.$anchor.is("[href]"))
	 					window.location.href = this.$anchor.attr("href");
	 			}.bind(this));
	 			
 		}
 		,hideActive: function()
 		{
 			if (Behaviors.Tooltip.active && Behaviors.Tooltip.active != this.$tooltip)
 				Behaviors.Tooltip.active.hide();
 		}
 		,show: function(x, y)
 		{
 			this.hideActive();
 			
	 		this.timeout = $clear(this.timeout);
	 		Behaviors.Tooltip.active = this.$tooltip.show();
 		}
 		,hide: function()
 		{
 			this.timeout = $clear(this.timeout);
 			this.closeTimeout = $clear(this.closeTimeout);
 			this.$tooltip.hide();
 		}
 	});
 	 	
    /*Tabs Behavior*/
    Behaviors.register("TabControl", "#edit-tab-labels, ul.tab-labels", {
		init: function()
		{
			this.$labels = this.$el.children().click(this.selectTab.bindWith(this));
			this.$tabs = null;
			
			for(var i = 0;i<this.$labels.length;i++)
			{
				var tabId = this.$labels.eq(i).attr("id").substring(3);
				this.$tabs = this.$tabs ? this.$tabs.add("#" + tabId) : $("#" + tabId);
			}
			
			this.$labels.eq(0).click();
		}
		
		,selectTab: function(el, e)
		{
			this.$tabs.hide();
			this.$labels.removeClass("selected");
			
			$(el).addClass("selected");
			this.$tabs.eq(this.$labels.index(el)).show();
		}
	});

	/* FilterMenu Behavior */
	Behaviors.register("FilterMenu", "div.custom-dropdown, div.filter-menu", {	

	    //* Initializers
		init: function()
		{
		    this.$h4 = this.$el.children("h4");
		    this.$div = this.$h4.next("div");
		}
		,initEvents: function()
		{
		    //* attach to html events
		    // make h4 element toggle menu 
			this.$h4.click(function(e){
				e.stopPropagation();
				this.toggle();  
			}.bind(this));
			
			// clicking in the body should hide an open menu
			$("body").click(function(){
		        this.close();
	        }.bind(this));	
	        
	        // 
	        this.$div.find("*").click(function(e){
				e.stopPropagation();
			});	
			
			this.$div.find("li input:checkbox")
			    .click(function(){this.blur()});
			    
			// fire change event whenever any sub-form elements change or keyup events are fired.
			this.$div.find("li input")
			    .bind("keyup change", function(el, e){
			        if (!this.isChanging)
			        {
			            this.isChanging = true;
	                    this.fireEvent("change", [el, e]);
	                    this.isChanging = false;
	                }
	            }.bindWith(this));
		}
		
		//* Methods:
		// TODO: use an explicit isOpen variable to make sure animations dont cause state issues?
		,isOpen: function(){ return this.$div.is(":visible"); }
		,close: function()
		{
		    if (!this.isOpen()) 
		        return false;

		    this.$div.fadeOut();
		    this.fireEvent('close');
		    return true;
		}
		,open: function()
		{
		    if (this.isOpen() || this.disabled)
		        return false;
		        
		    this.$div.fadeIn().fadeIn();
		    this.fireEvent('open');
		    return true;
		}
		,toggle: function()
		{       
		    return this.isOpen() ? this.close() : this.open()
		}
		
		,disable: function()
		{
		    if (this.disabled) return;
		    
		    this.disabled = true;
		    this.$h4.addClass("disabled");
		    this.close();
		    this.fireEvent('disable');
		}
		,enable: function()
		{
		    if (!this.disabled) return;
		    
		    this.disabled = false;
		    this.$h4.removeClass("disabled");
		    this.fireEvent('enable');
		}
		,getQueryParams: function()
		{
			return this.$el.collectValues().formValues.toQueryParams();
		}
	});   

    //* Global Nav Behavior
    Behaviors.register("GlobalNav", "#global-nav", (function()
    {   
    	function updateClass(className, oldEl, newEl)
    	{
    		if (oldEl)
    			$(oldEl).removeClass(className);
    			
    		return newEl ? $(newEl).addClass(className) : null;
    	}
    	
        return {
    	
	        //* Construction:	
	        init: function()
	        {	        	
		        //* init HTML and element pointers
        		
		        this.$nav = $("#global-nav").bind("mouseleave", this.blur.bind(this));
		        
		        $("#global-nav>ul>li")
			        .mouseover(this.activateMenu.bindWith(this))
			        .find("ul>li:has(ul>li>a)")
			            .addClass("hasChildren");	  		
	        }			
	        //* Methods
	        ,activateMenu: function(menu, e)
	        {
	        	this.setTimeout();
	        	
	        	if (this.activeMenu == menu)
	        		return false;
	        	
				var $menu = updateClass("over", this.activeMenu, this.activeMenu = menu);
												        
        		//BUGFIX: IE is giving issues on re-displaying the menu div so we will instead remove and then re-add it each time the menu is re-opened.
        		if (!this.$menuDiv || this.$menuDiv.is(":hidden"))
        		{
        			if (this.$menuDiv) this.$menuDiv.remove();
        			
	        		// create and append the menu divs
			        this.$menuDiv = $("<div id='secondary-nav'></div>")
				        .appendTo(this.$nav)
				        .mouseover(this.setTimeout.bind(this));
				}
				   
		        var childNodes = $menu.children("ul");
		        childNodes.clone().appendTo(this.$menuDiv.empty());
    			
    			var anchor = $menu.children(":first");
			    this.$subMenuDiv = $("<div id='tertiary-nav'></div>").appendTo(this.$menuDiv).hide();
			        
    			if (childNodes.length > 0)
    			{
			        $("#secondary-nav li").click(this.activateSubMenu.bindWith(this));
	    					
			        this.$menuDiv.supaFlyIn();
			        this.$menuDiv				        
			        	.find("a")
				        .mousedown(function(e){
					        e.preventDefault();
				        });
				        
				    this.fireEvent("menuactivated", [menu, e]);
				 }
				 else
				 	updateClass("over", this.activeMenu, this.activeMenu = null);
			}
	        ,blur: function(e)
	        {	
		        if (this.$menuDiv && this.$menuDiv.is(":visible"))
			        this.setTimeout(this.collapse.delay(1000, this));
	        }
	        ,activateSubMenu: function(subMenu, e)
	        {	        	
	            var $subMenu = $(subMenu);
		        var childNodes = $subMenu.children("ul");
		        // if there is a sub menu and this menu isnt already selected
		        if (childNodes.length > 0)
		        {
		        	// if not selected then select now
		        	if (subMenu != this.activeSubMenu)
		        	{
		        		updateClass("isSelected", this.activeSubMenu, this.activeSubMenu = subMenu);
		        		var $subAnchor = $subMenu.children(":first");
		        		var $anchor = $(this.activeMenu).children(":first");
		        		this.$subTitleDiv = $("<div><a href='{0}' title='{1}'><h5>{2}<span>&nbsp;</span></h5>{3} Page</a></div>"
		        			.format(
		        				$subAnchor.attr("href"),
		        				$subAnchor.attr("title"),
		        				$subAnchor.attr("title"),
			        			$anchor.text().substring(0,$anchor.text().length-1)
		        			)).appendTo(this.$subMenuDiv.empty());
		        	
	        		   	childNodes.clone().appendTo(this.$subMenuDiv);
				        //this.$ftr.appendTo(this.$subMenuDiv);
				        	//.children("a")
				        		//.text("See All " + $subMenu.children(":first").text() + " " + this.$ftr.name);
				       	
				        this.$subMenuDiv.supaFlyIn(true);
        			}
        			// otherwise unselect
        			else
        			{
        				updateClass("isSelected", this.activeSubMenu, this.activeSubMenu = null);
        				this.$subMenuDiv.supaFlyOut(true);
        			}
			        e.preventDefault();
			    }
		        // otherwise just let the event continue so that the href is handled
	        }
	        ,collapse: function(bForce)
	        {		
		        if (this.timeout || bForce)
		        {
			        updateClass("over", this.activeMenu, this.activeMenu = null);
			        	
			        if (this.$menuDiv)
			        	this.$menuDiv.supaFlyOut();
			        	
			        this.setTimeout();
		        }
	        }
	        ,setTimeout: function(newValue)
	        {
	        	if (this.timeout)
			        $clear(this.timeout);
			    
		       	this.timeout = newValue || null;
	        }
	    }
    })());
    
    Behaviors.register("NavSearch", "#nav-search", {
    	/*reqScripts: [
    		"/PublishingJS/Search-Suggestions.js",
    		"/js/jquery.autocomplete.js"    	
    	]

		,*/
		
		init: function()
		{
			this.$input = this.$el.find("input:text")
				.autocomplete(Behaviors.SEARCH_SUGGESTIONS || []);
				
			this.$menuLbl = this.find("h4");
			this.$menu = this.$menuLbl.next("ul");
			
			var qsHash = window.location.getQueryHash();
			//set the initial selected item in order of preference.
			var s = "li.selected, li:contains('{0}'), li:contains('All Sites'), li:first"
				.format(qsHash && qsHash.has("s") ? qsHash.get("s") : "Entire Site");
				
			this.select(this.$menu.children(s));
			
		}
		,initEvents: function()
		{
			this.$el.mousedown(function(e){
				Behaviors.first("GlobalNav").collapse(true);
			});
			
			Behaviors.first("GlobalNav").addEvent("menuactivated", this.blur.bind(this));
			
			this.$menuLbl.click(function(e){
				this.isExpanded ? this.collapse() : this.expand();
				utilPos = (this.isExpanded) ? "static":"relative";
				$("#utils").css("position",utilPos);
				e.stopPropagation();
			}.bind(this));
			
			this.$input.focus(this.collapse.bind(this));
			
			$(document.body)
				.click(this.collapse.bindWith(this));
			
			this.$menu
				.children()
					.linkHover()
					.click(this.select.bindWith(this));
					
			this.find("button").preventDefault();
		}
		,blur: function()
		{
			this.$input[0].autocompleter.hideResultsNow();
			this.$input.blur();
			
			this.collapse();
		}
		,expand: function()
		{
			if (this.isExpanded) 
				return false;
			
			this.$menu.fadeIn();
			this.isExpanded = true;
			return true;
		}
		,collapse: function()
		{
			if (!this.isExpanded) 
				return false;
				
			this.$menu.fadeOut();
			this.isExpanded = false;
			return true;
		}
		,select: function(el)
		{
			this.$menuLbl.children("span").text($(el).eq(0).text());
			this.find("#OSB_Scopes").val($(el).attr("name"));
			this.collapse();
		}	
	});
	
	Behaviors.register("SlideShow", ".wp-slideshow", {
        reqScripts: [SCRIPTS.JUI]
    	,inactiveImgOpacity: .6
		,slideDuration: 10
		
		,init: function()
		{    
		    this.$ul = this.$el.find("ul:first");
		    this.$li = this.$ul.find("li");
		    this.imgCount = this.$li.length;
		    
		    this.layout = this.isRail 
		        ? this.imgCount > 1 
		            ? Behaviors.SlideShow.LAYOUT_CAROUSEL 
		            : Behaviors.SlideShow.LAYOUT_SINGLE 
		        : Behaviors.SlideShow.LAYOUT_SLIDE;	
		}
		,initLayout: function()
		{
		    this.visibleNdx = 0;
		    
		    switch (this.layout)
		    {
		        case Behaviors.SlideShow.LAYOUT_CAROUSEL:
		            //this.stopNdx = (this.imgCount - 3) + (this.imgCount % 3);
		            this.stopNdx = (Math.ceil(this.imgCount / 3) -1) * 3;
		             
		            this.$ul.wrap('<div class="image-strip"><div /></div>');
		            this.$el
		                .children()
		                    .prepend('<span class="prev-arrow" title="Previous">&nbsp;</span>')
		                    .append('<span class="next-arrow" title="Next">&nbsp;</span>');
		                    
		            this.$content = $("<div class='content-box'></div>").appendTo(this.$el);
		            
		            this.$li.css("opacity", this.inactiveImgOpacity);	
		            
		            // disable mouse selection on image strip
					$.ui.disableSelection(this.find(".image-strip").get(0));

		            this.$ul.show();
		            break;
		            
		        case Behaviors.SlideShow.LAYOUT_SLIDE:
		        	this.$el.parent().removeClass("rail-module");
		            this.stopNdx = this.imgCount-1;
		            if (this.imgCount > 1)
                    {
		                this.$arrows = $('<ol><li class="prev-arrow">&nbsp;</li><li class="slide-number"></li><li class="next-arrow">&nbsp;</li></ol>')
		                    .appendTo(this.$el);
    		                
		                this.$slideInfo = this.$arrows.children().eq(1);
		            }
		            break;
		            
		        case Behaviors.SlideShow.LAYOUT_SINGLE:
		            this.$el.addClass("wp-slideshow-single")
		            return;
		    }
		    
		    if (this.imgCount > 0)
            {
                this.select(0);
		        
                if (this.imgCount > 1)
                    this.resetTimer();
            }
	    }
		,initEvents: function()
		{
		    this.$el.find(".prev-arrow").click(this.previous.bind(this));	
	        this.$el.find(".next-arrow").click(this.next.bind(this));
	        var self = this;
	        
		    switch (this.layout)
		    {
		        case Behaviors.SlideShow.LAYOUT_CAROUSEL:
		            this.$li
	                    .hover(function(){
	                        if (self.$selectedList && self.$selectedList.get(0) != this)
	                            $(this).css("opacity", .9);
	                       },function(){
	                        if (self.$selectedList && self.$selectedList.get(0) != this)
	                            $(this).css("opacity", self.inactiveImgOpacity);
	                    })
	                    .children("img")
	                    	.click(function(e){
		                    	self.select(self.$li.index(this.parentElement));
		                        e.preventDefault();
		                        e.stopPropagation();
		                    });
	                    
		            break;
		            
		        case Behaviors.SlideShow.LAYOUT_SLIDE:
		            break;
		    }

		}
		,next: function(e)
		{
			if (this.selectedNdx == (this.imgCount-1))
			{
				if (this.stopNdx == 0)
					this.select(0);
				else
		        	this.slideTo(0);
		    }   
		    else if (this.selectedNdx < this.stopNdx && 
		        this.selectedNdx > this.visibleNdx + 1)
		        this.slideTo(this.selectedNdx + 1);  
		        
		    else
		        this.select(this.selectedNdx + 1);
		        
		    this.resetTimer();   
		}
		,previous: function()
		{
		    if (this.selectedNdx == 0)
		    {
		    	if (this.stopNdx == 0)
		    		this.select(this.imgCount-1);
		    	else
		        	this.slideTo(this.stopNdx, this.imgCount-1);
		    }   
		    else if (this.selectedNdx > this.visibleNdx)
		        this.select(this.selectedNdx-1);
		    else
		        this.slideTo(this.visibleNdx - 3, this.visibleNdx - 1);
		        //this.slideTo(this.visibleNdx - 1);
		        
		    this.resetTimer();
		}
	    ,slideTo: function(ndx, selectedNdx)
	    {
	        if (ndx < 0)
	            return this.slideTo(this.stopNdx + (ndx+1));
	            
	        else if (ndx > this.stopNdx)
	            return this.slideTo((ndx-1) % this.stopNdx);
	        
	        if (this.layout == Behaviors.SlideShow.LAYOUT_CAROUSEL)  
	        {
	            if (ndx == this.visibleNdx)
	                return false;
	            
	            this.$ul.animate({
		            left: (ndx * -74) + "px"
		        });
		    }
		    this.select(selectedNdx || ndx);
		    
		    return (this.visibleNdx = ndx) && true;
	    }

	    ,select: function(ndx)
	    {
	        if (ndx == this.selectedNdx)
	            return false;
	            
	        switch (this.layout)
		    {
		        case Behaviors.SlideShow.LAYOUT_CAROUSEL:
		        
		            // hide current content
                    this.$content.hide();
                    //* unselect current item
	                if (this.$selectedList && this.$selectedList.length > 0)
	                {
	                    this.$selectedList.css("opacity", this.inactiveImgOpacity);
	                    this.$selectedList.removeClass("selected");
	                }
        	        
        	        //* select new item and attach its content with effect  
	                this.$selectedList = this.$li.eq(ndx);
	                this.$selectedList
	                    .css("opacity", 1)
	                    .addClass("selected")
	                    .find("div *")
	                        .clone()
	                        .appendTo(this.$content.empty());
        	        
	                this.$content.customFadeIn("slow");
		            break;
		            
		        case Behaviors.SlideShow.LAYOUT_SLIDE:
		            // hide previously selected list
		            if (this.$selectedList && this.$selectedList.length > 0)
	                    this.$selectedList.hide();
	                    
		            this.$selectedList = this.$li.eq(ndx);
		            
		            //* fade the LI in while fading its img in a bit slower for affect
		            this.$selectedList.find("img").hide();
		            this.$selectedList.customFadeIn();
		            this.$selectedList.find("img").customFadeIn("slow");
		            
		            //* update slide info
		            if (this.$slideInfo)
		                this.$slideInfo.text((ndx+1) + " of " + this.imgCount);
		            break;
		    }
	            
	        return (this.selectedNdx = ndx) && true;
	    }
	    ,resetTimer: function()
	    {
	        if (this.timeout)
	            $clear(this.timeout);
	            
	        if (this.slideDuration > 0)
	        	this.timeout = this.next.delay(this.slideDuration * 1000, this);
	    }
	});
	
	Behaviors.register("collegeConnectionsList", "#college-connections-list", {
		init: function()
			{
				//* College Alumni Shading
				$("#college-connections-list li ul li:nth-child(odd)").addClass("striped");
				$("#college-connections-list li ul li ul > li").removeClass("striped");
				
				$("#college-connections-list ul li div p>span b").click(function(){
					var isVisible = $("span",$(this).parent()).is(':visible');
					$("#college-connections-list ul li div p span span").fadeOut();
					$("#college-connections-list ul li div p span b").fadeIn(400, function() {
						$(this).css('zoom', '');
					}).html("Read more &raquo;");
					
					if (!isVisible){
						$("span",$(this).parent()).fadeIn(400, function() {
							$(this).css('zoom', '');
						});
						$(this).html("&laquo; Read less ");
					} else  $(this).html(" Read more &raquo;");
				});
			}
	});	
	
	Behaviors.SlideShow.LAYOUT_CAROUSEL = "carousel";
	Behaviors.SlideShow.LAYOUT_SLIDE = "slide";
    Behaviors.SlideShow.LAYOUT_SINGLE = "single";
    
    
	
})(jQuery);