// JavaScript Document

var GBEC = {
	
	duration:Number = 0.2,
	searchbar:Object,
	
	init : function()
	{
		$$('a[id^=eyemaginations_btn]').each( function(btn)
		{
			btn.onclick = function()
			{
				// fade in the overlay (re)used by lightbox
				var arrayPageSize = Lightbox.prototype.getPageSize();
				$('overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
				new Effect.Appear('overlay', { duration: .3, from: 0.0, to: 0.85 });
				
				// position and show the Eyemaginations movie
				var arrayPageScroll = document.viewport.getScrollOffsets();
				var lightboxTop = arrayPageScroll[1] + Math.round(document.viewport.getHeight()-$('eyemagbox').getHeight())/2;
				var lightboxLeft = Math.round(arrayPageSize[0]-$('eyemagbox').getWidth())/2;
				$('eyemagbox').setStyle({ top: lightboxTop + 'px', left: lightboxLeft + 'px' }).show();
				
				swfobject.embedSWF( "http://eyemaginations.com/3deyeonline/3deonlinev20.swf", "eyemag_popup", "402", "400", "9.0.0", "expressInstall.swf", {aID:this.rel},{wmode:"transparent"} );
				
				$('close_eyemagbox').onclick = function()
				{
					$('close_eyemagbox').parentNode.hide();
					new Effect.Fade('overlay', { duration: 0.3 });
					return false;
				}
				
				return false;
			}
		});
		
		swfobject.embedSWF( "http://eyemaginations.com/3deyeonline/3deonlinev20.swf", "tdeonline", "402", "400", "9.0.0", "expressInstall.swf", {aID:"9fa82ddc6d4cd937c01c1a064ef060e8"},{wmode:"transparent"} );
		
		
		/**** which procedure is right for you? ****/
		GBEC.setWPIRFYActions();
		
		
		// fix menu
		$$('td.mainmenuitem').each(function(menuitem)
			{
				menuitem.onmousedown = function()
				{
					
				}
				
				menuitem.onmouseup = function()
				{
					
				}
				
				menuitem.onclick = function()
				{
					document.location.href = this.firstDescendant().href;
				}
			});
		
		/**** video player ****/
		var player = null;
		
		$$('a.videolink').each( function(vidlink)
		{
			vidlink.has_desc = vidlink.firstDescendant();
			vidlink.active = false;
			if( vidlink.has_desc )
			{
				vidlink.inactivestate = vidlink.firstDescendant().src;
				vidlink.activestate = vidlink.firstDescendant().src.replace('.jpg','_active.jpg');
				
				vidlink.onmouseover = function()
				{
					this.firstDescendant().src = this.activestate;
				}

				vidlink.onmouseout = function()
				{
					if(!this.active) this.firstDescendant().src = this.inactivestate;
				}
			}

			
			vidlink.onclick = function()
			{
				// set and unset active thumbs
				this.active = true;
				this.ancestors()[0].siblings().each( function(sibling)
											  	{ 
													//alert(sibling);
													var a = sibling.firstDescendant();
													if(a.active)
													{
														a.active = false;
														if(a.firstDescendant()) a.firstDescendant().src = a.inactivestate;
													}
												} );
				
				$('instruction').hide();
				if($('patient_name')) $('patient_name').update( this.rel.split('|')[0] );
				if($('procedure')) $('procedure').update( this.rel.split('|')[1]);
				if($('procedure_link')) $('procedure_link').update('<a href="'+this.rel.split('|')[2]+'">Learn more about this procedure</a>');
				
				if(!player)
				{
					player = flashembed("videoplayer", 
						{
							src:'/swf/FlowPlayerDark.swf',
							width: 360,
							height: 230
						},
						{config: {   
							autoPlay: true,
							autoBuffering: true,
							controlBarBackgroundColor:'0x000000',
							videoFile: this.href,
							showOnLoadBegin: true,
							loop: false,
							timeDisplayFontColor: '0x7df3e1',
							showMenu: false
						}} 
					);
				}
				else
				{
					player.setConfig(
						{
							autoPlay: true,
							autoBuffering: true,
							controlBarBackgroundColor:'0x000000',
							videoFile: this.href,
							showOnLoadBegin: true,
							loop: false,
							timeDisplayFontColor: '0x7df3e1',
							showMenu: false
						}
					);
				}
				 return false;
			}
		});
		
		
		/**** search functionality ****/
		
		if( GBEC.searchbar = $('searchbar'))
		{
			var input 			= GBEC.searchbar.childElements()[0];
			var button		= GBEC.searchbar.childElements()[1];
			var results 		= $('results');
			
			//input.value = 'Search';
			input.blur();
			
			button.onclick = function()
			{
				//
			}
			
			input.onfocus = function()
			{
				if(this.value == 'Search') this.value = '';
			}
			
			input.onblur = function()
			{
				new Effect.Fade( results, { duration:GBEC.duration });
			}
			
			input.onkeyup = function()
			{
				if(input.value.length > 1)
				{
					new Effect.Appear( results, { duration:GBEC.duration });
					new Ajax.Request('/includes/ajax/smartSearch.php',
					{
						method:'get',
						parameters:{ string:input.value },
						onSuccess: function(transport)
						{
							var response = transport.responseText || "<li>Your search had no results...</li>";
							$('result_links').update(response);
							//$('num_results').update(response);
							//$('result_s').update(myUtils.plural(response));
						},
						onFailure: function(){ alert('Something went wrong...') }
					});
				}
				else
				{
					new Effect.Fade( results, { duration:GBEC.duration });
				}
			}
		}		
	},
	
	setWPIRFYActions : function()
	{
		$$('a.wpirfy').each( function(question)
			{
				var qa = question.rel;
				question.onclick = function()
				{
					new Ajax.Request('/includes/ajax/wpirfy.php',
					{
						method:'get',
						parameters: { qa:qa },
						onSuccess: function(transport)
						{
							var response = transport.responseText || "no response text";
							//$('wpirfy_questionnaire').setOpacity(0.01);
							$('wpirfy_questionnaire').update('<div>'+response+'<p>Need to change an answer? <a href="are_you_a_candidate">Restart the online consultation.</a></p></div>' );
							//new Effect.Appear('wpirfy_questionnaire');
							GBEC.setWPIRFYActions();
						},
						onFailure: function(){ alert('Something went wrong...') }
					});

					return false;
				}
			});
	}
}

Event.observe( window, 'load', GBEC.init );