/*
	functions.dom.js
	DOM Interactivity Functions
	Created: 1.14.08
	Creator: Matt Kircher, Mainline Media LLC
	Modified: 4.12.08
*/

function setupPage(){
	
	translateEmails();
	
	//proximity styles
	$('#amble').find(':header + p').each(function(){
		$(this).css({paddingTop:'0px'})
		.prev().css({paddingBottom:'0px'});
	});
	$('#amble').find('p + :header, div + :header, ul + :header, ol + :header, blockquote + :header').each(function(){
		$(this).css({marginTop:'15px'});
	});
	
	checkAutoUpdateQuotes();
}

function translateEmails(){
	$('span[class="email"]').each(function(){
		var spt = $(this);
		var at = / at /;
		var dot = / dot /g;		
		
		//EXAMPLE: <span class="email" title="link title | email address | email subject"> link content </span>		
		
		var inner_content = $(this).html();					//inner HTML of span tag
		var t = $(this).attr('title');						//email, link options from title attribute
		
		var title = t.substring(0, t.indexOf('|'));				//title for the link
		t = t.substring(t.indexOf('|')+1);
				
		var addr = t.substring(0, t.indexOf('|'));				//email address from id attribute
		addr = addr.replace(at,"@");						//replace words with @
		addr = addr.replace(dot,".");						//replace words with
				
		var subject = t.substring(t.indexOf('|')+1);				//subject for email, if needed
				
		var fulladdr = ($.trim(subject) != "")?addr+'?subject='+subject:addr;	//full address formed with subject, if needed

		inner_content = ($.trim(inner_content) == "" || $.trim(inner_content) == "&nbsp;")?addr:inner_content;
		
		$(this).html('<a href="mailto:'+fulladdr+'" title="'+title+'">'+ inner_content +'</a>');
	});
}

function setupContactForm(){
	if($('#contactForm')){
		
		//format reCaptcha form
		//$('#recaptcha_widget_div table tr').find('td:eq(2)').remove();
		
		//disable company type other, bind onchange
		$('#contact_company_type_other').attr('disabled',true);
		$('#contact_company_type').bind('change', function(){
			if($(this).val() == "Other"){
				$('#contact_company_type_other')
				.removeAttr('disabled')
				.addClass('required').focus();
			}
			else { $('#contact_company_type_other').val('').attr('disabled',true).removeClass('required'); }
		});
		
		//remove required attention classes from required fields
		$('#contactForm .required').bind('change', function(){
			if($.trim($(this).val()) != ""){
				$(this).removeClass('required_attention');
				if($(this).get(0).nodeName == "SELECT"){
					if($(this).next().attr('class') == "select_required_attention"){
						$(this).next().remove();
					}
				}
			}
		});
		
		//bind blur / focus actions
		if(!$.browser.msie){
			$('#contactForm input[type="text"]')
			.bind('focus', function(){ $(this).addClass('highlighted_form_field'); })
			.bind('blur', function(){  $(this).removeClass('highlighted_form_field'); });
		}
		
		//bind product checkbox select/deselect button
		$('#products_all')
		.bind('click', function(){
			if($(this).attr('value') == "Select All"){
				$('#contactForm input[name="product[]"]').attr('checked', true);
				$(this).attr('value','Deselect All');
			} else {
				$('#contactForm input[name="product[]"]').removeAttr('checked');
				$(this).attr('value','Select All');
			}
		});
		
		//set focus to first form field
		$('#contactForm input[tabindex="1"]').focus();
	}
}
function validateContactForm(){
	
	//setup valid object
	var valid = { status:true, response:'', element:null };
	
	//remove classes, go through and check for non-values
	$('#contactForm .required').removeClass('required_attention');
	$('#contactForm .select_required_attention').remove();
	$('#contactForm .required').each(function(){
		if(($(this).val() == "" || $(this).val() == null)){
			$(this).addClass('required_attention');
			valid.status = false;
			valid.response = 'One or more required fields has not been completed. Please complete them and resubmit the form.';
			if(valid.element == null){ valid.element = $(this); }
			
			if($(this).get(0).nodeName == "SELECT"){
				$(this).after('<span class="select_required_attention">&lsaquo;&mdash;</span>');
			}
		}
	});
	
	//if email is not of the form 'name@email.com', don't validate
	if(valid.status){
		$('#contactForm .required').each(function(){
			if($(this).attr('name') == "email"){
				var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if(!filter.test($.trim($(this).val()))){
					$(this).addClass('required_attention');
					valid.status = false;
					valid.response = 'Please supply a valid email address.';	
					valid.element = $(this);
				}
			}
		});
	}
	
	//display alert, focus on first non-valued field
	if(!valid.status){ alert(valid.response); $(valid.element).focus(); }
	return valid.status;
}

function setupRealDealDiagram(){
	if($('#realdeal-diagram')){
		$('#rd-diagram-links a').each(function(){
			$(this)
			.bind('mouseover', function(){
				$('#rd-diagram-content').html('<b>'+$(this).attr('title')+'</b><br />'+$(this).next().text());
			})
			.bind('click', function(){ return false; });
		});
		$('#rd-diagram-content').html('<br /><br /><br /><b><i>Rollover each stage for more information.</i></b>');
	}
}
function applyIE6FlickerFix(){
	try {
	  document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
}

function checkAutoUpdateQuotes(){
	if($('#expert-quotes').length){

		$.ajax({
			type: "GET",
			url: "includes/public_datajax.php?process=getQuoteOptions",
			dataType: "json",
			success: function(data){
				
				if(data['update'] == 1){
					var ms = data['period'] * 1000;
					setupAutoUpdateQuotes(ms);
				}
			}
		
		});
	}
}

function setupAutoUpdateQuotes(ms){
	if($('#expert-quotes').length){
		var i = setInterval(function(){
			
			var ts = new Date().getTime();
			
			$.ajax({
				type: "GET",
				url: "includes/module.listen_experts.inc.php?ts="+ts,
				dataType: "html",
				success: function(data){
					
					newData = $(data).find('p');
					$(newData).css({ opacity:0 });
					
					$('#expert-quotes p').fadeOut(500, function(){
						$(this).replaceWith(newData);
						$('#expert-quotes p').animate({ opacity:1 }, 500, function(){
							if(!$.support.opacity){
								//IE filter, makes anti-aliasing happen
								$(this).get(0).style.removeAttribute('filter');
							}
						});
					});
				}
			
			});
		}, ms);
	}
}

$(document).ready(function(){
	setupPage();
	setupContactForm(); 		//contact form setup
	setupRealDealDiagram(); 	//RealDeal HTML diagram
	applyIE6FlickerFix();
});