function topadvert_request_handler(id,feed,params){
	for( var i = 0; i < Math.min(params['limit'],topadvert_feed['advs'].length); i++ ){
		var adv = topadvert_feed['advs'][i];
		var item = adv.items[0];
	//	log(adv['name']);
	//		document.'<div style="margin-top: 4px"><a class="topadvert_link" target="_blank" href="' + item['url'] + '">«' + adv['name'] + '» &ndash; ' + item['cost_str'] + '</a></div>';
	} 
}

/*log*/
function log(a,a1,a2,a3,a4,a5,a6,a7) {
 window.console && window.console.log && window.console.log(a,a1,a2,a3,a4,a5,a6,a7);
}
var sgO;
$.browser.msie6 = $.browser.msie && /MSIE 6\.0/i.test(window.navigator.userAgent) && !/MSIE 7\.0/i.test(window.navigator.userAgent);
/*
function initOnLoad(sElementName,loadFunction) {  
	var oElement = (sElementName == "body") ? document[sElementName] : 
		document.getElementById(sElementName); 
	if(oElement != null && typeof(oElement) != "undefined") { loadFunction(oElement); } 
	else { setTimeout(function() { initOnLoad(sElementName,loadFunction); }, 0); } 
}
function trJs(o){
	o.className += ' js';

	for ( ii =1; ii < parseInt(o.innerHTML,10) + 1 ; ii++){
		initOnLoad("subgall_" + ii,trJsSingle);
	}

}
*/
var Alter;
var boockGall;

jQuery(document).ready(function() {
sgO =  $.browser.msie6 ? $('#iefixed') :$(document);
initMenuinfoblock()

initBoockScroll()

boockGall = $('.boockgall .b_scroller')


	$('#newsboock').PageredBoock()

	$('body').addClass('js')

	if($.browser.msie){
		$('.menuwr:first').width($('#menu0').width() + 9)	
	}

	$('.pages:first .i_back').hover(function(){
		$(this).addClass('i_back_hover')
	},function(){
		$(this).removeClass('i_back_hover')
	})
	$('.pages:first .i_next').hover(function(){
		$(this).addClass('i_next_hover')
	},function(){
		$(this).removeClass('i_next_hover')
	})
	
	initMops()
	initIeFtFix()

	var bgfon = $('#bgfon');
	var bgfonImg = $('#bgfon').find('img:first');
	bgfonImg[0].setter = bgfonImg.parent()
	imgFon = new Image();
	
	imgFon.onload = function() {	
	//	bgfonImg[0].src = imgFon.src;

		$(window).bind('resize',function(){
			clearTimeout(window.int0)
			window.int0 = setImageFon(bgfonImg,imgFon)
		//	initSubPage()
		})	

		initImageFon(bgfonImg,imgFon)	
	}
	imgFon.src = bgfonImg[0].src;	

/*menu*/
$('.menulist:first').each(function(){
	$(this).removeClass('simphover')

	if(!$.browser.msie){
		$(this).find('li:not(.active) a').hover(menuHover,menuOut)
	}else{
		$(this).find('li:not(.active) a').hover(menuHoverIe,menuOutIe)
	}

	function menuHoverIe(){
		$(this).addClass('hover')
	}
	function menuOutIe(){
		$(this).removeClass('hover')
	}

	function menuHover(){
		this.hover = true;
		if(!this.lock){
			this.lock = true;
			var af = this;
			
			$(this).addClass('hover')
			$('.iwr',this).animate({top:0},400,function(){
				af.lock = false;
				$(af).css({zoom:1})
				if(!af.hover){
					$(af).removeClass('hover')
					$(this).css({top:36})
				}
			})
		}
	}
	function menuOut(){
		this.hover = false;
		if(!this.lock){
			var af = this;
			this.lock = true;
			
			$(this).removeClass('hover')
			$('.iwr',this).animate({top:36},100,function(){
				af.lock = false;
				if(af.hover){
					$(af).addClass('hover')
					$(this).css({top:0})
				}
			})		
		}
	}
	
})




if($.browser.msie && $('.games_pl span').length){
	$('.games_pl span').textShadow('3px 4px 26px #000000')
}

/*b test*/
	var tmBoock = $('.boocks li')
	
/*	for(ii=0 ; ii < 30; ii++){

		$('.boocks').append(tmBoock.clone())
	}*/

	$('.boocks li').find('a').bind('click',function(){return false})

/*boocks*/
var gLo;
$('.boocks:first').each(function(){

		initAlter()		
		gLo = this;
		
		$(this).find('li').unbind('mouseout').hover(boockHover2,function(e){boockOut2(this,e)})

	function boockHover2(){

		gLo.active = this
		this.hover = true
		if(!boockGall[0].shown){

				$(this).addClass('hover s_hover')

				Alter[0].sub = this
				sowAlter(this)
		}
	}

	
})
	function boockOut2(o,e){

			if(Alter[0].vis){			
				boockHide(o)
				hideAlter()				
			}else{
				boockHide(o)
			}
	
		function	boockHide(oo){
			oo.hover = false;
				$(oo).removeClass('hover s_hover')
		}
	}

	
function initAlter(){
	Alter = $('<div id="libalter" style="display:none;"><div class="libwr"><div class="shadowed gl-wdt">\
									<div class="c">\
									</div><i class="t tl"></i><i class="t tr"></i><i class="b bl"></i><i class="b br"></i><i class="b arr"><i></i></i><i class="sc"></i>\
							</div></div></div>')
	
	$('body').append(Alter)
	Alter[0].sub = false;
	Alter.bind('mouseover',function(){
		this.over = true;
		hideAlter()
	}).bind('mouseout',function(){
		this.over = false;
	//	boockOut(this.sub,false)
		hideAlter()
	}).bind('click',function(){
		$.facebox({ ajax: Alter[0].aObj[0].href }, 'popup_slim');
	})
}
function sowAlter(liO){

	setTimeout(function() {

		if(Alter[0].sub == liO && liO.hover){
			Alter[0].aObj = $('a.sc:first',liO)
			$('.c:first',Alter).html('<h3>&laquo;' + $('i',Alter[0].aObj).html() + '&raquo;</h3><p>' + $('.hidden:first',liO).html() + '</p>')
			Alter.css({visibility:'hidden'}).show()
			
			var vals = getCurOffset(liO);
		
			liO.subalter = vals;
			
			Alter[0].vis = true		
			
			Alter.css({
				top:vals.top,
				left:vals.left,
				visibility:'visible'
			}).show()
			$('.arr',Alter).css({marginLeft:vals.arr})
		}
	}, 400);
}

function getCurOffset(liO){

	var arr,top,left,
	setter = $('.shadowed',Alter),
	liOffset = $(liO).offset(),
	tmSize = {w:setter.width(),h:setter.height()},
	bodyWidth = document.documentElement.clientWidth || document.body.clientWidth
	
	tmOffset = {
		left: liOffset.left - 50,
		top: liOffset.top + 124
	};

	if(tmOffset.left + tmSize.w + 10 > bodyWidth){
		left = bodyWidth - (tmSize.w + 10)
		arr = liOffset.left - left -73;
		arr = arr > tmSize.w -105 ? 999 : arr;
	}else if(tmOffset.left < 10){
		left = 10;
		arr = liOffset.left - left + -55;
		arr = arr < -70 ? - 999 : arr;
	}else{
		left  = tmOffset.left;
		arr = 'auto';
	}

	return {top:tmOffset.top,left:left,arr:arr,w:tmSize.w,h:tmSize.h}
}



	

});
function hideAlter(o,liO){
	setTimeout(function() {
		if(!Alter[0].over){
			Alter[0].vis = false;	
			Alter.hide()
		
		setTimeout(function() {		
			if(!Alter[0].sub){
				Alter[0].sub = false;
			}
		}, 10);
		
		}
	}, 10);	

}

function initIeFtFix(){

	var ft = $('#ft')
	var Page = $('#iepage')

	var forumSect
	
	if($.browser.msie6){

		forumSect = $('body#wrap').size() ? {0:$('.rside:first'),1:$('#ballign')} : false;
		
		$('#outwrap').append(ft)
		initIeFt()
		$(window).bind('resize',function(){
					clearTimeout(window.int1)
					window.int1 = setTimeout(function() {
						initIeFt()
					 }, 0);
		})	
	}
	function initIeFt(){

		var tmW = (document.documentElement.clientWidth || document.body.clientWidth) - 17;
		var tmMb = Page.width() < (document.documentElement.clientWidth || document.body.clientWidth) ? '-30px' : '-47px'; 
		ft.width(tmW).css({marginTop:tmMb})
//	log(forumSect,'forumSect')
		if(forumSect){
			forumSect[0].css({zoom:0})
			forumSect[0].css({zoom:1})
			Page.height(forumSect[1].height()+forumSect[1].offset().top)
			//Page.height()
		}
	}
}

function initMops(){
	var mopsDiv = $('#fttoper').prepend('<i id="mopsheighter"></i>')
	var mopsWr = $('.toperwr',mopsDiv)
	var topHeighter = $('#main_logo')
	var Page = $('#outline')
	var mops = $('#mops').bind('click',mopsClick)
	var mopsHeighter = $('#mopsheighter',mopsDiv)

	mopsHeighter[0].marg = parseInt(mopsHeighter.css('marginTop'),10)
	
	$('#ft .mdside:first').prepend(mops.clone(true))


	$(window).bind('resize',function(){
		if(mopsDiv[0].open){
		
			clearTimeout(window.int2)
			window.int2 = setTimeout(function() {
				initMopsHeight()
			 }, 0);
		}
	})

	function mopsClick(){
		
		if(!mopsDiv[0].open && !mopsDiv[0].lock){
			mopsDiv[0].lock = true;
			mopsDiv[0].open = true;
			mopsDiv[0].close = false;
			
			showMops(mopsDiv)
			initMopsHeight()
		
		
		}else if(!mopsDiv[0].close && !mopsDiv[0].lock){
			mopsDiv[0].lock = true;
			mopsDiv[0].close = true;
			mopsDiv[0].open = false;
			
			hideMops(mopsDiv)
		}
	}

	function showMops(mDiv){
		mDiv.css({marginLeft:0})
		setTimeout(function() {
			mopsHeighter.animate({height:0},300,function(){
				mDiv[0].lock = false;
			});
		 }, 0);

		
	}
	function hideMops(mDiv){
		setTimeout(function() {	
			mopsHeighter.animate({height:mDiv.height()},300,function(){
				mDiv.css({marginLeft:'-999em'})
				mDiv[0].lock = false;
			});
		 }, 0);
	}
		
	function initMopsHeight(){

		var tmH = topHeighter.length ? $(window).height() - ( topHeighter.offset().top - $('#iepage').offset().top + topHeighter.height() +45 ) : $(window).height() - ( $('#main_logo_forum').offset().top - $('#iepage').offset().top + $('#main_logo_forum').height());
		var mopscontent = $('#mopscontent')
		var mopsmap = $('#mopsmap')
		
		var tmW,tmMb
		if($.browser.msie6){
			if(Page.width() < (document.documentElement.clientWidth || document.body.clientWidth)){
				tmW = Page.width()
				tmMb = ''
			}else{
				tmW = (document.documentElement.clientWidth || document.body.clientWidth) - 17;
				tmMb = '17px'
			}
	
		}
		mopsDiv.css({
			marginTop: '-' + tmH,
			marginBottom: tmMb
		}).height(tmH).width(tmW)
		mopsWr.height(tmH - mopsHeighter[0].marg)

		mopscontent.width((document.documentElement.clientWidth || document.body.clientWidth)-50).height(tmH - 40)	
		
		var tmmH = mopscontent.height() - mopsmap.height()
		
		mopsmap.css({
			marginTop: tmmH > 0 ? ( mopscontent.height() - mopsmap.height() ) / 2 : 0
			})
	}
}

function initImageFon(fon,imgF){
	setImageFon(fon,imgF)
}
function setImageFonWidth(fon,imgF){
	var tmPage = getPageSize()

	var tmHeight =fon.height() / fon.width() * tmPage[2]
	var tmWidth = fon.width()/fon.height()*tmPage[3]
//console.log(tmHeight,tmPage)
	if(tmHeight > tmPage[3]){
		fon[0].actal = 0;
	
		fon.css({
			height:'auto',
			width:fon.parent().width()
		})
	}else if(tmHeight < tmPage[3]){
		fon[0].actal = 1;
		fon.css({
			height:fon.parent().height(),
			width:'auto'
		})
	}
}



function setImageFon(fon,imgF){
	var tmPage = getPageSize()

	var tmHeight =fon.height() / fon.width() * tmPage[2]
	var tmWidth = fon.width()/fon.height()*tmPage[3]

	if(tmHeight > tmPage[3] && fon[0].actal != 0){
		fon[0].actal = 0;
		fon.css({
			height:'auto',
			width:'100%'
		})
	}else if(tmHeight < tmPage[3] && fon[0].actal != 1){
		fon[0].actal = 1;
		fon.css({
			height:'100%',
			width:'auto'
		});
	}
}
	
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}


$.fn.PageredBoock = function(options) {
	  var settings = {
				item: '.cnt',
				galleredLeft: '.lp',
				galleredRight: '.rp',
				speed:300,
				//hrefUpp : '&from=',
				pagerO : '.pager:first',
				pagerLeft : '.leftside:first',
				pagerRight : '.rightside:first',
				page : $('<li><a title="" href=""><b></b><i></i><ins></ins></a></li>'),
				activePage : $('<li class="active"><span><b></b><i></i><ins></ins></span></li>'),
				activePageNum : 0,
		//		pager : '<div class="pages"><div class="pager"></div></div> ',
				pDott : $('<li class="dotted"><span><b>&nbsp;...&nbsp;</b><i></i><ins></ins></span></li>'),
				dottLimit : 3
		};
	   settings = $.extend(settings, options);

 	var main = $(this);
	if( main.size() == 0 ) {

		return;
	}
	var gLeft = $(settings.galleredLeft, this),
		gRight = $(settings.galleredRight, this)
		pagerObj = $(settings.pagerO),
		pagerLeft = $(settings.pagerLeft,pagerObj),
		pagerRight = $(settings.pagerRight,pagerObj)

		gLeft.pages = $(settings.item,gLeft)
		gRight.pages = $(settings.item,gRight)

		var totCount = gLeft.pages.length + gRight.pages.length
		totCount = totCount%2 !=0 ? totCount+1 : totCount;

		setPages(settings.activePageNum)

		function setPages(num){
			snum = num*2
			var to
	
			pagerLeft.empty()
			pagerRight.empty()

			var links = setLeftPager()
			links = links.add(setRightPager())

			gLeft.pages.hide().filter(':eq(' + num + ')').show()
			gRight.pages.hide().filter(':eq(' + num + ')').show()
			
			$('a',links).bind('click',initHref)
			
			function setLeftPager(){
				var tO
				var dotted
				for(ii=totCount - 1;ii> snum;ii--){

					if(totCount - 1 < settings.dottLimit || ( ii <  settings.dottLimit + snum + 1 || ii > totCount - 3 ) ){	
						if(ii == snum + 1){
							to = settings.activePage.clone()
						}else{
							to = settings.page.clone().find('a').attr('href',location.href).end()
						}
						to = to.find('b').html(ii+1).end()
						to[0].num = Math.ceil((ii - 1)/2);
						tO = tO? tO.add(to) : to
					}else if(!dotted){
						dotted = true
						to = settings.pDott.clone()
						tO = tO? tO.add(to) : to							
					}
				}
				pagerRight.append(tO)
				return tO
			}	
			function setRightPager(){
				var tO;
				var dotted
				
				for(ii=0;ii<snum + 1;ii++){
					if(snum - 1 < settings.dottLimit || ( ii <  2 || ii > snum - settings.dottLimit ) ){
						if(ii == snum){
							to = settings.activePage.clone()
						}else{
							to = settings.page.clone().find('a').attr('href',location.href).end()
						}
						to = to.find('b').html(ii+1).end()
						
						to[0].num = Math.ceil((ii - 1)/2);
						tO = tO? tO.add(to) : to				
					}else if(!dotted){
						dotted = true
						to = settings.pDott.clone()
						tO = tO? tO.add(to) : to							
					}
				}
				pagerLeft.append(tO)
				return tO
			}	
			
		}

	function initHref(){
		setPages($(this).parents('li')[0].num)
		return false
	}
}

function initBoockScroll(){

$('.boock .bk3,.boock .bk').bind('scroll',function(){
	
	initSubB(this)

	return false;
})

	function initSubB(o){

//log($.browser.webkit,$('body').scrollTop(),$('html').scrollTop(),$(document).scrollTop())	
		if(!o.scrolled){
			sgO.scrollTop(sgO.scrollTop()+o.scrollTop);
			o.scrollTop = 0;
			o.scrolled = true;
		}else{
			o.scrolled = false
		}
	}

}

function initMenuinfoblock(){
	var $menuInfoBlock = $('#menuinfoblock')
	var $bd = $('body');
	var $hasClass1 = $bd.hasClass('pug');
	var $hasClass2 = $bd.hasClass('games');
	var $hasClass3 = $bd.hasClass('garden');
	if($menuInfoBlock.size()> 0 && !$bd.hasClass('.section') && ($hasClass1 || $hasClass2 || $hasClass3)){
		setTimeout(function() { setInfoBlock($menuInfoBlock) }, 0); 
		
	}
	function setInfoBlock($o){
		var scT = sgO.scrollTop()
		var $ch = $o.children().eq(0).height()
		var tmTop = $bd.height() + scT- $ch - 0;
		var $tmPrev = $o.prev().prev().children().eq(0);

		if($o.offset().top > tmTop && ( $bd.height() < 550  )){

			tmTop = tmTop > 300 ? tmTop : 300;
			//$o.animate({top:tmTop},400)
			$o.css({top:tmTop})
			
			if($hasClass1 && ( $tmPrev.offset().top > tmTop - $ch - 90 ) ){
			//	$o.prev().prev().animate({top:tmTop + $ch + 30},400)
				$o.prev().prev().css({top:tmTop + $ch + 30})
			}else if(($hasClass2 ) && $tmPrev.offset().top > tmTop - $ch){
			log($tmPrev.offset().top , tmTop - $ch)
			//	$o.prev().prev().animate({top:tmTop + $ch + 30},400)
				$o.prev().prev().css({top:tmTop - $ch + 30})
			}
		}
	}
}

/*

*/