/*


Dynamic Application of Functions to DOM
*/
var _debugMode = false;
document.observe('dom:loaded', init);
Event.observe(window, 'load', initAfterImages);

function init () {
  if (Prototype.Browser.IE && !window.XMLHttpRequest) ie6Fixes();
  
  $$('#navFoot li:nth-child(even)').invoke('addClassName', 'even');
  
  showRandomPicture();
  
  // $('picture-frame').observe('click', function(e) {
  //   window.location = $$('#list-of-images li').find(function(el){
  //     return el.visible();
  //   }).down().readAttribute('href');
  // });
  
  // $$("#navPrimary li").each(function(node, i){
  //    var node = $(node);
  //    if(node.select("ul").length > 0){
  //      var ul = node.select("ul").first();
  //      var toggleStuff = function(){
  //         node.toggleClassName("active");
  //        ul.toggle();
  //      };
  //      node.observe('mouseover', toggleStuff).observe('mouseout', toggleStuff);             
  //    }
  //  });
  // 
  // $('test').observe('mouseover', function(e) {
  //   e.element().next('ul').show();
  // })
  
  setupDropDownIE6();
  
  setupEmailValidator();
  
  clearThumbnailRows();
}

function clearThumbnailRows () {
  $$('.thumb:nth-child(4n)').invoke('insert', { 'after': '<div class="clear"></div> '});
}

function initAfterImages () {
  
}

function ie6Fixes() {
  [$('banner-text', 'cocoDesign')].flatten().compact().invoke('pngHack');
}

function showRandomPicture() {
  if (!$('list-of-images')) return;
  $$('#list-of-images li')[(3).rand() - 1].show();
}

function setupDropDownIE6() {
  //IE6 is the only browser that can't handle drop downs via css but it does understand mousenter
  //and mouseleave
  if (!Prototype.Browser.IE && window.XMLHttpRequest) return;
  $$('ul#navPrimary li ul').invoke('hide').each(function(el, i){
    var target;
    el.up('li').observe('mouseenter', function(e) {
      target = e.element().down('ul').show();
    }).observe('mouseleave', function(e) {
      target.hide();
    });
  });
}

function setupEmailValidator() {
  var submit = $('newsletterSubmit');
  submit.disable();
  $('emailInput').observe('keyup', function(e){
    if (validateEmail(e.element().value)) submit.enable();
  })
  
  
  function validateEmail(elementValue){      
     var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
     return emailPattern.test(elementValue); 
   }
}

