/**
 * Basic AJAX-class for the AJAX-frontend
 *
 * @author maj <martin.jahn@communardo.de>
 * @since 2007-07-10
 * @access public
 */
var SidebarBox = new Class({
	options: {
	},
	
	initialize: function(sysid, options) {

		this.setOptions(this.options, options);

		this.status = '';
		this.sysid = sysid;
		this.iframe = true;
		if(this.options.overlay.active) {
	 		if(this.options.overlay.top) new Element('div', {'id': 'overlay1', 'class': 'overlay'}).setOpacity('0').injectTop('page_margins');
	 		if(this.options.overlay.left) new Element('div', {'id': 'overlay2', 'class': 'overlay'}).setOpacity('0').injectTop('page_margins');
	 		if(this.options.overlay.bottom) new Element('div', {'id': 'overlay3', 'class': 'overlay'}).setOpacity('0').injectTop('page_margins');
	 		if(this.options.overlay.right) new Element('div', {'id': 'overlay4', 'class': 'overlay'}).setOpacity('0').injectTop('page_margins');
		}
		if($('switch_contextmenu_link')) {
			this.button = $('switch_contextmenu_link');
			this.button.removeProperty('href', '');
  
  			if(this.options.hover) {
				$('col3').addEvent('mouseover', function () {this.showSidebar();}.bind(this));
				$('col3').addEvent('mouseout', function () {this.hideSidebar();}.bind(this));
			}

			$('col3_switch').addEvent('click', function () {this.switchSidebar();}.bind(this));
			$('col3').setOpacity('1');
			if($('col3_content') && $('col2')) {
				sizes2 = $('col2_content').getSize();
				sizes3 = $('col3_content').getSize();
				height2 = sizes2.size.y;
				height3 = sizes3.size.y;
				if(height3 > height2) {
					$('col2_content').setStyle('height', height3);
				}
			}
		}
	},
	showSidebar: function(event) {
		if(this.status == 'close' || this.status == '') {
			this.showOverlay();
			this.status = 'close';
		}
	},
	hideSidebar: function(event) {
		if(this.status == 'close') {
			this.hideOverlay();
			this.close();
			this.status = 'close';
		}
	},
	switchSidebar: function(event) {

		this.hideOverlay();

		if(this.status == 'close') {
			this.maximizebox();
		} else {
			bodyClass = document.getElementsByTagName('body')[0].className;
			if(bodyClass == 'body12' || bodyClass == 'body2') {
				this.maximizebox();
			} else {
				this.closebox();
			}
		}
	},
	closebox: function(event) {
		this.close();
		userProfile.updateServer(this.sysid);
		this.hideOverlay();
	},
	minimizebox: function(event) {
		this.maximizebox();
	},
	maximizebox: function(event) {
		this.maximize();
		userProfile.updateServer(this.sysid);
		this.showOverlay();
	},
	showOverlay: function() {
			col3_pos = $('col3_content').getPosition();
			col3_size = $('col3_content').getSize();
			border_width = 20;
			
 			if(this.options.overlay.active) {
 				if(this.options.overlay.top) {
 					$('overlay1').setStyles({'left': col3_pos.x - border_width, 'top': col3_pos.y, 'height': border_width - 14, 'width': col3_size.size.x + 2 * border_width });
		 			$('overlay1').setOpacity(this.options.overlay.opacity);
 				}
				if(this.options.overlay.left) {
					$('overlay2').setStyles({'left': col3_pos.x - border_width, 'top': col3_pos.y, 'height': col3_size.size.y + border_width, 'width': border_width });
		 			$('overlay2').setOpacity(this.options.overlay.opacity);
				}
				if(this.options.overlay.bottom) {
					$('overlay3').setStyles({'left': col3_pos.x, 'top': col3_pos.y + col3_size.size.y, 'height': border_width, 'width': col3_size.size.x});
		 			$('overlay3').setOpacity(this.options.overlay.opacity);
				}
 				if(this.options.overlay.right) {
 					$('overlay4').setStyles({'left': col3_pos.x + col3_size.size.x, 'top': col3_pos.y, 'height':  col3_size.size.y + border_width, 'width': border_width });
		 			$('overlay4').setOpacity(this.options.overlay.opacity);
 				}
			}
			
			$('col3').addClass('hover');
	},
	hideOverlay: function() {
		if(this.options.overlay.active) {
 			if(this.options.overlay.top) {
  				$('overlay1').setOpacity('0');
 			}
			if(this.options.overlay.left) {
 				$('overlay2').setOpacity('0');
			}
			if(this.options.overlay.bottom) {
 				$('overlay3').setOpacity('0');
			}
 			if(this.options.overlay.right) {
  				$('overlay4').setOpacity('0');
			}
		}
		$('col3').removeClass('hover');
	},
	close: function() {
		var bodyClass = document.getElementsByTagName('body')[0].className;
		switch (bodyClass) {
			case 'body23':
			case 'body2':
				newClass = 'body2';
				break;
			case 'body12':
			case 'body123':
			default:
				newClass = 'body12';
				break;
		}
		if(this.button) {
			this.button.removeClass('active');
			this.button.title = this.button.title.replace('zu', 'auf');
		}
		document.getElementsByTagName('body')[0].className = newClass;
		this.status = 'close';
	},
	minimize: function() {
		this.maximize();
	},
	maximize: function() {
		var bodyClass = document.getElementsByTagName('body')[0].className;
		switch (bodyClass) {
			case 'body2':
			case 'body23':
				newClass = 'body23';
				break;
			case 'body12':
			case 'body123':
			default:
				newClass = 'body123';
				break;
		}
		if(this.button) {
			this.button.addClass('active');
			this.button.title = this.button.title.replace('auf', 'zu');
		}
		document.getElementsByTagName('body')[0].className = newClass;

		if($('col2')) {
			sizes2 = $('col2_content').getSize();
			sizes3 = $('col3_content').getSize();
			height2 = sizes2.size.y;
			height3 = sizes3.size.y;
			if(document.all && $('col3_frame')) {
				$('col3_frame').setStyle('height', height3);
			}
			if(height3 > height2) {
				$('col2_content').setStyle('height', height3);
			}
		}


		this.status = 'max';
	},
	reload: function() {
		this.load();
	},
	load: function () {
		this.ajax = new Ajax (this.url, {
			onComplete: function (responseText, responseXML) {
				}.bind(this),
			onFailure: function () {
				}.bind(this),
			method: 'get'
			}).request();
	},
	store: function() {
		this.ajax = new Ajax (this.url, {
			onComplete: function (responseText, responseXML) {
				}.bind(this),
			onFailure: function () {
				}.bind(this),
			method: 'get'
			}).request();
	}
});
SidebarBox.implement(new Options);
