// WindowListener Object
// ImageHelper Object
// NodeHelper Object
// WindowLauncher Object
// MakeTween Class
// MakeElement Class
// MakeSlideMenu Class
// initSlideMenu Function

// --------------------------------------------------
// WindowListener Object

WindowListener = new Object();
WindowListener.functions = new Object();

WindowListener.add = function(event,func){
	var e = this.functions[event];
	e[e.length] = func;
}

// --- Private Methods ---

WindowListener.addEvent = function(event){
	window["on"+event] = function(){WindowListener.run(event)};
	this.functions[event] = new Array();
}
WindowListener.run = function(event){
	var e = this.functions[event];
	for(var i in e) eval(e[i]+"()");
}
WindowListener.addEvent("load");

// --------------------------------------------------
// ImageHelper Object

ImageHelper = new Object();

ImageHelper.preload = function(src){
	var img = new Image();
	img.src = src;
	return img;
}
ImageHelper.swap = function(image, obj){
	document.images[image].src = obj.src;
}

// --------------------------------------------------
// NodeHelper Object

NodeHelper = new Object();

NodeHelper.addAnchor = function(node){
	var a = document.createElement("a");
	var parent = node.parentNode;
	parent.replaceChild(a, node);
	a.appendChild(node);
	a.setAttribute("href","#");
	return a;
}
NodeHelper.replaceImage = function(image, src){
	var image = (typeof image == "string") ? document.getElementById(image) : image;
	var i = image.cloneNode(true);
	i.setAttribute("src", src);
	var parent = image.parentNode;
	parent.replaceChild(i, image);
	return i;
}

// --------------------------------------------------
// WindowLauncher Object

WindowLauncher = new Object();
WindowLauncher.open = function(url,name,width,height,toolbar,scroll,center){
	toolbar = toolbar ? "yes" : "no";
	scroll = scroll ? "yes" : "no";
	var features = "toolbar="+toolbar+",menubar="+toolbar+",location="+toolbar+",status="+toolbar+",scrollbars="+scroll+",resizable="+scroll;
	if(width) features += ",width="+width;
	if(height) features += ",height="+height;
	if(center){
		var x = 0, y = 0;
		if(width && window.screen.availWidth){
			x = Math.round((window.screen.availWidth-parseInt(width))/2);
			features += ",screenX="+x+",left="+x;
		}
		if(height && window.screen.availHeight){
			y = Math.round((window.screen.availHeight-parseInt(height))/2);
			features += ",screenY="+y+",top="+y;
		}
	}
	WindowLauncherReference = window.open(url,name,features);
	if(WindowLauncherReference != null && !WindowLauncherReference.closed){
		WindowLauncherReference.focus();
		if(center) WindowLauncherReference.moveTo(x,y);
	}
}
WindowLauncher.openCenter = function(url,name,width,height){
	this.open(url,name,width,height,false,false,true);
}
WindowLauncher.openZoom = function(img){
	var url = "enlarge" + img;
	this.openCenter(url, "enlarge", 710, 400);
}
WindowLauncher.openStaticZoom = function(img){
	this.openCenter(img, "enlarge", 710, 400);
}

// --------------------------------------------------
// MakeTween Class

MakeTween = function(el, getter, setter){
	this.el = el;
	this.getter = getter;
	this.setter = setter;
	this.callback = null;
	this.interval = null;
	this.obj = "MakeTweenInstance_"+(++MakeTween.instance);
	eval(this.obj+"=this");
}
MakeTween.instance = 0;

MakeTween.prototype.tween = function(endpos, callback){
	this.callback = callback;
	this.clearTween();
	this.doTween(endpos);
	this.interval = setInterval(this.obj+".doTween("+endpos+")", 100);
}
MakeTween.prototype.doTween = function(endpos){
	var pos = this.getter();
	if(Math.abs(pos-endpos)<=1){
		this.setter(endpos);
		this.clearTween();
		if(this.callback) this.callback();
	}else{
		this.setter(pos+(endpos-pos)/2);
	}
}
MakeTween.prototype.clearTween = function(){
	clearInterval(this.interval);
	this.interval = null;
}

// --------------------------------------------------
// MakeElement Class

// Required:
// - MakeTween Class

MakeElement = function(idname){
	this.el = document.getElementById(idname);
	this.css = this.el.style;
	this.obj = "MakeElementInstance_"+idname;
	eval(this.obj+"=this");
}
// event
MakeElement.prototype.addEvent = function(type, callback){
	this.el["on"+type] = callback;
}
MakeElement.prototype.removeEvent = function(type){
	this.addEvent(type, null);
}
// visibility
MakeElement.prototype.show = function(){
	this.css.visibility = "visible";
}
MakeElement.prototype.hide = function(){
	this.css.visibility = "hidden";
}
// background color
MakeElement.prototype.getBgColor = function(){
	return this.css.backgroundColor || null;
}
MakeElement.prototype.setBgColor = function(color){
	this.css.backgroundColor = color || "";
}
// background alpha
MakeElement.prototype.setBgAlpha = function(png){
	var ua = navigator.userAgent.toLowerCase();
	if(ua.indexOf("msie 6") != -1){
		this.css.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true sizingMethod=scale src="'+png+'");';
	}else{
		this.css.backgroundImage = "url("+png+")";
	}
}
// top
MakeElement.prototype.getTop = function(){
	return this.el.offsetTop;
}
MakeElement.prototype.setTop = function(top){
	this.css.top = top+"px";
}
// left
MakeElement.prototype.getLeft = function(){
	return this.el.offsetLeft;
}
MakeElement.prototype.setLeft = function(left){
	this.css.left = left+"px";
}
// width
MakeElement.prototype.getWidth = function(){
	return this.el.offsetWidth;
}
MakeElement.prototype.setWidth = function(width){
	this.css.width = width+"px";
}
// height
MakeElement.prototype.getHeight = function(){
	return this.el.offsetHeight;
}
MakeElement.prototype.setHeight = function(height){
	this.css.height = height+"px";
}
// clip
MakeElement.prototype.getClipWidth = function(){
	var clip = this.getClip();
	return clip.right - clip.left || this.getWidth();
}
MakeElement.prototype.getClipHeight = function(){
	var clip = this.getClip();
	return clip.bottom - clip.top || this.getHeight();
}
MakeElement.prototype.getClip = function(){
	var c = this.css.clip;
	c = c.slice(5, c.length-1);
	c = c.split(" ");
	for(var i=0; i<4; i++){
		c[i] = parseInt(c[i]);
	}
	return {top:c[0], right:c[1], bottom:c[2], left:c[3]};
}
MakeElement.prototype.setClip = function(top, right, bottom, left){
	this.css.clip = "rect("+top+"px "+right+"px "+bottom+"px "+left+"px)";
}
// z-index
MakeElement.prototype.getZindex = function(){
	return this.css.zIndex || 0;
}
MakeElement.prototype.setZindex = function(z){
	this.css.zIndex = z;
}
// tween
MakeElement.prototype.tweenTop = function(top, callback){
	var self = this;
	if(!this.topTween) this.topTween = new MakeTween(this, function(){return self.getTop()}, function(y){self.setTop(y)});
	this.topTween.tween(top, callback);
}
MakeElement.prototype.tweenLeft = function(left, callback){
	var self = this;
	if(!this.leftTween) this.leftTween = new MakeTween(this, function(){return self.getLeft()}, function(x){self.setLeft(x)});
	this.leftTween.tween(left, callback);
}

// --------------------------------------------------
// MakeSlideMenu Class

// Required:
// - MakeElement Class

MakeSlideMenu = function(){
	this.menus = new Array();
	this.timeout = null;
	this.active = null;
	this.activeBgColor = null;
	this.depth = 100;
	this.obj = "MakeSlideMenuInstance_"+(++MakeSlideMenu.instance);
	eval(this.obj+"=this");
}
MakeSlideMenu.instance = 0;

// --- Public Methods ---

MakeSlideMenu.prototype.setActiveBgColor = function(color){
	this.activeBgColor = color;
}
MakeSlideMenu.prototype.build = function(parent, mask, child){
	var self = this;
	var index = this.menus.length;
	var menu = this.menus[index] = new Object();
	// Build Objects
	menu.parent = new MakeElement(parent);
	menu.mask = mask ? new MakeElement(mask) : null;
	menu.child = child ? new MakeElement(child) : null;
	// Attach Events
	menu.parent.addEvent("mouseover", function(){self.hide(); self.show(index)});
	menu.parent.addEvent("mouseout", function(){self.out()});
	if(menu.child){
		//menu.parent.addEvent("click", function(){return false});
		menu.child.addEvent("mouseover", function(){self.show(index)});
		menu.child.addEvent("mouseout", function(){self.out()});
	}
}

// --- Runtime Methods ---

MakeSlideMenu.prototype.show = function(index){
	if(this.timeout) clearTimeout(this.timeout);
	var menu = this.menus[index];
	if(this.activeBgColor) menu.parent.setBgColor(this.activeBgColor);
	if(menu.child){
		menu.mask.show();
		menu.mask.setZindex(++this.depth);
		menu.child.tweenLeft(0);
	}
	this.active = index;
}
MakeSlideMenu.prototype.hide = function(){
	if(this.active != null){
		var menu = this.menus[this.active];
		if(this.activeBgColor) menu.parent.setBgColor(null);
		if(menu.child) menu.child.tweenLeft(-menu.child.getWidth(), function(){menu.mask.hide()});
		this.active = null;
	}
}
MakeSlideMenu.prototype.out = function(){
	this.timeout = setTimeout(this.obj+".hide()", 300);
}

// --------------------------------------------------
// ||||||||||||||||||||||||||||||||||||||||||||||||||
// Build SlideMenu

function initSlideMenu(){
	if(!document.getElementById) return;
	
	var menu = new MakeSlideMenu();
	menu.setActiveBgColor ("#dadada");
	menu.build ("exhibitionsMenu", "exhibitionsMask", "exhibitionsSubmenu");
	menu.build ("collectionMenu", "collectionMask", "collectionSubmenu");
	menu.build ("oppenheimerMenu", "oppenheimerMask", "oppenheimerSubmenu");
	menu.build ("galleryMenu", "galleryMask", "gallerySubmenu");
	menu.build ("membershipMenu", "membershipMask", "membershipSubmenu");
	menu.build ("contactMenu");
}

WindowListener.add("load","initSlideMenu");

// ||||||||||||||||||||||||||||||||||||||||||||||||||
// --------------------------------------------------

