/* -- -- -- -- -- -- --
 * proteach.js
 *
 * -- -- -- -- -- -- --
 */

// some of the curvy box stuff is not possible in ie without 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 external sites in new window (fn below)
	$('a[href^="http"]').each(function(){ openNewWindow( $(this) ); });

	// 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)");

		$(this).attr('title','This link opens in a new window');

		$(this).unbind();

		$(this).click(function(e){

			e.preventDefault();
			$(this).bind('click',function(){return false();});

			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

	$('div#corp-links ul li:last').addClass('last'); // remove the border-right from the last corp link

	try{ $('.rounded-noborder').addClass('{transparent}').corners('4px'); } catch(error){}
	try{ $('.content-box').addClass('{transparent}').corners('10px'); } catch(error){}

	// 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');

		// here are some thin-bordered outline divs
		try{ curvyCorners( curve12_settings, document.getElementById("promo-wrap-1") ) } catch(error){}
		try{ curvyCorners( curve12_settings, document.getElementById("promo-wrap-2") ) } catch(error){}
		try{ curvyCorners( curve12_settings, document.getElementById("promo-wrap-3") ) } catch(error){}
	} else {
		$('.promo-wrap').addClass('{transpaernt}').corners('10px');
	}
	// end: msie only

	// work some functionality for anchors with 'class="popup"'
	$('a.popup').each(function (i){
		// Add an onClick behavior to this link
		$(this).click(function(event) {
			// Prevent the browser's default onClick handler
			event.preventDefault();

			// Use the target attribute as the window name
			if( $(this).attr('class') ) {
				//params.windowName = $(this).attr("target");

				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);

				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


function openNewWindow(obj) {
	if( ! /popup/i.test($(obj).attr('class')) ) {
		if( ! /search.ets.org\/waproteach/i.test($(obj).attr('href')) && ! /^(http:\/\/|http:\/\/www\.)?waproteach.com/i.test($(obj).attr('href')) ) {
			$(obj).attr('title','This link opens in a new window');
			$(obj).click(function(e){
				e.preventDefault();
				window.open ($(obj).attr('href'), "","status=1,toolbar=1,location=1,menubar=1,directories=1,resizable=1,scrollbars=1,width=800,height=600");
			});
		}
	}
}

// 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

