/*  File: basic.js          */
/*  By:   Jeremy Tredway    */
/*  Ver:  2008-09-08        */

function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }


// onload routines
$(function(){

  $(".module").wrapInner("<div class='module_top'><div class='module_bot'><div class='module_bdr'></div></div></div>");

  // Clip player instantiation
  function instantiateClip(el, options) {
    options.caption = false;
    options.height = 222;
    options.width = 334;
    options.src = $(el).attr("rel");
    options.autoplay = false;
    options.bgColor = '';
    options.flashVersion = "8";
  }

  // initialize blog feed
  $("#blog_feed .feed_item_content, .module_item .content").css({display:"none"});
  $("#blog_feed .feed_item:last, .module_item:last").addClass("last_item");
  $("#blog_feed .feed_item_content:first, .module_item:first .content").css({display:"block"});
  // Instantiate the first clip, if present:
  $('.module_item:first .video_clip').each(function() {
    $(this).append('<div></div>').find('div:first').attr('rel', $(this).attr('rel')).media(instantiateClip);
  });
  
  $("#blog_feed .feed_item_title:first, .module_item .title:first").find(".toggle").each(function(){
    var toggle_text = ($(this).text() == "+") ? ("–") : ("+");
    $(this).text(toggle_text);
  });
  $("#blog_feed .feed_item_title, .module_item .title").click(function(){
    $("#blog_feed .feed_item_content, .module_item .content").css({display:"none"});
    $("#blog_feed .toggle, .module_item .toggle").text("+");
    // Destroy all Flash players:
    $('.module_item .video_clip').text('');
    $(this).find(".toggle").each(function(){
      var toggle_text = ($(this).text() == "+") ? ("–") : ("+");
      $(this).text(toggle_text);
    });
    // Show the newly selected item, and instantiate the Flash player if applicable:
    $(this).parent().find(".feed_item_content, .content").toggle().find('.video_clip').each(function() {
      $(this).append('<div></div>').find('div:first').attr('rel', $(this).attr('rel')).media(instantiateClip);
    });
  });

  // dynamically replace flash elements
  $(".flash_static").css({"display":"none"});
  $('#flash_container').media(function(el, options) { 
    options.caption = false; 
    options.height = 351; 
    options.width = 457; 
    options.src = $(el).attr("rel"); 
    options.autoplay = true; 
    options.bgColor = "#ffffff"; 
    options.flashVersion = "8"; 
  });
  $('#flash_pictures').media(function(el, options) { 
    options.caption = false; 
    options.height = 351; 
    options.width = 536; 
    options.src = $(el).attr("rel"); 
    options.autoplay = true; 
    options.bgColor = "#ffffff"; 
    options.flashVersion = "8"; 
  });

  // set store locator form parameters
  var DEF_VAL = "Zip Code";
  var store_locator_key = document.getElementById("store_locator_key");
  if (store_locator_key.value == "") {
    store_locator_key.value = DEF_VAL;
  }
  $("#store_locator_key").focus(function() {
    $("#store_locator_key").attr("value","");
  });
  $("#store_locator_key").blur(function() {
    if (store_locator_key.value == "") {
      $("#store_locator_key").attr("value",DEF_VAL);
    }
  });

  // set external links
  $("a[@href^=http]").each(function () {
    if(this.href.indexOf(location.hostname) == -1) {
      $(this).attr("target", "_blank");
    }
  });
  
  // Preload images in CSS file
  $.preloadCssImages();

});

