/*
@Author: Забродин Д.В.
@Name: Вертикальное меню.
@Date: 18.02.2009
@Version: 1.03
*/

FloatMenu = Class.create();
FloatMenuItem = Class.create();

FloatMenu.prototype = {
	initialize: function(htmlPrefix, highlightMarker)
	{
		this.Elements = Array();
		this.HtmlPrefix = htmlPrefix;
		this.HighlightMarker = highlightMarker;

		for (i = 1; i < 30; i++) {
		    htmlN1 = this.HtmlPrefix + "" + i + "-header";
			htmlN2 = this.HtmlPrefix + "" + i + "-content";
			testObj = $(htmlN1);
			if (testObj != null) {
				this.Elements.push(new FloatMenuItem(htmlN1, htmlN2, this.HighlightMarker, this));
			} else {
			    break;
			}
		}
	},

	closeAllBut: function (element)
	{
		for (i = 0; i < this.Elements.length; i++) {
		    if (this.Elements[i] != element) {
				this.Elements[i].closeImmediate();
			}
		}
	}
};

FloatMenuItem.prototype = {
	initialize: function(htmlElementHeader, htmlElementBody, highlightMarker, parent)
	{
		this.HtmlElementHeader = htmlElementHeader;
		this.HtmlElementBody = htmlElementBody;
		this.HighlightMarker = highlightMarker;
		this.Parent = parent;
		this.TickClock = 0;
		this.MaxTickCount = 20;
		$(this.HtmlElementBody).style.overflow = "hidden";
		$(this.HtmlElementBody).style.display = "block";
		this.Height = $(this.HtmlElementBody).offsetHeight;
		this.closeImmediate();
		$(this.HtmlElementBody).style.position = "relative";
		
		Event.observe($(htmlElementHeader), 'click', this.mouseclick.bindAsEventListener(this));
		
		this.testCookie();
	},

	testStatus: function()
	{
		status = "closed";
		if ($(this.HtmlElementBody).style.display == "block") {
			status = "open";
		}
	},
	
	mouseclick: function()
	{
	    if (this.Status == "closed") {
	        this.open();
	        return;
	    }
	    this.close();
	    //this.closeImmediate();
	},

	open: function()
	{
		if (this.Status == "closed") {
			this.Status = "opening";
			$(this.HtmlElementHeader).addClassName(this.HighlightMarker);
			$(this.HtmlElementBody).style.height = "0px";
			$(this.HtmlElementBody).style.opacity = "0";
			$(this.HtmlElementBody).style.filter = 'alpha(opacity = 0)';
			$(this.HtmlElementBody).style.display = "block";
			this.TickClock = 0;
			this.TickInterval = setInterval(this.openClock.bindAsEventListener(this), 1);
		}
	},

	openClock: function ()
	{
		if (this.Status != "opening") {
			this.closeImmediate();
			return;
		}
		
		height = this.TickClock * (this.Height / this.MaxTickCount);
	    $(this.HtmlElementBody).style.height = height + "px";
		$(this.HtmlElementBody).style.opacity = (this.TickClock * (0.6/this.MaxTickCount)) + 0.4;
		$(this.HtmlElementBody).style.filter = 'alpha(opacity = ' + ((this.TickClock * (60/this.MaxTickCount)) + 40) + ')';
		this.TickClock++;
		if (this.TickClock >= this.MaxTickCount) {
		    clearInterval(this.TickInterval);
			$(this.HtmlElementBody).style.height = this.Height + "px";
			$(this.HtmlElementBody).style.opacity = "1";
			$(this.HtmlElementBody).style.filter = 'alpha(opacity = 100)';
			this.Status = "open";
			this.setCookie();
		}
	},
	
	close: function ()
	{
	    if (this.Status == "open") {
			this.Status = "closing";
			this.TickClock = this.MaxTickCount;
			this.TickInterval = setInterval(this.closeClock.bindAsEventListener(this), 1);
		}
	},
	
	closeClock: function ()
	{
		if (this.Status != "closing") {
			this.closeImmediate();
			return;
		}
		
		height = this.TickClock * (this.Height / this.MaxTickCount);
	    $(this.HtmlElementBody).style.height = height + "px";
		$(this.HtmlElementBody).style.opacity = (this.TickClock * (0.6/this.MaxTickCount)) + 0.4;
		$(this.HtmlElementBody).style.filter = 'alpha(opacity = ' + ((this.TickClock * (60/this.MaxTickCount)) + 40) + ')';
		this.TickClock--;
		if (this.TickClock < 0) {
		    this.closeImmediate();
		    this.setCookie();
		}
	},

	closeImmediate: function ()
	{
		clearInterval(this.TickInterval);
		$(this.HtmlElementHeader).removeClassName(this.HighlightMarker);
		$(this.HtmlElementBody).style.height = "0px";
		$(this.HtmlElementBody).style.opacity = "0";
		$(this.HtmlElementBody).style.filter = 'alpha(opacity = 0)';
		$(this.HtmlElementBody).style.display = "none";
		this.Status = "closed";
	},
	
	getUrlTypeParam: function()
	{
	    url = document.location.href;
	    params = url.split("?");
	    if (params.length > 1) {
	        qparams = params[1].toQueryParams();
	        type = qparams["type"] 
	    } else {
	        type = 1;
	    }
	    
	    return type;
	},
	
	setCookie: function ()
	{
	    cookiename = this.HtmlElementHeader + "-" + this.getUrlTypeParam();
	    cookievalue = this.Status;
	    document.cookie = cookiename + "=" + cookievalue + ";";
	},
	
	testCookie: function ()
	{
	    var cookies = document.cookie.split( ';' );
	    for (j = 0; j < cookies.length; j++) {
	        cookie = cookies[j].split('=');
	        cookiename = cookie[0];
	        cookievalue = cookie[1];
	        cookiename = cookiename.replace(/^\s+|\s+$/g, '');
	                
	        if (cookiename == this.HtmlElementHeader + "-" + this.getUrlTypeParam()) {
	            if (cookievalue == "open" || cookievalue == "opening") {
	                this.open();
	            }
	        }
	    }
	}
};
