var nxcTips= new Class({
	options			: {
		closeButton				: ".close",
		showHideTipTime	: 200,
		over							: false,
		scroll						: false,
		scrollbar					: ".scroll-box"
	},
	tip						: "",
	status				: false,
	mouseOnTip	: false,
	tipConteiner		: false,
	hideDelay		: 0,
	scroller				: false,
	scrollMax			: 0,
	scrollingValue	: 0,
	scrollingProcces	: false,
	showTip			: function() {
		this.status=true;
		this.tipConteiner.setStyle("display","block");
		this.tipConteiner.setPosition(this.getPosition());
	},
	setScrolling	: function() {
		this.tipConteiner.setStyles({
			'visibility'	: 'hidden',
			'display'	: 'block'
		});
		var content = this.tipConteiner.getElement( '.tip-content' );
		this.scrollingValue = content.getScrollSize().y - content.getSize().y;
		if( this.scrollingValue > 0 ) this.setScrollBar( content );
		this.tipConteiner.setStyles({
			'visibility'	: 'visible',
			'display'	: 'none'
		});
	},
	setScrollBar : function(content){
		var scrollbar = this.tipConteiner.getElement( this.options.scrollbar );
		var handle = document.id( document.body ).getElement( this.options.scroll );
		handle.addEvent('mousedown', function(){
			this.scrollingProcces=true;
		}.bind(this));
		document.id( document.body ).addEvent('mouseup', function(){
			$clear(this.scrollingProcces);
			this.scrollingProcces=false;
			if(!this.mouseOnTip) this.hideTip();
		}.bind(this));
		scrollbar.setStyle( "display", "block" );
		content.setStyle( "width", ( this.tipConteiner.getElement('.tip-content-box').getStyle("width").toInt() - scrollbar.getStyle("width").toInt() ) );
		this.scroller = new Slider(scrollbar, handle, {	
			steps: this.scrollingValue,
			mode: 'vertical',
			onChange: function(step){
				var y = step;
				content.scrollTo( 0, y );
			}
		}).set(0);
		 this.tipConteiner.getElement('.tip-content-box').addEvent('mousewheel', function(e){
			e = new Event(e).stop();
			this.scrollContent( - e.wheel * 30);			
		}.bind(this));
		if(this.tipConteiner.getElement('.scroll-up')){
			this.tipConteiner.getElement('.scroll-up').addEvent('mousedown', function(e){	e.stop	});
			this.tipConteiner.getElement('.scroll-down').addEvent('mousedown', function(e){	e.stop	});
			this.tipConteiner.getElement('.scroll-up').addEvent('mousedown', function(e){
				e.stop();
				this.scrollingProcces=this.scrollContent.periodical( 100, this, [-30] );	
			}.bind(this));
			this.tipConteiner.getElement('.scroll-down').addEvent('mousedown', function(e){
				e.stop();
				this.scrollingProcces=this.scrollContent.periodical( 100, this, [30] );
			}.bind(this));
		}
	},
	scrollContent	: function( value ){
		var step = this.scroller.step + value;
		this.scroller.set(step);	
	},
	getPosition	: function() {
		position=this.tip.getPosition();
		conSize=this.tipConteiner.getSize();
		size=this.tip.getSize();
		if (this.options.over){
			if(position.y>conSize.y)position.y=position.y-conSize.y;
			else position.y=0;
		}
		else {
			position.y=position.y+size.y;
			if (position.x>(conSize.x-size.x)/2) position.x=position.x-(conSize.x/2 - size.x/2);
			else position.x=0;
		}
		return position;
	},
	hideTip			: function() {
		if(!this.scrollingProcces){
			this.status=false;
			this.tipConteiner.setStyle("display","none");
		}
	},
	initialize		: function(tip, tipConteiner,options)
	{
		this.tip=tip;
		this.tipConteiner=tipConteiner;
		if(options.closeButton)			this.options.closeButton = options.closeButton;
		if(options.showHideTipTime)	this.options.showHideTipTime = options.showHideTipTime;
		if(options.over)						this.options.over = options.over;
		if(options.scroll)						this.options.scroll = options.scroll;
		if(options.scrollbar)					this.options.scrollbar = options.scrollbar;
		this.tipConteiner.setStyles({
														"display"	: "none",
														"position"	: "absolute",
														"float"		: "left"
													});
		this.tip.addEvent("mouseenter",function()
		{
				if(!this.status) this.showTip();
		}.bind(this));
		this.tip.addEvent("mouseleave",function()
		{
				if(!this.mouseOnTip) this.hideDelay=this.hideTip.delay(200, this);
		}.bind(this));
		this.tip.addEvent("click",function(e){ e.stop(); });
		this.tipConteiner.addEvent("mouseenter",function()
		{
			$clear(this.hideDelay);
			this.mouseOnTip=true;
			if(!this.status) this.showTip();
		}.bind(this));
		this.tipConteiner.addEvent("mouseleave",function()
		{
			this.mouseOnTip=false;
			this.hideTip();
		}.bind(this));
		if(this.options.closeButton){
			this.tipConteiner.getElement( this.options.closeButton ).addEvent("click",function(e){ 
				e.stop(); 
				this.mouseOnTip=false;
				this.hideTip();
			}.bind(this));			
		}
		if(this.options.scroll){
			this.setScrolling();
		}
	}
});


window.addEvent( 'domready', function (){
	if(document.id( document.body ).getElement( ".reg-tips" )){
		var options = {	
				closeButton			: ".close",
				showHideTipTime: 200,
				over						: false
			};
		document.id( document.body ).getElements( ".reg-tips" ).each(function(item,index){
			new nxcTips(item,document.id( document.body ).getElements( ".tips-cont" )[index], options );
		});
		var bigOptions = {	
				closeButton			: ".close",
				showHideTipTime: 200,
				over						: true,
				scroll					: ".scroll",
				scrollbar				: ".scroll-box"
			};
		document.id( document.body ).getElements( ".reg-tips-big img" ).each(function(item,index){
			new nxcTips(item,document.id( document.body ).getElements( ".reg-tips-big-cont" )[index], bigOptions );
		});
	}
});
