/*
 * Pagered carusel
 * @requires jQuery v1.4
 *
 * Copyright 2010
 *
 * Webway IT Labs | www.webway.ru
 *
 */
 /*

 */

 jQuery(document).ready(function() {
//	setTimeout(function() {

	$('.swr_cont').PageredCaruselSingle({
			gallered: '.vert',
			gallery: 'li',
			pagerNext : '.rarrow',
			pagerPrev : '.larrow',
			singleGage:18,
			maxHeight:3,
			hSetter:false,
			contHeight:'#layout',
			outHeighter:$.browser.msie6? '#prelayout' : '#abslayout'
		})
/*
	$('.swr_cont2').PageredCaruselSingle({
			gallered: '.vert',
			gallery: 'li',
			singleGage:18,
			maxHeight:3,
			hSetter:false,
			contHeight:'#layout',
			outHeighter:$.browser.msie6? '#prelayout' : '#abslayout',
			pagerDiv : '.spager',
			pagerO : '<span class="href"></span>',
			pagerActive : '<span class="corcolor"></span>'
		})
*/
 //});
 
 });
 
 
 $.fn.PageredCaruselSingle = function(options) {
	  var settings = {
				out: 'gall-wr',
				gallered: '.vert',
				gallery: 'li',
				singleGage:false,
				pagerDiv : false,
				pagerO : '<span class="href"></span>',
				pagerActive : '<span class="active"></span>',
				pDott : '<span>...</span>',
				centerPCount : 2,
				leftPCount : 1,
				afterInit:false
		};
	   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.pagerObj = $(settings.pagerDiv, this),
	this.active = 0,
	this.pagerO = $(settings.pagerO)
	this.pagerActive = $(settings.pagerActive)



	getBlockHeigh(this.g)//initinng Pager
	
	if(this.g[0].pager && this.g[0].pager.length > 1){
		initPager(this.g[0].pager.length,0,this.g[0].pager,this.list,this)
		this.list.hide()
	}else{
		this.pagerPrev.hide();
		this.pagerNext.hide();
		this.pagerObj.hide();
	}
	
	this.pagerNext.bind('click',function(){
		if(!ths.lock && ths.active<ths.g[0].pager.length -1)
			initPager(ths.g[0].pager.length,ths.active+1,ths.g[0].pager,ths.list,ths)

			return false;
	})
	this.pagerPrev.bind('click',function(){
		initPager(ths.g[0].pager.length,ths.active-1,ths.g[0].pager,ths.list,ths)
		return false;
	})


/**/
	function initPager(count,active,pager,lists,o){

		if(!ths.lock){
		
			ths.lock = true;
			active = active*1
			lists.fadeOut('100',function(){
	/**/			
				
				lists.css({
					marginTop:-pager[active].blTop,
					height:pager[active].blHeight+pager[active].blTop
					}).show()
				
				
				setTimeout(function() {
					lists.css({zoom:1})
				},0)
				lists[0].removeAttribute("filter")
				o.lock = false;
			})
			
			if(!settings.pagerDiv){
				o.active = buttonsPager(count,active,lists,o);
			}else{
				o.active = showPager(count,active,pager,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,pager,lists,o){

		var str= $('');

		var leftUpp;
		var rightUpp;
		var centerPCount = settings.centerPCount;
		var leftPCount = settings.leftPCount;
		var cCl = o.pagerObj.empty().append(str)
		
		for(ii = 0; ii < count ; ii ++ ){

			if( ( ( ii < leftPCount || ii > count - leftPCount - 1) || (ii > active - (centerPCount + 1) && ii < active + (centerPCount + 1) ) ) && ii != active ){
				cCl.append(o.pagerO.clone().html(ii + 1).attr('val',ii))
			}
			if((ii > leftPCount -1 && ii < active - centerPCount) && !leftUpp ){
				leftUpp = true;
				cCl.append($(settings.pDott))
			}

			if( ( ii < count - leftPCount && ii > active + centerPCount ) && !rightUpp ){
				rightUpp = true;
				cCl.append($(settings.pDott))
			}
			if(ii == active ){

				cCl.append(o.pagerActive.clone().html(ii + 1))
			}
		}

		setTimeout(function() {
			cCl.css({zoom:1})
		 },0);
		 	
		cCl.find('.href').unbind().bind('click',function(){

			initPager(count,parseInt($(this).attr('val'),10),pager,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)
		})
	}*/

 function getBlockHeigh(o){


	var defBdHeight,
		critFbxHeight,
		availMaxHeight,
		oHeight,
		prHeight,
		tmHeight

////////////////////	
if(settings.hSetter){//faceboxed

	defBdHeight = $(settings.hSetter).height()
	critFbxHeight = (getPageHeight() / 10) + 60
	availMaxHeight = settings.singleGage * settings.maxHeight
	oHeight = o.height()
	prHeight = $(settings.contHeight).height() - oHeight

	tmHeight = parseInt(defBdHeight - critFbxHeight - prHeight,10);

}else{

	defBdHeight = $(settings.outHeighter).height()
	critFbxHeight = 0
	availMaxHeight = settings.singleGage * settings.maxHeight
	oHeight = o.height()
	prHeight = $(settings.contHeight).height() - critFbxHeight - oHeight
	
	tmHeight = parseInt(defBdHeight - prHeight,10);

}
//////////////////////

	if(tmHeight > oHeight) return false ;

	initSplitters(o.children().children(),o.offset().top,o)

	var tmStep = tmHeight < availMaxHeight ? availMaxHeight : setTm(tmHeight,settings.singleGage)
	o.height(tmStep);

	if(settings.afterInit){
		settings.afterInit()
	}

	return initOPages(o,tmStep,0,[],settings.singleGage,0,oHeight);
 }

function initOPages(o,tmStep,page,array,sGage,curtop,oHeight){

	array[page] = new Object;

	array[page].blHeight = tmStep
	array[page].blTop	= setSplter(o,curtop)

//	log(array[page].blHeight,array[page].blTop,page,'page')

	var tmNext = array[page].blTop + array[page].blHeight
	
	if(tmNext + 2 < oHeight){

		initOPages(o,tmStep,page + 1,array,sGage,tmNext,oHeight)
	}else{

		o[0].pager = array
	}

	
	function setSplter(oo,ct){
		
		var top = ct;
		var brs = oo[0].br
		
		for(ii=0;ii < brs.length ; ii++ ){

			if(brs[ii].st > ct && brs[ii].st < ct+settings.singleGage+2 ){
				top = parseInt(brs[ii].st + settings.singleGage,10);

				return top
			}
	
		}
	return top
	}
}

function setTm(tmStep,sGage){
	return tmStep-(tmStep % sGage)
}

function initSplitters(sp,mainTop,o){
	o[0].br = sp.filter('br + br,p + br,div + br')

//	o[0].p = sp.filter('p')

	for(ii=0;ii < o[0].br.length ; ii++ ){
		o[0].br[ii].st = $(o[0].br[ii]).offset().top - mainTop
//////////////////////////////////////////////////////////////////////
	//	setOBr(o[0].br[ii],o[0].br[ii],$(o[0].br[ii]))
		
	}
//	for(ii=0;ii < o[0].p.length ; ii++ ){
//		o[0].p[ii].st = $(o[0].p[ii]).offset().top - mainTop
//	}
	function setOBr(curBr0,curBr,$curBr){
		var next = curBr.nextSibling
		var $next = $(next).prev()
	//	log($next[0] , curBr)
	//	log(next.textContent.indexOf('\n'),next,next.textContent)
		
		//log($next,next,$next.length , $next.filter('br').length)
		if($next.length && $next.filter('br').length){
			setOBr(curBr0,$next[0],$next)
		}else{
			curBr0.st = $curBr.offset().top - mainTop
		}
	}
}



	function getPageHeight() {
		var windowHeight;

		if(self.innerHeight) {
			// all except Explorer
			windowHeight = self.innerHeight;
		} else if(document.documentElement && document.documentElement.clientHeight) {
			// Explorer 6 Strict
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) {
			// other Explorers
			windowHeight = document.body.clientHeight;
		}

		return windowHeight;
	}

	this.each($.fn.PageredCarusel.init)




}






