// JavaScript Document

var GBEC = {
	
	duration: 0.2,
	searchbar:null,
	active_video:null,
	player:null,
	
	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;
			}
		});*/
		
		$('#wpirfy_submit').live('click', GBEC.wpirfy_submit);
		
		$('a.expand').bind('click', GBEC.toggle_expand);
		
		if ($("#flashboard:not('.sub')").length > 0){
			$("#flashboard:not('.sub')").flash({
				swf: '/swf/billboard.swf',
				height: 216,
				width: 840
			});
		}
		if ($("#refractive_billboard").length > 0){
			$('#refractive_billboard').flash({
		    	swf: '/swf/refractive_billboard.swf',
		    	width: 820,
		    	height: 250
			});
		}
		
		if ($("#tdeonline").length > 0){
			$('#tdeonline').flash({
				swf: "http://eyemaginations.com/3deyeonline/3deonlinev20.swf",
				height: 400,   
				width: 402,
			
				flashvars: { aID: '9fa82ddc6d4cd937c01c1a064ef060e8' },
			
				params: { wmode:'transparent' }
			});
		}
		
		/**** which procedure is right for you? ****/
		GBEC.setWPIRFYActions();
		
		
		// fix menu
		$('td.mainmenuitem').bind('click',function(){
			document.location.href = $(this).children('a').attr('href');
		});
		
		/**** video player ****/
		$('a.videolink').bind('videoover',GBEC.videoover)
						.bind('videoout',GBEC.videoout)
						.bind('videoclick',GBEC.videoclick)
						.each( function()
		{
			var vidlink = $(this);
			
			vidlink.img = vidlink.children('img');
			if( vidlink.img )
			{
				vidlink.bind('mouseover', function()
				{
					$(this).trigger('videoover');
				});
				vidlink.bind('mouseout', function()
				{
					$(this).trigger('videoout');
				});
			}
			vidlink.bind('click', function(){ $(this).trigger('videoclick'); return false; });
		});
		
		
		/**** search functionality ****/
		
		
		if( GBEC.searchbar = $('#searchbar'))
		{
			// alert('ds');
			var input 			= $('#search_field');
			var button			= $('#search_button');
			var results 		= $('#results');
			
			//input.value = 'Search';
			input.blur();
			
			input.bind('focus', function(){ if($(this).val() == 'Search') $(this).val(''); })
				.bind('blur', function(){ results.fadeOut(); })
				.bind( 'keyup', function(){
					if($(this).val().length > 1)
					{
						results.fadeIn();
						$.post(
							'/ajax/smart_search',
							{ string: $(this).val() },
							function(resp)
							{
								var response = resp || "<li>Your search had no results...</li>";
								$('#result_links').html(response);
							}
						);
					}
					else
					{
						results.fadeOut();
					}
				});
			
		}		
		if ($(".captchaform").length > 0){
			$(".captchaform").slideLock({
		
				// set the options - all are given, not all are required
				labelText: "Slide to unlock for verification purposes.",
				noteText: "",
				lockText: "Locked",
				unlockText: "Unlocked",
				iconURL: "images/chrome/arrow_right.png",
				inputID: "sliderInput",
				onCSS: "#333",
				offCSS: "#aaa",
				inputValue: 1,
				saltValue: 9,
				checkValue: 10,
				js_check: "js_check",
				submitID: "#submit"
		
			});
		}
	},
	
	wpirfy_submit : function()
	{
		$.post(
			'/ajax/wpirfy_submit',
			{ email_address:$('#wpirfy_email_address').val(), name:$('#wpirfy_name').val(), phone:$('#wpirfy_phone').val() },
			function(resp)
			{
				$('#wpirfy_email').html(resp).hide().fadeIn();
			}
		);

		
		return false;
	},
	
	toggle_expand : function()
	{
		var id = $(this).attr('rel');
		$('#'+id).toggle();
		return false;
	},
	
	videoover : function()
	{
		var v = $(this);
		var img = v.children('img');
		img.attr('src',img.attr('src').replace('.jpg','_active.jpg'));
	},
	
	videoout : function()
	{
		var v = $(this);
		var img = v.children('img');
		if(v!=GBEC.active_video) img.attr('src',img.attr('src').replace('_active.jpg','.jpg'));
	},
	
	videoclick : function()
	{
		var v = $(this);
		GBEC.active_video = v;
		$('#instruction').hide();
		
		if($('#patient_name')) $('#patient_name').html( v.attr('rel').split('|')[0] );
		if($('#procedure')) $('#procedure').html( v.attr('rel').split('|')[1]);
		if($('#procedure_link')) $('#procedure_link').html('<a href="'+v.attr('rel').split('|')[2]+'">Learn more about this procedure</a>');
		
		if($f())
		{
			$f().play({url:v.attr('href')});
		}
		else
		{
			$f('videoplayer','/swf/flowplayer-3.1.5.swf',
			{
				clip: {
					autoPlay: true,
					autoBuffering: true,
					url: v.attr('href')
				}
			});
		}
		return false;
	},
	
	setWPIRFYActions : function()
	{
		
		$('a.wpirfy').live('click',function()
		{
			$.post(
				'/ajax/wpirfy/',
				{ qa:$(this).attr('rel') },
				function(resp)
				{
					$('#wpirfy_questionnaire').html('<div>'+resp+'<p>Need to change an answer? <a href="are_you_a_candidate">Restart the online consultation.</a></p></div>' );
				}
			);

			return false;
		});
		
	},

	validate_form : function(form)
	{
		if(!form) form = document.forms[0];
		var submit_it = true;
		var email_pattern = new RegExp(/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i);

		$(form).find(".REQUIRED").each(function(req)
		{
			if(($(this).attr('type')!='checkbox' && this.value == '') || 
				($(this).attr('type')=='checkbox' && !this.checked) || 
				($(this).hasClass('email') && !email_pattern.test($(this).val()))) 
			{
				$(this).parents("div.formfield").addClass('error');
				submit_it = false;
			}
			else
			{
				$(this).parents("div.formfield").removeClass('error');
			}			
		});
		
		if(isset($_POST['sliderInput']) && $_POST['sliderInput'] == 10) {
		
			// sanatize and validate user input
			submit_it = true;
		
		}else{
		
			// validation has failed, inform user
			submit_it = true;
		
		}
		if( !submit_it ) $('#error_message').fadeIn(250);
	//	if($('#please_confirm_you_are_human_by_typing_gbec_in_the_field_below').val() != "GBEC") {
	//		submit_it = false;
	//	}
		return submit_it;
	}
}

$(document).ready(function() {
	GBEC.init();
});

