var gallery={
	data: {
		articleId: 0		//current article id
	},


	mouseScroller: {
		a: null,				//acceleration
		coords: null,			//coordinates (left top corner) of sensitive zones
		interval: 25, 			//interval for periodical scroll check 
		k: null,				//coefficient (between -1 and 1)
		step: 50, 				//step of scroll animation
		zones: {w: 123,h: 320} 	//dimensions of sensitive zones
	},

	init: function(){
		if (hiddenoOverflow==0)
		{
			//inititalize mouse events
			U.addEvent(document,'mousemove',gallery.clientXY);
			//set interval for periodical scroll check 
			setInterval('gallery.contentBlockScroll()',gallery.mouseScroller.interval);
		}
		else
		{
			$('#arrowLeft').hide();
			$('#arrowRight').hide();		
		}

		//change step parameter for Chrome
		if(BrowserDetect.browser=='Chrome')
		{
			//gallery.mouseScroller.step = 150;
			gallery.mouseScroller.interval = 25;
		}
		
		$(window).bind('resize', function() {
				gallery.redrawBlocks();
			}
		);
		
		//add event for window resizing
		//U.addEvent(window,'resize',gallery.redrawBlocks);
		gallery.redrawBlocks();

	
	},



	clientXY: function(evt){
		//define mouse coordinates
		var cx=evt.clientX;
		var cy=evt.clientY;
		var msZonesCoords=gallery.mouseScroller.coords;
		//check mouse position
		gallery.mouseScroller.k=gallery.checkMouseInZone(cx,cy,msZonesCoords);
	},

	contentBlockScroll: function(){
		var bak_a=gallery.mouseScroller.a;
		var new_a=gallery.mouseScroller.k;
		gallery.mouseScroller.a=(bak_a-new_a)*.7+new_a;
		if(Math.abs(gallery.mouseScroller.a)<0.01){gallery.mouseScroller.a=0;}
		if(gallery.mouseScroller.a){
			//scroll contentBlock
			gallery.doContentBlockScroll();
		}
		//show or hide scrolling arrows
		if( BrowserDetect.browser!='Explorer'){
			gallery.showScrollArrow();
		}
	},

	//-----------------------------------------------------------------

	doContentBlockScroll: function(){
		var cb=U.gbi('contentBlock');
		var bak_x=cb.scrollLeft;
		var new_x=bak_x + gallery.mouseScroller.a*gallery.mouseScroller.step;
		cb.scrollLeft=new_x;
	},

	showScrollArrow: function(){
		//hide or show arrow
		if(gallery.mouseScroller.a>0){

			$('#arrowLeft').hide();
			$('#arrowRight').show();
		}else if(gallery.mouseScroller.a<0){
			$('#arrowLeft').show();
			$('#arrowRight').hide();
		}else{
			$('#arrowLeft').hide();
			$('#arrowRight').hide();
		}
	},





	redrawBlocks: function(){
		$('#contentBlock').ready(function() {
				gallery.redraw={
				menuHeight: U.gbi('tags').offsetHeight+170,
				contentHeight: 470,
				lineHeight:390,
				logoHeight:52,
				contentBlockHeight: 390,
				num: 0
			};
			//set body height
			if( !gallery.setBodyHeight() ){
				if( !gallery.tryPlaceCbDown() ){
					gallery.placeMinimalCb()
				}
			}
			
			$('#contentBlock').fadeIn("fast");			
			if (hiddenoOverflow==0)
			{
				gallery.setMouseScrollZonesCoords();
			}		
			
			gallery.redraw=null;
		});
	},
	
	setBodyHeight: function(){
		var htmlHeight=(U.htmlTag().clientHeight || (U.htmlTag().offsetHeight-4));
		
		//if(htmlHeight > (gallery.redraw.menuHeight*2 + gallery.redraw.contentBlockHeight)){
			var top=parseInt((htmlHeight - gallery.redraw.menuHeight - gallery.redraw.contentHeight)*0.4+gallery.redraw.menuHeight);
			top = 213;
			var height=(htmlHeight - gallery.redraw.menuHeight);
			$('#contentBlock').css({height: height+'px'});
			return true;
		//}else{
		//	return false;
		//}
	},

	tryPlaceCbDown: function(){//пытаемся разместить contentBLock под menuBlock, чтобы не было прокрутки
		var htmlHeight=(U.htmlTag().clientHeight || (U.htmlTag().offsetHeight-4))
		if(
			 htmlHeight > gallery.redraw.menuHeight + gallery.redraw.contentBlockHeight
			 || gallery.redraw.num>40
		){
			//place Content Block just right after Menu Block
			var top=gallery.redraw.menuHeight;
			var bodyHeight=(U.bodyTag().offsetHeight>htmlHeight)
				?U.bodyTag().offsetHeight
				:htmlHeight;
			var height=(bodyHeight - gallery.redraw.menuHeight);

			$('#contentBlock').css({height: height+'px',top: top+'px'});
			
			var lineBottom = top + (gallery.redraw.contentHeight-gallery.redraw.lineHeight)/2+gallery.redraw.lineHeight;
			return true;
		}else{
			gallery.redraw.num++;
			return false;
		}
	},

	placeMinimalCb: function(){

		var top=gallery.redraw.menuHeight;
		var height=gallery.redraw.contentBlockHeight;
		$('#contentBlock').css({height: height+5+'px',top: top+'px'});
		if(window.opera){U.bodyTag().style.height=top+height+'px';}
	},


	setMouseScrollZonesCoords: function(){
		//define #contentBlock position
		//top position of gallery bar
		var barTop=U.layerY('galleryBar');

		//height of bar
		var barHeight=340;
		//width of content block
		var contentBlockWidth=U.gbi('contentBlock').offsetWidth;
		//top MouseScroll
		var coordsTop= barTop;// + (barHeight/2) - (gallery.mouseScroller.zones.h/2);
		gallery.mouseScroller.coords={
			left: [0, coordsTop],
			right: [(contentBlockWidth - gallery.mouseScroller.zones.w), coordsTop]
		}
		$('#arrowLeft').css({left: gallery.mouseScroller.coords.left[0]+ 'px',top:gallery.mouseScroller.coords.left[1]+ 'px'});
		$('#arrowRight').css({left: gallery.mouseScroller.coords.right[0] + 'px',top:gallery.mouseScroller.coords.right[1]+ 'px'});
	},

	checkMouseInZone: function(cx,cy,msZonesCoords){
		if(true
			&& msZonesCoords
			&& cy <= (msZonesCoords.left[1] + gallery.mouseScroller.zones.h)
			&& msZonesCoords.left[1] <= cy
			&& cx <= (msZonesCoords.left[0] + gallery.mouseScroller.zones.w)
			&& msZonesCoords.left[0] <= cx
		){
			var a= (gallery.mouseScroller.zones.w + msZonesCoords.left[0] - cx) / gallery.mouseScroller.zones.w;
			a=a*a*-1;
			return a;
		}
		if(true
			&& msZonesCoords
			&& msZonesCoords.right[1] <= cy
			&& cy <= (msZonesCoords.right[1] + gallery.mouseScroller.zones.h)
			&& msZonesCoords.right[0] <= cx
			&& cx <= (msZonesCoords.right[0] + gallery.mouseScroller.zones.w)
		){
			var a= (cx - msZonesCoords.right[0]) / gallery.mouseScroller.zones.w;
			a=a*a;
			return a;
		}
		return 0;
	}
}
