var dfCookies = {
    init: function() {
        this._0 = [];
        var a = document.cookie.split('; ');
        for (var i = 0; i < a.length; i++) {
            var b = a[i].split('=');
            this._0[b[0]] = b[1];
        }
    },
    create: function(a, b, c) {
		var e = "";
		var d = new Date();
		
        if (c) {
            d.setTime(d.getTime() + (c * 86400000));
            e = "; expires=" + d.toGMTString();
        } else {
            e = "";
        }
        document.cookie = a + "=" + b + e + "; path=/";
        this._0[a] = b;
    },
    erase: function(a) {
        this.create(a, '', -1);
        this._0[a] = undefined;
    },
    get: function(a) {
        return this._0[a];
    }
};
dfCookies.init();

var dfGrid = new Class({
    initialize: function(a, b, c, d, e, f, g) {
        this.wrapper = this.initDiv(a);
		this.center = this.initDiv(d);
		this.centermax = this.initDiv(e);
        this.left = this.initDiv(b);
        this.right = this.initDiv(c);
        this.fadeSpeed = (f) ? f : 500;
        this.swipeSpeed = (g) ? g : 500;
        this.wrapper.resizer = new Fx.Morph(this.wrapper.id, {
            duration: this.swipeSpeed,
            onComplete: function() {
                this.element.toggleReady = true;
                this.element.dfGrid.saveState();
            }
        });
		this.fadeOut = new Class({});
        this.blocks = new Array();
        this.loadState();
    },
    saveState: function() {
        var a = [];
        for (i in this.blocks) {
            if (typeof this.blocks[i] == 'number') {
                a.push(i);
                a.push(this.blocks[i]);
            }
        }
        var b = [this.left.style.opacity, this.right.style.opacity, this.wrapper.resizer.to, this.wrapper.resizer.options.unit, document.body.style.fontSize, a.join(":")];
        dfCookies.create('dfgrid', b, 7);
    },
    loadState: function() {
        val = dfCookies.get('dfgrid');
        if (val) {
            val = val.split(',');
            if (val[0] == '0') {
                this.left.style.width = '0px';
                this.left.style.opacity = 0;
            }
            if (val[1] == '0') {
                this.right.style.width = '0px';
                this.right.style.opacity = 0;
            }
            var wrapperVal = parseInt(val[2], 10);
            if (!isNaN(wrapperVal)) {
                unit = val[3];
                if (unit !== '') {
                    wrapperVal = (unit == '%' && wrapperVal > 100) ? 100 : wrapperVal;
                    unit = (wrapperVal <= 100) ? '%': unit;
                    this.wrapper.resizer.to = wrapperVal;
                    this.wrapper.resizer.options.unit = unit;
                    this.wrapper.style.width = wrapperVal + unit;
                }
            }
            if (val[5] !== '') {
                blockval = val[5].split(":");
                for (i = 0; i < blockval.length; i += 2) {
                    if (blockval[i + 1] == "0") {
                        var div = $(blockval[i]);
                        this.blocks[div.id] = 0;
                        div.style.height = 0;
                        div.style.overflow = 'hidden';
                    }
                }
            }
            fontSize = parseFloat(val[4]);
            if (!isNaN(fontSize)) {
                this.resizeFont(fontSize, true);
            }
        }
    },
    initDiv: function(a) {
        a = $(a);
        a.toggleReady = true;
        a.dfGrid = this;
        if (a.id == 'maxcenter_column') {
			a.style.opacity = 0;
			a.initWidth = 1;
		}
		else {
			a.style.opacity = 1;
			a.initWidth = a.offsetWidth;
		}
        return(a);
    },
    toggleDiv: function(a) {
        if (a.toggleReady) {
            if (a.style.opacity == 1) {
				cmax = this.centermax;
				cmax.morph = new Fx.Morph(cmax.id, {
					transition: Fx.Transitions.Circ.easeInOut,
					onComplete: function() {
						this.element.toggleReady = true;
						this.element.dfGrid.saveState();
					}					
				});
				
				a.morph = new Fx.Morph(a.id, {
					transition: Fx.Transitions.Circ.easeInOut,
					onComplete: function() {
						this.element.toggleReady = true;
						this.element.dfGrid.saveState();
						
						cmax.morph.start({
							opacity:	'1'
						});
					}
				});
				
				c = $('mincenter_column');
				c.morph = new Fx.Morph(c, {
					transition: Fx.Transitions.Circ.easeInOut,
					onComplete: function() {
						this.element.toggleReady = true;
						this.element.dfGrid.saveState();
						
						a.morph.start({
							opacity:	'0',
							width:		'0px'
						});			
					}					
				});
						
				c.morph.start({
					opacity:	'0'
				});
            } 
			else {
				c = $('mincenter_column');
				c.morph = new Fx.Morph(c, {
					transition: Fx.Transitions.Circ.easeInOut,
					onComplete: function() {
						this.element.toggleReady = true;
						this.element.dfGrid.saveState();
					}					
				});
				
				a.morph = new Fx.Morph(a.id, {
					transition: Fx.Transitions.Circ.easeInOut,
					onComplete: function() {
						this.element.toggleReady = true;
						this.element.dfGrid.saveState();
						
						c.morph.start({
							opacity:	'1'
						});
					}					
				});
				
				cmax = this.centermax;
				cmax.morph = new Fx.Morph(cmax.id, {
					transition: Fx.Transitions.Circ.easeInOut,
					onComplete: function() {
						this.element.toggleReady = true;
						this.element.dfGrid.saveState();
						
						a.morph.start({
							opacity:	'1',
							width:		a.initWidth
						});
					}					
				});
				
				cmax.morph.start({
					opacity:	'0'
				});
			}
		}
	},
	toggleLeft: function() {
		this.toggleDiv(this.left);
	},
	toggleCenter: function() {
		a = this.center;
		if (!a.heightFx) {
			a.heightFx = new Fx.Slide(a, {
				duration: this.swipeSpeed,
				onComplete: function() {
					
				}
			});
		}
		this.blocks[a.id] = (a.offsetHeight > 0) ? 0 : 1;
		this.saveState();
		a.heightFx.toggle();
	},
	toggleRight: function() {
		this.toggleDiv(this.right);
	},
	resize: function(a, b) {
		if (this.wrapper.toggleReady) {
			this.wrapper.toggleReady = false;
			if (b) {
				this.wrapper.resizer.options.unit = b;
				if (b == '%') {
					initialWidth = this.wrapper.offsetWidth * 100 / document.body.offsetWidth;
				} else {
					initialWidth = this.wrapper.offsetWidth;
				}
			}
			this.wrapper.resizer.start({width: initialWidth});
		}
	},
	resizeFont: function(a, b) {
		body = document.body;
		if (b) {
			start = 0;
		} else {
			start = parseFloat(body.style.fontSize);
			start = isNaN(start) ? 0.75 : start;
		}
		body.style.fontSize = start + a + 'em';
		this.saveState();
	},
	toggleBlock: function(a) {
		a = $(a);
		if (!a.heightFx) {
			a.heightFx = new Fx.Slide(a, {
				duration: this.swipeSpeed
			});
		}
		this.blocks[a.id] = (a.offsetHeight > 0) ? 0 : 1;
		this.saveState();
		a.heightFx.toggle();
	}
});
