// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// 
jQuery.noConflict();

function caseStudyPage(which) {
  new Ajax.Updater('case_study_page', '/case-study-page/' + which, {method:'get'});
}

function clientList(category, page_no) {
  // new Ajax.Updater('client-list', '/clients/' + category, { parameters: { page: page_no }, method:'get' });

	jQuery.getScript('/clients/' + category + '?page=' + page_no );	

}

jQuery(document).ajaxSend(function(event, request, settings) {
  if (typeof(AUTH_TOKEN) == "undefined") return;
  // settings.data is a serialized string like "foo=bar&baz=boink" (or null)
  settings.data = settings.data || "";
  settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});

function redisplayTestimonial(r) {
	html_text = r.responseText,
	jQuery('#teaser').fadeOut(500, function() {
		jQuery('#teaser').html(html_text);
	});
	jQuery('#teaser').fadeIn(500);
}

function load() {
	display_fields();
	jQuery('#or_text').hide();

	jQuery('#brochure_post_brochure').change(function() {   
		display_fields();
	});
	
	jQuery('#brochure_marketing_source').change(function() {   
		display_lead();
	});
}

function display_fields() {
	jQuery("#brochure_post_brochure option:selected").each(function () {
		// if( jQuery(this).text() == '' ) {
		// 	jQuery('#email_field').hide()
		// 	jQuery('#address_fields').hide()
		// 	jQuery('#form_button').hide()
		// }
		// else
		if( jQuery(this).text() == 'Post' ) {
			jQuery('#email_field').hide()
			jQuery('#address_fields').slideDown()
			jQuery('#form_button').show()
		}
		else if( jQuery(this).text() == 'Email' ) {
			jQuery('#email_field').show()
			jQuery('#address_fields').hide()
			jQuery('#form_button').show()
		}
	});
}

function display_lead() {
	jQuery("#brochure_marketing_source option:selected").each(function () {

		jQuery.ajax({
			type: "GET",
			url: "/brochure/lead/"+jQuery(this).text()+".json",
			data: "action=lead",
			success: function(msg){
				jQuery('#lead_field #container').remove();
				jQuery('#lead_field').prepend("<div id='container'>" + msg + "</div>");
			}				
		});

	});
}

jQuery(document).ready(function() {
	load();
});

//<![CDATA[
	new PeriodicalExecuter(function()
	{
		new Ajax.Request('/testimonials/teaser', {asynchronous:true, evalScripts:true, method:'get', onSuccess:function(request){redisplayTestimonial(request)},
		parameters:'authenticity_token=' + encodeURIComponent(AUTH_TOKEN)})
		}, 10
	)
//]]>
