  jQuery(document).ready(function() {

	$('body').addClass('bodyJs')

	 $('.boock_gall').PageredCarusel({nums : true})
	
	
	var newsGall = $('#news_gall')
	var oGall = $('#news_gall').find('ul')
	var chdlr = oGall.children()
	var oBjs = chdlr.filter('li').find('a')
	
	initRoyImg(rSources)   
	initScroll(newsGall,oGall,chdlr,oBjs)   

	//++clock
	
	var clock = $('#clock');
	
	if(clock.length){
	
		clock.hide();
		clock.hour = clock.find('.hour');
		clock.minute = clock.find('.minute');
		
		clock.update = function(){
			var 
				currentDate = new Date(),
				minutes = currentDate.getMinutes(),
				hours = currentDate.getHours();
			
			hours = (hours > 12) ? hours-12 : hours;
			
			var
				hourDegree = 360 / 12 * hours,
				minuteDegree = 360 / 60 * minutes;
			
			hourDegree += minutes/60 * 360 / 12;
			
			clock.hour.css({
				"-webkit-transform": "rotate(" + hourDegree + "deg)",
				"-moz-transform": "rotate(" + hourDegree + "deg)",
				"-o-transform": "rotate(" + hourDegree + "deg)"
			});
			
			clock.minute.css({
				"-webkit-transform": "rotate(" + minuteDegree + "deg)",
				"-moz-transform": "rotate(" + minuteDegree + "deg)",
				"-o-transform": "rotate(" + minuteDegree + "deg)"
			});		
		};
		
		setInterval(clock.update, 1000);	
		clock.update();
		clock.show();

	}
	
	//--clock		
	   
 });

	function initRoyImg(arr){
		
		images = new Object();
		images.length = arr.length;
		
		for(ii =0; ii < images.length;ii++){
			images[ii] = {src:arr[ii]};
		}
		afterFade(images[0],0)
	//	images[0] = loadImgRoy(images[0],0,true)
		
		var ieMode = $.browser.msie ? $('#roy-main').append('<b id="ie-sub-fon"></b>').find('#ie-sub-fon') : false;

		if(ieMode) $('#roy-main i:first').show();
		
		
		function loadImgRoy(gObjPart,ind,firstload){

			newImg = new Image();
			
			newImg.onload = function() {
					
					gObjPart.img = '<img ' + 
					'width="' + newImg.width + 
					'" height="' + newImg.height + 
					( $.browser.msie6 ?
						'" src="/_pic/p.gif"' +
						' style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=\'true\',src=\'' + newImg.src + '\',sizingMethod=\'scale\');'
						: '" src="' + newImg.src + '" style="'
					 ) + 
					 'margin-left:-' + newImg.width/2 + 'px;display:none"' +
					' alt=""/>';

					gObjPart.img = $(gObjPart.img)
					if(!firstload)setImage(gObjPart,ind,'loaded');
					
			}
			newImg.src = gObjPart.src;			
			
			return gObjPart;
		}
		
		function setImage(gObjPart,ind,from){

		//	log(gObjPart,ind,from)
			var rWr = $('#r-wrm').append(gObjPart.img);
			var rWrInc = $('#roy-main i:first');
		
			if(ieMode){
				
				setTimeout(function() {

					ieMode.fadeIn('340',function(){
					
						rWr.find('img:first').hide().remove()
						rWr.find('img:last').show()

						setTimeout(function() {
							ieMode.fadeOut('440',function(){
								afterFade(gObjPart,ind)
							});
						},0)
					});
				},0)

			}else{
				setTimeout(function() {

					rWr.find('img:first').fadeOut('340',function(){

						$(this).remove()
						setTimeout(function() {
							rWr.find('img:first').fadeIn('440',function(){

								afterFade(gObjPart,ind)
							});
						},0)
						setTimeout(function() {
							rWrInc.fadeIn('300')
						},90)
					});
				},200)
				setTimeout(function() {
					rWrInc.fadeOut('300')
				},0)
			}

		}
		function afterFade(gObjPart,ind){
		
			var nextInd = ind < images.length -1 ? ind + 1 : 0;
			
			setTimeout(function() {
				if(!images[nextInd].img){
					loadImgRoy(images[nextInd],nextInd);
				}else{
					setImage(images[nextInd],nextInd,'afterFade')
				}	
			},7000)
		}
	} 

 $.fn.PageredCarusel = function(options) {
	  var settings = {
				gallered: '.hor',
				gallery: 'li',
				pagerDiv : false,
				pagerNext : '.i_snext',
				pagerPrev : '.i_sprev'
		};
	   settings = $.extend(settings, options);

	if( this.size() == 0 ) return;

$.fn.PageredCarusel.init = function(){



	var ths = this

	this.g = $(settings.gallered, this)


	this.list = $(settings.gallery, this.g),
	this.pagerNext = $(settings.pagerNext,this),
	this.pagerPrev = $(settings.pagerPrev,this)
	this.active = 0,
	this.pagerObj = $(settings.pagerDiv, this),
	this.pagerO = $(settings.pagerO)
	this.pagerActive = $(settings.pagerActive)

	if(this.list.length > 1){
		if(settings.autoHeight){
			initListHeight(this.list.length,this.list)
		}
		this.list.hide()
		initPager(this.list.length,0,this.list,this)
	}else{
		this.pagerPrev.hide();
		this.pagerNext.hide();
		this.pagerObj.hide();
	}
	this.pagerNext.bind('click',function(){
		if(!ths.lock && ths.active<ths.list.length -1)
		initPager(ths.list.length,ths.active+1,ths.list,ths)
	//	initPager(list.length,0,list)
		return false;
	})
	this.pagerPrev.bind('click',function(){
		initPager(ths.list.length,ths.active-1,ths.list,ths)
		return false;
	})
	$('.cl_o',ths.list.not(':last')).addClass('i_clicable').bind('click',function(){
		ths.pagerNext.click()
	})
	function initPager(count,active,lists,o){

		if(!ths.lock){

			ths.lock = true;
			active = active*1
			lists.filter(':eq(' + o.active + ')').fadeOut('100',function(){
				$(lists[active]).show()
				setTimeout(function() {
					$(lists[active]).css({zoom:1})
				},0)
				$(lists[active])[0].removeAttribute("filter")
				o.lock = false;
			})

			if(!settings.pagerDiv){
				o.active = buttonsPager(count,active,lists,o);
			}else{
				o.active = showPager(count,active,lists,o);
			}

		}
	}

	function buttonsPager(count,active,lists,o){
			if(active > 0){
				o.pagerPrev.css({'visibility':'visible'})
			}else{
				o.pagerPrev.css({'visibility':'hidden'})
			}
			if(active == count-1){
				o.pagerNext.css({'visibility':'hidden'})
			}else{
				o.pagerNext.css({'visibility':'visible'})
			}
			if(settings.nums){
				$('b',o.pagerPrev).html(active)
				$('b',o.pagerNext).html(count - active - 1)
			}
		return active
	}

	function showPager(count,active,lists,o){



		var str= $('');

		var leftUpp;
		var rightUpp;
		var centerPCount = settings.centerPCount;
		var leftPCount = settings.leftPCount;

		for(ii = 0; ii < count ; ii ++ ){

			if( ( ( ii < leftPCount || ii > count - leftPCount - 1) || (ii > active - (centerPCount + 1) && ii < active + (centerPCount + 1) ) ) && ii != active ){
				str = str.add(o.pagerO.clone().html(ii + 1).attr('val',ii))
			}
			if((ii > leftPCount -1 && ii < active - centerPCount) && !leftUpp ){
				leftUpp = true;
				str =  str.add($(settings.pDott))
			}

			if( ( ii < count - leftPCount && ii > active + centerPCount ) && !rightUpp ){
				rightUpp = true;
				str = str.add($(settings.pDott))
			}
			if(ii == active ){

				str = str.add(o.pagerActive.clone().html(ii + 1))
			}
		}

		var cCl = o.pagerObj.empty().append(str)


		cCl.find('.href').unbind().bind('click',function(){

			initPager(count,parseInt($(this).attr('val'),10),lists,o)
			return false;
		})
	return parseInt(active,10)
/**/
	}

}
	function initListHeight(length,lists){
		var tmH = 0
		for(kk=0;kk<length;kk++){
			var curH = $(lists[kk]).height();
			tmH = tmH > curH ? tmH : curH;
		}
		lists.each(function(){
			$(this).height(tmH)
		})
	}

	this.each($.fn.PageredCarusel.init)



}
function initScroll(O,oGall,chdlr,oBjs){

	oBjs.each(function(){
	
		var sQuerySep;

		this.href.lastIndexOf('?') != -1 ? sQuerySep = '&' : sQuerySep = '?';
		this.href += sQuerySep + 'viewgall=1';
	})


	var tmWd =0;
		chdlr.each(function(){
			tmWd += $(this).width() 
		})
		var scMo = oGall.parent();
		if(tmWd/scMo.width()<=1) return;
		$('#news_scroll').hide()
		O.addClass('js')
		oGall.width(tmWd+1)
	
	
		$(".scroll").jscroll({axis : {y:false,x:true}});
	
		
}

/*plagin*/
/*
function initScroll(O,oGall,chdlr){
	O.addClass('js')
	
	var tmWd = 0;
	
	chdlr.each(function(){
		tmWd += $(this).width() 
	})

	$('#news').html(tmWd+ ' | ' +oGall.width())
	oGall.width(tmWd+1)	
	var scMo = oGall.parent();


$('#ft').hide()
	if(oGall.width()/scMo.width()<1) return;

	var scroller = O.find('.scroller').css({visibility:'visible'})
	var tmPrv = scroller.parent().width()
	var prst = ( oGall.width() - scMo.width() )/(tmPrv);

	var newsScroll = $('#news_scroll')

	$(window).bind('resize',function(){
		tmPrv = scroller.parent().width()
		prst = ( oGall.width() - scMo.width() )/tmPrv;
		
		$('#news').append(prst  + ' | ' + scMo.width() )
	})

	scroller.hover(function(){$(this).toggleClass('hover')},function(){$(this).toggleClass('hover')})

	scroller.bind('mousedown',setmousDown).bind('mouseup',setmousUp)

	function setmousDown(e){
		
		this.startVal = e.pageX;
		$('body').bind('mousemove',setmousMove)

		function setmousMove(e){
			
			//if(e.pageX<19 || e.pageX > $('body').width() - 19) return false;
			
			var lft = (e.pageX - scroller.parent().offset().left)
	
			
			$('#news').html(prst  + ' | ' + oGall.width()  + ' | ' + ' | ' + -lft*prst)
			scroller.css({left:lft})
			
			oGall.css({left:-lft*prst})	
			
		}
	}
	function setmousUp(e){
		this.startVal = false;
		$('body').unbind('mousemove')
	}


}

*/

