/*
JS file for Market Leader
*/




/* FUNCTIONS INCLUDED:
 * -
 * -
 * ---------------------------------------------------------------------- */

$(document).ready(function() {
  $(function(){
    $("#slides").slides({
      effect: 'fade',
  	  play: 8000,
  	  pauseBtn: true,
  	  crossfade: true,
  	  fadeSpeed: 750,
  	  generateNextPrev: true,
  	  paginationClass: 'nav-pagination'
    });
  });
  
  jQuery('#carousel').jcarousel({
    scroll: 2
  });
  
  $(".sidebar-link-copy").each(function() {
    $(this).append("<img src='/images/site/bg_link-arrow.png'>");
  });
  
  $(".sidebar-copy h4").each(function() {
    if ($(this).html() == "Have Market Leader contact you") {
      $(this).closest("li").addClass("contact-action");
    }
    if ($(this).html() == "Be a Market Leader.") {
      $(this).closest("li").addClass("call-to-action");
    }
  });
  
  $(".nav-breadcrumb li").each(function(index) {
    if ($(this).html() == " ") {
      $(this).remove();
    }
    
    if (index == 1) {
      $(this).css({'text-transform': 'capitalize'});
    }
  });
  
  $(".sitemap .article-page li a").each(function() {
    if ($(this).attr("href") == "") {
      $(this).attr("href") == "#";
    }
  });
  
  if ($(".partner").length > 0) {
    var html = $("h2").html();
    $("h2").html(html.replace("®", "<super>&reg;</super>"));
  }
  
  /* videos on other pages */
  $("a[rel^='prettyPhoto']").each(function() {
	    $(this).attr("href", normalizeVideoHref($(this).attr("href")));
  });
  /* end of: videos on other pages*/
  
  $(".item-tooltip a").each(function() {
    $(this).attr("rel", "prettyPhoto");
    $(this).attr("title", $(this).html());
    $(this).attr("href", normalizeVideoHref($(this).attr("href")));
  });

  $("a[rel^='prettyPhoto']").prettyPhoto();
    
  /* Contact Form*/
  var dmsUrl = 'http://info.marketleader.com/form/';
  var dmsFormHeight = "230px";

  if( $("#iframe-contact-form").attr("src") != '' && parseInt($("#iframe-contact-form").attr("src")) > 0 ){  
	  $("#iframe-contact-form").attr("src" , dmsUrl + $("#iframe-contact-form").attr("src"));
  }
  
  //immediately show single page-level contact form
  if( $("#iframe-contact-form.contact-page").length ){
      $("#iframe-contact-form").attr("height" , dmsFormHeight);	  
      $("#iframe-contact-form").show();
  }
  
  $("li.contact-action .sidebar-link").click(function(e){
	  e.preventDefault();
  	  $(this).parent("li.contact-action").css({'height' : '100%'});
	  $(this).after($(".sidebar-contact-form").html());
	  $(".sidebar-contact-form").remove();
      if( $("li.contact-action .contact-form-options").length ){
    	  $("#iframe-contact-form").hide();    	  
          $("li.contact-action .contact-form-options").show();
      }else{
          $("#iframe-contact-form").attr("height" , dmsFormHeight);
          $("#iframe-contact-form").show();
      }
	  return false;
  });
  
  $(".contact-form-options input[name=form-type]").live("click", function(){
	  if($(this).hasClass("option-agent")){
		  $("#iframe-contact-form").attr("src" , dmsUrl + $(this).val());
	  } else {
		  $("#iframe-contact-form").attr("src" , dmsUrl + $(this).val());
	  }
      $("#iframe-contact-form").attr("height" , dmsFormHeight);
      $("#iframe-contact-form").show();
      $(this).parent(".contact-form-options").hide();
  });
  /* end of: Contact Form*/
});

function normalizeVideoHref(href) {
    href = href + "&iframe=true";
    var videoHref = href.replace(/width=[0-9]{0,5}&height=[0-9]{0,5}&/gi, 'width=640&height=480&');    	
	return videoHref;
}

function clearField(element, val) {
	if (element.value == val) {
		element.value = "";
	}
}

function setField(element, val) {
	if ((element.value == null) || (element.value == "")) {
		element.value = val;
	}
}
