/* 2009-06-25 */

var isIE7older=false; 
/*@cc_on @if(@_jscript_version <= 5.7) isIE7older=true; @end @*/


$(document).ready(function(){
  $('#loginForms').addClass('offleft').insertBefore('#container');
  $('#loginForms').find('.wrapper').prepend('<a id="loginFormsAnchor" href="#"></a>');
  $('#loginFormsTab').find('a').click(function(e){
    if($('#loginForms').hasClass('offleft')) {
      $('#loginForms').css('display','none').removeClass('offleft').
      slideDown(function(){
        setTimeout("$('#loginFormsAnchor').get(0).focus()",200);
      });
      $('#loginFormsTab').addClass('noBorder').animate({'top':93});
    } else {
      if(isIE7older){
        $('#loginForms').find('form').css('visibility','hidden');
      }
      $('#loginForms').slideUp(function(){
        $(this).addClass('offleft').css('display','');
        if(isIE7older){
          $('#loginForms').find('form').css('visibility','visible');
        }
      });
      $('#loginFormsTab').animate({'top':0},function(){
        $('#loginFormsTab').removeClass('noBorder');
      }).addClass('noCoda');
    }
    if(e.stopPropagation) e.stopPropagation();
    return false;
  });
  $('ul.sidenav').find('ul').each(function(idx){
    $(this).wrap('<div class="toggle"></div>');
    if(!$(this).parents('li').hasClass('active')) $(this).parent().hide();
  });
  $('ul.sidenav').find('div.toggle').each(function(){
    $(this).prev('a').click(function(e){
      $(this).next('div.toggle').slideToggle();
      if(e.stopPropagation) e.stopPropagation();
      return false;
    });
  });

  // zebra striping code block lines
  var isIE6older;
  if(isIE7older && typeof(document.body.style.maxHeight)=='undefined') { // IE6
    document.styleSheets[0].addRule('pre.code .stripe.alt',
      'filter:progid:DXImageTransform.Microsoft.gradient'+
      '(startColorstr=#28ffffff,endColorstr=#28ffffff)');
    isIE6older=true;
  }
  $('pre.code').each(function(){
    var lines = $(this).find('code').text().split(/\r\n|\r|\n/).length;
    var lineHeight = $(this).find('code').height()/lines;
    $(this).append('<span class="stripeBin"></span>');
    var bin = $(this).find('.stripeBin');
    if(isIE6older) {
      $(bin).width($(this).width());
    }
    for(var i=0;i<lines;i++){
      $(bin).append('<span class="stripe'+(i%2?' alt':'')+'">'+
        (i+1)+'.</span>');
    }
  });


  var field1=document.forms['projectCommunicator'].elements['user_name'];
  var field2=document.forms['echoShot'].elements['username'];
  var rmText = function(){
    if(this.value.toLowerCase()=='username') this.value='';
  }
  var addText = function(){
    var me=this;
    setTimeout(function(){
      if(me.value=='') me.value='Username';
    },10);
  }
  $(field1).focus(rmText).blur(addText);
  $(field2).focus(rmText).blur(addText);

  // Use a conditional to prevent the password field changer script from 
  // crashing Konqueror 3.4 and Safari 1.0
  var ua=navigator.userAgent.toLowerCase();
  if(!((ua.indexOf('konqueror')!=-1) && /khtml\/3\.[0-4]/.test(ua)) && 
    !(((ua.indexOf('safari')!=-1) && !window.print))) {

      var field3=document.forms['projectCommunicator'].password;
      var field4=document.forms['echoShot'].password;
      // Set the third value to the text you want to appear in the field.
      changeInputType(field3,'text','Password',false,true);
      changeInputType(field4,'text','Password',false,true);
  }

  $('#content').css('min-height',
    $(window).height()-$('#header').height()-$('#footer').get(0).offsetHeight
  );

});


function changeInputType(
  oldElm, // a reference to the input element
  iType, // value of the type property: 'text' or 'password'
  iValue, // the default value, set to 'password' in the demo
  blankValue, // true if the value should be empty, false otherwise
  noFocus) {  // set to true if the element should not be given focus
  // http://dynamicsitesolutions.com/javascript/change-input-type-dynamically/
  if(!oldElm || !oldElm.parentNode || (iType.length<4) || 
    !document.getElementById || !document.createElement) return;
  var isMSIE=/*@cc_on!@*/false; //http://dean.edwards.name/weblog/2007/03/sniff/
  if(!isMSIE){
    var newElm=document.createElement('input');
    newElm.type=iType;
  } else {
    var newElm=document.createElement('span');
    newElm.innerHTML='<input type="'+iType+'" name="'+oldElm.name+'">';
    newElm=newElm.firstChild;
  }
  var props=['name','id','className','size','tabIndex','accessKey'];
  for(var i=0,l=props.length;i<l;i++){
    if(oldElm[props[i]]) newElm[props[i]]=oldElm[props[i]];
  }
  $(newElm).focus(function(){return function(){
    if(this.hasFocus) return;
    var newElm=changeInputType(this,'password',iValue,
      (this.value.toLowerCase()==iValue.toLowerCase())?true:false);
    if(newElm) newElm.hasFocus=true;
  }}());
  $(newElm).blur(function(){return function(){
    if(this.hasFocus)
    if(this.value=='' || (this.value.toLowerCase()==iValue.toLowerCase())) {
      changeInputType(this,'text',iValue,false,true);
    }
  }}());
 // hasFocus is to prevent a loop where onfocus is triggered over and over again
  newElm.hasFocus=false;
  // some browsers need the value set before the element is added to the page
  // while others need it set after
  if(!blankValue) newElm.value=iValue;
  oldElm.parentNode.replaceChild(newElm,oldElm);
  if(!isMSIE && !blankValue) newElm.value=iValue;
  if(!noFocus || typeof(noFocus)=='undefined') {
    window.tempElm=newElm;
    setTimeout("tempElm.hasFocus=true;tempElm.focus();",1);
  }
  return newElm;
}
