// for use with mootools v1.11
// Stewart Smith - WGBH Interactive
// http://interactive.wgbh.org/

/* namimg conventions and classes*/
var ncImgOver = '_over';
var ncClsRoll = 'roll';
var ncClsDblRoll = 'dbl_roll'; // name of class for double rollover
var ncClsEnlarge = 'img_enlarge';
var ncClsSendToFriend = 'sendtofriend';
var ncClsPromoVideo = 'pop_query';

/* for popups */
var ng = navigator;
var ua = ng.userAgent;
var nav = (ng.appName == 'Netscape');
var saf = (ng.appVersion.indexOf('Safari') != -1);
var ie = (ng.appVersion.indexOf('MSIE') != -1);
var mac = (ua.indexOf('Mac') != -1);
var aol = (ua.indexOf('AOL') != -1);

var amex = {
	init: function(){
		amex.seriesnav();
		if( $('cnt_highlights') )
			amex.initHome();
		amex.rollovers();
		amex.enlarge();
		amex.popups();
	},
	seriesnav: function() {
	// series nav dropdowns
		var parentClass = 'isParent';		//gets applied when the LI has a nested UL
		var activeParentClass = 'isActive';	//gets applied when the nested UL is visible
		var indicateJSClass = 'dhtml';		//gets applied to the main navigation when Javascript is available
		var toHideClass = 'hiddenChild';	//gets applied to hide the nested UL
		var toShowClass = 'shownChild';		//gets applied to show the nested UL
		var amexnav = $('amexnav');			//denotes the navigation element 
	
		if( amexnav ){
			var apply;
			// apply the class indicating DHTML
			amexnav.addClass(indicateJSClass);
			var lis = $ES('li', amexnav);
			lis.each(function(element) {
			  // check for nested UL
				var firstUL = $E('ul', element);
				if( firstUL ){
					element.addClass(parentClass);
					firstUL.addClass(toHideClass);
					element.addEvent('mouseover', function(e){ nested_nav(this); });
					element.addEvent('mouseout', function(e){ nested_nav(null); });
				}// end if fitstUL
			});// end lis.each
		}// end if( amexnav )
		function nested_nav(o){
			var childUL,isobj,swap;
			var lis = $ES('li', amexnav);
			lis.each(function(element) {
				isobj = element == o;
				if( !element.keepopen ){
					var childUL = $E('ul', element);
					if( childUL ){
						if( element.hasClass(activeParentClass) || !isobj ){
							childUL.removeClass(toShowClass);
							childUL.addClass(toHideClass);
							element.removeClass(activeParentClass);
							element.addClass(parentClass);
						}else{
							childUL.removeClass(toHideClass);
							childUL.addClass(toShowClass);
							element.removeClass(parentClass);
							element.addClass(activeParentClass);
						}
					}
				}
			});//end lis.each
		}//end inner function nested_nav
	},//end function seriesnav
	initHome: function() {
		// for homepage highlights only
		var highlights = $$('#cnt_highlights div.highlight');
		highlights.each(function(element) {
			var linkto = $E('a', element);
			element.linkto = linkto.href
			element.addEvent('mouseenter', function(){
				this.removeClass('highlight');
				this.addClass('highlight_over');
			});
			element.addEvent('mouseleave', function(){
				this.removeClass('highlight_over');
				this.addClass('highlight');
			});
			element.addEvent('click', function(e){
				event = new Event(e);
				event.stop();
				try{ window.location = this.linkto; }catch(e){}
			});
		});
	},
	rollovers: function() {
		// img rollovers
		var rollImgs = $$('img.' + ncClsRoll, 'input.' + ncClsRoll);
		rollImgs.each(function(element) {
			var imgFile = element.src.substr(0, element.src.length-4);
			var imgType = element.src.substr(element.src.length-3, element.src.length);
			element.roll_src = (new Image()).src = imgFile + ncImgOver + '.' + imgType;
			element.addEvent('mouseenter', function(){
				this.setProperty('src', this.roll_src);
			});
			element.addEvent('mouseleave', function(){
				this.setProperty('src', this.src.replace( new RegExp(ncImgOver+'\.'), '.') );
			});
	
		});
	},
	enlarge: function() {
		// enlarge image actions
		var links = $$('div.' + ncClsEnlarge + ' a');
		links.each(function(element) {
			element.addEvent('click', function(e){
				event = new Event(e);
				event.stop();
				amex.popWithSize(this.href, 'enlarged', 620, 550);
			});
		});
	},
	popups: function() {
		// common popups and link actions
		var links = $$('a');
		links.each(function(element) {
			// external links
			if( element.rel == 'external' )
				element.target = '_blank';
			// open send to friend links in popup
			if( element.hasClass(ncClsSendToFriend) ){
				element.addEvent('click', function(e){
					e = new Event(e).stop();
					amex.popWithSize(this.href, 'sendtofriend', 605, 380);
				});
			}
			// open promo video links in popup
			if( element.hasClass(ncClsPromoVideo) ){
				element.addEvent('click', function(e){
					e = new Event(e).stop();
					amex.popWithSize(this.href, 'promo', 600, 420);
				});
			}
		});
	},
	popWithSize: function (openURL, winName, w, h){
		var otherOptions = 'toolbar=0,location=0,status=1,menubar=0,scrollbars=1,resizable=1,';//+',screenx='+x+',screeny='+y+',left='+x+',top='+y);
		return window.open(openURL, winName, otherOptions + 'width=' + this.adjustWidth(w) + ',height=' + this.adjustHeight(h) );
	},
	adjustWidth: function (w){
		if( mac ){
			if( !nav && ie ) w -= 16;
			if( nav && saf ) w += 2;
		}else{
			if( ie ) w += 13;
		}
		return w;
	},
	adjustHeight: function (h){
		if( mac ){
			if( !nav && ie ) h -= 12;
			if( nav && saf ) h += 17;
			if( nav && !ie && !saf ) h += 5;
		}
		return h;
	},
	addCSS: function(cssFile){
		if(cssFile){
			var css_lnk = new Element('link').setProperty('type','text/css').setProperty('rel','stylesheet').setProperty('href', cssFile).setProperty('media','screen');
			$E('head').appendChild(css_lnk);
		}
	}
};
window.name = 'amex';
window.addEvent('domready', amex.init);