// Cufon.replace('.article h2 a, .error_page header h2, .interior .article h2, .interior-about .article h2, .interior-home .article h2, .interior-community .article h2, .interior-coaching .article h2, .interior-system .article h2, .interior-teleclasses .article h2, .interior-contact .article h2, .headliner h1, .headliner h2, .footer h3, .article .header h2');

function nextMarqueeQuote() {
  var $current = $(".marquee_quotes>:visible").fadeOut();
  if($current[0] == $(".marquee_quotes>:last")[0]) {
    $current = $(".marquee_quotes>:first");
  }
  else {
    $current = $current.next();
  }
  $current.fadeIn();
  setTimeout(nextMarqueeQuote, 10000);
}

$(document).ready(function() {
  
	// home marquee animation
  /*
	$('.marquee_quotes').cycle({
		fx:'fade',
		timeout: 10000
	});
  */
  if($(".marquee_quotes").size() > 0) {
    $(".marquee_quotes").children().hide().filter(":first").show();
    setTimeout(nextMarqueeQuote, 10000);
  }

	$('.telecasts .signup').hide();
  $('.telecasts .header .join .button').click(function () {
    //$('.telecasts .signup').slideDown('slow');
    //return false;
  });
  
  $(".sharethis").each(function() {
    var object = SHARETHIS.addEntry({
        title:$(this).attr("title"),
        url:$(this).attr("rel"),
        summary: 'Brought to you by BodyAwe.'
      },
      {button:false}
    );
    object.attachButton(this);
  });
  
  $("#book-discount-form").submit(function() {
    error = "";
    var contactName = $("#book-discount-form input[name=contactName]").val();
    var email = $("#book-discount-form input[name=email]").val();
    var phone = $("#book-discount-form input[name=phone]").val();
    var valid = true;
    if (contactName == '') {
      valid = false;
      error += "Please provide your name.\r\n";
    }
    if (email == '') {
      valid = false;
      error += "Please provide your email address.\r\n";
    } else if(!email.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) {
      valid = false;
      error += "Your email address is invalid.\r\n";
    }
    if (phone == '') {
      valid = false;
      error += "Please provide your phone number.\r\n";
    }
    if(valid) {
      if (!$.cookie("bodyawe-book-discount")) {
        $.cookie("bodyawe-book-discount", $.toJSON({ "submitted": "true", "name": contactName, "email": email, "phone": phone, "forward_to_dogear": "true"}), { path: '/' });
        $.cookie("bodyawe-book-discount-send-email", "true", { path: '/' });
      }
    } else {
      alert(error);
      return false;
    }
  });

  if($("#dogear_form").size() > 0) {
    var cookieHash = $.parseJSON($.cookie("bodyawe-book-discount"));
    if(cookieHash.forward_to_dogear == 'true') {
      cookieHash.forward_to_dogear = 'false';
      $.cookie("bodyawe-book-discount", $.toJSON(cookieHash), { path: '/' });
      $("#dogear_form").submit();
    }
  }

});

function reportAjaxResult(data) {
  if(data.errors.length > 0) {
    var str = '';
    for(var i in data.errors) {
      str += data.errors[i] + "\n";
    }
    alert(str);
  }
  if(data.messages.length > 0) {
    var str = '';
    for(var i in data.messages) {
      str += data.messages[i] + "\n";
    }
    alert(str);
  }
}

function signup(eventId) {
  var email = jQuery(".attendee-email.event-" + eventId).val();
  var name = jQuery(".attendee-name.event-" + eventId).val();
  jQuery.getJSON("/wp-event-signup.php", {
    'op': 'signup',
    'event-id': eventId,
    'name': name,
    'email': email,
    't': new Date().getTime()
  }, function(data) {
    reportAjaxResult(data);
    $('.telecasts .signup').slideUp('slow');
    return false;
  });
  return false;
}

$(document).ready(function() {
  if(window.location.hostname != 'dev.bodyawe.com' && window.location.pathname == '/signup/' && window.location.protocol == 'http:') {
    window.location.replace('https://' + window.location.host + window.location.pathname + window.location.search + window.location.hash);
  }
});
