// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
    
//KAKA
function show_menu(menu)
{
  hideAllExcept(menu,".sub_menu")
}

function show_tab(tab)
{
  var tab_menu_items = $$('.tab');
  for(var i=0;i<tab_menu_items.length;i++)
  {
    if(tab_menu_items[i].id == tab+"_tab")
    {
      tab_menu_items[i].addClassName("active");
      tab_menu_items[i].removeClassName("inactive");
    }
    else
    {
      tab_menu_items[i].addClassName("inactive");
      tab_menu_items[i].removeClassName("active");
    }
  }
  hideAllExcept(tab,".panel")
}

function hideAllExcept(item,style)
{
    var doms = $$(style);
    for(var i=0;i<doms.length;i++)
    {
      if(doms[i].id == item)
      {
        doms[i].show();
      }
      else
      {
        doms[i].hide();
      }
    }
}

function close_menu(item)
{
    $(item).style.display = "none";
}

function create_tip(id,title,content)
{
      tip = new Tip(id,
        content,
        {
            ajax:false,
            className:'round transparent',
            closeButton:false,
            duration:0.3,
            delay:0.2,
            effect:"appear",
            fixed:false,
            hideAfter:false,
            hideOn:'mouseout',
            hook:false,
            showOn:'mousemove',
            viewport:true,
            title: title
        });
        return tip;
}

function show_tab_menu(name)
{

    var sub_menu_items = $$(".tab_menu");
    for(var i=0;i<sub_menu_items.length;i++)
    {
      if(sub_menu_items[i].id == name)
      {
        sub_menu_items[i].show();

      }
      else
      {
        sub_menu_items[i].hide();
      }
    }

}

function slide(id)
{
   
    Effect.toggle(id, "slide",{
        delay:-0.5
    });
    
    if ($(id).style.display == "none")
    {
        $("navigate_icon").src = "/images/layout/navigate_close.jpg";
    }
    else
    {
        $("navigate_icon").src = "/images/layout/navigate_open.jpg";
    }

    return false;

}

function show_newsletter(blnShow)
{
    if(blnShow)
    {
      $('')  
      $('newsletter_subscribe').className="newsletter_show";
    }
    else
    {
        $('newsletter_subscribe').className="newsletter_hide";
    }
    return false;
}

function show_email(id)
{
 
     Effect.toggle(id, "slide",{
        delay:-0.5
    });
}
var ImageRotator = Class.create();
ImageRotator.prototype = {
  initialize: function(images)
  {
    this.delay        = 2000;
    this.active_index = 0 
    this.image_ids    = images;
    this.images_total = this.image_ids.length;
    this.images       = $A();
    for(i=0;i<this.images_total;i++)
    {
        this.images.push($(this.image_ids[i]));
    }
    this.start();
  },
  start:function()
  {
    this.images[this.active_index].show();
    this.cycle();
  },
  cycle:function()
  {
    setTimeout(this.show_next.bind(this),5000);
  },
  show_next:function()
  {
    this.images[this.active_index].fade();
    this.set_active_index();
    this.images[this.active_index].appear();
    this.cycle();
  },
  set_active_index:function()
  {
    this.active_index = (this.active_index<(this.images_total-1))? this.active_index+1 : 0
    //alert(this.active_index);  
  }
}

var ImageLoader =  Class.create();
ImageLoader.prototype = {
  initialize: function()
  {
    this.images_ids = $A();
    Event.observe(window,"load",this.load_images.bind(this),true);
  },
  add:function(id,src)
  {
    this.images_ids.push({'id':id,'src':src})
  },
  load_images:function()
  {
    var total = this.images_ids.length;
    for(var i = 0;i<total;i++)
    {
      id  = this.images_ids[i]['id']
      src = this.images_ids[i]['src']        
      //$(id).src = src;
    }
    
  }
}
var image_after_loader =  new ImageLoader(); 

String.prototype.startsWith = function(s) { if( this.indexOf(s) == 0 ) return true; return false; }


  function getMembers()
  {

   var array = $A();

   var num_members = $('members_view').getElementsByTagName('input');
   for(var i=0;i<num_members.length;i++)
    {
        if(num_members[i].checked  && num_members[i].type =="checkbox")
        {
          array[i] = num_members[i].id;

        }
    }

  return array;

  }

  var ApplicationLoader = {
  LOADING_DISPLAY_THRESHOLD: 200, // ms
  DISPLAY_OVERLAY_ID: 'app_loader_overlay',
  DISPLAY_LOADER_ID: 'app_loader',

  Initialize: function ()
  {
    // Create teh overlay
    this.Overlay = $(document.createElement('DIV'));
    this.Overlay.id = this.DISPLAY_OVERLAY_ID;
    this.Overlay.className = 'noprint';
    this.Overlay.style.display = 'none';
    this.Overlay.style.position = 'absolute';
    this.Overlay.style.top = '0px';
    this.Overlay.style.left = '0px';

    document.body.appendChild(this.Overlay);

    this.Loader = $(document.createElement('DIV'));
    this.Loader.id = this.DISPLAY_LOADER_ID;
    this.Loader.className = 'noprint';
    this.Loader.style.display = 'none';
    this.Loader.style.position = 'absolute';
    document.body.appendChild(this.Loader);

    this.LoaderTemplate = new Template ("<div id='loader_img'></div>\
                                         <span>#{loading_text}</span>");
  },
  Show: function (szMessage, bDiscreteMode)
  {
    this.szNextLoadingMessage = szMessage || 'Processing ...';
    this.bDiscreteMode = bDiscreteMode || false
    document.body.style.cursor = 'wait';

    if (!this.Overlay) this.Initialize();
    this.hLoad = setTimeout(this._RealDisplay.bind(this), this.LOADING_DISPLAY_THRESHOLD);

  },
  Hide: function ()
  {
    if (this.hLoad)
    {
      clearTimeout(this.hLoad);
      this.hLoad = null;
    }
    else
    {
      // Everything is already showing
      this.Overlay.style.display ='none';
      this.Loader.style.display = 'none';
    }
    document.body.style.cursor = 'auto';
  },

  _RealDisplay: function ()
  {
    var iDocumentHeight = $(document.body).getHeight(), iScreenHeight = document.viewport.getHeight(),
        iScreenWidth = document.viewport.getWidth(), aScroll = document.viewport.getScrollOffsets();

    this.hLoad = false; // Not loadin anymore

    this.Overlay.style.height = ((iDocumentHeight > iScreenHeight)? iDocumentHeight: iScreenHeight)+'px';

    // Displayed but not visible - otherwise weird jumping takes place
    this.Loader.style.display = 'block';
    this.Loader.style.visibility = 'hidden';

    if (!this.bDiscreteMode)
    {
      this.Overlay.style.display = 'block';
      this.Loader.style.top = (iScreenHeight>>1)-(this.Loader.getHeight()>>1)+aScroll[1]+'px'
      this.Loader.style.left = (iScreenWidth>>1)-(this.Loader.getWidth()>>1)+aScroll[0]+'px';
      this.Loader.style.right = 'auto';
    }
    else
    {
      this.Loader.style.top = iScreenHeight-this.Loader.getHeight()+aScroll[1]+'px';
      this.Loader.style.left = 'auto';
      this.Loader.style.right = '0px';
    }

    // Do we need
    this.Loader.innerHTML = this.LoaderTemplate.evaluate({loading_text: this.szNextLoadingMessage});

    // Finally, visiblize them
    this.Loader.style.visibility = 'visible';
  }
}
  //the form doesnt reset the fields to blank so I have to write this code to do it!
  function reset(id)
  {
      
    $A($(id).getElementsByTagName('input')).each(function(s)
    {    
        if($(s).type == 'text')
        {
            $(s).value="";
        }
    });
  }


  function validate_integers_only(myString)
  {
   
    var  result = myString.value.match(/^\d+$/);
    var length = myString.value.length;

    if (result == null)
    {
        myString.value = myString.value.substr(0,length - 1);
    }

  }
/*
  Convert input date string to date (or undefined if not a date string).
  The following date formats are supported:

  - ISO date format: yyyy-mm-dd, for example '2001-12-25'
  - d[ mmm[ yy[yy]]]: examples: '22', '22 feb', '22 feb 2003', '22 feb 03', '22 February 2003'
  - d[/m/[yy[yy]]]: examples: '21', '21/7', '21/7/07', '21/7/2007'

*/
function string_to_date(s) {
  var result = undefined;
  var today = new Date;
  if (mo = s.match(/^\s*(\d{4})-(\d{1,2})-(\d{1,2})\s*$/)) {
    // ISO date format 'yyyy-mm-dd'.
    result = new Date(mo[1], Number(mo[2])-1, mo[3]);
  }
  else if (mo = s.match(/^\s*(\d{1,2})(?:(?:\s+|-)([a-zA-Z]{3,9})(?:(?:\s+|-)(\d{2}(?:\d{2})?))?)?\s*$/)) {
    // 'd mmmm yyyy' format and abbreviations.
    mo[2] = mo[2] ? calendar.month_numbers[mo[2].substr(0,3).toLowerCase()] : today.getMonth();
    mo[3] = mo[3] || today.getFullYear();
    result = new Date(mo[3], mo[2], mo[1]);
  }
  else if (mo = s.match(/^\s*(\d{1,2})(?:(?:\/)(\d{1,2})(?:(?:\/)(\d{2}(?:\d{2})?))?)?\s*$/)) {
    // 'd/m/yyyy' format and abbreviations.
    mo[2] = mo[2] ? Number(mo[2])-1 : today.getMonth();
    mo[3] = mo[3] || today.getFullYear();
    mo[3] = Number(mo[3]);
    if (mo[3] < 99) mo[3] += 2000;
    result = new Date(mo[3], mo[2], mo[1]);
  }
  return result;
}


