function LineSwitcher(id){
	this.obj = jQuery(id);
	this.init();
}

LineSwitcher.prototype.init = function() {
	var _my = this;
	
	this.l1 = jQuery(document.createElement("div"));
	this.l1.appendTo(this.obj).addClass("line");
	this.l2 = this.l1.clone().appendTo(this.obj);
	this.l3 = this.l1.clone().appendTo(this.obj);
	
	this.noDraw = false;

	this.extend = jQuery(".extend");
	
	this.phrases = new Array();
	this.extend.find(".phrase").each(function(){
		_my.phrases.push(new Phrase(this, _my));
	})
	
	this.attachEvents();
	this.draw();
};

LineSwitcher.prototype.attachEvents = function() {
	var _my = this;
	jQuery(window).resize(function(){
		_my.draw();
	})
	
	this.body = jQuery('#layout').height();
	setInterval(function(){
		_my.checkFontSize();
	}, 50)
}

LineSwitcher.prototype.checkFontSize = function() {
	var b = document.getElementById('layout').offsetHeight;
	if(b != this.body){
		this.draw();
		this.body = b;
	}
}

LineSwitcher.prototype.draw = function() {
	if(!this.noDraw){
		var _my = this;
		var _cur = this.getSelected();
		var _ofs = _cur.getOffset();

		this.extend.css({ minHeight: (_ofs.text.top+_ofs.text.height)-_my.extend.offset().top+40 });
		if($.browser.msie){
			this.extend.css({ height: _my.correctHeight((_ofs.text.top+_ofs.text.height)-_my.extend.offset().top) });
		}
		_cur = this.getSelected();
		_ofs = _cur.getOffset();

		this.l1.css({ left: 0, top: _ofs.link.top+10, width: _ofs.link.left-5});
		this.l2.css({ left: _ofs.link.left+_ofs.link.width+5, top: _ofs.link.top+10, width: _ofs.text.left-(_ofs.link.left+_ofs.link.width+5)-5});
		this.l3.css({ left: _ofs.end.left+10, top: _ofs.end.top+Math.ceil(_ofs.end.height/2), width: _my.obj.width()-_ofs.end.left-10});
	}
}

LineSwitcher.prototype.correctHeight = function(h) {
	if(h < this.extend.height()){
		return this.extend.height()
	} else {
		return h
	}
}

LineSwitcher.prototype.show = function() {
	this.noDraw = false;
//	this.draw();
	this.l1.fadeIn("fast");
	this.l2.fadeIn("fast");
	this.l3.fadeIn("fast");
}

LineSwitcher.prototype.fHide = function() {
	this.noDraw = true;
	this.l1.fadeOut("fast");
	this.l2.fadeOut("fast");
	this.l3.fadeOut("fast");

}

LineSwitcher.prototype.hidePhrase = function() {
	for (var i=0; i < this.phrases.length; i++) {
		if(this.phrases[i].selected){
			this.prev = this.phrases[i].getOffset();
			this.phrases[i].hide();
		}
	};
}

LineSwitcher.prototype.move = function() {

	clearTimeout(this._t);

	var _cur = this.getSelected();
	var _ofs = _cur.getOffset();
	var _my = this;
	
	var h = (_ofs.text.top+_ofs.text.height)-_my.extend.offset().top;

	if(h > this.extend.height()){
		
		this.noDraw = true;
		this.extend.css({ minHeight: h });
		if($.browser.msie){
			this.extend.css({ height: _my.correctHeight(h) });
		}
		this.body = document.getElementById('layout').offsetHeight;
		this.noDraw = false;
		
		_cur = this.getSelected();
		_ofs = _cur.getOffset();
		this.correct();

	} else {

		this._t = setTimeout(function(){
			_my.extend.css({ minHeight: h });
			if($.browser.msie){
				_my.extend.css({ height: _my.correctHeight(h) });
			}
		}, 500)

	}
	
	this.l1.animate({ left: 0, top: _ofs.link.top+10, width: _ofs.link.left-5 }, 500)
	this.l2.animate({ left: _ofs.link.left+_ofs.link.width+5, top: _ofs.link.top+10, width: _ofs.text.left-(_ofs.link.left+_ofs.link.width+5)-5}, 500);
	this.l3.animate({ left: _ofs.end.left+10, top: _ofs.end.top+Math.ceil(_ofs.end.height/2), width: _my.obj.width()-_ofs.end.left-10}, 500);
	
}

LineSwitcher.prototype.correct = function() {
	var _ofs = this.prev;
	var _my = this;

	this.l3.css({ left: _ofs.end.left+10, top: _ofs.end.top+Math.ceil(_ofs.end.height/2), width: _my.obj.width()-_ofs.end.left-11});
}

LineSwitcher.prototype.getSelected = function() {
	for (var i=0; i < this.phrases.length; i++) {
		if(this.phrases[i].selected){
			return this.phrases[i];
		}
	};
}




function Phrase(obj, parent){
	this.obj = jQuery(obj);
	this.parent = parent;
	this.init();
}

Phrase.prototype.init = function() {
	var _my = this;
	this.selected = false;
	if(this.obj.attr("class").indexOf("selected") != -1){
		this.selected = true;
	}
	this._link = this.obj.find(".link")
	this._link.click(function(){
		if(!_my.selected){
			_my.parent.hidePhrase();
			_my.show();
			_my.parent.move();
		}
	})
}


Phrase.prototype.hide = function() {
	var _my = this;
	this.obj.find(".in").css({ opacity: 1 }).animate({ opacity: 0 }, 500, function(){
		jQuery(this).hide();
		_my.obj.removeClass("selected");
	});
	this.selected = false;
}

Phrase.prototype.show = function() {
	var _my = this;
	this.obj.find(".in").css({ opacity: 0 }).show();
	this.obj.addClass("selected");
	this.selected = true;
	setTimeout(function(){ _my.obj.find(".in").animate({ opacity: 1 }, 500); }, 300);
}

Phrase.prototype.getOffset = function(){
	var _my = this;
	
	var _d = this._link.offset();
	var _o = { left: _d.left, top: _d.top, width: _my._link.width() };
	
	var _in = this.obj.find(".in");
	_d = _in.offset();
	
	var _t = { left: _d.left, top: _d.top, width: _in.width(), height: _in.height() }

	var _end = _in.find(".end");
	
	var _e = _end.offset();
	_e = { left: _e.left, top: _e.top, height: _end.height() }
	
	return { link: _o, text: _t, end: _e };
}

