﻿$(document).ready(function(){
	navLastItems();
	customBreadcrumb();
	$('#tab-zone-layout').wpTabify();
	addContact();
	removeLinks();
	blueNav();
	buildDropDowns(); 
	insideThisIssue();
	randomTip();
	GetCollegeImages('.cic-college-rotor');
	
	$('li:first-child .slideshow-item').show();
	$('.slideshow-item a').click(function() {
		var ID = $(this).attr('class');
		$("a[class='" + ID + "']").colorbox({rel:'' + ID + '', slideshow:true});
	});
// loop through each audio resource
$('.resource-audio').each(function(i){
      
      // get the audio file information
      var audioUrl = $(this).find('.resource-audio-url').text();
      var audioTitle = $(this).find('.resource-audio-title').text();
      // replace the contents with a link that creates the player <object>
      $(this).html('<a title="' + audioTitle + '" class="play-link" href="/Style Library/CIC/scripts/AudioPlayer.aspx?audiourl=' + audioUrl  + '&audiotitle=' + audioTitle + '">' + audioTitle + '</a>');
	$(this).show();
      $(this).find('.play-link').click(function(e){
            e.preventDefault();
            $('.play-link').colorbox({iframe:true, width:"323px", height:"92px"});
      });
});
	confDate();
/*	rightColH3(); */
	removeArticleNumber();
	footerHeight();
	$('#tab-zone-layout a').click(function() {
		$('#left-nav').height('auto');
		$('#main-content').height('auto');
		$('#right-col').height('auto');
		$('#footer').height('172px');
		ColHeight();
		footerHeight();
	});
	$('#share a').click(function() {
		sharethis();
	});
});


$(window).load(function(){

	ColHeight();

});




$(function() {
    $("#main-nav ul.root > li.static").hover(function() {
      var timeout = $(this).data("timeout");
      if(timeout) clearTimeout(timeout);
		$('#main-nav ul.root > li.static > ul.dynamic').fadeOut(0);
      $(this).find("ul.dynamic").delay(300).css('left', '-1px').css('top', '59px').fadeIn(200);
    }, function() {
        $(this).data("timeout", setTimeout($.proxy(function() {
            $(this).find("ul.dynamic").removeAttr('style').css('left','-999em');
            $(this).find("ul.dynamic").removeAttr('style');
        }, this), 0));
    });
});


function FixRibbonAndWorkspaceDimensions(){
  ULSxSy:;
  g_frl = true;
  var elmRibbon = GetCachedElement("s4-ribbonrow");
  var elmWorkspace = GetCachedElement("s4-workspace");
  var elmTitleArea = GetCachedElement("s4-titlerow");
  var elmBodyTable = GetCachedElement("s4-bodyContainer");
  if(!elmRibbon || !elmWorkspace || !elmBodyTable){
    return;
  }
  if (!g_setWidthInited){
    var setWidth = true;
    if (elmWorkspace.className.indexOf("s4-nosetwidth") > -1)
      setWidth = false;
    g_setWidth = setWidth;
    g_setWidthInited = true;
  }
  else{
    var setWidth = g_setWidth;
  }
  var baseRibbonHeight = RibbonIsMinimized() ? 44 : 135;
  var ribbonHeight = baseRibbonHeight + g_wpadderHeight;
  if(GetCurrentEltStyle(elmRibbon, "visibility") == "hidden"){
    ribbonHeight = 0;
  }

  // Override default resizing behavior
  // -- adds padding to the top of the "s4-workspace" <div> if the ribbon exists and has content
  // -- allows the ribbon to be positioned using CSS instead of JavaScript (more accessible)
  // -- checks to see if the page is inside a "no-ribbon" dialog
  if(elmRibbon.children.length > 0 && document.getElementsByTagName("html")[0].className.indexOf('ms-dialog-nr') == -1){
    elmWorkspace.style.paddingTop = ribbonHeight + 'px';
  }
  else{
  	$('#s4-workspace').addClass('noRibbon');	
  }
}

	
function ColHeight() {

	var lheight = parseInt($('#left-nav').css('height'));
	var rheight = parseInt($('#right-col').css('height'));
	var mheight = parseInt($('#main-content').css('height'));
	var theight = parseInt($('h1.title').css('height'));
	var bheight = parseInt($('#banner-image').css('height'));
	var bimage = parseInt($('#banner-image img').css('height'));
	var theightpad = 20;
	var mwidth = parseInt($('h1.title').css('width'));

	if(mwidth < 705) {
		if (bheight > 110) {
			$('#banner-image').height(bimage+1);
		}
		if(lheight > mheight) {
		    if(lheight > rheight) {
		    	if (bheight == 110) {
		    		$('#right-col').height(lheight-bheight);
		    	}
		    	else {
		    		rheight = lheight;
		    		$('#right-col').height(rheight);
		    	}
		    }
		    else {
		    	if (bheight == 110) {
		    		$('#left-nav').height(mheight+bheight);
		    	}
		    	else {
			    	$('#left-nav').height(mheight);
			    }
		    }
		}
		else {
	    	if (bheight == 110) {
	    		$('#left-nav').height(mheight);
	    	}
	    	else {
		    	$('#left-nav').height(mheight);
		    	$('#right-col').height(mheight);
		    }
		}
	}
	else {
		if (bheight > 110) {
			$('#banner-image').height(bimage+1);
		}
		if(lheight > mheight) {
		    if(lheight > rheight) {
		    	var midheight = (lheight-theight-theightpad-bheight);
		    	$('#main-content').height(lheight);
		    	$('#right-col').height(midheight);
		    }
		}
		else {
		    var midheight = (mheight-theight-theightpad-bheight);
			$('#left-nav').height(mheight);
	    	$('#right-col').height(midheight);
		}
	}
}


function footerHeight() {
	var pHeight = $('#page').height();
	var bHeight = $(window).height()-$('#s4-ribbonrow').height();
	if (pHeight < bHeight) {
		var dHeight = bHeight-pHeight;
	}
	else {
		dHeight = 0;
	}
	var fHeight = $('#footer').height();
	
	$('#footer').height(fHeight+dHeight);
}

function mainNavAnds() {
$('#main-nav ul.root > li.static > a > span > span.menu-item-text:contains(" and ")').each(function(i) {
	$(this).html($('#main-nav  ul.root > li.static > a > span > span.menu-item-text:contains(" and ")').html().replace(' and ','<span class="mainNavAnd"></span>'));
});
}

function navLastItems() {
	$('#main-nav ul.root > li.static > a > span > span.menu-item-text:contains("FIHE")').parents().filter('a').addClass('navLast');
}

(function($){$.fn.wpTabify = function(){ if($('.ms-WPAddButton').size() == 0){ return this.each(function(i){ var tabList = $('<ul class="wpt-ui-tabs-nav"/>'); var panels = $('<div class="wpt-ui-tabs-wrapper"/>'); $(this).find('.s4-wpTopTable,td[id^="MSOZoneCell_"] > table').each(function(j){ $(tabList).append('<li><a href="#ui-tab-panel' + i + j + '"><span class="tab-left"></span>' + $(this).find('h3.ms-WPTitle').text() + '<span class="tab-right"></span></a></li>'); var thisPanel = $('<div id="ui-tab-panel' + i + j + '" class="wpt-ui-tabs-panel"/>'); var panelContents = $(this).detach(); $(thisPanel).append($(panelContents).find('.ms-WPBody').html()); $(panels).append(thisPanel); }); if($(tabList).find('li').size() > 0){ $(this).prepend(panels); $(this).prepend(tabList); $(this).tabs(); } }); } else{ return false; } };})(jQuery);

function emailFriend() {
	var email = "mailto:?subject=Sending You a Link From the CIC Website:  " + document.title;
	email += "&body=I thought you might be interested in this page on The Council of Independent Colleges website. \n";
	email += location.href;
	location.href = email;
}

function removeLinks() {
	$('#left-nav ul.root > li.static > a.static > span.additional-background > span.menu-item-text').each(function() {
		var linkText = $(this).text();
		$(this).parents().filter('a').wrap('<div class="left-nav-header" />');
		$(this).parents().filter('a').replaceWith(linkText);
	});
}

function buildDropDowns() {
	var counter = 0;
	$('#main-nav ul.root > li.static > ul.dynamic > li.dynamic > a.dynamic > span.additional-background > span.menu-item-text').each(function() {
		var linkText = $(this).text();
		$(this).parents().filter('a').wrap('<div class="main-nav-header" />');
		$(this).parents().filter('a').replaceWith(linkText);
	});
	$('#main-nav ul.root > li.static > ul.dynamic > li.dynamic > ul.dynamic > li.dynamic').each(function() {
		var linkHTML = $(this).html();
		$(this).replaceWith(linkHTML);
	});
	$('#main-nav ul.root > li.static').each(function() {
		counter = 0;
		$(this).find('ul.dynamic > li.dynamic > ul.dynamic').each(function() {
			counter++;
			$(this).parent().addClass('drop' + counter);
			var linkHTML = $(this).html();
			$(this).replaceWith(linkHTML);
	
			if (counter == 4) {
				counter = 0;
			}
		});
	});
}

function addContact() {
	$('#bottom-nav ul.root > li.static > ul.static').append('<li class="static"><a href="/About-CIC/Who-We-Are/Pages/Staff-and-Senior-Advisors.aspx" class="static menu-item navLast"><span class="additional-background"><span class="menu-item-text">Contact Us</span></span></a></li>');
}

function customBreadcrumb() {
	$("#ctl00_PlaceHolderMain_breadcrumb > span:eq(4)").hide();
	$("#ctl00_PlaceHolderMain_breadcrumb > span:eq(5)").hide();
}

function blueNav() {
	$('.blue-nav').parents().filter('ul').wrap('<div id="conferenceNav" />');
	$('#conferenceNav').prepend('<div class="corner TL"></div><div class="corner TR"></div><div class="corner BL"></div><div class="corner BR"></div>');
}

/*function deadlineDate() {
	var deadlineDate = $('#deadline-date').text();
	var fullDate1 = new Date(deadlineDate);
	var fullDate = new Date()
	$('#deadline-date').hide();
	$('#deadline-date .ms-formfieldcontainer').parent().show();
	if (deadlineDate.length < 11) {
		$('#deadline').hide();
	}
	if (fullDate1.getFullYear() <  fullDate.getFullYear()) {
		$('#deadline').hide();
	}
	if (fullDate1.getFullYear() == fullDate.getFullYear()) {
		if ((fullDate1.getMonth()+1) < fullDate.getMonth()+1) {
			$('#deadline').hide();
		}
		else {
			if (fullDate1.getDate() < fullDate.getDate()) {
				$('#deadline').hide();
			}
		}
	}
}
*/

function sharethis() {
	$('#share .list').toggle();
}


function insideThisIssue() {
	$('.groupheader:contains("Conferences")').addClass('group-conferences');
	$('.groupheader:contains("Desk")').addClass('group-desk');
	$('.groupheader:contains("Projects")').addClass('group-projects');
	$('.groupheader:contains("Campus")').addClass('group-campus');
	$('.groupheader:contains("Information")').addClass('group-news');
	$('.groupheader:contains("Board")').addClass('group-board');
	$('.groupheader:contains("Books")').addClass('group-books');
	$('.article-section:contains("Conferences")').addClass('title-conferences');
	$('.article-section:contains("Desk")').addClass('title-desk');
	$('.article-section:contains("Projects")').addClass('title-projects');
	$('.article-section:contains("Campus")').addClass('title-campus');
	$('.article-section:contains("Information")').addClass('title-news');
	$('.article-section:contains("Board")').addClass('title-board');
	$('.article-section:contains("Books")').addClass('title-books');
	$('.article-section:contains("N/A")').hide();
}


function removeArticleNumber() {
$('.article-section').each(function(i) {
	var articleString = $(this).text().trim();
	$(this).html(articleString.substring(2));
	$(this).show();
});
}


function equalHeight() {
 var tallest = 0;
 $('ul.root > li.static > ul.dynamic > li.dynamic').each(function(index) {
  var thisHeight = $(this).height();
  if(thisHeight > tallest) {
   tallest = thisHeight;
  }
 });
 $('ul.root > li.static > ul.dynamic > li.dynamic').height(tallest);
}


function randomTip(){
	$('.quote-text').parents().filter('ul.dfwp-column').addClass('quotesList');
	var length = $('.quotesList li').length;
	var ran = Math.floor(Math.random()*length) + 1;
	$(".quotesList li:nth-child(" + ran + ")").children().show();
}

function confDate() {
	var cStart = $('.conf-start').text();
	var cEnd = $('.conf-end').text();
	var startMonth = cStart.substr(0,cStart.indexOf(" "));
	var endMonth = cEnd.substr(0,cEnd.indexOf(" "));
	var startDay = cStart.substring(cStart.indexOf(' '),cStart.indexOf(','));
	var endDay = cEnd.substring(cEnd.indexOf(" "),cEnd.lastIndexOf(","));
	var startYear = cStart.substring(cStart.lastIndexOf(",") + 1);
	var endYear = cEnd.substring(cEnd.lastIndexOf(",") + 1);
	if (startYear != endYear) {
		$('#conf-date').prepend(startMonth + ' ' + startDay + ', ' + startYear + ' - ' + endMonth + ' ' + endDay + ', ' + endYear + ' ');
	}
	else {
		if (startMonth != endMonth) {
			$('#conf-date').prepend(startMonth + ' ' + startDay + ' - ' + endMonth + ' ' + endDay + ', ' + startYear + ' ');
		}
		else {
			if (startDay != endDay) {
				$('#conf-date').prepend(startMonth + ' ' + startDay + ' - ' + endDay + ', ' + startYear + ' ');
			}
			else {
				$('#conf-date').prepend(startMonth + ' ' + startDay + ', ' + startYear + ' ');
			}
		}
	}
}


function rightColH3() {
	$('#right-col-inner > table > tbody > tr:first-child > td > .s4-wpTopTable h3.ms-WPTitle').addClass('first-title');
}

function GetCollegeImages(destination) {

	try {
		
		if($(destination).size() > 0) {
			$(destination).html('loading...');
		  var query = "<Query><Where><And><Eq><FieldRef Name='CollegeMemberType' /><Value Type='Choice'>Institutional</Value></Eq><Eq><FieldRef Name='SectionHighlight' /><Value Type='Integer'>0</Value></Eq></And></Where></Query>";
		  $().SPServices({
		    operation: "GetListItems",
		    async: false,
		    listName: "Colleges",
		    CAMLQuery: query,
		    CAMLViewFields: "<ViewFields><FieldRef Name='ID' /><FieldRef Name='CollegeTitle' /><FieldRef Name='CollegeURL' /></ViewFields>",
		    completefunc: function (xData, Status) {
		      
		      //create array to hold ids
		      var count = $(xData.responseXML).find("[nodeName='z:row']").size();		    	
		      
		      $(destination).html('');
		      // generate random number		      
		      for(var i = 0; i < 5; i++) {
			      var randomNumber = (Math.round((Math.random()*count)+1));
			      var item = $(xData.responseXML).find("[nodeName='z:row']")[randomNumber];
			      var imageRef = "/"+$(item).attr("ows_FileRef").split('#')[1];
			      var title = $(item).attr("ows_CollegeTitle");
			      var url = $(item).attr("ows_CollegeURL").split(',')[0];
			      
			      if(imageRef != "") {
				      var itemHtml = '' +
			    		'<div class="rotor-college"><div class="rotor-top"><div class="rotor-tl">&#160;</div><div class="rotor-tr">&#160;</div></div>' +
						'<div class="item rotor-banner">' +
							'<div class="link-item">' +
								'<div class="link-item-inner">' +
								'<div class="rotor-banner-image">' +
									'<a target="_blank" href="'+url+'">' +
									     '<img src="'+imageRef+'" />' +
									'</a>' +
								'</div>' +
								'<div class="title">' +
									'<a target="_blank" href="'+url+'">'+title+'</a>' +
								'</div>' +
								'</div>' +
							'</div>' +
						'</div>' +
			    		'<div class="rotor-bot"><div class="rotor-bl">&#160;</div><div class="rotor-br">&#160;</div></div></div>';		    		
				      
				      $(destination).append(itemHtml);			      
					}				      
		      }		      		   		         		      
		      
				$(destination).cycle({
				    fx:      'fade',
				    timeout:  8000,
				    prev:    '#collegePrevLink',
				    next:    '#collegeNextLink'
				});
				
		      $('.cic-college-rotor').append('<div class="view-all once"><a href="/About-CIC/Who-We-Are/Members-of-CIC/Pages/default.aspx" alt="">View All CIC Members</a></div>');				
		    }
		  });
		}

	}
	catch (err) {$(destination).html('error loading colleges');}

}
