$(document).ready(function () {
	/* 
	* This script creates the video output for the Education Center hub pages, On-Demand Video page and the 
	* XpressMethod page. It also controls the Xpound/Xpresso Registration form on the Inside hub page.
	*
	* Written Alan Eicker <aeicker@optionsxpress.com>
	* Created on 3/20/2010 - Last Modified on 4/7/2010
	*/
	
	// Detect if flash is intalled and replace video with images if flash is not installed
	var version = '0';
	function getFlashVersion(){ 
	  // ie 
	  try { 
		try { 
		  var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6'); 
		  try { axo.AllowScriptAccess = 'always'; } 
		  catch(e) { return '6,0,0'; } 
		} catch(e) {} 
		return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1]; 
	  // other browsers 
	  } catch(e) { 
		try { 
		  if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){ 
			return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1]; 
		  } 
		} catch(e) {} 
	  } 
	  return '0,0,0'; 
	} 
	version = getFlashVersion().split(',').shift(); 
	
	// controls video output for On-Demand and XpressMethod pages.
	if ($(document.body).attr('id') == 'education-videos' || $(document.body).attr('id') == 'xpressmethod') {
		if (version == '0') { // If user does not have Flash Player installed
			$('#main-column').hide(); // Hide menu
			$('#secondary-column').hide(); // Hide video
			
			// If user does not have Flash installed - prompt user to download and install Flash
			var error = '<div id="download-flash" style="padding-left:10px;">';
			error += '<h3 style="color:red;">This page requires Flash Player.</h3>';
			error += '<p><strong>You can download and install the latest version <a href="http://get.adobe.com/flashplayer/" target="_blank">here</a>.</strong></p>';
			error += '</div>';
			
			$(error).appendTo('#content'); // append error message to '#content' div
		} else {
			$('#video-container a').click(function () { return false; }); // return false to deactivate link
		
			var width;
			var height;
			
			// controls height and width of video object
			if (s.pageName == 'Pre:On-Demand Videos') { // If On-Demand Videos page
				width = '530px';
				height = '323px';
			} else { // If XpressMethod page 
				width = '402px';
				height = '251px';
			}
			
			// controls accordion left menu
			$('#video-list-container').accordion({
				autoHeight: false,
				navigation: false,
				header: '.section-head'
			});
			
			var curItem = $('a.video-item:first'); // set current selected video item to first item in menu
			$(curItem).css('font-weight','bold'); // make current selected menu item bold
			
			$('a.video-item').click(function () {
				document.getElementById('video-container').innerHTML = '';
				$(curItem).css('font-weight','normal');
				$(this).css('font-weight','bold');
				var linkId = $(this).attr('id'); // gets 'id' attribute of link 
				var id = linkId.replace('vid_',''); // strips 'vid_' from id attribute to get a numerical value
				$('#caption').text($(this).attr('title')); // gets 'title' attribute of link clicked and created caption for video
				var html = '<a href="#" rel="videoGUID=' + id + '" style="border:1px solid #bbb; display:block; width:' + width + '; height:' + height + ';" id="' + id + '">' + $(this).attr('title') + '</a> <script type="text/javascript" src="http://360.sorensonmedia.com/' + id + '/embed.js?autoPlay=true"></script>';
				$('#video-container').append(html); // replace html in '#video-container' with new video object
				curItem = $(this);
				return false;
			});		
		}
		$('#adv-order-disclosure').click(function () {
			window.location = '/security_risks/disclosures.aspx#advanced-orders-2';
			return false;
		});
		$('#xtend-disclosure').click(function () {
			window.location = '/security_risks/disclosures.aspx#advanced-orders-3';
			return false;
		});

	}
	
	// controls video output for education Center hub pages.
	if ($(document.body).attr('class') == 'free-education hub' && $('#container').attr('class') == 'edu') {
		if (version == '0') { // If user does not have Flash Player installed
			$('#container').removeClass('edu'); // remove edu class from '#container' div
			$('.image').remove('a.video'); // remove video
			$('<p>' + s.pageName.replace('Pre:','') + ' screenshot</p>').appendTo('.image'); // append '.image' div with '<p>text</p>' tag
		} else { // If user has Flash Player installed
			$('.video').click(function () { return false; }); // return false to deactivate link
			var video_id = $('.video').attr('rel').replace('videoGUID=',''); // strips 'videoGUID=' from rel attribute to get a numerical value 
			$('.video').show(function() {
				this.id = video_id; // add 'id' '.video_id' anchor
			});
			var embedCodeSrc = '<script type="text/javascript" src="http://360.sorensonmedia.com/' + video_id + '/embed.js"><' + '/' + 'script>'; // create javascript
			$(embedCodeSrc).insertAfter('.video'); // insert javascript '.video_id' anchor
		}
	}	
	
	// controls bottom-content on Inside hub page
	if ($(document.body).attr('id') == 'insider-landing') {
		/* Controls bio text */
		var curBio = '#bio-insider1'; // set current bio 
		$('dl.insider-bios').hide(); 
		$(curBio).show(); 
		$('a.insider-thumbs').hover(function () {
			var newBio = '#bio-' + $(this).attr('id'); // get id of selected anchor - will be used to reset 'curBio'
			if (curBio != newBio) { // prevents multiple mouseovers on current item
				$(curBio).hide();
				$(newBio).fadeIn(); 
				curBio = newBio; 
			}
			$(this).click(function () { return false; }); // deactivates 'a.insider-thumbs'
		});
	
		/* validates newsletter registration on */
		$('form#xpresso-registration-form').validate({
			errorPlacement: function (error, element) {
				var $parentElement = element.parent();
				error.appendTo($parentElement);
			}
		});
		
		/* swaps newsletter registration form header */
		$('.choose-registration').click(function () {
			var newContainerClass = $(this).attr('id') + '-registration'; // Sets new class for'#registration-form' form
			var newFormHeadClass = $(this).attr('id') + '-head'; // sets new heading class for '#form-heading' heading
			$('#signup-choose').hide();
			$('#registration-form').hide();
			$('#registration-form').removeClass('hide');
			$('#registration-form').addClass(newContainerClass); // Add new Class to '#registration-form' form 
			$('#form-heading').addClass(newFormHeadClass); // Add new Class to '#form-heading' heading
			$('#registration-form').fadeIn(); // show selected form
		});
		
		/* takes user back to registration selection*/
		$('#go-back').click(function () {
			$('#form-heading').removeClass();
			$('#registration-form').hide();
			var newContainerClass = '#signup-choose';
			$(newContainerClass).show();
			return false;
		});
				
		/* swaps out for action depending on what newsletter user is registering for */
		$('.submit').click(function () {
			var newClass = $('#form-heading').attr('class');
			$('#form').addClass(newClass);
			var form = document.getElementById('xpresso-registration-form');
			// change action to route form to proper location in Exact Target
			if ($('#form-heading').is('.xpresso-head')) {
				form.action = 'http://cl.exct.net/subscribe.aspx?lid=14867704'; // If Xpresso
			} else {
				form.action = 'http://cl.exct.net/subscribe.aspx?lid=15087653'; // If Xpound 
			}
			$('#xpresso-registration-form').submit();
		});
	} 
		
});