/* At start
=================================================*/
$(document).ready(	
	function(){
		/* Search and initialize scrolled areas*/
		window.scrolled=[];
		$('.'+IHSCROLL_INIT_SCROLL_CLASS).each(
			function(el){
				window.scrolled.push(new iHScroll(this));				
				}
			);
		
		/* Search and initialize rolling areas*/
		window.rolling=[];
		$('.rolling').each(
			function(el){
				window.rolling.push(new iRolling(this));
				}
			);
		
		
		/* Search and initialize tabs */		
		window.iTabSets=[];
		$('.tabs').each(
			function(el){
				window.iTabSets.push(new iTabSet(this));
				}
			);
		
		/* Search and initialize popup-links */
		$('a.popup').click(
			function(){
				var 
					widthClassBase='popup-width_',
					heightClassBase='popup-height_',
					params={
						url:this.href
						};
				$.each(
					this.className.split(' '),
					function(i,cl){
						if (cl.search(widthClassBase)==0){
							params.width=Number(cl.substr(widthClassBase.length));
							}
						if (cl.search(heightClassBase)==0){
							params.height=Number(cl.substr(heightClassBase.length));
							}						
						}
					);
				popUp(params);
				return false;
					  
				}
			);	
				

		}
	);

	

	
/* Horizontal scrolled areas
================================================*/
var 
	IHSCROLL_INIT_SCROLL_CLASS='to-hscrolled',
	IHSCROLL_CONTAINER_CLASS='hscrolled',
	IHSCROLL_OPTIONS_CLASS_BASE='hscroll-',
	IHSCROLL_CONTAINER_EXPAND_STATE_CLASS=IHSCROLL_CONTAINER_CLASS+'-expanded',
	IHSCROLL_WRAPPER_CLASS=IHSCROLL_CONTAINER_CLASS+'-wrap',
	IHSCROLL_BODYWRAPPER_CLASS=IHSCROLL_CONTAINER_CLASS+'-body-wrap',
	IHSCROLL_BODY_CLASS=IHSCROLL_CONTAINER_CLASS+'-body',
	IHSCROLL_SCROLL_CONTAINER_CLASS=IHSCROLL_CONTAINER_CLASS+'-scroll',
	IHSCROLL_EXPAND_CLASS=IHSCROLL_CONTAINER_CLASS+'-expand filled',
	IHSCROLL_MIN_SCROLL_HANDLER_SIZE=50,
	IHSCROLL_LISTENER_BASE_NAME='iscrollListener',
	IHSCROLL_EXPANDLINK_TITLE_EXPANDED='Свернуть',
	IHSCROLL_EXPANDLINK_TITLE_COLLAPSED='Посмотреть всё';
function iHScroll(core){	
	this.STATE_COLLAPSED=0;
	this.STATE_EXPANDED=1;
	this.EVENT_INITIALIZED='onInitialized';
	this._state=this.STATE_COLLAPSED;	
	this._inAction=false;
	this._initialized=false;
	this._options={};
	this.core=core;
	this.initialize();
	this.hasTable=false;
	this.names=[];
	
	//this.defaultScrollPosition=0;
	}
	
iHScroll.prototype={
	initialize:function(){
		var $this=this;
		

		$(this.core).wrap('<div class="'+IHSCROLL_CONTAINER_CLASS+'"></div>');
		
		var container=this.container=$(this.core).parent('.'+IHSCROLL_CONTAINER_CLASS)[0];
		
		var names=[];
		$.each(
			this.core.className.split(' '),
			function(i,el){
				if (el.substr(0,IHSCROLL_OPTIONS_CLASS_BASE.length)==IHSCROLL_OPTIONS_CLASS_BASE){
					//get user-defined options
					var optName=el.substr(IHSCROLL_OPTIONS_CLASS_BASE.length);
					if (!$this._options[optName]){
						$this._options[el.substr(IHSCROLL_OPTIONS_CLASS_BASE.length)]={
							name:optName
							};
						}
					}
				else if (el!=IHSCROLL_INIT_SCROLL_CLASS){
					//get user-defined scroll names
					names.push(el);
					$(container).addClass(IHSCROLL_CONTAINER_CLASS+'-'+el);
					}
				}
			);
		this.names=names.concat();
		$(this.core).wrap('<div class="'+IHSCROLL_WRAPPER_CLASS+'"></div>');
			var wrapper=this.wrapper=$(this.core).parent('.'+IHSCROLL_WRAPPER_CLASS)[0];
			
		$(this.core).wrap('<div class="'+IHSCROLL_BODYWRAPPER_CLASS+'"></div>');
			var bodyWrapper=this.bodyWrapper=$(this.core).parent('.'+IHSCROLL_BODYWRAPPER_CLASS)[0];
			
		$(this.core).wrap('<div class="'+IHSCROLL_BODY_CLASS+'"></div>');
			var body=this.body=$(this.core).parent('.'+IHSCROLL_BODY_CLASS)[0];
		
		
		/* Actualize body width */
		$([this.core,this.body]).width( $(this.core).children().width() ); //at pixels
		//$([this.core,this.body]).css({'width': $(this.core).children().width()/12+'em' }); //at em
		
		
		$(window).resize(function(){
			if ($this.isInitialized() && !$this.isInAction()){
				$this.coreResize()
				}
			}
		);
		
		var scrollContainer=this.scrollContainer=($('<div class="'+IHSCROLL_SCROLL_CONTAINER_CLASS+'"><span class="handler"><span class="l"><span class="r">&nbsp;</span></span></span></div>')).appendTo(wrapper)[0];
		var scrollHandler=this.scrollHandler=$('.handler',scrollContainer)[0];
			
		if (this.hasOption('fadeside')){
			$('<span class="fade"></span>').prependTo(container);	
			}
		

		/* onScroll event */				
		$(scrollHandler).css({top:1,left:1}).mousedown(
			function(e){
				if ($this.isInAction()){
					return false;
					}
				var 				
					scrollingSize=$($this.bodyWrapper).width(),
					coreSize=$($this.core).width(),
					handlerSize=$($this.scrollHandler).width(),
					minHandlerPos=1,
					maxHandlerPos=scrollingSize-1-handlerSize,
					minCoreScroll=0,
					maxCoreScroll=(coreSize-scrollingSize)+10;

				$this._inAction=true;				
				startDrag(
					e,this,this,
					{
						maxYDelta:0,
						minLeft:1,
						maxLeft:maxHandlerPos,
						onmove:function(el,left,top){
							var coreScroll=minCoreScroll+(maxCoreScroll-minCoreScroll)*(left-minHandlerPos)/(maxHandlerPos-minHandlerPos);						
							$this.bodyWrapper.scrollLeft=coreScroll;							
							},
						oncomplete:function(){
							$this._inAction=false;
							}
						}
					)
				}
			);
	
		/* OnScroll event */
		$(bodyWrapper).scroll(
			function(){
				if ($this.isInAction()){
					return;
					}
				$this.actualizeScroll();
				}
			);		

		
		this.occurEvent(this.EVENT_INITIALIZED);
		this._initialized=true;
		},

	occurEvent:function(eventName){
		var $this=this;
		$.each(
			$this.names,
			function(i,el){
				var m=IHSCROLL_LISTENER_BASE_NAME+'_'+el+(eventName?'_'+eventName:'');				
				try{					
					eval(m)($this);
					}
				catch(e){}
				}
			);
		},
	coreResize:function(){	
		/* Count base sizes */
		this.scrollingSize=$(this.bodyWrapper).width();
		this.coreSize=$(this.core).width();
		

		if (this.coreSize <= this.scrollingSize){
			this.hide();			
			return;
			}
		else{
			this.show();
			}
		this.setHandlerSize();
		this.actualizeScroll();
		},
	actualizeScroll:function(){
		var
			$this=this,
			scrollingSize=$(this.bodyWrapper).width(),
			coreSize=$(this.core).width(),
			handlerSize=$(this.scrollHandler).width(),
			minHandlerPos=1,
			maxHandlerPos=scrollingSize-1-handlerSize,
			coreScroll=$(this.bodyWrapper).scrollLeft(),
			minCoreScroll=0,
			maxCoreScroll=(coreSize-scrollingSize)+10,
			handlerPos=(minHandlerPos+(maxHandlerPos-minHandlerPos)*(coreScroll-minCoreScroll)/(maxCoreScroll-minCoreScroll)).between(minHandlerPos,maxHandlerPos);

		$(this.scrollHandler).css('left',handlerPos);
		
		},	
	setHandlerSize:function(){
		/* Count apropriate handler size */		
		var handlerSize=(this.scrollingSize*this.scrollingSize/this.coreSize).between(
			IHSCROLL_MIN_SCROLL_HANDLER_SIZE,
			this.scrollingSize
			);
		$(this.scrollHandler).width(handlerSize);
		$('span',this.scrollHandler).width(handlerSize);
		},
	kill:function(){
		$([this.container,this.wrapper,this.bodyWrapper,this.body]).removeClass();
		$([this.expandContainer,this.scrollContainer]).remove();
		},
	hide:function(){
		$(this.scrollContainer).hide();
		},
	show:function(){
		$(this.scrollContainer).show();
		},
	isInAction:function(){
		return this._inAction;
		},
	isInitialized:function(){
		return this._initialized;
		},
	hasOption:function(opt){
		return !!this._options[opt];
		}
	};
	

	

	
/* Tabs
=================================================*/
function iTabSet(container,settings){
	this.container=container;
	this.pairs={};
	this.settings={
		titlesSearchMask:'.titles>li a',
		titleActiveClass:'active',
		activeId:''
		};
	if (settings){
		this.settings=concatObjects(settings,this.settings);
		}	
	this.initialize();
	}

iTabSet.prototype.initialize=function(){
	var $this=this;
	/* get pairs */
	$(this.settings.titlesSearchMask,this.container).each(
		function(){
			if (!this.href){
				return false;
				}				
			var 
				id=this.href.split('#').pop(),
				pair={
					title:$(this),
					body:$('#'+id)
					};
			if (!id){
				return false;
				}
			$this.pairs[id]=pair;
			$(this).focus(selfBlur);
			$(this).click(
				function(){
					$this.switchTab(id);
					return false;
					}
				);
			if ($(this).parent().hasClass($this.settings.titleActiveClass)){
				$this.settings.activeId=id;
				}			
			}
		);
	$this.switchTab();
	}

iTabSet.prototype.switchTab=function(id){
	var $this=this;
	if ($this.settings.activeId && $this.settings.activeId==id){
		return false;
		}
	if (!id && $this.settings.activeId){
		id=$this.settings.activeId;
		}
	if (!id){
		return false;
		}
	for (var _id in $this.pairs){
		var pair=$this.pairs[_id];
		if (_id==id){
			pair.body.fadeIn(150);
			pair.title.parent().addClass($this.settings.titleActiveClass);
			}
		else{
			pair.body.hide();			
			pair.title.parent().removeClass($this.settings.titleActiveClass);
			}						
		}
	updateAfterEvent();
	$this.settings.activeId=id;
	$(window).resize();
	}
iTabSet.prototype.isBuilded=function(){
	return !!this.container;
	}



function buildDiagram(p){
	if (location.host && !p.params.host){
		p.params.host='http://'+location.host;
		}
	return buildFlashExt(p);
	}
	
function updateAfterEvent(){
	}	


/* Banners
=================================================*/
function buildBanner(options){
	var 
		defaultOptions={
			version:7,
			width:'100%',
			height:90,
			name:'ad-banner',
			isWrite:true
			},
		resOptions={};
	if (is_object(options) && options.src){
		resOptions=concatObjects(options,defaultOptions);
		}
	else if(is_string(options)){
		resOptions=concatObjects(defaultOptions,{src:options});
		}
	else{
		return false;
		}
	return buildFlashExt(resOptions);
	}



/* Rolling
=================================================*/

function iRolling(container){
	this.ARROWDISABLED_CLASS='arrow-disabled';
	this.container=container;
	
	this._inAction=false;
	this._interval=null;
	this._timeStep=10;
	this._defaultMoveStep=4;
	this._moveStep=this._defaultMoveStep;
	this._stepByCursor=this._defaultMoveStep;
	this.initialize();
	}
	
iRolling.prototype={
	initialize:function(){
		var 
			$this=this,			
			coreWrapper=this.coreWrapper=$('.rolling-core',this.container)[0],
			core=this.core=$('ul',coreWrapper)[0],			
			arrowLeft=this.arrowLeft=$('.arrow-left',this.container)[0],
			arrowRight=this.arrowRight=$('.arrow-right',this.container)[0];
		arrowLeft.direction=1;
		arrowRight.direction=-1;
		if (!core || !coreWrapper || !arrowLeft || !arrowRight){
			return false;
			}
		$([arrowLeft,arrowRight]).mousedown(
			function(){				
				$this.move(this.direction);
				return false;
				}
			);
		$(document).mouseup(
			function(){
				$this.stop();
				$this.actualize();
				return false;
				}
			);
		$(window).resize(
			function(){
				$this.actualize();
				}
			);
		$(this.coreWrapper)
			.mouseover(
				function(e){					
					$this.moveByCursor(e);
					}
				)
			.mousemove(
				function(e){
					$this.moveByCursor(e);
					}
				)
			.mouseout(
				function(){
					$this.stop();
					}
				)
			
			
		this.setRandomPosition();		
		},
	actualize:function(){
		var pos=this.getPosition();
		var 
			aLeft=$('span',this.arrowLeft),
			aRight=$('span',this.arrowRight),
			cl=this.ARROWDISABLED_CLASS;

		if ( ( (pos.x>=pos.maxX && this.arrowLeft.direction==-1) || (pos.x<=pos.minX && this.arrowLeft.direction==1) )){
			aRight.addClass(cl);
			}
		else if (aRight.hasClass(cl)){
			aRight.removeClass(cl);
			}
		if ((pos.x<=pos.minX &&  this.arrowLeft.direction==-1 || pos.x>=pos.maxX && this.arrowLeft.direction==1)){
			aLeft.addClass(cl);
			}
		else if (aLeft.hasClass(cl)){
			aLeft.removeClass(cl);
			}
		
		},
	moveByCursor:function(e){
		
		this.setStepByCursor(e);
		if (this.isInAction()){			
			return false;
			} 
		var 
			$this=this;			
		
		//this.stop();
		clearInterval(this._interval);
		this._interval=null;
		this._interval=setInterval(
			function(){
				//$id('status').innerHTML+='interval ';
				//$this.setStepByCursor();
				
				/* if (step<0.5){
					step=0;
					} */
				$this.step($this._stepByCursor);
				},
			$this._timeStep
			);
		$this._inAction=true;
		
		},
	setStepByCursor:function(e){
		var 
			hCenter=$(this.coreWrapper).width()/2,
			wrapperPos=$where(this.coreWrapper),
			cursorPos=getWhereIsEvent(e),
			pos={
				left:cursorPos.left-wrapperPos.left,
				top:cursorPos.top-wrapperPos.top
				},
			direction=(pos.left>hCenter?-1:1),
			step=Math.abs(pos.left-hCenter)/50;
		this._stepByCursor=direction*step;

		return this._stepByCursor;
		},
	move:function(direction){
		var $this=this;
		if (!direction){
			direction=1;
			}
		if (this.isInAction()){
			return false;
			}

		this._interval=setInterval(
			function(){
				$this.step(direction*$this._moveStep);			
				},
			$this._timeStep
			);
		$this._inAction=true;
		
		},
	step:function(x){
		var pos=this.getPosition();
		/* var 
			coreWrapperWidth=$(this.coreWrapper).width(),
			coreWidth=$(this.core).width(),
			minX=coreWrapperWidth-coreWidth,
			maxX=0,
			cur=$(this.core).css('left');			
		if (cur.slice(-2)=='px'){
			cur=Number(cur.slice(0,-2));			
			}
		else if (isNaN(Number(cur))){
			cur=0;
			} */
		$(this.core).css('left',(Number(pos.x+x)).between(pos.minX,pos.maxX));
		this.actualize();
		},
	getPosition:function(){
		var x=$(this.core).css('left');
		if (x.slice(-2)=='px'){
			x=Number(x.slice(0,-2));
			}
		else if (isNaN(Number(x))){
			x=0;
			}
		return {
			x:x,
			minX:$(this.coreWrapper).width()-$(this.core).width(),
			maxX:0
			};
		},
setRandomPosition:function(){
	var 
		minVisibleChildren=3,
		children=$(this.core).children(),
		rcNum=Math.floor(Math.random()*(children.length-minVisibleChildren)),
		rc=children[rcNum];
	$(this.core).css('left',-rc.offsetLeft);
	this.actualize();
	},							
	stop:function(){
		var $this=this;
		if (!this.isInAction()){
			return false;
			}
		clearInterval(this._interval);
		this._interval=null;
		$this._inAction=false;
		},
	isInAction:function(){
		return this._inAction;
		}
	}