/* $Id: core.js 9 2007-11-16 14:14:02Z jure.merhar $ */


/* variable for debuging */
var debug;


/* StartUp */
var StartUp = Class.create(); StartUp.prototype =
{
	initialize: function(runnable)
	{
	  Event.observe(document, 'dom:loaded', 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);
		if (target) {
			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, onComplete)
	{
	  this.container = container;
	  this.links = links;
	  this.onComplete = onComplete;
	},

	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)
	{
		$('ajax_loading').style.visibility = 'visible';
		new Ajax.Updater(
			this.ajaxLinks.containerObject,
			this.href,
			{
				method: 'get',
				onComplete: this.ajaxLinks.complete.bindAsEventListener(this.ajaxLinks)
			}
		);
		Event.stop(e);
	},

	complete: function()
	{
		$('ajax_loading').style.visibility = 'hidden';
		if (this.onComplete) {
			this.onComplete();
		}
  }
}


/* AjaxRelatedSelect */
var AjaxRelatedSelect = Class.create(); AjaxRelatedSelect.prototype = {
	initialize: function(main, sub, complete) {
		this.main = main;
		this.sub = sub;
		this.complete = complete;
	},

	run: function() {
		this.mainElement = $$(this.main).first();
		this.subElement = $$(this.sub).first();

		if (this.mainElement && this.subElement) {
			if (this.subElement.length < 2) {
				this.subElement.disabled = true;
			}
			this.mainElement.observe('change', this.changeListener.bindAsEventListener(this));
		}
	},

	changeListener: function(event) {
		var form = this.mainElement.up('form')
		var url = form.action.split('#');
		url = url[0];
		var params = { 'ajax' : form.id }
		var name = this.mainElement.name;
		params[name] = this.mainElement.value;

		new Ajax.Request(url, {
			method: 'get',
			parameters: params,
			onSuccess: this.onSuccess.bindAsEventListener(this),
			onComplete: this.onComplete.bindAsEventListener(this)
		});
	},

	onSuccess: function(transport) {
		this.subElement.innerHTML = '';
		var json = transport.responseJSON;
		for (var item in json) {
			var option = new Option(json[item], item);
			this.subElement.options[this.subElement.options.length] = option; 
		}
		if (Object.keys(json).length > 1) {
			this.subElement.disabled = false;
		} else {
			this.subElement.disabled = true;
		}
	},

	onComplete: function() {
		if (this.complete) {
			this.complete();
		}
	}

}


/* AjaxSelect */
var AjaxSelect = Class.create(); AjaxSelect.prototype =
{
	initialize: function(container, select, submit, onComplete)
	{
	  this.container = container;
	  this.select = select;
	  this.submit = submit;
	  this.onComplete = onComplete;
	},

	run: function()
	{
		this.containerObject = $$(this.container).first();
    var select = $$(this.select).first();
	  if ((typeof this.containerObject != 'undefined') && (typeof select != 'undefined')) {
	    var submit = $$(this.submit).first();
		  if (typeof submit != 'undefined') {
				Element.hide(submit);
			}
		  select.ajaxSelect = this;
			Event.observe(select, 'change', this.changeListener.bindAsEventListener(select));
		}
	},

	changeListener: function (e)
	{
		var url = this.form.action;
		var elements = Form.getElements(this.form);
		var delimeter = (url.indexOf('?') == -1) ? '?' : ((url.indexOf('?') < url.length - 1) ? '&' : '');
		for (var i = 0; i < elements.length; i++) {
			var element = elements[i];
			if (element.name) {
				url += delimeter + element.name + '=' + element.value;
				delimeter = '&';
	  	}
		}
		$('ajax_loading').style.visibility = 'visible';
		new Ajax.Updater(
			this.ajaxSelect.containerObject,
			url,
			{
				method: 'get',
				onComplete: this.ajaxSelect.complete.bindAsEventListener(this.ajaxSelect)
			}
		);
		Event.stop(e);
	},

	complete: function()
	{
		$('ajax_loading').style.visibility = 'hidden';
		if (this.onComplete) {
			this.onComplete();
		}
  }
}


/* External & Back Links */
var ExternalAndBackLinks = {

	run: function() {
		$$('a').each(ExternalAndBackLinks.assignListener);
	},

	assignListener: function(anchor) {
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.observe('click', ExternalAndBackLinks.externalClickListener.bindAsEventListener(anchor));
		} else
		if (anchor.hasClassName('back')) {
			anchor.observe('click', ExternalAndBackLinks.backClickListener.bindAsEventListener(anchor));
		}
	},

	externalClickListener: function(event) {
		event.stop();
		open(this.href);
	},

	backClickListener: function(event) {
		event.stop();
		history.go(-1);
	}

}


/* 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;
	  }
	}
}


/* Tooltips */
var Tooltips =
{
	run: function()
	{
		$$('a.tooltip').each(function(link) {
			var title = link.title;

			if (title && title.length) {
				Event.observe(link, "mouseover", Tooltips.showTipListener.bindAsEventListener(link));
				Event.observe(link, "mousemove", Tooltips.moveTipListener.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");
		var span = document.createElement("span");
		var span2 = document.createElement("span");
		span2.innerHTML = link.title;
		tip.className = "tooltip";
		tip.style.display = 'none';
		tip.appendChild(span);
		span.appendChild(span2);
		link.appendChild(tip);
		link._tooltip = tip;
		link._title = link.title;
		link.title = "";

		// Fix for Safari2/Opera9 repaint issue
		//document.documentElement.style.position = "relative";
	},

	hideTip: function(link)
	{
		if (link._tooltip) {
			link.title = link._title;
			link.removeChild(link._tooltip);
			link._tooltip = null;

			// Fix for Safari2/Opera9 repaint issue
			//document.documentElement.style.position = "static";
		}
	},

	moveTip: function(link, x, y)
	{
		var tip = link._tooltip;
		if (tip) {
			tip.style.top = (y+10) + 'px';
			tip.style.left = (x+20) + 'px';
			tip.style.display = 'block';
		}
	},

	showTipListener: function(event)
	{
		var x = Tooltips.getPointerXPosition(event);
		var y = Tooltips.getPointerYPosition(event);
		//this._timer = setTimeout((function() { Tooltips.showTip(this); Tooltips.moveTip(this, x, y); }).bindAsEventListener(this), 500);
		Tooltips.showTip(this);
		Tooltips.moveTip(this, x, y);
		Event.stop(event);
	},

	hideTipListener: function(event)
	{
		//clearTimeout(this._timer);
		Tooltips.hideTip(this);
	},

	moveTipListener: function(event)
	{
		var x = Tooltips.getPointerXPosition(event);
		var y = Tooltips.getPointerYPosition(event);
		Tooltips.moveTip(this, x, y);
	},

	getPointerXPosition: function(event) {
		var x = Event.pointerX(event);
		if (!x) {
			x = event.screenX;
		}
		return x;
	},
	getPointerYPosition: function(event) {
		var y= Event.pointerY(event);
		if (!y) {
			y = event.screenY;
		}
		return y;
	}
}


/* Captcha */
var Captcha = {

	run: function() {
		$$('div.captcha a.reload').each(Captcha.applyReload);
	},

	applyReload: function(item) {
		item.style.display = 'inline';
		Event.observe(item, 'click', Captcha.clickReload.bindAsEventListener(item));
	},

	clickReload: function(event) {
		event.stop();
		var img = this.previous('img.image');
		if (img) {
			img.src += '&reload=1';
		}
	}

}


/* HighSlide */
var HighSlide = {

	slideshowGroups: new Object,

	run: function()
	{
		// 87e18fc07f5c729c8640280620cce7c8
		HighSlide.setup();
		HighSlide.start();
	},

	setup: function()
	{
		hs.graphicsDir = 'js/highslide/graphics/';
		hs.outlineType = 'rounded-white';
		hs.outlineWhileAnimating = true;
		hs.dimmingOpacity = 0.75;
		hs.align = 'center';
		hs.showCredits = false;
		hs.moveText = '<img src="js/highslide/graphics/move.gif" alt="" />';
		hs.closeText = '<img src="js/highslide/graphics/close.gif" alt="" />';
		hs.loadingText = '&nbsp;';
	},

	fetch_object_overrides: function(classes)
	{
		var object_overrides = new Object;
		var regex = /^hs_(.+)_([^_]*)$/;
		classes.each(function(class_name) {
			var result = class_name.match(regex);
			if (result) {
				if (result[1] == 'slideshowGroup') {
					HighSlide.register_overlay(result[2]);
				}
				object_overrides[result[1]] = result[2];
			}
		});
		return object_overrides;
	},

	start: function()
	{
		$$('a.highslide').each(HighSlide.start_each);
	},
	start_each: function(link)
	{
		link.onclick = HighSlide.start_each_click;
		link.observe('click', function(event) { event.stop(); });
	},
	start_each_click: function(event) {
		var object_overrides = HighSlide.fetch_object_overrides(this.classNames());
		if (this.hasClassName('htmlExpand')) {
			return hs.htmlExpand(this, object_overrides);
		} else {
			return hs.expand(this, object_overrides);
		}
	},

	register_overlay: function(slideshowGroup)
	{
		var i = HighSlide.slideshowGroups[slideshowGroup];
		if (i) {
			i = i+1;
		} else {
			i = 1;
		}
		HighSlide.slideshowGroups[slideshowGroup] = i;

		if (i == 1) {
			hs.registerOverlay({
				thumbnailId: null,
				slideshowGroup: slideshowGroup,
				overlayId: 'highslide_controlbar',
				position: 'top right',
				hideOnMouseOut: true
			});
		}
	}

}

new StartUp(ExternalAndBackLinks);
new StartUp(InlineText);
//new StartUp(Captcha);

if (typeof hs == 'object') {
	new StartUp(HighSlide);
}

