/*
 * jQuery ajaxTabSlide v1.1.0 
 */
jQuery.fn.ajaxTabSlide = function(_options){
	// defaults options
	var _options = jQuery.extend({
			btPrev: 'a.prev',
			btNext: 'a.next',
			tabs: 'ul.tabset a',
			holder: 'div',
			scrollEl: 'li',
			loadIco: '.loading',
			duration: 2000,
			autoSlide:false,
			ajaxComplete:function(){}
	},_options);

	return this.each(function(){
		var _this = $(this);
		
		var _holder = $(_options.holder, _this),
			_scrollEl = $('> '+_options.scrollEl, _holder),
			_next = $(_options.btNext,  _this),
			_prev = $(_options.btPrev,  _this),
			_tabs = $(_options.tabs,  _this),
			_loadIco = $(_options.loadIco,  _this),
			_sliding = false, _clickEl = false;
			
		var _margin = 0, _duration = _options.duration,
			_current = 0, _step = _holder.innerWidth(),
			_length = _tabs.length, _activeSlide = _scrollEl,
			_activeIndex = _current, _Wheight;
			
		if (window.innerHeight) _Wheight = window.innerHeight; else _Wheight = document.documentElement.clientHeight;
			
		$(window).resize(function(){
			_step = _holder.innerWidth();
			if (window.innerHeight) _Wheight = window.innerHeight; else _Wheight = document.documentElement.clientHeight;
			var _sliders = $('> '+_options.scrollEl, _holder);
			if (_sliders.length > 1) {
				_sliders.each(function(){
					var _left = parseInt($(this).css('left'));
					if (_left != 0) {
						$(this).css('left',-_step);
					}
					$(this).find('div.align-center').css({'marginTop':_Wheight/2,'position':'relative','top':-$(this).find('div.align-center').outerHeight()/2});
				});
			}
		})

		if (_options.btNext) {
			_next.click(function(){
				_click = true;
				nextSlides();
				return false;
			});
		}
		if (_options.btPrev) {
			_prev.click(function(){
				_click = true;
				if (!$('> '+_options.scrollEl, _holder).is(':animated') && !_sliding) {
					_clickEl = this;
					_current -= 1;
					if (_current < 0) _current = _length-1;
					animateTab();
					setActive();
				}
				return false;
			});
		}
		
		if (!_tabs.filter('.active').length) {
			_tabs.eq(0).addClass('active');
		}
		_scrollEl.addClass('loadTab').attr('rel',_tabs.filter('.active').attr('href'));
		_scrollEl.find('div.align-center').css({'marginTop':_Wheight/2,'position':'relative','top':-_scrollEl.find('div.align-center').outerHeight()/2});
		
		/*_tabs.click(function(){
			if (!$(this).hasClass('active') && !$('> '+_options.scrollEl, _holder).is(':animated')) {
				_clickEl = this;
				var i = _tabs.index(this);
				_tabs.removeClass('active');
				$(this).addClass('active');
				_current = i;
				animateTab();
			}
			return false;
		});*/
		
		var _slideTimer, _click = false;
		if (_options.autoSlide)
			_slideTimer = setTimeout(nextSlides,_options.autoSlide);
		
		function nextSlides(){
			if (!$('> '+_options.scrollEl, _holder).is(':animated') && !_sliding) {
				_clickEl = _next.get(0);
				_current += 1;
				if (_current >= _length) _current = 0;
				setActive();
				animateTab();
			}
			return false;
		}
		
		function animateTab(){
			var _rel = _tabs.eq(_current).attr('href');
			var _el = $('.loadTab[rel='+_rel+']');
			if (_el.length) {
				slideThis();
			} else {
				_loadIco.show();
				$.ajax({
					url: _rel,
					success: function(msg){
						var _loadSlide = $(msg);
						_sliding = true;
						var _images = _loadSlide.find('img');
						if (_images.length) {
							var _loadIndex = 0;
							_images.each(function(){
								var _imageLoad = new Image();
								if (_imageLoad.complete) {
									_loadIndex++;
									if (_loadIndex >= _images.length) initSlide();
								} else {
									if ($.browser.msie) {
										var _timerLoad = setTimeout(function(){
											_loadIndex++;
											if (_loadIndex >= _images.length) {
												initSlide();
											}
										}, 500);
									};
									_imageLoad.onload = function(){
										if (_timerLoad) clearTimeout(_timerLoad);
										_loadIndex++;
										if (_loadIndex >= _images.length) {
											initSlide();
										}
										this.onload = function(){};
									};
								}
								_imageLoad.src = $(_imageLoad).attr('src');
							});
						} else {
							initSlide();
						}
						function initSlide(){
							_loadSlide.addClass('loadTab').attr('rel',_rel).css({'left':-_step});
							_holder.append(_loadSlide);
							_loadSlide.find('div.align-center').css({'marginTop':_Wheight/2,'position':'relative','top':-_loadSlide.find('div.align-center').outerHeight()/2});
							_loadIco.hide();
							_el = _loadSlide;
							slideThis();
							_options.ajaxComplete(_loadSlide);
						}
					}
				});
			}
			function slideThis(){
				if (_options.autoSlide) {
					if (_slideTimer || _click) {
						clearTimeout(_slideTimer)
						_slideTimer = setTimeout(nextSlides,_options.autoSlide);
						_click = false;
					}
				}
				var _tempEl, _f = false;
				_sliding = false;
				if ((_tabs.is(':visible') && _activeIndex < _current) || (_tabs.is(':hidden') && _clickEl == _next.get(0))) _f = true;
				
				if (_f) {
					_el.css({'left':_step});
					_activeSlide.animate({'left':-_step}, {duration:_duration, step:function(u,_opt){
						var _tempEnd = _opt.end,
							_tempNow = _opt.now;
						if (_tempEnd < 0) {
							_tempNow = (_tempEnd - _tempNow)* -1;
						}
						if (_tempEl)
							_tempEl.css({'left':_tempNow});
					}});
				} else {
					_el.css({'left':-_step});
					_activeSlide.animate({'left':_step}, {duration:_duration, step:function(u,_opt){
						var _tempNow = _opt.end - _opt.now;
						if (_tempEl)
							_tempEl.css({'left':-_tempNow});
					}});
				}
				_activeSlide = _tempEl = _el;
				_activeIndex = _current;
			}
		}
		function setActive () {
			if (_options.tabs) {
				_tabs.removeClass('active');
				_tabs.eq(_current).addClass('active');
			}
		}
	});
}

$(window).bind('load',function(){
	$('div.album-carousel').ajaxTabSlide({
		btPrev: 'a.prev',
		btNext: 'a.next',
		tabs: 'ul.slide-urls a',
		holder: 'ul.ajax-holder',
		scrollEl: 'li',
		duration: 1000,
		autoSlide:false,
		ajaxComplete:function(_tab){
			Cufon.replace('.time-table h1, .showbox .visual, .linkbox a, .sidebox .title, .sidebox h4, #content h2, div.info h2, .loading, .alt7-page .t .footer-holder .heading, .album-carousel h2, .album-carousel h3, .music-box .info h2, .music-box .info h3, .album-box h2, .band-box h2');
		}
	});
});