/* begin prototype */
var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}
Object.extend = function(destination, source) {
  for (property in source) {
    destination[property] = source[property];
  }
  return destination;
}
Function.prototype.bind = function() {
  var __method = this, args = $A(arguments), object = args.shift();
  return function() {
    return __method.apply(object, args.concat($A(arguments)));
  }
}
function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}
var $A = Array.from = function(iterable) {
  if (!iterable) return [];
  if (iterable.toArray) {
    return iterable.toArray();
  } else {
    var results = [];
    for (var i = 0; i < iterable.length; i++)
      results.push(iterable[i]);
    return results;
  }
}
/* end prototype */

function showBubble(row, out) {
	row.getElementsByTagName("img")[0].src = "fileadmin/templates/" + ((out) ? "pixel.gif" : "red-bubble.gif");
}

var ua = navigator.userAgent.toLowerCase();
var isOpera = (ua.indexOf('opera') != -1);
var is_ie = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof
var needsPngHack = (is_ie && ((ua.indexOf('msie 5.5') != -1) || (ua.indexOf('msie 6.0') != -1)));

function rollIn(row, bubble_id) {
	//try {
		var bubbles = Array();
		/*
		bubbles[10] = 'circle_yellow.gif';
		bubbles[9] = 'circle_blue.gif';
		bubbles[8] = 'circle_green.gif';
		*/
		bubbles[10] = 'circle_yellow.png';
		bubbles[9] = 'circle_blue.png';
		bubbles[8] = 'circle_green.png';
		//var img = document.createElement('img');
		var img = row.firstChild.firstChild;
		//img.style.position = "absolute";
		if (needsPngHack) {
			img.src = "fileadmin/templates/spacer.gif";
			img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fileadmin/templates/" + bubbles[bubble_id] + "', sizingMethod='scale')";
		} else {
			img.src = "fileadmin/templates/" + bubbles[bubble_id];
			/*
			img.style.top = "-5px";
			img.style.left = "0px";
			img.style.height = "26px";
			img.style.width = "26px";
			*/
		}
		//row.appendChild(img);
		//row.bubble = img;
	/*
	} catch (e) {
		//ignore it. could happen with fast mouse-movements.
		alert(e);
	}*/
}

function rollOut(row) {
	//try {
		//row.removeChild(row.bubble);
		var img = row.firstChild.firstChild;
		img.style.filter = "";
		img.src = "fileadmin/templates/spacer.gif";
	/*
	} catch (e) {
		//ignore it. could happen with fast mouse-movements.
	}
	*/
}

Slider = Class.create();
Slider.prototype = Object.extend(
	new Object(),
	{
		initialize : function(menu_name) {
			this.menu = $(menu_name);
			this.submenu = $(menu_name + "_submenu");	
			var top = this.submenu.style.top;
			this.submenu.style.top = -5000;
			this.submenu.style.display = "inline";			
			this.maxHeight = this.submenu.offsetHeight;
			this.submenu.style.height = 1;
			this.submenu.style.top = top;
			this.show = true;
			this.visible = true;
			this.menu.onmouseover = this.setShowHide.bind(this, true);
			this.submenu.onmouseover = this.setShowHide.bind(this, true);
			this.menu.onmouseout = this.hideTimeout.bind(this, 1000);
			this.submenu.onmouseout = this.hideTimeout.bind(this, 1000);
			setInterval(this.showHide.bind(this), 50);
		},
		hideTimeout : function(interval) {
			if (!this.timerHide) this.timerHide = setTimeout(this.setShowHide.bind(this, false), interval);
		},
		slideShow : function(event) {
			this.submenu.style.overflow = "hidden";
			this.step = 5;
			this.interval = 0;
			this.height = ((this.height < this.maxHeight) && (this.height > 0)) ? this.height : 1;
			this.finalHeight = this.maxHeight;
			this.timerSlide = setTimeout(this.slide.bind(this), this.interval);
			this.submenu.style.display = "inline";
			this.setVisibility(true);
		},
		slideHide : function(event) {
			this.submenu.style.overflow = "hidden";
			this.step = -5;
			this.interval = 50;
			this.height = ((this.height < this.maxHeight) && (this.height > 0)) ? this.height : this.maxHeight;
			this.finalHeight = 1;
			this.timerSlide = setTimeout(this.slide.bind(this), this.interval);
			this.setVisibility(false);
		},
		setVisibility : function(visible) {
			this.visible = visible;
		},
		setShowHide : function(show) {
			this.show = show;
			if (show && this.timerHide) {
				clearTimeout(this.timerHide);
				this.timerHide = null;
			}
		},
		slide : function() {
			this.submenu.style.height = this.height;
			this.height += this.step;
			if ((this.height < this.maxHeight) && (this.height > 0)) {				
				this.timerSlide = setTimeout(this.slide.bind(this), this.interval);
			} else {
				clearTimeout(this.timerSlide);
				this.height = this.submenu.style.height = this.finalHeight;
				if (!this.visible) {
					this.submenu.style.display = "none";					
				}
				this.submenu.style.overflow = "visible";
			}
		},
		showHide : function() {
			if (this.show) {
				if (!this.visible) this.slideShow();
			} else {
				if (this.visible) this.slideHide();
			}
		}
	}
)
