

/*******************************************/
/****   PAGE MANAGER                    ****/
/*******************************************/

/** 
 * Common starting point for all page initialization.
 *
 * Page initialization has two "hooks" to use:
 * - pageLoadInit()     : For things that needs to be done as soon as possible
 * - pageLoadLateInit() : For everything else
 * 
 * Every page that needs JavaScript functionality should make a call like this:
 *      DNV_PageManager.setPage( <PAGE_CONTROLLER> );
 * In the case of the Global home page, it looks like this:
 *      DNV_PageManager.setPage( new DNV_HomePage() );
 */
var DNV_PageManager = {
	page:null,
	initCalled:false,
	lateInitCalled:false,
	
	/** Set the page controller object to use */
	setPage:function( page ) {
		this.page = page;
	},
	
	initSiteSelector:function() {
		var p = DNV_PageManager.page;
		if( p == null ) { return false; }
		if( p.initSiteSelector == null ) {
			p = p.basePage;
			if( p.initSiteSelector == null ) { return false; }
		}
		p.initSiteSelector();
	},
	
	pageLoadInit:function() {
		if( DNV_PageManager.initCalled ) { return false; }
		var p = DNV_PageManager.page;
		if( p == null ) { return false; }
		if( p.pageLoadInit == null ) { return false; }
		var isReady = p.isReadyForPageLoadInit();
		if( !isReady ) { return false; }
		DNV_PageManager.initCalled = true;
		p.pageLoadInit();
		return true;
	},
	
	pageLoadLateInit:function() {
		if( DNV_PageManager.lateInitCalled ) { return false; }
		var p = DNV_PageManager.page;
		if( p == null || p.isReadyForPageLoadInit == null ) { return false; }
		p.pageLoadLateInit();
	},
	
	handleDocumentOnContentLoaded:function() {
		DNV_PageManager.pageLoadInit();
	},
	
	handleWindowOnLoad:function() {
		DNV_PageManager.pageLoadInit();
		window.setTimeout( DNV_PageManager.handleLoadTimeout, 1 );
	},
	
	handleLoadTimeout:function() {
		DNV_PageManager.pageLoadInit();
		DNV_PageManager.pageLoadLateInit();
	},
	
	handleWindowOnUnLoad:function() {
		var p = DNV_PageManager.page;
		if( p == null || p.pageUnloadCleanup == null ) { return false; }
		p.pageUnloadCleanup();
		DNV_PageManager.page = null;
		DNV_PageManager = null;
	}
	
}

document.observe( "contentloaded", DNV_PageManager.handleDocumentOnContentLoaded );
Event.observe( window, "load", DNV_PageManager.handleWindowOnLoad );
Event.observe( window, "unload", DNV_PageManager.handleWindowOnUnLoad );



/*******************************************/
/****   BASE PAGE CLASS                 ****/
/*******************************************/

var DNV_BasePage = Class.create();
DNV_BasePage.prototype = {
	
	siteSelectorDropdown:null,

	initialize : function() {
	},
	
	isReadyForPageLoadInit : function() {
		return $("RootContainer") != null && $("SiteSelector") != null;
	},
	
	pageLoadInit:function() {
		this.initJavaScriptMarker();
		this.checkScreenWidth();
		this.initSiteSelector();
	},
	
	pageLoadLateInit:function() {
		this.initPagedComponents();
		this.initLinkedComponentContent();
		this.initServiceSelectors();
	},
	
	pageUnloadCleanup:function() {
		this.siteSelectorDropdown.unload();
		this.siteSelectorDropdown = null;
		DNV_PagedComponentBehavior.unload();
		DNV_LinkedComponentContentBehavior.unload();
		DNV_ServiceSelector.unload();
	},
	
	/**
	 * Add a meta class name to RootContainer to indicate to CSS that user has javascript.
	 */
	initJavaScriptMarker:function() {
		$("RootContainer").addClassName( "userHasJavaScript" );
	},
	
	/** 
	 * Check if screen is more than 1024px wide, and apply meta class to RootContainer.
	 */
	checkScreenWidth:function() {
		if( screen.availWidth > 1024 ) {
			$("RootContainer").addClassName( "screenWiderThan1024" );
		}
	},
	
	initSiteSelector:function() {
		if( this.siteSelectorDropdown == null ) {
			this.siteSelectorDropdown = new DropdownControl( $("SiteSelector"), null, 40 );
		}
	},
	
	initPagedComponents:function() {
		DNV_PagedComponentBehavior.findAndInit();
	},
	
	initLinkedComponentContent:function() {
		DNV_LinkedComponentContentBehavior.findAndInit();
	},
	
	initServiceSelectors:function() {
		DNV_ServiceSelector.findAndInit();
	}
}



/*******************************************/
/****   HOME PAGE CLASS                 ****/
/*******************************************/


var DNV_HomePage = Class.create();
DNV_HomePage.prototype = {
	
	basePage:null,
	
	HOT_TOPICS_PAGE_COOKIE_NAME:"hotTopicsPage",
	HOT_TOPICS_PAGE_COOKIE_EXPIRES_DAYS:10,
	
	initialize : function() {
		this.basePage = new DNV_BasePage();
	},
	
	isReadyForPageLoadInit : function() {
		return this.basePage.isReadyForPageLoadInit() && $("HomePageNavigationContainer") != null;
	},

	pageLoadInit:function() {
		this.basePage.pageLoadInit();
		this.adjustNavigationHeights();	
		this.adjustNavigationWidths();	
		var componentColumns = new DNV_ComponentColumnsContainer();
		this.adjustRightComponentHeight(componentColumns);
	},
	
	pageLoadLateInit:function() {
		this.basePage.pageLoadLateInit();
		this.initRotatingHotTopics();
		this.initFlashBanner();
	},
	
	pageUnloadCleanup:function() {
		this.basePage.pageUnloadCleanup();
		this.basePage = null;
	},

	initFlashBanner:function() {
		var banner = DNV_FlashBanner.instance;
		if( banner != null ) {
			banner.delayedRender();
		}
	},
	
	/**
	 * Adjust height of all ul elements int the navigation to match the tallest one.
	 */
	adjustNavigationHeights : function() {
		var height = 0;
		var container = $("HomePageNavigationContainer");
		var elements = container.getElementsBySelector("ul");
		elements.each( function( ul ) { 
			var h = ul.getHeight();
			if( h > height ) { height = h };
		} );
		elements.each( function( ul ) { 
			ul.setStyle( { height: height + "px" } );
		} );
	},

	/**
	 * Adjust height of all .section elements in the navigation to fill the width of the container.
	 */
	adjustNavigationWidths:function() {
		var container = $("HomePageNavigationContainer");
		var containerWidth = container.getWidth() - 30;
		var sections = container.getElementsBySelector(".section");
		var sectionWidth = Math.floor( containerWidth / sections.length );
		sections.each( function(section) {
			section.style.width = sectionWidth + "px";
		} );
	},
	
	/** 
	 * @param componentColumns Reference to the controller for the components at the bottom of the page
	 */
	adjustRightComponentHeight:function(componentColumns) {	
		var container = $("HomePageRightComponentContainer");
		if( container == null ) { return; }
		var pos = Position.page(container);
		var otherComponents = document.getElementsByClassName("ComponentColumns")[0];
		if( otherComponents == null ) { return; }
		var otherPos = Position.page(otherComponents);
		var posDiff = otherPos[1] - pos[1];
		var minHeight = componentColumns.getBodyHeight() + posDiff;
		var height = 0;
		var footerHeight = 0;
		var footer = container.getElementsByClassName("footer")[0];
		if( footer != null ) { footerHeight = $(footer).getHeight(); }
		var elements = $A(container.getElementsByClassName("body"));
		elements.each( function( element ) { 
			element.setStyle( {display:"block"} );
			var h = element.getHeight();
			if( h > height ) { height = h };
			var contentHeight = 0;
			var contents = $A(element.getElementsByClassName("content"));
			contents.each(function(content){
				contentHeight += content.getHeight();
			});
			if( contentHeight > height ) {
				height = contentHeight;
			}
			element.setStyle( {display:""} );
		} );
		height += footerHeight * 2;
		if( height < minHeight ) {
			height = Math.max( height, minHeight );
		} else {
			componentColumns.adjustHeights(height-minHeight);
		}
		elements.each( function( element ) { 
			element.setStyle( { height: height + "px" } );
		} );
	},
	
	/** 
	 * Assume the Hot Topics component is the first paged, numbered component on the page.
	 */
	initRotatingHotTopics:function() {
		var comp = DNV_PagedComponentBehavior.getFirstPagedNumberedComponent();
		var paging = comp.getPagingControl();
		paging.gotoPage( this.getStoredHotTopicsPage() );
		this.storeHotTopicsPage( paging.getCurrentPageIndex() + 1 );
	},
	
	getStoredHotTopicsPage:function() {
		var index = DNV_CookieManager.getCookie( this.HOT_TOPICS_PAGE_COOKIE_NAME );
		if( index == null ) {
			return 0;
		} else {
			return index;
		}
	},
	
	storeHotTopicsPage:function( pageIndex ) {
		DNV_CookieManager.setCookie( this.HOT_TOPICS_PAGE_COOKIE_NAME, pageIndex, this.HOT_TOPICS_PAGE_COOKIE_EXPIRES_DAYS );
	}
}



/*******************************************/
/****   SUB PAGE CLASS                  ****/
/*******************************************/


var DNV_SubPage = Class.create();
DNV_SubPage.prototype = {
	
	basePage:null,
	contentContainer:null,
	comboMenu:null,
	linkedContentItems:null,

	initialize : function() {
		this.basePage = new DNV_BasePage();
	},
	
	isReadyForPageLoadInit : function() {
		return this.basePage.isReadyForPageLoadInit() && $("ContentContainer") != null;
	},
	
	pageLoadInit:function() {
		this.contentContainer = $("ContentContainer");
		this.basePage.pageLoadInit();
		this.initContentTools();
	},
	
	pageLoadLateInit:function() {
		this.contentContainer = $("ContentContainer");
		this.basePage.pageLoadLateInit();
		this.initNavigation();
		this.initContent();
		this.initLinkedContentItems();
	},
	
	pageUnloadCleanup:function() {
		this.basePage.pageUnloadCleanup();
		this.comboMenu.unload();
		this.comboMenu = null;
		this.linkedContentItems.each(function(lc){lc.unload()});
		this.linkedContentItems = null;
	},
	
	initNavigation:function() {
		this.comboMenu = new ComboMenu( $("ComboMenuContainer") );
	},
	
	initContent:function() {
		if( this.contentContainer == null ) { return; }
		this.initPagedContent();
		$A(this.contentContainer.getElementsByClassName("faq")).each( function(faq){ new DNV_FaqContent( faq ) } );
		$A(this.contentContainer.getElementsByClassName("contactUs")).each( function(contactUs){ new DNV_ContactUsContent( contactUs ) } );
	},
	
	initPagedContent:function() {
		if( this.contentContainer == null ) { return; }
		var collections = this.contentContainer.getElementsByClassName("pages");
		if( collections.length != 1 ) { return; }
		var pages = collections[0].getElementsByClassName("page");
		if( pages != null && pages.length > 1 ) {
			new DNV_PagedContent( this.contentContainer );
		}
	},
	
	initLinkedContentItems:function() {
		if(this.contentContainer==null){return false;}
		var _lc = this.linkedContentItems = [];
		var tStart = (new Date()).getTime();
		var items = this.contentContainer.getElementsByClassName("item");
		// var tOne = (new Date()).getTime() - tStart;
		var count = items.length;
		var isIE6 = (
			window.external &&
			typeof window.XMLHttpRequest == "undefined"
		);
		if(isIE6) { count = Math.min(10,count); }
		for( var i=0; i < count; i++ ) {
			_lc.push(new DNV_LinkedContentBehavior(items[i]));
		}
		// var tTwo = (new Date()).getTime() - tStart;
		// alert( tOne + ", " + tTwo );
	},

	initContentTools:function() {
		if( this.contentContainer == null ) { return; }
		var tools = this.contentContainer.getElementsByClassName("tools")[0];
		if( tools != null ) { 
			var fontSize = tools.down(".fontSize");
			if( fontSize != null ) {
				new DNV_ContentFontSizeTools( fontSize );
			}
			var print = tools.down( ".print" );
			if( print != null ) {
				new DNV_ContentPrintButton( print );
			}
			var emailButton = tools.down( ".email" );
			var emailForm = $("EmailAFriend");
			if( emailButton != null && emailForm != null ) {
				new DNV_ContentEmailFriend( emailButton, emailForm );
			}
		}
	}
};





/*******************************************/
/****   ENTRY PAGE CLASS                ****/
/*******************************************/

var DNV_EntryPage = Class.create();
DNV_EntryPage.prototype = {
	
	basePage:null,
	linkedContentItemsIn3rdColumn:null,
	
	initialize : function() {
		this.basePage = new DNV_SubPage();
	},
	
	isReadyForPageLoadInit : function() {
		return this.basePage.isReadyForPageLoadInit();
	},
	
	pageLoadInit:function() {
		this.basePage.pageLoadInit();
		this.adjustHeights();
	},
	
	pageLoadLateInit:function() {
		this.basePage.pageLoadLateInit();
		this.initLinkedContentItemsIn3rdColumn();
	},
	
	pageUnloadCleanup:function() {
		this.basePage.pageUnloadCleanup();
		this.linkedContentItemsIn3rdColumn.each(function(lc){lc.unload()});
	},
	
	initLinkedContentItemsIn3rdColumn:function() {
		var container = $("EntryPageComponentContainer");
		if(container==null){return false;}
		var _lc = this.linkedContentItemsIn3rdColumn = [];
		var items = container.getElementsBySelector(".group .item");
		items.each(
			function(item){ _lc.push(new DNV_LinkedContentBehavior( item ));}
		);
	},
	
	adjustHeights:function() {
		var contentContainer = $("ContentContainer");
		var primaryContent = contentContainer.getElementsByClassName("primary")[0];
		var secondaryContent = contentContainer.getElementsByClassName("secondary")[0];
		var height = 0;
		height = Math.max( height, $("EntryPageComponentContainer").getHeight() );
		height = Math.max( height, primaryContent.getHeight() );
		height = Math.max( height, secondaryContent.getHeight() );
		primaryContent.setStyle({height:height+'px'});
	}
	
}

/*******************************************/
/****   PAGED CONTENT                   ****/
/*******************************************/


var DNV_PagedContent = Class.create();
DNV_PagedContent.prototype = {

	target:null,
	pages:null,
	pageNavigationContainer:null,
	pagingControl:null,
	
	/** Constructor */
	initialize:function( target ) {
		this.target = target;
		this.initPaging();
	},
	
	initPaging:function() {
		this.target.down(".pages").addClassName("enabled");
		this.target.down(".pageNavigation").addClassName("enabled");
		this.pages = this.target.getElementsBySelector( ".page" );
		this.buildPageNavigationContainer();
		var previousButton = this.target.getElementsBySelector( ".button.previous" )[0];
		var nextButton = this.target.getElementsBySelector( ".button.next" )[0];
		this.pagingControl = new PagingControl( this.pages, this.pageNavigationContainer, previousButton, nextButton );
		this.pagingControl.gotoPage(0);
		this.pagingControl.setScrollToElement( document.body );
	},
	
	buildPageNavigationContainer:function() {
		var c = this.target.getElementsBySelector(".pageNavigation .list")[0];
		var ol = Builder.node( "ol" );
		c.appendChild( ol );
		this.pageNavigationContainer = ol;
	}	
}



/*******************************************/
/****   FAQ CONTENT                     ****/
/*******************************************/


var DNV_FaqContent = Class.create();
DNV_FaqContent.prototype = {

	target:null,
	toggleAllVisible:true,
	
	/** Constructor */
	initialize:function( target ) {
		this.target = target;
		this.toggleAllButton = this.target.getElementsByClassName("toggleAllButton")[0];
		this.initEvents();
		this.toggleAll();
	},
	
	/** Use one event listener for all click events */
	initEvents:function() {
		Event.observe( this.target, "click", this.handleClick.bindAsEventListener(this) );
	},
	
	/** Handle click for all elements inside target container. */
	handleClick:function( event ) {
		var element = Event.element( event );
		if( element == this.toggleAllButton ) {
			Event.stop(event);
			this.toggleAll();
		} else if( element.tagName.toLowerCase() == "dt" ) {
			Event.stop(event);
			this.toggleOne( element );
		}
	},
	
	toggleAll:function() {
		if( this.toggleAllVisible ) {
			this.target.getElementsBySelector("dd").each( Element.hide );
			this.toggleAllVisible = false;
		} else {
			this.target.getElementsBySelector("dd").each( Element.show );
			this.toggleAllVisible = true;
		}
	},
	
	toggleOne:function( element ) {
		var dd = null;
		if( element.tagName.toLowerCase() == "dt" ) {
			dd = element.next("dd");
		} else if( element.tagName.toLowerCase() == "dd" ) {
			dd = element;
		}
		if( dd != null ) {
			dd.toggle();
		}
	}

}



/*******************************************/
/****   CONTACT US                      ****/
/*******************************************/


var DNV_ContactUsContent = Class.create();
DNV_ContactUsContent.prototype = {
	target:null,
	/** Constructor */
	initialize:function( target ) {
		this.target = target;
		this.initEvents();
		this.units = $A(this.target.getElementsByClassName("unit"));
		this.update();
	},
	initEvents:function() {
		Event.observe( this.target, "click", this.handleClick.bindAsEventListener(this) );
	},
	handleClick:function( event ) {
		if( "INPUT" != event.target.tagName ) { return; }
		this.update();
	},
	update:function() {
		this.units.each( function( unit ) {
			var radio = unit.down("input");
			if( radio.checked ) {
				unit.addClassName("selectedUnit");
			} else {
				unit.removeClassName("selectedUnit");
			}
		} );
	}
}



/*******************************************/
/****   CONTENT PRINT BUTTON            ****/
/*******************************************/

var DNV_ContentPrintButton = Class.create();
DNV_ContentPrintButton.prototype = {
	initialize:function( target ) {
		Event.observe( target, "click", this.handleClick.bindAsEventListener(this) );
	},
	handleClick:function( event ) {
		Event.stop( event );
		window.print();
		Event.element( event ).blur();
	}
}

/*******************************************/
/****   CONTENT FONT SIZE TOOLS         ****/
/*******************************************/

var DNV_ContentFontSizeTools = Class.create();
DNV_ContentFontSizeTools.prototype = {
	
	PREFERENCE_COOKIE_NAME:"fontSize",
	PREFERENCE_COOKIE_EXPIREDAYS:10,
	ALLOWED_FONTSIZE_NAMES:["normal","larger","largest"],
	target:null,
	buttons:null,
	
	initialize:function( target ) {
		if( !this.verifyTarget( target ) ) { return; }
		
		this.target = target;
		this.initButtonProperties();
		this.initSelected();
		this.initEvents();
		this.updateButtonClassNames();
	},
	
	verifyTarget:function( target ) {
		if( target == null ) { return false; }
		if( target.down(".button") == null ) { return false; }
		return true;
	},
	
	initButtonProperties:function() {
		var b = this.buttons = this.target.getElementsBySelector("div");
		b[0]._fontSize = "normal";
		b[0]._hovered = false;
		b[0]._selected = false;
		b[1]._fontSize = "larger";
		b[1]._hovered = false;
		b[1]._selected = false;
		b[2]._fontSize = "largest";
		b[2]._hovered = false;
		b[2]._selected = false;
	},
	
	updateButtonClassNames:function() {
		var len = this.buttons.length;
		for( var i = 0; i < len; i++ ) {
			var b = this.buttons[i];
			this.removeClassNames( b );
			var className = b._fontSize;
			if( b._selected ) {
				className += "Selected";
			} else if( b._hovered ) {
				className += "Hovered";
			}
			b.addClassName( className );
		}
	},
	
	removeClassNames:function( button ) {
		button.removeClassName( button._fontSize );
		button.removeClassName( button._fontSize + "Selected" );
		button.removeClassName( button._fontSize + "Hovered" );
	},
	
	initSelected:function() {
		var size = this.getStoredPreference();
		this.setFontSize( size );
		this.buttons.each( function(b){ 
			b._selected = b.className.indexOf( size ) != -1; 
		} );
	},
	
	initEvents:function() {
		var len = this.buttons.length;
		for( var i = 0; i < len; i++ ) {
			Event.observe( this.buttons[i], "mouseover", this.handleMouseOver.bindAsEventListener(this) );
			Event.observe( this.buttons[i], "mouseout", this.handleMouseOut.bindAsEventListener(this) );
			Event.observe( this.buttons[i], "click", this.handleClick.bindAsEventListener(this) );
		}
	},
	
	handleClick:function( event ) {
		var element = Event.element( event );
		if( this.buttons.include( element ) ) {
			this.buttons.each( function(b){ b._selected = false; } );
			element._selected = true;
		}
		this.updateButtonClassNames();
		this.setFontSize( element._fontSize );
	},
	
	handleMouseOver:function( event ) {
		var element = Event.element( event );
		if( this.buttons.include( element ) ) {
			this.buttons.each( function(b){ b._hovered = false; } );
			element._hovered = true;
		}
		this.updateButtonClassNames();
	},
	
	handleMouseOut:function( event ) {
		var element = Event.element( event );
		if( this.buttons.include( element ) ) {
			element._hovered = false;
		}
		this.updateButtonClassNames();
	},
	
	/**
	 * If name is accepted return it unchanged.
	 * If name is accepted, but wrong case, return correct case.
	 * If name is not accepted, return default size name
	 */
	normalizeSizeName:function( name ) {
		if( name == null ) {
			name = this.ALLOWED_FONTSIZE_NAMES[0];
		} else {
			name = name.toLowerCase();
			if( !this.ALLOWED_FONTSIZE_NAMES.include( name ) ) {
				name = this.ALLOWED_FONTSIZE_NAMES[0];
			}
		}
		return name;
	},
	
	setFontSize:function( fontSize ) {
		fontSize = this.normalizeSizeName( fontSize );
		this.storePreference( fontSize );
		var b = $(document.body);
		b.removeClassName( "normalFontSize" );
		b.removeClassName( "largerFontSize" );
		b.removeClassName( "largestFontSize" );
		b.addClassName( fontSize + "FontSize" );
	},
	
	getStoredPreference:function() {
		var stored = DNV_CookieManager.getCookie( this.PREFERENCE_COOKIE_NAME );
		return this.normalizeSizeName( stored );
	},
	
	storePreference:function( preferredFontSize ) {
		preferredFontSize = this.normalizeSizeName( preferredFontSize );
		DNV_CookieManager.setCookie( this.PREFERENCE_COOKIE_NAME, preferredFontSize, this.PREFERENCE_COOKIE_EXPIREDAYS );
	}
}



/*******************************************/
/****   EMAIL A FRIEND                 *****/
/*******************************************/

var DNV_ContentEmailFriend = Class.create();
DNV_ContentEmailFriend.prototype = {
	
	button:null,
	form:null,
	
	initialize:function(button,form) {
		this.button = $(button);
		this.form = $(form);
		this.initEvents();
	},
	
	initEvents:function() {
		Event.observe( this.button, "click", this.handleButtonClick.bindAsEventListener(this) );
		Event.observe( this.form.down(".cancel"), "click", this.handleCancelClick.bindAsEventListener(this) );
		Event.observe( this.form, "submit", this.handleSubmit.bindAsEventListener(this) );
	},
	
	handleButtonClick:function(event) {
		Event.stop(event);
		this.toggleForm();
	},

	handleCancelClick:function(event) {
		this.toggleForm();
	},
	
	toggleForm:function() {
		this.form.toggle();
		
		var selectArr = $A(document.getElementsByTagName("select"));
		
		if( this.form.visible() ) {
			this.form.down(".fields").show();
			this.form.down(".message").hide();
			this.form.elements[0].focus();
			this.freezeHeight();
			
			selectArr.each(function(item){
				$(item).hide();
			});
		}else{
			selectArr.each(function(item){
				$(item).show();
			});
		}
	},
	
	freezeHeight:function() {
		this.form.style.height = (this.form.getHeight()-40) + "px";
	},
	
	handleSubmit:function(event) {
		Event.stop(event);
		if( this.validateForm() ) {
			this.showMessage("WAIT");
			new Ajax.Request(
				this.form.action,
				{	method:this.form.method,
					parameters:this.form.serialize(true),
					onSuccess:this.handleSubmitSuccess.bind(this),
					onFailure:this.handleSubmitFailure.bind(this) } );
		}
	},
	
	validateForm:function() {
		var hasErrors = true;
		var elements = this.form.serialize(true);
		hasErrors = ( elements.from.indexOf("@") == -1 || elements.to.indexOf("@") == -1 );
		if( hasErrors ) {
			this.form.down(".validationError").show();
		} else {
			this.form.down(".validationError").hide();
		}
		return !hasErrors;
	},
	
	handleSubmitSuccess:function(transport) {
		this.showMessage(transport.responseText);
		new Effect.Fade( this.form, {delay:1} );
	},
	
	handleSubmitFailure:function(transport) {
		this.showMessage("ERROR");
	},
	
	showMessage:function(message) {
		this.form.down(".fields").hide();
		var container = this.form.down(".message");
		container.show();
		container.getElementsBySelector("p").each(function(el){$(el).hide()});
		switch(message) {
			case "WAIT" : container.down(".wait").show(); break;
			case "ERROR" : container.down(".error").show(); break;
			default: 
				container.down(".result").show();
				container.down(".result").update(message);
				break;
		}
	}
	
}







/*******************************************/
/****   FLASH BANNER                    ****/
/*******************************************/


var DNV_FlashBanner = Class.create();

DNV_FlashBanner.prototype = {
	
	REQUIRED_FLASH_VERSION:"8",
	pathToSwf:null,
	debug:false,
	messages:[],
	
	COOKIE_NAME:"flashMessageIndex",
	COOKIE_EXPIRES_DAYS:10,
	RENDER_DELAY:200,
	renderTargetId:null,
	
	initialize:function( pathToSwf ) {
		this.pathToSwf = pathToSwf;
		DNV_FlashBanner.instance = this;
	},
	
	/** Place the flash content in the container */
	renderInElement:function( targetId ) {
		this.renderTargetId = targetId;
		$(targetId).update("");
	},
	
	delayedRender:function() {
		var targetId = this.renderTargetId;
		var target = $(targetId);
		var so = new SWFObject( this.pathToSwf, targetId, target.getWidth(), target.getHeight(), this.REQUIRED_FLASH_VERSION, "#FFFFFF" );
		so.addParam( "wmode", "transparent" );
		so.addParam( "menu", "false" );
		so.addVariable( "debug", this.debug ? "true" : "false" );
		for( var i = 0; i < this.messages.length; i++ ) {
			var message = this.messages[i];
			so.addVariable( "template-"+i, message.getTemplate() );
			so.addVariable( "title-"+i, message.getTitle() );
			so.addVariable( "text-"+i, message.getText() );
			so.addVariable( "color-"+i, message.getColor() );
			so.addVariable( "linkUrl-"+i, message.getLinkUrl() );
			so.addVariable( "imageUrl-"+i, message.getImageUrl() );
			so.addVariable( "animation-"+i, message.getAnimation() );
		}
		var index = this.getStoredMessageIndex();
		if( index >= this.messages.length ) { index = 0; }
		this.storeMessageIndex( index + 1 );
		so.addVariable( "startIndex", index );
		so.write( targetId );
	},
	
	setDebugMode:function( mode ) {
		this.debug = mode;
	},
	
	addMessage:function() {
		var message = new DNV_FlashBannerMessage();
		this.messages.push( message );
		return message;
	},
	
	getStoredMessageIndex:function() {
		var index = parseInt(DNV_CookieManager.getCookie( this.COOKIE_NAME ));
		if( index == null || isNaN( index ) ) {
			return 0;
		} else {
			return index;
		}
	},
	
	storeMessageIndex:function( index ) {
		DNV_CookieManager.setCookie( this.COOKIE_NAME, index, this.COOKIE_EXPIRES_DAYS );
	}
	
}

/** Banner message value object **/
var DNV_FlashBannerMessage = Class.create();
DNV_FlashBannerMessage.prototype = {
	
	template:"",
	title:"",
	text:"",
	color:"",
	linkUrl:"",
	imageUrl:"",
	animation:"",
	
	initialize:function() {},
	
	setTemplate:function( template ) { this.template = template; },
	getTemplate:function() { return this.template; },
	setTitle:function( title ) { this.title = title; },
	getTitle:function() { return this.title; },
	setText:function( text ) { this.text = text; },
	getText:function() { return this.text; },
	setColor:function( color ) { this.color = color; },
	getColor:function() { return this.color; },
	setLinkUrl:function( url ) { this.linkUrl = url; },
	getLinkUrl:function() { return this.linkUrl; },
	setImageUrl:function( url ) { this.imageUrl = url; },
	getImageUrl:function() { return this.imageUrl; },
	setAnimation:function( animation ) { this.animation = animation; },
	getAnimation:function() { return this.animation; }
	
}



/*******************************************/
/****   FLV PLAYER                      ****/
/*******************************************/


var DNV_FlvPlayer = Class.create();
DNV_FlvPlayer.prototype = {
	
	videoWidth:null,
	videoHeight:null,
	videoUrl:null,
	stillUrl:null,
	playerUrl:null,
	text:{},
	
	initialize:function( playerUrl ) {
		this.playerUrl = playerUrl;
	},
	renderInElement:function( targetId ) {
		if(this.validate()) {
			var playerSize = this.getPlayerSize();
			var so = new SWFObject(this.playerUrl,"flvplayer",playerSize.width,playerSize.height,"8","#FFFFFFF");
			so.addParam("wmode","transparent");
			so.addParam("menu","false");
			so.addVariable("videoUrl",this.videoUrl);
			so.addVariable("stillUrl",this.stillUrl);
			for( var t in this.text ) {
				so.addVariable("text."+t,this.text[t]);
			}
			so.write(targetId);
		}
	},
	validate:function() {
		var valid = 
			this.videoWidth != null &&
			this.videoHeight != null &&
			this.videoUrl   != null &&
			this.stillUrl  != null
			;
		return valid;
	},
	getPlayerSize:function() {
		var size = {};
		size.padding = 4;
		size.controlsHeight = 34;
		size.width  = this.videoWidth  + (size.padding*2);
		size.height = this.videoHeight + (size.padding*3) + size.controlsHeight;	
		return size;
	}
	
}





