/* Javascript Created Links
--------------------------------------------------
This allows us to have links to other pages, without passing page rank to unimportant pages.
*/
$(function(){
  $("[data-href]").each(function(){
    // var link = $('<a href="'+$(this).attr("data-href")+'">'+$(this).html()+'</a>')
    $(this).html($('<a href="'+$(this).attr("data-href")+'">'+$(this).html()+'</a>'))
    // alert($(this).attr("data-href"));
  });
});

/* Twitter Footer
--------------------------------------------------*/ 
$(document).ready(function(){
  $(".tweet").tweet({
    username: "zencoderinc",
    join_text: "auto",
    avatar_size: 32,
    count: 1,
    auto_join_text_default: "we said,", 
    auto_join_text_ed: "we",
    auto_join_text_ing: "we were",
    auto_join_text_reply: "we replied to",
    auto_join_text_url: "we were checking out",
    loading_text: "loading tweets..."
  });
});

/* Homepage Feature Rotation
--------------------------------------------------*/
var FeatureRotator = {
  delay: 7000,
  fadeTime: ($.browser.msie) ? 50 : 400, // IE does weird things with alpha shadow fading, so speed up fading
  current: 0,
  timer: null,
  featureCount: null,

  init: function(){
    if ($.browser.msie && $.browser.version < 7) return;
    FeatureRotator.featureCount = $(".main_feature").length;
    $(".slide-next").click(function(){
      FeatureRotator.stop();
      FeatureRotator.next(true);
      return false;
    });
    $(".slide-prev").click(function(){
      FeatureRotator.stop();
      FeatureRotator.previous(true);
      return false;
    });
    $("#main_feature_list li").click(function(){
      FeatureRotator.stop();
      FeatureRotator.jump($("#main_feature_list li").index(this));
      return false;
    });
    FeatureRotator.start();
  },

  start: function(){
    FeatureRotator.nextWithTimeout();
  },

  stop: function(){
    clearTimeout(FeatureRotator.timer);
  },

  nextWithTimeout: function(){
    FeatureRotator.timer = setTimeout(function(){
      FeatureRotator.next();
      FeatureRotator.nextWithTimeout();
    }, FeatureRotator.delay);
  },

  next: function(fast){
    var current = FeatureRotator.current;
    if (current >= (FeatureRotator.featureCount - 1)) {
      var next = 0;
    } else {
      var next = current + 1;
    }
    FeatureRotator.swap(current, next, fast);
  },

  previous: function(fast){
    var current = FeatureRotator.current;
    if (current <= 0) {
      var prev = (FeatureRotator.featureCount - 1);
    } else {
      var prev = current - 1;
    }
    FeatureRotator.swap(current, prev, fast);
  },

  jump: function(to){
    FeatureRotator.swap(FeatureRotator.current, to, true);
  },

  swap: function(from, to, fast){
    $($(".main_feature")[from]).fadeOut((fast ? 1 : FeatureRotator.fadeTime), function(){
      $("#main_feature_list li").removeClass("active");
      $($(".main_feature")[to]).fadeIn((fast ? 1 : FeatureRotator.fadeTime), function(){
        $($("#main_feature_list li")[to]).addClass("active");
      });
    });
    FeatureRotator.current = to;
  }
}

$(function(){
  if ($("#home").length > 0 && $(".main_feature").length > 1) {
    FeatureRotator.init();
  }
});


/* FAQ Toggle
--------------------------------------------------*/ 
$(document).ready(function() {
  $(".toggle_container").hide();
  $("a.trigger").click(function() {
    $(this).toggleClass("active").next().slideToggle("slow");
  });
});

/* Facebox
--------------------------------------------------*/
$(function(){
  $('a[rel*=facebox]').facebox()
});

