//JavaScript

Event.observe(window, "load", initialize);

function initialize(){
	if ($('screen_capture')){
		var size = getPageSizeInfo();
		$('screen_capture').setStyle({width: size.pageWidth+'px', height: size.pageHeight+'px'});
	}
	
	if($('nav')) {
		Event.observe('rideicon', 'mouseover', handleWindowHover);
		Event.observe('nav', 'click', handleNavClick);
	}
	
	//set listeners for tab and view clicks
	if($('featured_product')){
		init_product_details();	
	}
	
	// swatches
	if($$('.swatches')) {
		$$('.swatch a').each(function(el) {
			$(el).observe('click', swap_swatch);
		});
	}
	
	//set listeners for homepage thumbnails
	if($('index_thumbs')){
		var ind_thumbs = $('index_thumbs').getElementsByTagName('a');
		for(var i=0; i<ind_thumbs.length; i++){Event.observe(ind_thumbs[i], 'click', changeHomeImage)}
	}
	
	Event.observe('search_field','click',clearSearchField);
	//validate form submissions
	// if ($('dealer_search_form')){$('dealer_search_form').onsubmit = function(){return validateDealerSearch();}}
	if ($('contact_form')){$('contact_form').onsubmit = function(){return validateForm(['name', 'message']);}}
	if ($('catalog_request_form')){
		$('catalog_request_form').onsubmit = function(){
			return validateForm(['first_name', 'last_name', 'address', 'city', 'state', 'zip_code']);
		}
	}
	
	//newsletter sign-up
	if($('pdx_flash') || $('nolimit_flash') || $('splash_video')) {
		Effect.Appear('NewsletterForm', { duration: 0.90, delay: 0.0 });
		Event.observe('newsletter', 'submit', emailSubmission);
		$('signup').onclick = clearNewsletterSignup;
	}
	
	//observe clicks on media and media display area buttons
	if ($('media_content')){
		var printAds = getPrintAds();
		for (var i=0; i<printAds.length; i++){Event.observe(printAds[i],'click',showMedia);}
		Event.observe('previous_media','click',prevMedia);
		Event.observe('next_media','click',nextMedia);
		Event.observe('close_media_tab','click',hideMedia);
		Event.observe('media_image', 'mouseover', showMediaPagination)
		Event.observe('next_media', 'mouseover', showMediaPagination)
		Event.observe('previous_media', 'mouseover', showMediaPagination)
		Event.observe('media_image', 'mouseout', hideMediaPagination)
		Event.observe('next_media', 'mouseout', hideMediaPagination)
		Event.observe('previous_media', 'mouseout', hideMediaPagination)
	}
	
	if($('secondaryInfo')){
		Event.observe('country', 'change', showSecondaryInfo)
	}
	
	var scrollerWidth = 0;
	
	if($('media_thumbs')){
		var thumbs_a	= $('media_thumbs').getElementsByTagName('a');
		var thumbs = $('media_thumbs').getElementsByTagName('img');
		for(var i=0; i<thumbs.length; i++){
			scrollerWidth += thumbs[i].width + 8;
			if(!thumbs_a[i].id) {
				Event.observe(thumbs_a[i],'click', setMainMediaImage); 
			} else {
				Event.observe(thumbs_a[i], 'click', setMainMediaVideo);
			}
			//Event.observe(thumbs[i],'click', setMainMediaImage); 
		}
		$('media_thumbs').setStyle({height: '85px', overflowX: 'scroll', overflowY: 'hidden'});
		$('media_scroll_container').setStyle({width: scrollerWidth+'px'});
	}	
}


var init_product_details = function() {
	var tabs = ['overview', 'manual', 'numbers', 'sizing', 'care', 'accessories','details','media','armor', 'pantarmor']
	for (var i=0; i<tabs.length; i++){if($(tabs[i]+'_tab')){Event.observe(tabs[i]+'_tab','click',openTab);}}
		
	if($('front_view')){$('front_view').onclick = function(){return showFrontView('1' + getCurrentRank());}}
	if($('back_view')){$('back_view').onclick = showBackView;}
	if($('threesixty_view')){$('threesixty_view').onclick = showThreesixty;}
	if($('zoom_view')){$('zoom_view').onclick = showZoom;}
	if($('video_view')){$('video_view').onclick = showVideo;}
	if($('dfahidden_view')){$('dfahidden_view').onclick = showDfaVideo;}
		
	//set listeners for colorway clicks
	if($('colorways')){
		var thumbs = $('colorways').getElementsByTagName('a');
		for(var i=0; i<thumbs.length; i++){thumbs[i].onclick = setColorway;}
	}
	//details views
	if($('features_view')){$('features_view').onclick = function(){return showFeatures('features');}}
	if($('venting_view')){$('venting_view').onclick = function(){return showFeatures('venting');}}
	if($('construction_view')){$('construction_view').onclick = function(){return showFeatures('construction');}}
	if($('comfort_liner_view')){$('comfort_liner_view').onclick = function(){return showFeatures('comfort_liner');}}
	if($('shield_view')){$('shield_view').onclick = function(){return showGraphic('shield');}}
	//links 
	if($('helmet_fit_link')){$('helmet_fit_link').onclick = function(){return changeLink();}}
};

var changeLink = function() {
	location.href='product_details.jsp?tab=sizing&category='+getCategoryId()+'&id='+getProductId()+'&view=front&rank=100';
	return false;
}

var init_armor_details = function() {
	var thumbs = $('armor_colorways').getElementsByTagName('a');
	for(var i=0; i<thumbs.length; i++){
		Event.observe(thumbs[i], 'click', setArmor);
	}
};

function setArmor(e){
	Event.stop(e);
	var target = Event.element(e).src;
	var text = Event.element(e).next();
	if (target) {
		var img = target.sub('thumbs/', '');
		$('featured_product').setStyle({
			background: '#fff'
		});
		$('featured_product').update("<img src='" + img + "' />");
		$('overview').update(text.innerHTML);
	}
	return false;
}

var showFeatures = function(detailView){
	$('featured_product').setStyle({background: '#fff'});
	changeSelectionLink('product_views', detailView+'_view', 'active_view');
	url='tabs/inc_details_desc.jsp?details='+detailView+'&category='+getCategoryId()+'&id='+getProductId()+'&rank=100&tab=details';
	new Ajax.Request(url, {onSuccess: function(request){
		$('overview').update(request.responseText);//show copy contents
		showDetailsVideo($(detailView+'_video').innerHTML);//show video
		//links 
		if($('helmet_fit_link')){$('helmet_fit_link').onclick = function(){return changeLink('comfort_liner_link');}}
		}
	});
	return false;
};

var showGraphic = function(detailView){
	$('featured_product').setStyle({background: '#fff'});
	changeSelectionLink('product_views', detailView+'_view', 'active_view');
	url='tabs/inc_details_desc.jsp?details='+detailView+'&category='+getCategoryId()+'&id='+getProductId()+'&rank=100&tab=details';
	new Ajax.Request(url, {onSuccess: function(request){
		$('overview').update(request.responseText);//show copy contents
		$('splash_video').innerHTML = '<img src="' + $(detailView+'_video').innerHTML + '" />';
		}
	});
	return false;
};

var showDetailsVideo = function(videoFile){
	var so = new SWFObject("flash/videoPlayer.swf", "index", "385", "385", "8");
	so.addParam('wmode', 'transparent');
	so.addParam('allowFullScreen', 'true');
	so.addVariable('wmode', 'transparent');
	so.addVariable('allowFullScreen', 'true');
	so.addVariable("documentRoot", "flash/");
	so.addVariable('videoFile', videoFile);
	so.addVariable('videoConfigFile', 'detailsSmallVideo.xml');
	so.write('splash_video');
};

var show_360 = function() {
	var so = new SWFObject("flash/360.swf", "index", "385", "385", "8");
	so.addVariable("imagePath", "images/360/");
	so.addVariable("imageCount", '15');
	so.addParam('wmode', 'transparent');
	so.write('featured_product');
};

var show_zoom = function(product, zoom) {
	console.log('testing');
	console.log('test' + zoom);
	var so = new SWFObject("flash/zoom.swf", "index", "385", "385", "8");
	so.addVariable("imagePath", escape("showImage.jsp?class_id="+ product +"&image_type=fullsize&rank=" + rank));
	so.addParam('wmode', 'transparent');
	so.write('featured_product');
};

var handleWindowHover = function(e) {
	var a = Event.element(e);
	var nav = false;
	var name = false;
	var hasClass = false; 
	
	if(a) {
		name = a.id.split('_')[1];
		nav = $('sub_' + name);
		hasClass = a.className.include('nav_item');
	}

	if(nav && a.id.split('_')[0] == 'a' && nav.getStyle('display') != 'block') {
		clearSelected();
		nav.style.display = 'block';
		a.addClassName('active_hover');
	}
	if(!hasClass && !nav) { clearSelected(); }
};



var handleNavClick = function(e) {
	var target = Event.element(e).id;
	// ugly, but it works.
	if(target == 'a_products' || target == 'a_company' || target == 'a_culture' || target == 'a_products_active' || target == 'a_company_active' || target == 'a_culture_active') {
		Event.stop(e);
	}
};

var clearSelected = function() {
	$('sub_products').style.display = 'none';
	$('sub_company').style.display = 'none';
	$('sub_culture').style.display = 'none';
	var class_array = $$('.active_hover');
	for(var i=0; i<class_array.length; i++) {
		class_array[i].removeClassName('active_hover');
	}
};

/*
 * START     Dealer Locator Section
 */
function showSecondaryInfo() {
	var selected = $F('country');
	var url = 'includes/secondaryDealerInfo.jsp'; 
	new Ajax.Request(url, {parameters: 'country='+selected,onSuccess: function(request){			
			$('secondaryInfo').update(request.responseText);
		}
	});
}
/* 
 * END       Dealer Locator Section
 */ 


function validateForm(fields){
	var isValid = true;
	isValid = validateTextFields(fields);
	return validateEmail() && isValid ? true : false;
}

function validateDealerSearch(){
	var isValid = true;
	return isValid = validateTextFields(['zip_code']);
}

function validateTextFields(fields){
	var isValid = true;
	for(var i=0; i<fields.length; i++){
		$(fields[i]+'_label').className = '';
		if ($F(fields[i])==''){
	  		$(fields[i]+'_label').className = 'errorLabel';
	  		isValid = false;
	  	}
	  	if (fields[i]=='zip_code' && $F(fields[i]).length<5){
	  		$('zip_code_label').className = 'errorLabel';
	  		isValid = false;
	  	}
	}
	return isValid;
}

function validateEmail(){
	$('email_label').className = '';
	var emailPattern = /[a-z_]*[a-z0-9._%-]?[a-z0-9]+@[a-z0-9][a-z0-9._-]+\.[a-z0-9]+/i;
	if ($F('email')=='' || $F('email').search(emailPattern)==-1){
		$('email_label').className = 'errorLabel';
		return false;
	} else {return true;}
}

function numbersOnly(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode;
 	return (charCode > 31 && (charCode < 48 || charCode > 57)) ? false : true;
}

function showFrontView(rank){
	if(!rank) { rank = '100'; } else { setCurrentRank(rank); }
	$('featured_product').setStyle({background: '#fff'});
	changeSelectionLink('product_views', 'front_view', 'active_view');
	$('featured_product').update("<img src='showImage.jsp?class_id="+getProductId()+"&image_type=fullsize&rank="+rank+"' />");
	return false;
}

function showBackView(){
	var rank = checkRank('2' + getCurrentRank());
	if(!rank) { rank = '200'; } else { setCurrentRank(rank); }
	$('featured_product').setStyle({background: '#fff'});
	changeSelectionLink('product_views', 'back_view', 'active_view');
	$('featured_product').update("<img src='showImage.jsp?class_id="+getProductId()+"&image_type=fullsize&rank=" + rank + "' />"); 
	return false;
}

function checkRank(rank) {
	var exists = false;
	if($('product_rankmap')) {
		var ranks = $('product_rankmap').innerHTML;
		var rank_map = eval('new Array(' + ranks.gsub(/(\s|\{).{1,5}=/, '').gsub('{', '').gsub('}', '') + ')');
		for(var i=0; i<rank_map.length; i++) {
			if(rank_map[i] == rank) {
				exists = true;
			} 
		}
	}
	if(exists) {
		return rank;
	} else { return false; }
}


/*
 * Checks what rank is currently being viewed.
 */
function getCurrentRank() { 
	var rank = $('product_current_rank');
	
	if(rank) {
		var num = rank.innerHTML;
		if(num) {
			return num.substring(1)
		} else {
			return null;
		}
	} else { return null; }
}

/*
 * Sets the rank that is currently being viewed.
 */
function setCurrentRank(rank) { 
	$('product_current_rank').innerHTML = rank;
}



/*
 * When the user clicks on a color swatch, the thumbnail will swap out with 
 * that colorway. The link to the product will also change so the specified 
 * colorway will be shown on load.
 */ 
function swap_swatch(e) {
	Event.stop(e);
	var target = Event.element(e);
	var category = $(target).down('span').innerHTML.split('_')[0];
	var product = $(target).down('span').innerHTML.split('_')[1];
	var rank = $(target).down('span').innerHTML.split('_')[2];
	var link = $(target).up('.product_swatch').down('.swatch_link');
	var img = $(target).up('.product_swatch').down('.swatch_preview');
	$(img).src = 'showImage.jsp?class_id=' + product + '&image_type=fullsize&rank=5' + rank.substr(1);
	$(link).href = 'product_details.jsp?category=' + category + '&id=' + product + '&rank=' + rank;
}

function showThreesixty(){
	$('featured_product').setStyle({background: '#000'});
	changeSelectionLink('product_views', 'threesixty_view', 'active_view');
	var so = new SWFObject("flash/360.swf", "index", "385", "385", "8");
	so.addVariable("imagePath", "images/360/"+getProductId()+"/");
	so.addVariable("imageCount", '15');
	so.addParam('wmode', 'transparent');
	so.write('featured_product');
	return false;
}

function showZoom(){
	var rank = checkRank('3' + getFullImageRank());
	if(!rank) { rank = '300'; } else { setCurrentRank(rank); }
	$('featured_product').setStyle({background: '#000'});
	changeSelectionLink('product_views', 'zoom_view', 'active_view');
	var so = new SWFObject("flash/zoom.swf", "index", "385", "385", "8");
	so.addVariable("imagePath", escape("showImage.jsp?class_id="+getProductId()+"&image_type=fullsize&rank=" + rank));
	so.addParam('wmode', 'transparent');
	so.write('featured_product');
	return false;
}

function showVideo(){
	$('featured_product').setStyle({background: '#fff'});
	changeSelectionLink('product_views', 'video_view', 'active_view');
	var so = new SWFObject("flash/iconVideoPlayer.swf", "index", "385", "385", "8");
	var product=getProductId();
	if (product==8020){
		so.addVariable("videoPath", "video/Sha_Do.flv");
	}
	else if(product==6941 || product==6944){
		so.addVariable("videoPath", "video/shieldChange.flv");
	}else{
		so.addVariable("videoPath", "video/obsession.flv");
	}
	so.addVariable('autoPlay','true'); 
	so.addParam('wmode', 'transparent');
	so.write('featured_product');
	return false;
}

function showDfaVideo(){
	$('featured_product').setStyle({background: '#fff'});
	changeSelectionLink('product_views', 'dfahidden_view', 'active');
	var so = new SWFObject("flash/iconVideoPlayer.swf", "index", "385", "385", "8");
	so.addVariable("videoPath", "dfa_video.flv");
	so.addVariable('autoPlay','true'); 
	so.addParam('wmode', 'transparent');
	so.write('featured_product');
	return false;
}

function openTab(e){
	changeSelectionLink('product_tabs', Event.element(e).id, 'active');
	var url = "tabs/"+Event.element(e).id.split('_')[0]+".jsp";
	showLoadingOverlay();
	new Ajax.Request(url, {parameters: 'id='+getProductId()+'&category='+getCategoryId(),
		onSuccess: function(request){			
			$('product_content_wrapper').update(request.responseText);
			hideLoadingOverlay();
		},
		onFailure: function() {
			hideLoadingOverlay();
		}
	});
	Event.stop(e);
}

function showLoadingOverlay() {
	var width = $('product_content_wrapper').getWidth();
	var height = $('product_content_wrapper').getHeight();
	var div = document.createElement('div');
	div.style.backgroundColor = '#fff';
	//div.setOpacity(0.85); //breaks Internet explorer.
	div.id = 'loadingChild';
	div.style.width = (width - 20) + 'px';
	div.style.height = height + 'px';
	div.style.marginLeft = 20 + 'px';
	div.style.display = 'block';
	div.style.position = 'absolute';
	div.style.zIndex = 99999;
	$('loadingOverlay').appendChild(div);
	
}

function hideLoadingOverlay() {
	$('loadingChild').remove();
}


function openManual(e){
	var url = "tabs/manual.jsp";
	showLoadingOverlay();
	new Ajax.Request(url, {parameters: 'category='+getCategoryId()+'&id='+getProductId()+'&shellType='+Event.element(e).id.split('_')[0] + "&manual=" + Event.element(e).id.split('_')[1],
	onSuccess: function(request){			
			$('product_content_wrapper').update(request.responseText);
			hideLoadingOverlay();
			if(Event.element(e).id.split('_')[2]) {
				changeManualPageLink('manual_nav_list', Event.element(e).id, 'active');
			} else { 
				changeManualPageLink('manual_nav_list', Event.element(e).id + '_nl', 'active');
			}
		},
	onFailure: function() {
		hideLoadingOverlay();
	}
	});
	Event.stop(e);
}


function changeHomeImage(e){
	var id = Event.element(e).parentNode.id.split('_')[1];
	var url = 'includes/homeImage.jsp';
	new Ajax.Request(url, {method: 'post', parameters: 'homeImage=' + id + '&call=yes', onSuccess: function(request){			
			$('index_main').update(request.responseText);
		}
	});
	Event.stop(e);
	//remove the newsletter form on other featured splash pages
	if ($('nolimit_flash') || $('pdx_flash') || $('splash_video')){
		Effect.Fade('NewsletterForm', { duration: 0.25, delay: 0.0 });
	}
	return;
}



function setColorway(){
	var rank = this.id.split('_')[1];
	showFrontView(rank);
	return false;
}

function changeSelectionLink(container, activeLink, style){
	var links = $(container).getElementsByTagName('a');
	for (var i=0; i<links.length; i++){links[i].className='';}
	$(activeLink).className = style;
}

function changeManualPageLink(container, activeLink, style){
	var links = $(container).getElementsByTagName('a');
	i = 0;
	while(i < links.length){
		
		if(links[i].id.split('_')[1] == 'toc'){ 
			links[i].className='toc_a'; 
		}
		else {
			links[i].className = '';
		}
		i++;	
	}
	
	if($(activeLink).id.split('_')[1] == 'toc'){
		$(activeLink).className = style + " toc_a";
	} else {
		$(activeLink).className = style;
	}
	
}


function showMedia(e){
	changeMediaImage(Event.element(e).src.split('media_images/thumbs/')[1]);
	$('screen_capture').show();
	$('media_display_window').show();
	Event.stop(e);
}

function hideMedia(){
	$('media_display_window').hide();
	$('screen_capture').hide();
}

function prevMedia(){
	var ads = getPrintAds();
	var newIndex = ((getCurrentPosition()-1) == -1 ? ads.length-1 : getCurrentPosition()-1);
	changeMediaImage(ads[newIndex].href.split('media_images/full/')[1]);
}

function nextMedia(){
	var ads = getPrintAds();
	var newIndex = ((getCurrentPosition()+1) >= ads.length ? 0 : getCurrentPosition()+1);
	changeMediaImage(ads[newIndex].href.split('media_images/full/')[1]);
}

function getCurrentPosition(){
	var printAds = getPrintAds();
	var currentAd = $('media_image').getElementsByTagName('img')[0].src;
	var position = 0;
	for (var i=0; i<printAds.length; i++){if (printAds[i].href == currentAd){position = i;}}
	return position;
}

function getPrintAds(){return $('print_ads').getElementsByTagName('a');}

function changeMediaImage(imageRef){$('media_image').update("<img src=images/media_images/full/"+imageRef+" />");}

function getCategoryId(){return $F('product_numbers').split(',')[0];}
function getProductId(){return $F('product_numbers').split(',')[1];}

/*
 * This function only returns the current rank image without
 * the first digit. This allows you to append whatever rank style 
 * you need.
 */
function getFullImageRank() { 
	var img = $$('#featured_product img')[0]; 
	
	if(img) {
		var rank = $(img).src; 
		rank = rank.match(/(?:rank=)[0-9]*/)[0];
		if (rank) {
			return rank.split('=')[1].substring(1);
		} else {
			return '00';
		}
	} else {
		return '00';
	}
	
}


function showMediaPagination(){
	$('previous_media').show();
	$('next_media').show();
}

function hideMediaPagination(){
	$('previous_media').hide();
	$('next_media').hide();
}

function clearSearchField(){if($F('search_field')=='PRODUCT SEARCH'){$('search_field').value='';}}

function getPageSizeInfo(){
	var pageSizeObject = new Object();
	var xScroll, yScroll;
	var xOffset, yOffset;
	if (window.innerHeight){
		posY = window.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){
		posY = document.documentElement.scrollTop;
	} else if (document.body){
		posY = document.body.scrollTop;
	}
	if (self.pageYOffset){ // all except Explorer
		xOffset = self.pageXOffset;
		yOffset = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
		xOffset = document.documentElement.scrollLeft;
		yOffset = document.documentElement.scrollTop;
	} else if (document.body) { // all other Explorers
		xOffset = document.body.scrollLeft;
		yOffset = document.body.scrollTop;
	}          
	if (window.innerHeight && window.scrollMaxY) {  
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}            
	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
	    windowHeight = document.body.clientHeight;
	}              
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
          pageHeight = windowHeight;
    } else { 
          pageHeight = yScroll;
    }   
  	// for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){    
          pageWidth = windowWidth;
    } else {
          pageWidth = xScroll;
    }    
    pageSizeObject.pageWidth = pageWidth;
    pageSizeObject.pageHeight = pageHeight;
    pageSizeObject.windowWidth = windowWidth;
    pageSizeObject.windowHeight = windowHeight;
    pageSizeObject.xScroll = xScroll;
    pageSizeObject.yScroll = yScroll;
    pageSizeObject.xOffset = xOffset;
    pageSizeObject.yOffset = yOffset;
    return pageSizeObject;
}

function setMainMediaImage(e){ 
	Event.stop(e); clearMediaVideo(); 
	$('main_media_image').src = Event.element(e).src.gsub('thumb/Thumb', 'full/Full');
	//$('main_media_image').src = Event.element(e).src.gsub('Thumb', 'Full'); 
	
	if($('pd_large_img')) {
		$('pd_large_img').href = Event.element(e).src.gsub('thumb/Thumb', 'large/Large');
	}
	if($('pd_large')) {
		$('pd_large').href = Event.element(e).src.gsub('thumb/Thumb', 'large/Large');
	}
	if($('pd_medium')) {
		$('pd_medium').href = Event.element(e).src.gsub('thumb/Thumb', 'medium/Medium');
	}
	if($('pd_small')) {
		$('pd_small').href = Event.element(e).src.gsub('thumb/Thumb', 'small/Small');
	}
}

function setMainMediaVideo(e){ 
	Event.stop(e); clearMediaVideo(); 
	var so = new SWFObject("flash/culture_vp.swf", "index", "385", "271", "8");
	var path = Event.element(e).id;
	so.addVariable("videoPath", path);
	so.addVariable('autoPlay','true'); 
	so.addParam('wmode', 'transparent');
	so.write('main_media_container');
}

function clearMediaVideo() {
	if(!$('main_media_image')) {
		$('main_media_container').innerHTML = '<img id="main_media_image" />'; 
	}
}

//this function provides email validation for the newsletter sign-up form
//(on the no limits home page video screen). we should have reused validateEmail
//(on line 183-ish) but that function was providing too much functionality to
//re-purpose here, however much of that code WAS reused/repeated.
function emailSubmission(e){
	Event.stop(e);
	var emailAddr = $F('signup');
	var emailPattern = /[a-z_]*[a-z0-9._%-]?[a-z0-9]+@[a-z0-9][a-z0-9._-]+\.[a-z0-9]+/i;
	
	if (emailAddr =='' || emailAddr.search(emailPattern)==-1){
		//you suck
		$('newsletter_message').addClassName('errorLabel');
		$('newsletter_message').innerHTML = 'Not a valid email.';
	} else {
		new Ajax.Request('newsletter_response.jsp', {
			method:'post',
			parameters: {signup: emailAddr},
			onCreate: function() {
				$('newsletter_loading').style.visibility = 'visible';
			},
			onSuccess: function(transport){
				$('newsletter_loading').style.visibility = 'hidden';
				Effect.Fade('newsletter', { duration: 1.0, delay: 0.0 });
				$('newsletter_message').addClassName('iconRed');
				$('newsletter_message').innerHTML = 'Thank you! We have received your newsletter request.';
			},
			//server/network issue
			onFailure: function(transport){
				$('newsletter_loading').style.visibility = 'hidden';
				$('newsletter_message').addClassName('iconRed');
				$('newsletter_message').innerHTML = 'Error, please try again.';$('signup').value='';
			}
		});
		
	}
	return false;
};

function clearNewsletterSignup(){
	if ($F('signup')=='E-Mail'){
		$('signup').value='';
	}
}