/* -- -- -- -- -- -- --
 * ets.js
 * -- -- -- -- -- -- --
 */

// curvy boxes needs some additional scripting
var curve4_settings = { tl: { radius: 4 }, tr: { radius: 4 }, bl: { radius: 4 }, br: { radius: 4 }, antiAlias: true };
var curve6_settings = { tl: { radius: 6 }, tr: { radius: 6 }, bl: { radius: 6 }, br: { radius: 6 }, antiAlias: true };
var curve8_settings = { tl: { radius: 8 }, tr: { radius: 8 }, bl: { radius: 8 }, br: { radius: 8 }, antiAlias: true };
var curve10_settings = { tl: { radius: 10 }, tr: { radius: 10 }, bl: { radius: 10 }, br: { radius: 10 }, antiAlias: true };
var curve12_settings = { tl: { radius: 12 }, tr: { radius: 12 }, bl: { radius: 12 }, br: { radius: 12 }, antiAlias: true };

$(document).ready(function(){

/*
	//open all external sites in new window
	$('#page-contents a[href^="http"]').each(function(){
		// make the title spiffy
		if( ! /popup/i.test($(this).attr('class')) ) {
			var currentTitle = $(this).attr('title');
			if( currentTitle.length > 0 ) {
				$(this).attr('title',currentTitle + '. This link opens in a new window');
			} else {
				$(this).attr('title','This link opens in a new window');
			}

			$(this).click(function(e){
				e.preventDefault();
				window.open ($(this).attr('href'), "","status=1,toolbar=1,location=1,menubar=1,directories=1,resizable=1,scrollbars=1,width=800,height=600");
			});
		}
	});
*/
	// pdfs open in a new window.
	$('a[href$=".pdf"]').each(function(){
		// only add the PDF link if the link does not contain an image
		$(this).filter(function(index){
			return $("img", this).length == 0;
		}).append(" (PDF)");

		var currentTitle = $(this).attr('title');
		if( currentTitle.length > 0 ) {
			$(this).attr('title',currentTitle + '. This link opens in a new window');
		} else {
			$(this).attr('title','This link opens in a new window');
		}

		if( !(/^http/.test($(this).attr('href'))) ) {
			$(this).click(function(e){
				e.preventDefault();
				window.open ($(this).attr('href'), "etsOpenWindow","status=1,toolbar=1,location=1,menubar=1,directories=1,resizable=1,scrollbars=1,width=800,height=600");
			});
		}
	}); // end: .pdf-each

	$('a[href$=".txt"]').each(function(){ $(this).append(" (Text)"); if( !(/^http/.test($(this).attr('href'))) ) { $(this).click(function(e){ e.preventDefault(); window.open ($(this).attr('href'), "etsOpenWindow","status=1,toolbar=1,location=1,menubar=1,directories=1,resizable=1,scrollbars=1,width=800,height=600"); }); } });
	$('a[href$=".doc"]').each(function(){ $(this).append(" (Word)"); if( !(/^http/.test($(this).attr('href'))) ) { $(this).click(function(e){ e.preventDefault(); window.open ($(this).attr('href'), "etsOpenWindow","status=1,toolbar=1,location=1,menubar=1,directories=1,resizable=1,scrollbars=1,width=800,height=600"); }); } });
	$('a[href$=".xls"]').each(function(){ $(this).append(" (Excel)"); if( !(/^http/.test($(this).attr('href'))) ) { $(this).click(function(e){ e.preventDefault(); window.open ($(this).attr('href'), "etsOpenWindow","status=1,toolbar=1,location=1,menubar=1,directories=1,resizable=1,scrollbars=1,width=800,height=600"); }); } });
	$('a[href$=".swf"]').each(function(){ $(this).append(" (Flash)"); if( !(/^http/.test($(this).attr('href'))) ) { $(this).click(function(e){ e.preventDefault(); window.open ($(this).attr('href'), "etsOpenWindow","status=1,toolbar=1,location=1,menubar=1,directories=1,resizable=1,scrollbars=1,width=800,height=600"); }); } });
	$('a[href$=".ppt"]').each(function(){ $(this).append(" (PowerPoint)"); if( !(/^http/.test($(this).attr('href'))) ) { $(this).click(function(e){ e.preventDefault(); window.open ($(this).attr('href'), "etsOpenWindow","status=1,toolbar=1,location=1,menubar=1,directories=1,resizable=1,scrollbars=1,width=800,height=600"); }); } });

	// there is a problem with Ephox where inline anchors
	// cannot be empty, or the tag is removed
	// so, if we add '&nbsp;' to the tag to keep is alive in Ephox,
	// we need to remove that from here.
	$('a[name]').filter(function(){ return $(this).html() === '&nbsp;'; }).each(function() { $(this).html(''); });
	// save this for a rainy day...
	// $('a[name]').filter(function(){ return $(this).html() === '<strong>&nbsp;</strong>'; }).each(function() { $(this).html(''); });
	// $('a[name]').filter(function(){ return $(this).html() === '<em>&nbsp;</em>'; }).each(function() { $(this).html(''); });

	// start: item cleanup
	$('div#site-nav ul#site-nav-top li:last-child').addClass('last');
	$('div#section-navigation ul#section-nav li:last').addClass('last');
	$('div#section-navigation ul.subnav li:last').addClass('last');
	$('div#corp-links ul li:last').addClass('last');
	$('.important-update p:last-child').addClass('last');
	$('.feature p:last-child').addClass('last');
	$('.ad-banner p:last-child').addClass('last');
	$('.news-blurb p:last-child').addClass('last');
	$('.home-right-blurb p:last-child').addClass('last');
	$('.promo p:last-child').addClass('last');
	$('div#breadcrumbs ul li:last-child').addClass('last');

	// end: item cleanup

	// start: the default text for the search box
	/*
	var searchBox = $('div#site-search input#site-search-form-keyword');
	var defaultSearchText = "Keywords";
	searchBox.attr('value','Keywords');
	searchBox.focus(function(){ if($(this).attr('value') == defaultSearchText) $(this).attr('value', ''); });
	searchBox.blur(function(){ if($(this).attr('value') == "") $(this).attr('value', defaultSearchText); });
	*/
	// end: the default text for the search box

	// start: corp-nav-top suckerfish trigger
	$("#site-nav-top li").hover(
		function(){ $("ul", this).fadeIn("fast"); },
		function(){ $("ul", this).fadeOut(""); } // ie7 need this
	);
	if (document.all) {
		$("#site-nav-top li").hoverClass ("sfHover");
	}
	// end: corp-nav-top suckerfish trigger

	// start: section-navigation suckerfish trigger
	$("#section-navigation li").hover(
		function(){ $("ul", this).fadeIn("fast"); },
		function(){ $("ul", this).fadeOut("slow"); } // ie7 need this
	);
	if (document.all) {
		$("#section-navigation li").hoverClass ("sfHover");
	}
	// end: section-navigation suckerfish trigger

	$('.rounded-border').addClass('{transparent}').corners('4px');
	$('.rounded-noborder').addClass('{transparent}').corners('4px');
	$('.important-update').addClass('{transparent}').corners('10px');
	$('.content-box').addClass('{transparent}').corners('10px');
	$('.banner-box').addClass('{transparent}').corners('10px');
	$('.feature').addClass('{transparent}').corners('10px');
	$('.ad-banner').addClass('{transparent}').corners('10px');

	// for the ets home page, we need to do some cleanup before
	// applying the rounded corners to the column boxes
	balance2Divs('column-box-1-left', 'column-box-1-right');
	if ($.browser.msie) {
		// umm, yea, so the width here is the same as listed in the css file
		// but if you remove these directives, the boxes aren't wide enough
		$('div.container-760 div.content-box-nofiller div.column-box-1-left').css('width','328px');
		$('div.container-760 div.content-box-nofiller div.column-box-1-right').css('width','328px');

		$('div.container-640 div.content-box-nofiller div.column-box-1-left').css('width','278px');
		$('div.container-640 div.content-box-nofiller div.column-box-1-right').css('width','278px');

		$('div.container-560 div.content-box-nofiller div.column-box-1-left').css('width','238px');
		$('div.container-560 div.content-box-nofiller div.column-box-1-right').css('width','238px');
	}
	$('div.content-box-nofiller .column-box-1-left').addClass('{transparent}').corners('10px');
	$('div.content-box-nofiller .column-box-1-right').addClass('{transparent}').corners('10px');
	// end for ets home page

	// start: left (main) navigation
	$('.nav-box').addClass('{transparent}').corners('10px');
	$('.nav-box ul.subnav li:last').addClass('last');
	$('#main-navigation li span.has-arrow').parent().addClass('{transparent}').corners('8px').addClass('white-bkgrnd');
	// end: left (main) navigation

	// start: safari only
	if ($.browser.safari) {
   	$('.go-button').css('padding', '3px');
	}
	// end: safari only

	// start: msie only
	if ($.browser.msie) {
		// fix the actions buttons for msie--the rewrite from button link crushes it
		$('a.action-button td').css('font-weight','bold');
		$('a.action-button td').css('line-height','1em');

		try{ curvyCorners( curve12_settings, document.getElementById("promo-wrap-1") ) } catch(error){}
		try{ curvyCorners( curve12_settings, document.getElementById("promo-wrap-2") ) } catch(error){}
	} else {
		$('.promo-wrap').addClass('{transpaernt}').corners('10px');
	}
	// end: msie only

	// work some functionality for anchors with 'class="bookmarkfunction"'
	$('a.bookmarkfunction').each(function (i){
		$(this).click(function(event) {
			event.preventDefault();

			url = window.location;
			title = jQuery('title').html();

			if( window.sidebar ) {
				// Mozilla Firefox Bookmark
				try{
					window.sidebar.addPanel(title, url,"");
				} catch(error){
					alert("use CTRL + D to bookmark this page");
				}
			} else if( window.external ) {
				// IE Favorite
				try{
					window.external.AddFavorite( url, title);
				} catch(error){
					alert("use CTRL + D to bookmark this page");
				}
			} else if( window.opera && window.print ) {
				// Opera Hotlist
				try{
					return true;
				} catch(error){
					alert("use CTRL + T to bookmark this page");
				}
			}
		}); // end this.click
	}); // end: a.bookmarkfunction each

	// work some functionality for anchors with 'class="popup"'
	$('a.popup').each(function (i){
		$(this).click(function(event) {
			event.preventDefault();

			if( $(this).attr('class') ) {
				var pop_width = 800;
				var pop_height = 600;

				var class_info = $(this).attr('class');
				var regexp = /(h-\d+|w-\d+)/gi;

				var info = class_info.match(regexp);
				if( info != null ) {
					info.forEach(function(x,idx){
						if( /w-\d+/.test(x) ){ pop_width = x.substr(2); }
						if( /h-\d+/.test(x) ){ pop_height = x.substr(2); }
					});
				}
			}
			pop_url = $(this).attr('href');
			pop_window = window.open( pop_url,
			                          "ets_pop",
			                          "location=1,resizable=1,scrollbars=1,status=1,width="+pop_width+",height="+pop_height
			                        );
		}); // end this.click
	}); // end: a.popup each
}); // end: document.ready

// suckerfish navigation functionality
// 	used by corp-top-nav
$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover(
			function() { $(this).addClass(c); },
			function() { $(this).removeClass(c); }
		);
	});
};

// resize twp divs to a single height
//    be sure to balance the heights BEFORE apply the curves
function balance2Divs( a, b) {
	var leftHeight = $('#' + a).height();
	var rightHeight = $('#' + b).height();

	// find the tallest
	var tall = leftHeight;
	if( leftHeight < rightHeight ) {
		tall = rightHeight
	}

	// reset all the heights to the tallest
	$('#' + a).height(tall);
	$('#' + b).height(tall);

} // end balance2Divs

// Internet Explorer doesn't have 'foreach' support
// --> provide some instructions for it.
if( !Array.prototype.forEach ){
	Array.prototype.forEach = function(fun /*, thisp*/) {
		var len = this.length >>> 0;
		if (typeof fun != "function") { throw new TypeError(); }
		var thisp = arguments[1];
		for (var i = 0; i < len; i++) {
			if (i in this) { fun.call(thisp, this[i], i, this); }
		}
	};
}

