/* $Id: core.js 9 2007-11-16 14:14:02Z jure.merhar $ */

/* debug variable */
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);
	  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();
		}
  }
}


/* 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();
		}
  }
}


/* 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();
		}
	}

}


/* 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 =
{
	run: function()
	{
		$$('a.tooltip').each(function(link) {
			var title = link.title;
			Event.observe(link, "click", function(event) { event.stop(); });

			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 div_top = document.createElement("div");
		div_top.className = "tooltip_top";

		var div_middle = document.createElement("div");
		div_middle.className = "tooltip_middle";
		div_middle.innerHTML = link.title;

		var div_bottom = document.createElement("div");
		div_bottom.className = "tooltip_bottom";

		var tip = document.createElement("div");
		tip.className = "tooltip";
		tip.style.display = 'none';
		tip.appendChild(div_top);
		tip.appendChild(div_middle);
		tip.appendChild(div_bottom);

		link.appendChild(tip);
		link._tooltip = tip;
		link._title = link.title;
		link.title = "";

		//$$('select').each(function(item) { item.style.visibility = 'hidden'; });

		// 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;

			//$$('select').each(function(item) { item.style.visibility = 'visible'; });

			// 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;
	}
}

/* SifrText */

var SifrText =
{
	counter: 0,
	folder: '/dsg/sifr/',

	run: function()
	{
		$$('.sifr').each(SifrText.apply);
	},

	apply: function(item)
	{
		if (!item.id) {
			item.id = 'sifr_' + SifrText.counter++;
		}
		var font = SifrText.convertFont(Element.getStyle(item, 'font-family'));
		var weight = Element.getStyle(item, 'font-weight');
		if ((weight == 'bold') || (parseInt(weight) > 500)) {
			font += 'Bold';
		}
		var style = Element.getStyle(item, 'font-style');
		if (style == 'italic') {
			font += 'Italic';
		}
		font = SifrText.folder + font + '.swf';
		sIFR.replaceElement('#' + item.id, named({
			sFlashSrc: font,
			sColor: SifrText.convertColor(Element.getStyle(item, 'color')),
			sWmode: 'transparent',
			sFlashVars: 'textalign=' + Element.getStyle(item, 'text-align'),
			nPaddingTop:    parseInt(Element.getStyle(item, 'padding-top')),
			nPaddingRight:  parseInt(Element.getStyle(item, 'padding-right')),
			nPaddingBottom: parseInt(Element.getStyle(item, 'padding-bottom')),
			nPaddingLeft:   parseInt(Element.getStyle(item, 'padding-left'))
		}));
	},

	toHex: function(val)
	{
		var hex = '0123456789abcdef';
		if (val == null) {
			return '00';
		}
		val = parseInt(val);
		if ((val == 0) || isNaN(val)) {
			return '00';
		}
		val = Math.max(0, val);
		val = Math.min(val, 255);
		val = Math.round(val);
		return hex.charAt((val - (val % 16)) / 16) + hex.charAt(val % 16);
	},

	convertColor: function(color)
	{
		if (color.charAt(0) == '#') {
			if (color.length == 4) {
				color = color.charAt(0) +
					color.charAt(1) + color.charAt(1) +
					color.charAt(2) + color.charAt(2) +
					color.charAt(3) + color.charAt(3);
	  	}
		} else if (color.substring(0, 3) == 'rgb') {
			var rgb = color.match(/^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/);
			color = '#' +
				SifrText.toHex(rgb[1]) +
				SifrText.toHex(rgb[2]) +
				SifrText.toHex(rgb[3]);
			return color;
		} else {
			color = null;
		}
		return color;
  },
	
	convertFont: function(font)
	{
		return font.split(',')[0].replace(/"| /g, '');
  }

}

/* SifrText */

var MenuLinks =
{
	run: function()
	{
		$$('#menu_main li').each(MenuLinks.apply);
	},

	apply: function(item)
	{
		item.setAttribute('href', item.down('a').href);
		Event.observe(item, 'click', MenuLinks.clickListener.bindAsEventListener(item));
	},
	
	clickListener: function(e)
	{
		location.href = this.getAttribute('href');
  }
}

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';
		}
	}

}

new StartUp(ExternalLinks);
new StartUp(InlineText);
new StartUp(PopupLinks);
new StartUp(Tooltips);
new StartUp(MenuLinks);
new StartUp(Captcha);

