jQuery(window).load(function(){
	initCycleGalleryV();
	initCycleGallery();
	ieHover();
	jQuery('a.link').click(function(){
		window.location.href=jQuery(this).attr('href');
	});
});

// gallery init
function initCycleGalleryV() {
	// settings
	var _activeClass = 'active';
	var _switchTime = 5000;
	if(jQuery('span#time_news').length){_switchTime = jQuery('span#time_news').text(); jQuery('span#time_news').hide();}
	var _speed = 450;

	jQuery('div#gallery-news').each(function(){
		// gallery options
		var _holder = jQuery(this);
		var _btnPrev = _holder.find('a.prev');
		var _btnNext = _holder.find('a.next');
		var _slidesHolder = _holder.find('div.holder');
		var _slider = _slidesHolder.find('ul');
		var _slides = _slider.children();
		var _slidesCount = _slides.length;
		var _currentIndex = 0;
		var _sumHeight = 0;
		var _animating = false;
		var _autoRotation = true;
		if(_switchTime == 0){_autoRotation = false;}
		var _rotateDirection = nextSlide;
		var _pauseOnHover = true;
		var _timer;

		// gallery init
		_slides.each(function(ind){
			_sumHeight += jQuery(this).attr('rel',ind).outerHeight(true);
		});
		_slider.prepend(_slides.clone()).append(_slides.clone());
		_slider.css({marginTop:-_sumHeight});
		
		if(_autoRotation){autoRotate();}
		
		function autoRotate() {
			if(_timer) clearInterval(_timer);
			_timer = setInterval(_rotateDirection, _switchTime);
		}
		
		if(_pauseOnHover){
			_holder.hover(function(){
				if(_timer) clearInterval(_timer);
			},function(){
				autoRotate();
			});
		}
		
		// gallery control
		_btnPrev.click(prevSlide);
		_btnNext.click(nextSlide);

		// gallery animation
		function prevSlide() {
			if(!_animating) {
				_currentIndex--;
				switchSlide();
			}
			return false;
		}
		function nextSlide() {
			if(!_animating) {
				_currentIndex++;
				switchSlide();
			}
			return false;
		}

		function switchSlide() {
			_animating = true;
			var offset = 0;
			if(_currentIndex>=0) offset = -_sumHeight- _slider.children().eq(_currentIndex ).position().top
			else offset = -_slider.children().eq(_slidesCount+_currentIndex).position().top;
			
			_slider.animate({marginTop:offset},{duration:_speed, queue:false,complete:function(){
				if(Math.abs(_currentIndex) == _slidesCount) {
					_currentIndex = 0;
					_slider.css({marginTop:-_sumHeight});
				}
				_animating = false;
			}});
		}

		// event handler
		_holder.bind('scrollto',function(e,h){
			if(!_animating) {
				if(_currentIndex == _slidesCount-1) _currentIndex = _slidesCount;
				else _currentIndex = h.num;
				switchSlide();
			}
		})

		// clone events
		_slider.children().each(function(){
			var link = jQuery(this);
			var ind = parseInt(link.attr('rel'));
			link.click(function(){
				_holder.trigger('setslide',{num:ind});
				return false;
			})
		})
	});
}

function initCycleGallery() {
	// settings
	var _activeClass = 'active';
	var _switchTime = 5000;
	if(jQuery('span#time_outcomes').length){_switchTime = jQuery('span#time_outcomes').text(); jQuery('span#time_outcomes').hide();}
	var _speed = 450;

	jQuery('div#gallery-inf').each(function(){
		// gallery options
		var _holder = jQuery(this);
		var _btnPrev = _holder.find('a.prev');
		var _btnNext = _holder.find('a.next');
		var _slidesHolder = _holder.find('div.holder');
		var _slider = _slidesHolder.find('ul');
		var _slides = _slider.children();
		var _slidesCount = _slides.length;
		var _currentIndex = 0;
		var _sumWidth = 0;
		var _animating = false;
		var _autoRotation = true;
		if(_switchTime == 0){_autoRotation = false;}
		var _rotateDirection = nextSlide;
		var _pauseOnHover = true;
		var _timer;

		// gallery init
		_slides.each(function(ind){
			_sumWidth += jQuery(this).attr('rel',ind).outerWidth(true);
		});
		_slider.prepend(_slides.clone()).append(_slides.clone());
		_slider.css({marginLeft:-_sumWidth});
		
		if(_autoRotation){autoRotate();}
		
		function autoRotate() {
			if(_timer) clearInterval(_timer);
			_timer = setInterval(_rotateDirection, _switchTime);
		}
		
		if(_pauseOnHover){
			_holder.hover(function(){
				if(_timer) clearInterval(_timer);
			},function(){
				autoRotate();
			});
		}
		
		// gallery control
		_btnPrev.click(prevSlide);
		_btnNext.click(nextSlide);

		// gallery animation
		function prevSlide() {
			if(!_animating) {
				_currentIndex--;
				switchSlide();
			}
			return false;
		}
		function nextSlide() {
			if(!_animating) {
				_currentIndex++;
				switchSlide();
			}
			return false;
		}

		function switchSlide() {
			_animating = true;
			var offset = 0;
			if(_currentIndex>=0) offset = -_sumWidth- _slider.children().eq(_currentIndex ).position().left
			else offset = -_slider.children().eq(_slidesCount+_currentIndex).position().left;
			
			_slider.animate({marginLeft:offset},{duration:_speed, queue:false,complete:function(){
				if(Math.abs(_currentIndex) == _slidesCount) {
					_currentIndex = 0;
					_slider.css({marginLeft:-_sumWidth});
				}
				_animating = false;
			}});
		}

		// event handler
		_holder.bind('scrollto',function(e,h){
			if(!_animating) {
				if(_currentIndex == _slidesCount-1) _currentIndex = _slidesCount;
				else _currentIndex = h.num;
				switchSlide();
			}
		})

		// clone events
		_slider.children().each(function(){
			var link = jQuery(this);
			var ind = parseInt(link.attr('rel'));
			link.click(function(){
				_holder.trigger('setslide',{num:ind});
				return false;
			})
		})
	});
}

//ie hover pack js
eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('9 t(a,b){g(3=0;3<a.h;3++){k c=[],d=[],n=[],e=[],4=[],l=[];8(a[3].5(\'.\')!=-1||a[3].5(\'#\')==-1){8(a[3].5(\'>\')!=-1){c[3]=a[3].f(a[3].5(\'>\')+2);a[3]=a[3].f(0,a[3].5(\'>\')-1)}8(a[3].5(\'.\')!=-1){d[3]=a[3].f(a[3].5(\'.\')+1);a[3]=a[3].f(0,a[3].5(\'.\'))}n[3]=a[3];8(!d[3])d[3]=\'\';8(c[3]){l[3]=r.o(n[3]);g(k j=0;j<l[3].h;j++){8(l[3][j].6.5(d[3])!=-1){4[3]=l[3][j].o(c[3]);g(k i=0;i<4[3].h;i++){4[3][i].p=9(){7.6+=\' \'+b};4[3][i].q=9(){7.6=7.6.m(b,\'\')}}}}}s{4[3]=r.o(n[3]);g(k i=0;i<4[3].h;i++){8(4[3][i].6.5(d[3])!=-1){4[3][i].p=9(){7.6+=\' \'+b};4[3][i].q=9(){7.6=7.6.m(b,\'\')}}}}}s 8(a[3].5(\'#\')!=-1){8(a[3].5(\'>\')!=-1){c[3]=a[3].f(a[3].5(\'>\')+2);a[3]=a[3].f(0,a[3].5(\'>\')-1)}a[3]=a[3].m(\'#\',\'\');e[3]=r.u(a[3]);8(e[3]){8(c[3]){4[3]=e[3].o(c[3]);g(k i=0;i<4[3].h;i++){4[3][i].p=9(){7.6+=\' \'+b};4[3][i].q=9(){7.6=7.6.m(b,\'\')}}}s{e[3].p=9(){7.6+=\' \'+b};e[3].q=9(){7.6=7.6.m(b,\'\')}}}}}}',31,31,'|||_hoverItem|_hoverElement|indexOf|className|this|if|function||||_class|_id|substr|for|length|||var|_parent|replace|_tag|getElementsByTagName|onmouseover|onmouseout|document|else|hoverForIE6|getElementById'.split('|'),0,{}));
/* insert your class and id ***********************
_hoverClassName = 'hover';
_hoverEl = ['ul.ul-class > li',	'div.div-class', 'span', '#box', '#nav > li'];
/**************************************************/
function ieHover() {
	hoverForIE6(['ul.nav > li','div.div-class','span','#box','#nav > li'], 'hover');
}
