var nxcTips= new Class({
	options			: {
		closeButton				: ".close",
		showHideTipTime	: 200,
		over							: false,
		alwaysHideOnleave: false,
		scroll						: false,
		scrollbar					: ".scroll-box",
    rootEl : '#content',
    floatInfoBlockRight : false,
    closeOnClick : false,
    showInRightCorner : false
	},
	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' );
		var inner_content = this.tipConteiner.getElement( '.tip-content-inner' );
		if( content.getScrollSize().y - content.getSize().y > 0 ) this.setScrollBar( content );
		this.tipConteiner.setStyles({
			'visibility'	: 'visible',
			'display'	: 'none'
		});
	},
	setScrollBar : function(content){
		var scrollbar = this.tipConteiner.getElement( this.options.scrollbar );
		var handle = this.tipConteiner.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.scrollingValue = content.getScrollSize().y - content.getSize().y;
		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.showInRightCorner ) {
        position = this.tip.getPosition( document.getElement( this.options.rootEl ) );
        position.y = position.y - conSize.y;
        position.x = position.x;
				if( this.options.floatInfoBlockRight ) {
        		position.x = position.x - conSize.x + size.x + 3;
				}
    }
		else 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.alwaysHideOnleave)	  this.options.alwaysHideOnleave = options.alwaysHideOnleave;
		if(options.over)						    this.options.over = options.over;
		if(options.scroll)						  this.options.scroll = options.scroll;
		if(options.scrollbar)					  this.options.scrollbar = options.scrollbar;
    if(options.closeOnClick)			  this.options.closeOnClick = options.closeOnClick;
    if(options.showInRightCorner)   this.options.showInRightCorner = options.showInRightCorner;
    if(options.rootEl)	            this.options.rootEl = options.rootEl;
    if(options.floatInfoBlockRight)	this.options.floatInfoBlockRight = options.floatInfoBlockRight;
        
		this.tipConteiner.setStyles({
														"display"	: "none",
														"position"	: "absolute",
														"float"		: "left"
													});

		if( this.options.floatInfoBlockRight ) {
      this.tipConteiner.addClass( 'right-position' );
		}

		this.tip.addEvent("mouseenter",function()
		{
				if(!this.status) this.showTip();
		}.bind(this));
		this.tip.addEvent("mouseleave",function()
		{
			if((!this.mouseOnTip || this.options.alwaysHideOnleave) && (!this.options.closeOnClick)){
				this.hideDelay=this.hideTip.delay(200, this);
			}
		}.bind(this));
		this.tip.addEvent("click",function(e){ e.stop(); });
		this.tipConteiner.addEvent("mouseenter",function()
		{
			if(!this.options.alwaysHideOnleave && !this.options.closeOnClick){
				$clear(this.hideDelay);
				this.mouseOnTip=true;
				if(!this.status) this.showTip();
			}
		}.bind(this));
		this.tipConteiner.addEvent("mouseleave",function()
		{
		  if (!this.options.closeOnClick)
            {
                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 );
		});
	}
});

