/* $Id: core.js 93 2008-09-10 14:37:14Z jure.merhar $ */

var debug;

/* StartUp */

var StartUp = Class.create(); StartUp.prototype =
{
	initialize: function(runnable)
	{
	  Event.onDOMReady(runnable.run.bindAsEventListener(runnable));
	}
}

/* PostBack */

var PostBack = Class.create(); PostBack.prototype =
{
	initialize: function(select, submit)
	{
	  this.select = select;
	  this.submit = submit;
	},

	run: function()
	{
	  var select = $(this.select);
		Event.observe(select, 'change', this.submitForm.bindAsEventListener(select));
		if (typeof this.submit != 'undefined') {
			$(this.submit).remove();
		}
	},

	submitForm: function()
	{
	  this.form.submit();
	}
}

/* RadioToggle */

var RadioToggle = Class.create(); RadioToggle.prototype =
{
	initialize: function(radioHide, radioShow, container)
	{
		this.radioHide = radioHide;
		this.radioShow = radioShow;
		this.container = container;
	},
	
	run: function()
	{
		this.radioHide = $(this.radioHide);
		this.radioShow = $(this.radioShow);
		this.container = $(this.container);
	  var listener = this.toggleForm.bindAsEventListener(this);
		if (this.radioHide && this.radioShow && this.container) {
		  if (this.radioHide.checked) {
	    	this.container.hide();
	    }
		  Event.observe(this.radioHide, 'click', listener);
		  Event.observe(this.radioShow, 'click', listener);
	  }
	},

	toggleForm: function(e)
	{
	  if (Event.element(e) == this.radioShow) {
	    this.container.show();
	  } else {
	    this.container.hide();
	  }
	}
}

/* ExpandingBoxes */

var ExpandingBoxes = Class.create(); ExpandingBoxes.prototype =
{
	initialize: function(selector)
	{
	  this.selector = selector;
	},

	run: function()
	{
		$$(this.selector).each(this.assignListeners.bindAsEventListener(this));
	},

	assignListeners: function(item)
	{
		var target = $(item.rel);
	  target.addClassName('collapsed');
		Event.observe(item, 'click', this.clickListener.bindAsEventListener(target));
	},

	clickListener: function (e)
	{
	  this.toggleClassName('collapsed');
		Event.stop(e);
	}
}

/* AjaxLinks */

var AjaxLinks = Class.create(); AjaxLinks.prototype =
{
	initialize: function(container, links)
	{
	  this.container = container;
	  this.links = links;
	},

	run: function()
	{
		this.containerObject = $$(this.container).first();
	  if (typeof this.containerObject != 'undefined') {
		  for (var i = 0; i < this.links.length; i++) {
		    var links = $$(this.links[i]);
			  for (var j = 0; j < links.length; j++) {
			    var link = links[j];
					if (link) {
					  link.ajaxLinks = this;
						Event.observe(link, 'click', this.clickListener.bindAsEventListener(link));
					}
			  }
			}
	  }
	},

	clickListener: function (e)
	{
		var url = this.href;
		new Ajax.Updater(
			this.ajaxLinks.containerObject,
			this.href,
			{ method: 'get', onComplete: this.ajaxLinks.run.bindAsEventListener(this.ajaxLinks) }
		);
		Event.stop(e);
	}
}

/* ExternalLinks */

var ExternalLinks =
{
	run: function()
	{
		$$('a').each(ExternalLinks.assignListener);
	},

	assignListener: function(anchor) {
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			Event.observe(anchor, 'click', ExternalLinks.clickListener.bindAsEventListener(anchor));
		}
	},

	clickListener: function(e)
	{
	  open(this.href);
	  Event.stop(e);
	}
}

/* InlineText */

var InlineText =
{
	run: function()
	{
		$$('.inline_text').each(InlineText.assignListeners);
	},

	assignListeners: function(field) {
		Event.observe(field, 'focus', InlineText.focusListener.bindAsEventListener(field));
		Event.observe(field, 'blur', InlineText.blurListener.bindAsEventListener(field));
		InlineText.blurListener.call(field);
	},

	focusListener: function()
	{
	  if (this.value == this.title) {
	    this.value = '';
	  }
	},

	blurListener: function()
	{
	  if (this.value == '') {
	    this.value = this.title;
	  }
	}
}

/* PopupLinks */

var PopupLinks =
{
	defaultWidth:  320,
	defaultHeight: 240,

	run: function()
	{
		$$('a.popup').each(PopupLinks.assignListener);
	},

	assignListener: function(anchor)
	{
	  var width  = PopupLinks.defaultWidth;
	  var height = PopupLinks.defaultHeight;
	  if (anchor.rel) {
		  var dims = anchor.rel.split('_');
		  if (dims.length == 2) {
				width = dims[0];
				height = dims[1];
		  }
	  }
		anchor.setAttribute('popup_width',  width);
		anchor.setAttribute('popup_height', height);
		anchor.setAttribute('popup_left', (screen.width  / 2) - (width  / 2));
		anchor.setAttribute('popup_top',  (screen.height / 2) - (height / 2));
		Event.observe(anchor, 'click', PopupLinks.clickListener.bindAsEventListener(anchor));
	},

	clickListener: function(e)
	{
	  var name = 'popup_' + Math.floor(Math.random() * 8999) + 1000;
	  var width  = this.getAttribute('popup_width');
	  var height = this.getAttribute('popup_height');
	  var left   = this.getAttribute('popup_left');
	  var top    = this.getAttribute('popup_top');
		open(this.href, name, 'resizable=1,scrollbars=no,status=no,toolbar=no,menubar=no,width=' + width + ',height=' + height + ',left=' + left + ',top=' + top);
	  Event.stop(e);
	}
}

/* Tooltips */

var Tooltips = {
	selects: null,

	run: function()
	{
		Tooltips.selects = $$('select');

		$$('a').each(function(link) {
			var title = link.title;
			var rel = link.rel;

			if (title && title.length > 0 && rel=="tooltip") {
				Event.observe(link, "mouseover", Tooltips.showTipListener.bindAsEventListener(link));
				Event.observe(link, "focus",     Tooltips.showTipListener.bindAsEventListener(link));
				Event.observe(link, "mouseout",  Tooltips.hideTipListener.bindAsEventListener(link));
				Event.observe(link, "blur",      Tooltips.hideTipListener.bindAsEventListener(link));
			}
		});
	},

	showTip: function(link)
	{
		Tooltips.hideTip(link);

		var tip = document.createElement("span");
		tip.className = "tooltip";
		var tipText = document.createTextNode(link.title);
		tip.appendChild(tipText);
		link.appendChild(tip);
		link._tooltip = tip;
		link.title = "";

		// Fix for Safari2/Opera9 repaint issue
		//document.documentElement.style.position = "relative";
	},

	hideTip: function(link)
	{
		if (link._tooltip) {
			link.title = link._tooltip.childNodes[0].nodeValue;
			link.removeChild(link._tooltip);
			link._tooltip = null;

			// Fix for Safari2/Opera9 repaint issue
			//document.documentElement.style.position = "static";
		}
	},

	showTipListener: function(event)
	{
		var link = this;
		this._timer = setTimeout(function() { Tooltips.showTip(link); }, 500);
		Event.stop(event);

		Tooltips.hideSelects();
	},

	hideTipListener: function(event)
	{
		var link = this;
		clearTimeout(this._timer);
		Tooltips.hideTip(link);

		Tooltips.showSelects();
	},
	
	showSelects: function() {
		if (Tooltips.selects != null) {
			Tooltips.selects.each(function(item) { item.style.visibility = 'visible'; });
		}
	},

	hideSelects: function() {
		if (Tooltips.selects != null) {
			Tooltips.selects.each(function(item) { item.style.visibility = 'hidden'; });
		}
	}
}

new StartUp(ExternalLinks);
new StartUp(InlineText);
new StartUp(PopupLinks);
new StartUp(Tooltips);
