/* all NEA.org pages use nea.js */

var NEA = {};

// flash constructor
NEA.Fla = function(file, h){
	this.file = file;
	this.h = h;
}
NEA.Fla.prototype.place = 'banner';
// different ones, have to change these to .prototype
// (and change usage below to point at the object instance)
NEA.Fla.w = 960;
NEA.Fla.path = "_res/swf/";

// collection of objects
NEA.flash = {
	academy: new NEA.Fla("NEA_Academy_Flash.swf",184),
	assoc: new NEA.Fla("NEA_Association_Flash.swf",199),
	org: new NEA.Fla("NEA_org_Flash.swf",239),
	working: new NEA.Fla("NEA_Working_Flash.swf",184),
	tour: new NEA.Fla("NEA_Tour.swf",409),
	queue : []
}
// different place
NEA.flash['tour'].place = 'tour-swf';

// collect these from the calling page
NEA.writeFlash = function(idx){
	NEA.flash.queue[NEA.flash.queue.length] = idx;
}

NEA.png = {};

// courtesy of http://ajaxcookbook.org/png-alpha-transparency/
NEA.png.createPNGImage = function(src, width, height) {
    if (navigator.userAgent.indexOf("MSIE") != -1) {
        var element = document.createElement("div");
        element.style.filter = "progid:DXImageTransform.Microsoft." +
                               "AlphaImageLoader(src='" + src + "')";
    	// element.style.filters(1) = "DXImageTransform.Microsoft.Alpha".Opacity[50];

	} else {
        var element = document.createElement("img");
        element.src = src;
    }
    element.style.width = width + "px";
    element.style.height = height + "px";
    return element;
}

NEA.video = {};
NEA.video.art = {
	path: "_res/images/design/",
	icon: "icon-play.png",
	loading: "icon-load.gif"
};
NEA.video.txt = {
	close: '<span class="err"><br />(<a href="javascript:NEA.video.close()">close</a>)</span>',
	mask: 	'<div id="vidlayer"></div>',
	box: 	'<div id="vidbox"><p id="icon-load"><img src="'
				+ NEA.video.art.path + NEA.video.art.loading + '" alt="Loading..." /> Loading...</p></div>'
}

NEA.video.set = function(item,tag){
	jQuery(item).hover(function(){
		jQuery(item).find(tag).addClass('play-over');
	},function(){
		jQuery(item).find(tag).removeClass('play-over');
	})
};

NEA.video.get = function(obj){
	jQuery.ajax({
		type: "GET",
		url: jQuery(obj).attr('href'),
		timeout : 2000,
		global : false,
		data : encodeURI(new Date()), // prevent cache
		error: function(){
			jQuery('#icon-load').empty().append('Loading movie failed. If this continues, please contact NEA for assistance.').addClass('vid-error').append(NEA.video.txt.close);
		},
		success: function(data){
			//jQuery('#vidlayer').fadeTo("fast",0.5) // broken in 1.1.3 in IE
			jQuery('#icon-load').remove();
			jQuery('#vidbox').html(data);
			jQuery('#btn-close').click(function(){
				NEA.video.close();
			})
		}
	});
};
NEA.video.close = function(){
	jQuery('#vidlayer').remove();
	jQuery('#vidbox').remove();
};
NEA.video.addLayer = function(obj){
	jQuery('body').append(NEA.video.txt.mask).append(NEA.video.txt.box).find('#vidbox #icon-load').append(NEA.video.txt.close);
	jQuery('#vidbox').css('top',jQuery().scrollTop() + 20 + 'px');
	NEA.video.get(obj);
};

jQuery(document).ready(function(){

	// ************* all pages
	// this inserts an empty div to allow dual bg colors
	jQuery('body').prepend("<div id='bg-extra'><\/div>");
	
	// better styles on HRs
	jQuery('hr').wrap('<div class="hr"><\/div>');

	// remove hr blurb from blurb page where it interferes
	// note: should not do this type of browser detection, really
	if (!(parseInt(jQuery.browser.version) < 6 && jQuery.browser.msie == true)) {
		jQuery('.drill hr.blurb').parents("div.hr").remove();
	}
	else {
		// ie5 only fix for width of nav drop-downs
		jQuery('link').each(function(){
			if (jQuery(this).attr('href').indexOf('academy.css') != -1) {
				jQuery('body').addClass('academy');
			}
		})
	}

	// last list item cleanup (UI only touch)
	jQuery("#nav-main li:last-child").addClass("last");
	jQuery("#site-info li:last-child").addClass("last");
	jQuery("#nav-section li:last-child").addClass("last");

	// home pages only
	jQuery('#content-col-a li:last-child a', jQuery('body.home')).addClass('last');
	jQuery('#content-col-b li:last-child a', jQuery('body.home')).addClass('last');
	jQuery('#content-rail li:last-child a', jQuery('body.home')).addClass('last');
	
	// all drills
	jQuery('#nav-utils li:first-child a').addClass('last');

	// VIDEO OVERLAYS
	// compliant browsers
	jQuery('a.video').each(function(){
		var src = jQuery(this).find('img').attr('src');
		jQuery(this).find('img').attr('src',NEA.video.art.path + NEA.video.art.icon).addClass('play');
		NEA.video.set(this,'img');
		jQuery(this).css('backgroundImage','url(' + src + ')').click(function(event){
			NEA.video.addLayer(this);
			event.preventDefault();
		})
		
	});

	// enable video overlays and pop-up layers for IE only
	if (jQuery.browser.msie) {
		jQuery('a.video img.play').remove();
		var play = NEA.png.createPNGImage(NEA.video.art.path + NEA.video.art.icon, 57, 57);
			jQuery('a.video').append(play).find('div').addClass('play')
		jQuery('a.video').each(function(){
			NEA.video.set(this,'div');
		})
	};

	// init drop down menus for browsers which do not support LI hovers
	// if browser ie statement below not really needed except for testing w/ cc's removed
	/*@cc_on
	/*@if (@_jscript_version < 5.7) // ie6 and lower only
	if (jQuery.browser.msie) {

		jQuery("#nav-main > li").hover(function(){
			jQuery(this).addClass("o");
		},function(){
			jQuery(this).removeClass("o");
		})
	}
	// browsers which don't support child selectors 
	jQuery('#nav-section .sub > a.on').addClass("on-sub");
	// handle headers post-blurb
	jQuery('#content-blurb + h2').addClass('post-blurb');
	jQuery('hr.blurb + h2').addClass('post-blurb');
	jQuery('#content-blurb + img + h2').addClass('post-img');
	
	// http://www.mister-pixel.com/#Content__state=is_that_simple
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
	/*@end
	@*/
	
	// test for and activate tabs
	var tabs = false;
	if (jQuery('#tab-1').get(0)){ // test for tab script
		jQuery('#tab-1').tabs();
		jQuery('#tab-1 li:gt(0)').addClass('no-edge');
		tabs = true;
	};
	
	// only resize content div height if not on a tab page
	if (0 && !tabs) {
		var sectionHeight = 0;
		var contentHeight = 0;
		jQuery("#nav-section>li").each(function(){
			sectionHeight += jQuery(this).height();
		});
		contentHeight = jQuery('#content-cols').height();
	
		if (contentHeight < sectionHeight) {
			sectionHeight = sectionHeight - contentHeight;
			sectionHeight = contentHeight + sectionHeight;
			jQuery('#content-cols').css('height',sectionHeight);
		}
	}

	jQuery('a[rel=external]').after('<span class="external">&nbsp;</span>');

	// Flash objects must be written to the page after the DOM mods or IE6 doesn't fire
	// 			jQuery document.ready event 
	///		THEREFORE THIS IF STATEMENT MUST BE LAST IN THE READY BLOCK!!!
	if (NEA.flash.queue.length > 0) {
		var x = 0;
		while (x < NEA.flash.queue.length) {
		 	var fla = new SWFObject(NEA.Fla.path + NEA.flash[NEA.flash.queue[x]].file, 'flash'+x, NEA.Fla.w, NEA.flash[NEA.flash.queue[x]].h, '8');
				 fla.addParam('wmode','transparent');
				 fla.write(NEA.flash[NEA.flash.queue[x]].place);
			x++;
		}
	}
});

/**
 * From the dimensions plugin: not including the whole thing, too much for too little, but thanks guys
 * Returns how many pixels the user has scrolled to the bottom (scrollTop).
 * Works on containers with overflow: auto and window/document.
 *
 * @example jQuery("#testdiv").scrollTop()
 * @result 100
 *
 * @name scrollTop
 * @type Number
 * @cat Plugins/Dimensions
 */
jQuery.fn.scrollTop = function() {
	if ( this[0] == window || this[0] == document )
		return self.pageYOffset ||
			jQuery.boxModel && document.documentElement.scrollTop ||
			document.body.scrollTop;

	return this[0].scrollTop;
};

