
function readMore(id){
  if ($("#"+id+"Show").css("display") == "none"){
    $("#"+id+"Show").show();
    $("#"+id).hide();
  } else {
    $("#"+id+"Show").hide();
    $("#"+id).show();
  }
}
var i = 0;
var callDotTimer;
function callingDots(){
  i++;
  callDotTimer = setTimeout("callingDots()",1000);
  if (i < 4) {
    $("#callForm span").append(" .");
  } else {
    i = 0;
    $("#callForm span").text("");
  }
}

function videoPlayed(){}

$(function() {
  $("a#topEmail").click(function(){
    $.get("/leads/?linktype=3&outbound="+escape("mailto:"+$(this).attr('rel')));
    window.open('mailto:'+$(this).attr('rel'));
    return false;
  });
  
  $("form[name=myForm]").submit(function(){
    if (document.myForm.name.value.length < 1) {
      alert("Please fill in your name.");
      document.myForm.name.focus();
      return false;
    }
    if (document.myForm.email.value.length < 5 || !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.myForm.email.value))) {
      alert("Please fill in your email address.");
      document.myForm.email.focus();
      return false;
    }
    if (document.myForm.phone.value.length < 1) {
      alert("Please fill in your phone number.");
      document.myForm.phone.focus();
      return false;
    }
    if (document.myForm.location.value.length < 1) {
      alert("Please fill in your location.");
      document.myForm.location.focus();
      return false;
    }

    $.ajax({url: "/leads/default.asp", type: "POST", data: $(this).serialize(), success: function(){
      $("#pricingForm").show();
      $("form[name=myForm] input").each(function(){
        $(this).val("");
      });
      document.myForm.name.focus();
      pageTracker._trackPageview("/leads/email");
      alert($("#pricingForm").html());
    }, error: function(data,err){
      alert(data);
      alert(err);
      alert("There was an error, please try again.");
    }});
    return false;
  });

  $("form[name=myForm2]").submit(function(){
    if (document.myForm2.name.value.length < 1) {
      alert("Please fill in your name.");
      document.myForm2.name.focus();
      return false;
    }
    if (document.myForm2.phone.value.length < 1) {
      alert("Please fill in your phone number.");
      document.myForm2.phone.focus();
      return false;
    }
    $("#Call input").each(function(){
      $(this).attr("readonly", "readonly");
    });
    $("input.submitCall").attr("disabled", "disabled");
    $("#callForm").show();
    callingDots();
    var dataStr = "";
    if (document.myForm.location.value.length > 0) {
      dataStr = "&location=" + document.myForm.location.value;
    }
    if (document.myForm.email.value.length > 4) {
      dataStr = dataStr + "&email=" + document.myForm.email.value;
    }
    if ($("select[name=model]").length) {
      dataStr = dataStr + "&model=" + $("select[name=model]").val();
    }
    
    $.ajax({url: "/leads/default.asp", type: "POST", data: $(this).serialize() + dataStr + '&call=true',
      success: function(){
        alert("Placing call now.");
        $("#Call input").each(function(){
          $(this).removeAttr("readonly");
        });
        $("input.submitCall").removeAttr("disabled");
        $("#callForm").hide();
        clearTimeout(callDotTimer);
        pageTracker._trackPageview("/leads/phone");
      }, error: function(){
        alert("There was an error, please try again./n/nMake sure to enter your area code.");
        $("#Call input").each(function(){
          $(this).removeAttr("readonly");
        });
        $("input.submitCall").removeAttr("disabled");
        $("#callForm").hide();
        clearTimeout(callDotTimer);
    }});
    return false;
  });
  
  var curBox = $("#imgHolder img");
  $(".popBox img").click(function(){
    var that = this;
    curBox = ($(this).parent().attr("id") != '') ? $("#" + $(this).parent().attr("id") + " img") : $("#" + $(this).parent().parent().attr("id") + " img");
    if (curBox.length == 1) { $("#imgNext, #imgPrev, #imgCount").hide(); } else { $("#imgNext, #imgPrev, #imgCount").show(); }
    $("#boxContentHolder").fadeOut("fast", function() {
      if ($(that).attr("longdesc")) {
        if ($(that).attr("longdesc") == "Embed") {
          $("#boxContentHolder").html($(that).parent().find("span.embed").html()).fadeIn("fast", function(){ $("#light").css("margin-left", "-" + ($("#boxContentHolder").width()/2) + "px"); });
        } else {
          $("#light").css("margin-left", "-405px");
          var src = "/include/VideoPlayer.swf?videoSkin=/include/skin.swf&amp;videoPath=" + $(that).attr("longdesc")
          $("#boxContentHolder").html("<object width='798' height='420'>" +
            "<param name='movie' value='" + src + "'><param name='wmode' value='transparent'>" +
            "<embed src='" + src + "' type='application/x-shockwave-flash' wmode='transparent' width='798' height='420'>" +
            "</embed></object>").fadeIn("fast");
        }
        $("#boxContentHolder").append("<img style='display:none;' id='largeImage' src='/images/fill.gif' alt='" + $(that).attr("class") + "' />");
        $("#imgCount").text((parseInt($(that).attr("class")) + 1) + " of " + curBox.length);
        $.ajax({url: "/analytics/default.asp", type: "POST", data: "video=true"});
      } else {
        $("#light").css("margin-left", "-345px");
        $("#boxContentHolder").html("<img id='largeImage' src='" + that.src + "' alt='" + $(that).attr("class") + "'>").fadeIn("fast");
        $("#imgCount").text((parseInt($(that).attr("class")) + 1) + " of " + curBox.length);
      }
    });
    if ($("#fade").css("display") != "block") {
      $("#fade").fadeTo(0, 0).show().fadeTo("fast", 0.8);
      $("#light").fadeIn("fast");
    }
    return false;
  });
  $("#imgNext, #largeImage").live("click", function(){
    var next = parseInt($("#largeImage").attr("alt")) + 1;
    if (curBox.length == next) { next = 0; }
    curBox.eq(next).click();
    return false;
  });
  $("#imgPrev").click(function(){
    var prev = parseInt($("#largeImage").attr("alt")) - 1;
    if (prev < 0) { prev = curBox.length - 1; }
    curBox.eq(prev).click();
    return false;
  });
  
  $("#light a.hideLarge").click(function(){
    $("#light, #fade").fadeOut("fast");
    $("#boxContentHolder object, #boxContentHolder embed").remove();
    $("#boxContentHolder").html();
    return false;
  });
  $("#imgHolder").css("width", (($("#imgHolder img").length * 343) + 3) + 'px');
  $("#videoHolder").css("width", (($("#videoHolder div").length * 343) + 3) + 'px');
  if ($("#videoHolder div").length < 3) {
    $("#Video").css("height", '250px');
  }
  
  $("a.showHideIncentive").click(function(){
    var loc = $(this).attr("href");
    var that = $(this);
    loc.replace("#", "");
    if ($(loc+":hidden").length == 1){
      $(loc).show();
      $(this).text("Hide terms");
    } else {
      $(loc).hide();
      $(this).text("Click here for the terms");
    }
    return false;
  });
  
  $("#printLink").click(function(){
    var printUrl = String(document.location);
    if (printUrl.slice(-1) == "/") {
      printUrl = printUrl.substring(0,printUrl.length-1);
    }
    $("#printFrame").attr("src", printUrl + "/Print");
    window.printFrame.focus();
    return false;
  });
});