document.observe("dom:loaded", function(){
	navEffect = new navEffect("navEffect");
});

var navEffect = {};
navEffect = Class.create();
navEffect.prototype = {
	initialize: function(objName){
		this.obj = null;
		this.objBar = null;
		this.instance = null;

		this.objName = objName;
		this.objBarName = this.objName+"Bar";
		this.options = Object.extend({
			setDefaultHeight: true,
			defaultHeightElements: ["header", "visual"],
			inspectMode: true,
			inspectWhat: "#mainNav ul li a.a1stLevel",
			selectedElement: "#mainNav li.selected a.a1stLevel",
			mainNavElement: "#mainNav ul",
			defaultOffset: -10,
			defaultBarOffset: -188,
			none: true
        }, arguments[1] || { });
		
		this.createElement();

		this.createBarElement();

		if(this.options.inspectMode){
			this.setInspectMode();
		}
	},

	createBarElement: function(){
		this.objBar = new Element("div", { "id": this.objBarName });
		$$(this.options.mainNavElement)[0].insert({ before: this.objBar });
		this.objBar = $(this.objBarName);

		newHeight = 0;
		if($$(this.options.selectedElement)[0]){
			newHeight = this.getNewPosition($$(this.options.selectedElement)[0]);
			newHeight = newHeight + this.options.defaultBarOffset;
		}

		this.objBar.setStyle({
			width: "6px",
			height: newHeight+"px"
		});
	},

	setInstance: function(obj){
		this.instance = obj;
	},

	createElement: function(){
		this.obj = new Element("div", { "id": this.objName });
		$$("body")[0].appendChild(this.obj);
		this.obj = $(this.objName);

		this.obj.setStyle({
			position: "absolute",
			left: "0px",
			top: "0px",
			width: "50%"
		});

		if(this.options.setDefaultHeight){
			if(this.options.setDefaultHeight){
				defaultHeight = 0;
				this.options.defaultHeightElements.each(function(id){
					defaultHeight += $(id).getHeight();
				});
				this.obj.setStyle({
					height: defaultHeight+"px"
				});
			}
			this.resetToDefaultPosition("asap");
		}
	},
	
	resetToDefaultPosition: function(effect){
		newHeight = 0;
		if($$(this.options.selectedElement)[0]){
			if($$(this.options.selectedElement)[0]){
				newHeight = this.getNewPosition($$(this.options.selectedElement)[0]);
			}
		}else{
			this.options.defaultHeightElements.each(function(id){
				newHeight += $(id).getHeight();
			});
		}

		if(effect == "morph"){
			if(instance.lastEffect){
				instance.lastEffect.cancel();
			}
			this.lastEffect = new Effect.Morph(instance.obj, {
				style: "height: "+newHeight+"px"
			});
		}else if(effect == "asap"){
			this.obj.setStyle({
				height: newHeight+"px"
			});
		}
	},

	setInspectMode: function(){
		if(this.options.inspectWhat && $$(this.options.inspectWhat)){
			instance = this;
			$$(this.options.inspectWhat).each(function(item){
				Event.observe(item, "mouseover", instance.inspectListener.bindAsEventListener(this, instance, item));
				Event.observe(item, "mouseout", instance.inspectListener.bindAsEventListener(this, instance, item, "reset"));
			});
		}
	},

	inspectListener: function(event, instance, item){
		if(arguments[3] == "reset"){
			instance.resetToDefaultPosition("morph");
			return;
		}
		
		new PeriodicalExecuter(function(pe){
			instance.morphIt(item);
			pe.stop();
		}, 0.25);
	},

	getNewPosition: function(item){
		itemPosition = item.positionedOffset();
		height = itemPosition.top+item.getHeight()+this.options.defaultOffset;
		return height;
	},

	morphIt: function(){
		height = 0;
		if(typeof arguments[0] == "number")
			height = arguments[0];
		if(typeof arguments[0] == "object")
			height = this.getNewPosition(arguments[0]);

		if(this.lastEffect){
			this.lastEffect.cancel();
		}
		this.lastEffect = new Effect.Morph(this.obj, {
			style: "height: "+height+"px"
		});
	},

	none: function(){
	}
};