var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open() {  
   jsddm_canceltimer();
   jsddm_close();
   
   ddmenuitem = $(this).find('ul');
   if (ddmenuitem) {
      var linkName = $(this).find("a").attr("id");
      $("#" + linkName).addClass('menu-button-current-hover');
       ddmenuitem.css('visibility', 'visible');
   }
}

function jsddm_close() {  
  if(ddmenuitem) {
    $("#menu-buttons > li a").removeClass('menu-button-current-hover');
    ddmenuitem.css('visibility', 'hidden');
  }
}

function jsddm_timer() {  
  closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer() {  
  if(closetimer)  {  
      window.clearTimeout(closetimer);
      closetimer = null;
  }
}

function initMenuHighlight() {
  try {
      var ix = location.pathname.lastIndexOf("/");
      if (ix == 0) {
        ix   = location.pathname.lastIndexOf("\\");
      }
      var page = location.pathname.substring(ix + 1);
      var pageToMenuButton = {};
      
      $('#menu-buttons > li > a').each(function(){
          pageToMenuButton[$(this).attr("href")] = $(this).attr("id"); 
      });
      
      $("#" + pageToMenuButton[page]).addClass("menu-button-current-page");
      
      if (page.indexOf("capabilities-") != -1) {
          $("#capabilities-link").addClass("menu-button-current-page");
      }
      
      if (page.indexOf("application.html") != -1) {
        $("#careers-link").addClass("menu-button-current-page"); 
      }
      
  } catch (e) {}
}

function fontFix() {
  // alert(navigator.userAgent);
  if (navigator.userAgent.indexOf("Windows NT 5.1") != -1) { 
    $("body").addClass("winXPFonts"); 
  }
}

var quotes =
[
   {text : "&quot;Kenzan Media is a pioneer in the production of cross-platform iTV applications.  Their knowledge and experience has made it easy for my clients and providers to design and distribute across multiple platforms.&quot;",
   attribution : "Larry Samuels, General Manager Advanced TV, Dish Networks"},
   {text : "<br/>&quot;I have been working with the Kenzan Media team for several years. Both their client management and their ITV design are exemplary.&quot;",
   attribution : "Michael Bologna, Director, Emerging Communications, GroupM"},
   {text : "&quot;Kenzan Media helped us bring two of our most important brands - HGTV and Food Network -- into the ITV space with both innovative design and cogent development, and they gave us the tools we need to manage the applications ourselves going forward.&quot;",
   attribution : "Channing Dawson, SVP of Emerging Media, Scripps Networks Interactive"},
   {text : "&quot;Kenzan Media has provided a wide range of technical development for our networks over the past ten years. The quality of development is excellent, and their project management is detail oriented and consistent.&quot;",
     attribution : "Michael Cagnazzi, VP of Product Development, AMC Networks"}
];

var quoteIx = quoteIx = Math.floor(Math.random()*quotes.length);

function initQuote() {
  $(".quote-text").each(function() {
      quoteIx = (quoteIx + 1) % quotes.length;
      var quote = quotes[ quoteIx ];
      $(this).html(quote.text
          + '<span class="quote-attribution">&nbsp; ' + quote.attribution + '</span>');
  });
}

function bgScaleAndCenter() {
  var bgSize = 1448;
  var winWidth = $(window).width();
  var leftOffset = winWidth >= bgSize? 0 : 
      ((bgSize - winWidth) / 2);
  $("img.bg").css("left", -1 * leftOffset);
}

document.onclick = jsddm_close;

/** anti-spam measure **/
function initEmAddr() {
  var domain = "kenzanmedia" + ".com";
  var aCharCode = '@'.charCodeAt();
  $("a.emaddr").each(function() {
    if (!$(this).hasClass("emtext")) {
      $(this).html( $(this).attr("link") + String.fromCharCode( aCharCode ) + domain );
    }
    $(this).attr("href", "mail" + "to" + ":" + $(this).attr("link") + String.fromCharCode( aCharCode ) + domain );
  }); 
}

var AppForm = {
  displayErrors : function(errorsList){
      var html = [];
      for (var i = 0; i < errorsList.length; i++) {
        html.push("<li>" + errorsList[i] + "</li>");
      }
      $("#error-list > ul").html( html.join("\n") );
      $("#error-list").show();
      $("html, body").animate({ scrollTop: 300 }, "slow");
  },
  requiredFields : [
    {id:"fullname", msg:"Please enter your <span class='name'>Full Name</span>."},
    {id:"street", msg:"Please enter your <span class='name'>Street</span>."},
    {id:"city", msg:"Please enter your <span class='name'>City/State</span>."},
    {id:"zip", msg:"Please enter your <span class='name'>Zip</span>."},
    {id:["homephone", "mobilephone"], msg:"Please enter your <span class='name'>Phone Number</span>."},
    {id:"email", msg:"Please enter your <span class='name'>Email</span>."},
    {id:"positionsought", msg:"Please enter your <span class='name'>Position Sought</span>."},
    {id:"availabledate", msg:"Please enter your <span class='name'>Available Start Date</span>."},
    {id:"payrange", msg:"Please enter your <span class='name'>Desired Pay Range</span>."}
  ],
    
  validate : function() {
    var errors = [];

    for (var i = 0; i < AppForm.requiredFields.length; i++) {
      var field = AppForm.requiredFields[i];
      
      var ids = typeof(field.id) == "string"? [field.id] : field.id;
      var empty = true;
      
      for (var j = 0; j < ids.length; j++) {
        var id = ids[j];
        if (!AppForm.isBlank($("#" + id).val())) {
          empty = false;
        }
      }
      
      
      if (empty) {
        errors.push(field.msg);
      }
    }

    return errors;
  },

  isBlank : function(str) {
    return (str == null || str.replace(/\s/g,"") === "");
  }
}

var urlParams = {};

$(document).ready(function() {
     (function () {
        var e,
            a = /\+/g,  // Regex for replacing addition symbol with a space
            r = /([^&=]+)=?([^&]*)/g,
            d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
            q = window.location.search.substring(1);

        while (e = r.exec(q))
           urlParams[d(e[1])] = d(e[2]);
     })();

    initQuote();
    // setInterval("initQuote()", 8000);
    fontFix();
    initMenuHighlight();
    initEmAddr();
    $('#menu-buttons > li').bind('mouseover', jsddm_open)
    $('#menu-buttons > li').bind('mouseout',  jsddm_timer)
    $("#year").html( new Date().getFullYear());
    
    
    $(window).resize(function() {
      bgScaleAndCenter();      
    });
    
    bgScaleAndCenter();
});



