﻿function initPage(){
	$('#ResourceCarusel').galleryScroll({
		btPrev: 'a.link-prev',
		btNext: 'a.link-next',
		circleSlide: false
	});
	initGal($('#gal'));
}
function initGal(_hold){
	var _delay = 5000;
	var btn_list = $('.pages a', _hold);
	var _body = $('div.image-holder', _hold);
	var box_list = $('.box', _body);
	var _active = 0;
	var _timer;
	box_list.each(function(i){ if(box_list.eq(i).hasClass('active')){ _active = i;}});
	
	function nextEl(){
		var _ind = box_list.length -1;
		if(_active < _ind) { _ind = _active + 1;}
		else { _ind = 0;}
		changeBox(_ind);
	}
	function changeBox(_ind){
		_body.css({height: _body.height()});
		box_list.eq(_active).css({position:'absolute', zIndex:1}).fadeOut(500);
		box_list.eq(_ind).css({position:'relative', zIndex:2}).fadeIn(500, function(){
			$(this).css('position','static');
			_body.animate({height: $(this).height()}, 300, function(){ $(this).height('auto');});
			
			box_list.removeClass('active');
			box_list.eq(_ind).addClass('active');
			box_list.eq(_active).css({display:'none'});
			_active = _ind;
			_timer = setTimeout(function(){nextEl();}, _delay);
		});
		
	}
	btn_list.click(function(){
		if(!box_list.is(':animated') && !_body.is(':animated')){
			var _ind = btn_list.index(this);
			if (_timer) clearTimeout(_timer);
			if(box_list.eq(_ind).get(0) && _ind != _active){
				if(box_list.eq(_ind).height() > box_list.eq(_active).height()){
					_body.animate({ height: box_list.eq(_ind).height()}, 300, function(){changeBox(_ind);});
				}
				else{
					changeBox(_ind);
				}
			}
			else{
				_timer = setTimeout(function(){nextEl();}, _delay);
			}
		}
		return false;
	});
	_timer = setTimeout(function(){nextEl();}, _delay);
}
if (window.addEventListener){
	window.addEventListener("load", initPage, false);
}
else if (window.attachEvent){
	window.attachEvent("onload", initPage);
}