var buttonMethods = {
  observe: function(element)	{
    element = $(element);
    Event.observe(element, 'click', function(event) {
      this.click(Event.element(event));
    });
    return element;
  },
  disable : function(element) {
     element = $(element);
     element.setOpacity(0.6).writeAttribute('disabled', true);
     return element;
  },
  enable : function(element) {
     element = $(element);
     element.setOpacity(1.0).writeAttribute('disabled', false);
     return element;
  },
  isEnabled : function(element) {
     element = $(element);
     return( element.readAttribute('disabled') == true
         || $(element).getStyle('opacity') == 1 ) ? true : false;
  },
  click: function(element) {
     element = $(element);
     if (element.id == 'b_contact') { // Don't Deactivate the contact button
       return element;
     }
     element.up().clear(); // Activate all buttons on the parent menu
     element.disable();
     //alert("Button Clicked: " + element.id);
     element.fire("uhkna:updateContent", { value: element.id });
     return element;
  }
};

var menuMethods = {
  clear: function(element) {
		element = $(element);
		element.childElements().each(function(button) {
      try {
        button.enable();
      } catch (e) {
        if (! e instanceof TypeError) throw e;
      }
    });
    return element;
	},
	observe: function(element) {
     element = $(element);
     element.childElements().each(function(button) {
       Event.observe(button, 'click', function(event) {
         button.click(Event.element(event));
       });
		});
		return element;
	}
};

Element.addMethods('div', menuMethods);
Element.addMethods('a', buttonMethods);

var uhkna = {
  version : '0.0.1',
  requiredPrototype : '1.6.1',
	lastButtonClicked : 'a_home',
	mainContextMenuId : 'mainContextMenu',
	contextHistory: $A({}),
	url : 'index.php',
	menuImgNdx : 0,
	enabledOpacity : '1',
	overOpacity : '0.80',
	disabledOpacity : '0.60',

  activeLink: Class.create(Template, {
    initialize: function ($super) {
      $super("<a id='a_#{name}' name='#{name}' title='#{title}'  href='#{href}'>#{label}</a>")
    },
    write: function (hash) {
       this.hash = hash;
       document.write(this.evaluate(this.hash));
    }
  }),
  inactiveLink: Class.create(Template, {
    initialize: function ($super) {
      $super ("<span id='a_#{name}' style='opacity:0.6'>#{label}</span>")
    },
    write: function (hash) {
       this.hash = hash;
       document.write(this.evaluate(this.hash));
    }
  }),

  barMenu: Class.create({
    initialize: function(menus, buttons) {
      this.menus = menus;
      this.buttons = buttons;
      this.disabledLink = new uhkna.inactiveLink();
      this.enabledLink  = new uhkna.activeLink();
    },
    write: function(id) {
        document.write('<div id="' + id + '" class="barMenu">')
        $H(this.menus.get(id)).each( function(button) {
            (button.value == 'active') ? this.enabledLink(this.buttons[button.key])
                                       : this.disenabledLink(this.buttons[button.key]);
        }, this );
        document.write('</div>')
    }
  }),

  load : function(element) {
    this.init();
    if ($('barMenu')) {
      $('barMenu').observe();
      //$('barMenu').set();
    };
  },

	init : function() {
    var agt = navigator.userAgent.toLowerCase();
    this.is_op = (agt.indexOf("opera") != -1);
    this.is_ie = (agt.indexOf("msie") != -1) && document.all && !this.is_op;
    this.is_ie6 = (agt.indexOf("msie 6") != -1) && document.all
      && !this.is_op;
    this.is_ie7 = (agt.indexOf("msie 7") != -1) && document.all
      && !this.is_op;
    this.checkRequiredPrototype(); // http://script.aculo.us/
	},

	/* http://script.aculo.us/ */
	checkRequiredPrototype : function() {
    if ((typeof Prototype == 'undefined')
        || (typeof Element == 'undefined')
        || (typeof Element.Methods == 'undefined')
        || (this.convertVersionString(Prototype.Version) < this
            .convertVersionString(this.requiredPrototype)))
      throw ("uhkna requires the Prototype JavaScript framework >= " + this.requiredPrototype);
	},

	/* http://script.aculo.us/ */
	convertVersionString : function(versionString) {
    var r = versionString.split('.');
    return parseInt(r[0]) * 100000 + parseInt(r[1]) * 1000 + parseInt(r[2]);
	},
  showDog: function(id) {
      alert("showDog:" + id);
      $element = $("show_" + id);
      //document.location="displayDog.php?id=" + id;
      document.location="index.php?id=" + id;
  },

  hideDog: function(id) {
      $element = $(id);
      $element.hide("show_" + id);
  }

}; // end uhkna

var listTemplate2 = Class.create (Template, {
  initialize: function ($super, hash) {
        $super('<div id="list_#{id}" class="listItem"><a class="thumbnail" href="javascript:void(0)" onclick="uhkna.showDog(&quot;#{id}&quot;)"><img src="images/#{id}_tn.gif" alt="Thumbnail of #{callName}"></a><div class="info"><h4>&quot;#{callName}&quot;</h4><a href="javascript:void(0)" onclick="uhkna.showDog(&quot;#{id}&quot;)"><h3>#{ch}#{registeredName}</h3></a><h4>#{titles}</h4></div> <!-- end dogList.info --></div>');
     this.hash = null;
  },
  write: function(hash) {
     this.hash = hash;
     document.write(this.evaluate(this.hash));
  }
});

var listTemplate = Class.create (Template, {
  initialize: function ($super, hash) {
       var TEMPLATE = '<div id="list_#{id}" class="listItem">';
  TEMPLATE+='<a class="thumbnail" href="index.php?page=showIg&id=#{id}">';
  TEMPLATE+='<img src="images/#{id}_tn.gif" alt="Thumbnail of #{callName}"/></a>';
  TEMPLATE+='<div class="info">';
  TEMPLATE+='<h4>"#{callName}"</h4>';
  TEMPLATE+='<a href="javascript:void(0)" onclick="uhkna.showDog(\"#{id}\")">';
  TEMPLATE+='<h3>#{ch}#{registeredName}</h3></a>';
  TEMPLATE+='<h4>#{titles}</h4>';
  TEMPLATE+='</div> <!-- end dogList.info -->';
  TEMPLATE+='</div>';
     $super(TEMPLATE);
     this.hash = null;
  },
  write: function(hash) {
     this.hash = hash;
     document.write(this.evaluate(this.hash));
  }
});

Event.observe(window, 'load', function (event) {
    uhkna.load(Event.element(event));
});

function loadContent(h) {
  alert('Updating:' + h);
}


