//global vars
var OBJ = window.location.pathname;
if(OBJ.indexOf('.') > 0){var OBJ = OBJ.substring(OBJ.lastIndexOf('/') + 1, OBJ.lastIndexOf('.'));}else{OBJ='index';}


//nav functions
Event.onDOMReady(function(){if($('nav')){
   $A($('nav').getElementsByTagName('a')).each(function(el,i){
      if(OBJ == el.href.substring(el.href.lastIndexOf('/') + 1, el.href.lastIndexOf('.')) || 
      (el.href.substring(el.href.lastIndexOf('/') + 1)=='' && OBJ=='index')){$(el).id='current';}
   });}
});

function toggle_current(el){
   el = $(el);
   if(Element.hasClassName(el,'current')){Element.removeClassName(el,'current');}
   else{Element.addClassName(el,'current');}
}

//FOR IMAGE UPLAOD
function rotate_image(value){
   get_waiting();
   var options = {
      method: 'post',
      parameters: 'type=rotate_image&value=' + value,
      onSuccess: function(req){
         $('effects_image').src += '&blah=blah';
         kill_waiting();
      }
   };
   new Ajax.Request('/includes/process_xml.php', options);
}

function grayscale_image(){
   get_waiting();
   var options = {
      method: 'post',
      parameters: 'type=grayscale_image',
      onSuccess: function(req){
         $('effects_image').src += '&blah=blah';
         kill_waiting();
      }
   };
   new Ajax.Request('/includes/process_xml.php', options);
}

function brighten_image(value){
   get_waiting();
   var options = {
      method: 'post',
      parameters: 'type=brighten_image&value=' + value,
      onSuccess: function(req){
         $('effects_image').src += '&blah=blah';
         kill_waiting();
      }
   };
   new Ajax.Request('/includes/process_xml.php', options);
}

function revert_image(){
   get_waiting();
   var options = {
      method: 'post',
      parameters: 'type=revert_image',
      onSuccess: function(req){
         $('effects_image').src += '&blah=blah';
         kill_waiting();
      }
   };
   new Ajax.Request('/includes/process_xml.php', options);
}

over = null;
bar = null;


function get_waiting(){
   over = document.createElement('div');
   document.body.appendChild(over);
   over.className = 'LB_overlay';   
   over.style.display = 'block';
   if (window.innerHeight && window.scrollMaxY) {  
         yScroll = window.innerHeight + window.scrollMaxY;
      } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
         yScroll = document.body.scrollHeight;
      } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
         yScroll = document.body.offsetHeight;
      }
   over.style.height = yScroll + 'px';

   bar = document.createElement('div');
   document.body.appendChild(bar);
   bar.className = 'progressBar';
   bar.innerHTML = 'Processing Image: <img src="/images/waiting.gif" />';
}

function kill_waiting(){
   Element.remove(bar);
   new Effect.Opacity(over, {from: 1, to: 0, afterFinishInternal: function(){
      Element.remove(over);
   }});
}


//FOR REORDERING
updateOrder = function(){
   var options = {
           method : 'post',
           parameters : 'type=titleList&obj=' + OBJ + '&' + Sortable.serialize('title_list')
         };
   new Ajax.Request('/includes/process_xml.php', options);
}
update_form = function(id, count){
   serialized = Form.serialize('object_form_' + id);
   var options = {
               method: 'post',
               parameters:  'type=update_form&obj=' + OBJ +'&id=' + id + '&' + serialized
   };
   new Ajax.Updater('link_' + count, '/includes/process_xml.php', options);
}
title_new = function(){
   var options = {
               method: 'post',
               parameters: 'type=title_new&obj=' + OBJ
   };
   new Ajax.Updater('new_item', '/includes/process_xml.php', options);
}

Event.onDOMReady(function(){
   if($("title_list")){
      Sortable.create("title_list", {onUpdate: updateOrder, handle: 'handle'});
   }
});

//message center functions
function display_editor(message_id){
   new LITBox('/includes/process_xml.php', {type: 'window', parameters: 'type=display_editor&message_id=' + message_id + '&showReply=' +arguments[1]});
}

function display_editor_old(message_id){
   var queue = Effect.Queues.get('global');
   if(!$A(queue).length){
      var options = {
         method: 'post',
         parameters: 'type=display_editor&message_id=' + message_id,
         onSuccess: function(){setTimeout(function(){
            var queue = Effect.Queues.get('global');
            if(!$A(queue).length){
               new Effect.SlideDown('editor_container', {queue: 'end'});
               new Effect.ScrollTo('object_form', {offset: -175, queue: 'end'})
               $('email_ico_' + message_id).src = '/images/ico_mesopen.gif';
            }
         }, 500);}
      };   
      if($('editor_container').offsetHeight)
      {
         new Effect.SlideUp('editor_container',{queue: 'end', afterFinishInternal:function(){
            $('editor_container').style.display = 'none';
            setTimeout(function(){
               new Ajax.Updater('editor', '/includes/process_xml.php', options);
            },250);
         }});
      }else{
         new Ajax.Updater('editor', '/includes/process_xml.php', options);
      }
   }
}

function close_message(message_id){
   //if($('editor_container').offsetHeight > 0){
    //  new Effect.SlideUp('editor_container');
   //}
   var options = {
      method: 'post',
      parameters: 'type=close_message&message_id=' + message_id,
      onSuccess: function(){
         Element.remove($('message_tr_' + message_id));
         update_table_classNames('message_table');
      }
   };
   new Ajax.Request('/includes/process_xml.php', options);
}

function update_table_classNames(element){
   element = $(element); i=0;
   $A(element.childNodes).each(function(node){
      node.className = (node.className=='headrow'?'headrow':(i++%2==1?'oddrow':'evenrow'));
   });
}
//Accessable Form Cleanup Script//
if( document.addEventListener )
document.addEventListener( 'DOMContentLoaded', mozform, false);
function mozform(){
   //http://alistapart.com/articles/prettyaccessibleforms
   $A(document.getElementsByTagName('form')).each(function(el,i){
      // Hide forms
      Element.hide(el);
      // Processing
      $A(el.getElementsByTagName('label')).each(function(e,j){if('LI' == e.up().tagName){
         labelspan = Builder.node('span',{
            style:'display:block',className:'label'});
         labelspan.innerHTML = e.innerHTML;
         e.style.display = '-moz-inline-box';
         e.innerHTML = null;
         e.appendChild(labelspan);}});
      // Show forms
      Element.show(el);
   });
}

//Accessable login form
Event.onDOMReady(function(){
   initOverLabels();
});

function initOverLabels () {
  var labels, id, field;

  // Set focus and blur handlers to hide and show 
  // LABELs with 'overlabel' class names.
  document.getElementsByClassName('overlabel').each(function(el,i){
      // Skip labels that do not have a named association
      // with another field.
      id = el.htmlFor || el.getAttribute('for');
      field = $(id);
      
      // Hide any fields having an initial value.
      if (field.value !== '') {
        hideLabel(field.getAttribute('id'), true);
      }
      
      // Set handlers to show and hide labels.
      field.onfocus = function () {
        hideLabel(this.getAttribute('id'), true);
      };
      field.onblur = function () {
        if (this.value === '') {
          hideLabel(this.getAttribute('id'), false);
        }
      };
      
      // Handle clicks to LABEL elements (for Safari).
      el.onclick = function () {
        var id, field;
        id = this.getAttribute('for');
        if (id && (field = $(id))) {
          field.focus();
        }
      };

  });
}

function hideLabel (field_id, hide) {
  var field_for;
  var labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
    field_for = labels[i].htmlFor || labels[i]. getAttribute('for');
    if (field_for == field_id) {
      //labels[i].style.textIndent = (hide) ? '-1000px' : '0px';
      labels[i].style.display = (hide) ? 'none' : 'block';
      return true;
    }
  }
}

// Function for HomeOwner Selection Dropdown
function selectHomeOwner(value){
   if(value != 0){
      location.href='./homeowner.php?page=new&homeowner_id=' + value;
   }else{
      location.href='./homeowner.php?page=new';
   }
}
