jQuery.noConflict();

jQuery(document).ready(function () {
	
	/**
	 * Carousel Controls
	 **/
	 
	 var supports = {video:false, h264:false, oggtheora:false, webm:false};
	 var player = document.createElement('video')
	 supports.video = !!player.canPlayType;
	 if(supports.video) {
		supports.h264 = player.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
		supports.oggtheora = player.canPlayType('video/ogg; codecs="theora, vorbis"');
		supports.webm = player.canPlayType('video/webm; codecs="vp8, vorbis"');
	 } 
	 
	jQuery("#main-content > .carousel").children('div').addClass('largeCarousel').children('ul').addClass('largeCarousel');
	jQuery("#main-content .columns .carousel").children('div').addClass('mediumCarousel').children('ul').addClass('mediumCarousel');
	
	var carousel_outer = jQuery("#main-content .carousel");
	var carousel_inner = carousel_outer.children('div').children('ul');
	var carousel_pages = carousel_outer.find('ul.pages');
	var carousel = carousel_inner.jcarousel({
		vertical: false,
		scroll: 1,
		visible:1,
		initCallback: carousel_init,
		itemVisibleOutCallback: carousel_shift,
		itemFallbackDimension: 923
	});
	
	function carousel_init(carousel) {
		carousel_pages.children('li').each(function (i) {
			jQuery(this).bind('click', function() {
				carousel.scroll(i+1, true);
				return false;
			});
		});
	}
	
	function carousel_shift(carousel) {
		carousel_pages.children('li').each(function (i) {
			var vid = carousel_inner.children('li').eq(i).children('video').get(0);
			if(i == carousel.first - 1) {
				jQuery(this).addClass('active');
				// Resume any visible html videos that were previously held
				autoresumeVideo(vid);
			} else {
				jQuery(this).removeClass('active');
				// Pause any non-visible html videos in this carousel
				autoholdVideo(vid);
			}
		});
	}
	
	function autoholdVideo(vid) {
		if(supports.video && vid != null && !vid.paused) {
			// A video that is paused by the user should not be held or it will auto-resume the next time the user visits this carousel item
			vid.pause();
			vid.held = true;
		}
	}
	
	function autoresumeVideo(vid) {
		if(supports.video && vid != null && vid.held) {
			// Only auto-resume videos that were auto-held without user intervention 
			vid.play();
			vid.held = false;
		}
	}
	
	/* ---------------
	* Search Box 
	* ---------------**/

	
	
	var searchPlaceholder = "SEARCH >";
	var searchText = queryString("q");
	var searchBox = jQuery("input[id$='_txtSearch']");

	
	
	if(searchText != null && searchText != '') {
		// Fill Search Results if on search page
		var qs = window.location.search.substring(1);
		if (queryString('cx') == null) {
			qs += "&cx=017280725682041736050%3Atcdjegxhkvk&cof=FORID%3A10&ie=UTF-8";
		}
		var searchResultContainer = document.getElementById('searchResultContainer');
		if(searchResultContainer != null) {
			searchResultContainer.innerHTML = '';
			var searchResults = document.createElement('iframe');
			searchResults.setAttribute('id', 'searchResults');
			searchResults.setAttribute('frameBorder', '0');
			searchResultContainer.appendChild(searchResults);
			searchResults.src = 'http://www.google.com/cse?' + qs;
		}
	}
	
	searchBox.keydown(function(event) { 
		if (event.keyCode == '13') {
			event.preventDefault(); 
			window.location.href="/aboutus/search.aspx?q=" + searchBox.val() + "&cx=017280725682041736050%3Atcdjegxhkvk&cof=FORID%3A10&ie=UTF-8";
		} 
	}); 


	function queryString(key) {
		var querystringvars = window.location.search.substring(1).split("&");
		for (i = 0 ; i < querystringvars.length;i++) {
			keyvaluepair = querystringvars[i].split("=");
			if (keyvaluepair[0] == key) {
				return keyvaluepair[1];
			}
		}
		return null;
	}
	
	
	
	/* -------------
	 * Modal windows
	 * ------------*/
	 // match css class named "modalSize[Width]x[Height]" where [Width] is the integer width and [Height] is the integer height of the modal window
	// ex: modalSize500x200. Negative width or height signify default width or height.
	function getModalSizeFromClasses(classes) {
		if(classes != null && classes.length >= 11) {
			var matches = classes.match(/modalSize[-]?[0-9]+x[-]?[0-9]+/i);
			
			if(matches != null && matches.length > 0) {
				var size = (matches[matches.length - 1].slice(("modalSize").length)).split("x");
				size[0] = parseInt(size[0]);
				size[1] = parseInt(size[1]);
				return size;
			}
		}
		return null;
	}
	
	function getModalSize(element) {
		return getModalSizeFromClasses(jQuery(element).attr('class'));
	}
	
	/* ---------------------------------------------------------------------------
	 * Create an iframe with the src attribute set to the href attribute of a link 
	 * who has the class "iFrame" Also looks for the 
	 * modalSizeWidthxHeight css class
	 * ------------------------------------------------------------------------- */
	jQuery(".iFrame").each(function () {
		var size = getModalSize(this);
		var url = jQuery(this).attr('href');
		if(size == null || size.length < 2) {
			size = new Array(640, 480);

		}
		var iframe = jQuery("<iframe frameborder=\"0\" src=\"" + url + "\" />");
		iframe.css('border-width', '0px');
		iframe.css('width', size[0] + "px");
		if(jQuery.browser.msie && parseInt(jQuery.browser.version) < 9) {
			iframe.css('height', Math.round(size[1] - (size[1]*.027)) + "px");
		} else {
			iframe.css('height', size[1] + "px");
		}
		jQuery(this).replaceWith(iframe);
	});

	/* Only displays modal on load if the user does not have a cookie with the same name as the value of the link's "name" attribute
	 * The number of days the cookie lasts (when the modal does not display) goes between the <a> and </a> tags. 
	 * Example: <a href="modalUrl" name="modal" class="modalSize500x200" id="modalOnLoad">14</a>
	 */
	jQuery("#modalOnLoad").each(function () {
		var link = jQuery(this);
		link.hide();
		if(cookieSupport()) {
			var name = link.attr('name');
			var ie6 = link.hasClass('ie6');
			if(!ie6 || (jQuery.browser.ie && jQuery.browser.version.substr(0,1) == "6")) {
				
				if(getCookie(name) == null) {
					var expires = new Date();
					var duration = parseInt(link.html()) * 86400000; // Milliseconds in a day
					expires.setTime(expires.getTime() + duration);
					var size = getModalSize(link);
					if(size == null || size.length < 2) {
						link.fancybox();
					} else {
						var params = {};
						params['hideOnContentClick'] = false;
						params['showCloseButton'] = true;
						if(link.attr('href').charAt(0) != '#') {
							params['type'] = 'iframe';
						}
						if(size[0] > 0) {
							params['width'] = size[0];
						}
						if(size[1] > 0) {
							params['height'] = size[1];
						}
						link.fancybox(params);
					}
					setCookie(name, 'DoNotDisplay', expires, null, null, null);
				}
			link.click();
			}
		}
	});
	
	jQuery(".modal").each(function(i) {
		var size = getModalSize(this);
		if(size == null || size.length < 2) {
			jQuery(this).fancybox({
				'hideOnContentClick'	: 'none',
				'showCloseButton'		: 'none',
				'width'					: 700
			});
		} else {
			var params = {};
			params['hideOnContentClick'] = false;
			params['showCloseButton'] = true;
			if(size[0] > 0) {
				params['width'] = size[0];
			}
			if(size[1] > 0) {
				params['height'] = size[1];
			}
			jQuery(this).fancybox(params);
		}
	});
	

	jQuery(".modalImage").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'image',
		'padding'			: 3
	});

	jQuery(".modalFlash").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'swf'
	});

	jQuery(".modalFrame").each(function () {
		var size = getModalSize(this);
		if(size == null || size.length < 2) {
			jQuery(this).fancybox({
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'				: 'iframe',
				width				: 1020,
				height				: 500
			});
		} else {
			var params = {};
			params['hideOnContentClick'] = false;
			params['showCloseButton'] = true;
			params['type'] = 'iframe';
			params['transitionIn'] = 'none';
			params['transitionOut'] = 'none';
			if(size[0] > 0) {
				params['width'] = size[0];
			}
			if(size[1] > 0) {
				params['height'] = size[1];
			}
			jQuery(this).fancybox(params);
		}
	});
	
	jQuery('.youtube').each(function () {
		var container = jQuery(this);
		var vidId = container.attr('id');
		var thumbnail = 'http://img.youtube.com/vi/' + vidId + '/0.jpg';
		
		if(container.children('a').children('img').size() > 0) {
			thumbnail = container.children('a').children('img').eq(0).attr('src');
		}
		
		var h = parseInt(container.attr('height'));
		var w = parseInt(container.attr('width')) - 2;
		var btnPlay =     '/~/media/3A8BC216940C48A1994A4AD21481F375.ashx';
		var btnPlayOver = '/~/media/FE6EB0B4F81E4B639C463665CC713384.ashx';
		
		var narrowPlayerWidth = 427;
		var widescreenPlayerWidth = 560;
		var aspectRatio = w/h;
		var narrowAspectRatio = 4/3;
		var wideAspectRatio = 16/9;
		var playerWidth = narrowPlayerWidth;
		var playerHeight = 340;
		
		var averageAspectRatio = (narrowAspectRatio + wideAspectRatio)/2;
		
		if(aspectRatio > averageAspectRatio) {
			playerWidth = widescreenPlayerWidth;
		}
		
		container.find('a').hide();
		
		// Set Video Width and Height
		container.css('width', w + 'px');
		container.css('height', h + 'px');
		// Get video thumbnail
		container.append('<img src="' + thumbnail + '" height="' + h + '" width="' + w + '" />');
		// Create Play Button
		container.append('<div class="playBtn"><img src="' + btnPlay + '" height="' + h + '" /></div>');
		// Play Button Click
		container.find('.playBtn').click(function () {
			container.fancybox({
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'href'				: ('http://www.youtube.com/v/' + vidId + '?fs=1&hl=en_US&autoplay=1'),
				'type'				: 'swf',
				'width'				: playerWidth,
				'height'			: playerHeight
			});
		});
		
		// Play Button Hover
		container.find('.playBtn').hover(
		function () {
			container.find('.playBtn img').attr('src', btnPlayOver);
		},
		function () {
			container.find('.playBtn img').attr('src', btnPlay);
		});
		
		container.css('border-left', '1px solid #000');
		container.css('border-right', '1px solid #000');
	});
});





