jQuery(function(){
	clearInputs();
	jQuery('div.navigation').each(function(){
		if(jQuery(this).find('a').length == 0) jQuery(this).remove();
	});
	initSlideShow();
	IE6Hover('.comments .btn', 'hover');
	initDrop();
	jQuery('a.addthis_button').live('mouseenter', function(){
		jQuery('#at_hover > div:odd').addClass('alt');
	});
	initNav();
	initGrid();
	initShare();
});
/*--- share ---*/
function initShare(){
	jQuery('div.topicbox div.links > ul').each(function(){
		var _hold = jQuery(this);
		var _el = _hold.children('li:eq(0)');
		var _f = false;
		_hold.find('a.open-btn').click(function(){
			if(_f){
				_f = false;
				_el.removeClass('hover');
			}
			else{
				_f = true;
				_el.addClass('hover');
			}
			return false;
		});
		jQuery(document).bind('mousedown', function(e){
			if(_f){
				e = e || event;
				var t = e.target || e.srcElement;
				t = jQuery(t);
				if(t.parents('li').filter(_el).length == 0 && t.get(0) != _el.get(0)){
					_f = false;
					_el.removeClass('hover');
				}
			}
		});
	});
}
/*--- initGrid ---*/
function initGrid(){
	var _hold = jQuery('#grid');
	if(_hold.length){
		var list_hold = _hold.find('div.list-hold');
		var _btn = _hold.find('div.navigation div.next a');
		var t_hold = jQuery('div');
		if(_btn.length){
			_hold.append('<div class="loader">Loading...</div>');
			var _loader = _hold.find('div.loader');
			var _t; 
			jQuery(window).scroll(function(){
				if(_t) clearTimeout(_t);
				_t = setTimeout(function(){
					_add();
				}, 100);
			});
			function _add(){
				if(_btn.length && jQuery(window).scrollTop() + jQuery(window).height() > list_hold.outerHeight() + list_hold.offset().top){
					if(!_btn.data('f')){
						var _url = _btn.attr('href');
						while(_url.indexOf('&#038') != -1) _url = _url.replace('&#038', '&');
						_btn.data('f', true);
						_loader.show();
						jQuery.ajax({
							url: _url,
							type: 'POST',
							success: function(_html){
								_loader.hide();
								_btn = jQuery(_html).find('div.navigation div.next a');
								jQuery(_html).find('div.list-hold ul').css('display', 'none').appendTo(list_hold).fadeIn(300);
							}
						});
					}
				}
			}
		}
	}
}
/*--- initNav ---*/
function initNav(){
	jQuery('div.wp-pagenavi').each(function(){
		var _hold = jQuery(this);
		var list_hold = _hold.find('ul');
		var _list = list_hold.children();
		
		var list_w = _list.length * _list.outerWidth();
		var hold_w = list_hold.parent().width();
		var _step = _list.outerWidth();
		var _a  = _list.children().index(_list.children('.active:eq(0)'));
		if(list_w > hold_w){
			_hold.append('<div class="sep"><div class="btn"></div><div class="a-el"></div></div>');
			var _btn = _hold.find('div.btn');
			var a_el = _hold.find('div.a-el');
			var _m = Math.ceil((_a*_step-hold_w/2)/_step)*_step;
			(hold_w/_list.length > 10)?(a_el.css('width', hold_w/_list.length)):(a_el.css('width', 10));
			if(_m < 0) _m = 0;
			else if(_m > list_w - hold_w) _m = list_w - hold_w;
			var _k = (list_w-hold_w)/(hold_w - _btn.width());
			list_hold.css('left', -_m);
			_btn.css('left', _m*(1/_k));
			
			
			var t_w = 0;
			if(_m == 0) t_w = 0;
			else if(_m == list_w - hold_w) t_w = hold_w - a_el.width();
			else t_w = _btn.position().left + _btn.width()/2 - a_el.width()/2;

			a_el.css('left', t_w);

			var _f1 = false;
			var _f2 = true;
			var t_post = 0, n_t_post = 0;
			_btn.mousedown(function(e){
				_f1 = true;
				t_post = e.pageX - _btn.position().left;
			}).mouseup(function(){
				_f1 = false;
			});
			
			_hold.find('div.sep').mousemove(function(e){
				if(_f1){
					n_t_post = e.pageX - t_post;
					scrollBox();
					_f2 = false;
				}
			}).click(function(e){
				if(_f2){
					n_t_post = e.pageX - jQuery(this).offset().left-_btn.width()/2;
					scrollBox();
				}
				else{
					_f2 = true;
				}
				
			}).mouseleave(function(){
				_f1 = false;
				_f2 = true;
			});
			
			function scrollBox(){
				if(n_t_post < 0) n_t_post = 0;
				else if(n_t_post + _btn.width() > hold_w) n_t_post = hold_w - _btn.width();
				_m = Math.ceil((_k*n_t_post)/_step)*_step;
				if(_m < 0) _m = 0;
				else if(_m > list_w - hold_w) _m = list_w - hold_w;
				_btn.css('left', _m*(1/_k));
				list_hold.css('left', -_m);
			}
			
			document.body.onselectstart = function(){ if(_f1) return false;}
		}
		else{
			_hold.append('<div class="sep"><div class="btn"></div></div>');
			var _btn = _hold.find('div.btn');
			var t_l = _list.eq(_a).position().left + _list.eq(_a).outerWidth()/2 - _btn.outerWidth()/2;
			if(t_l < 0) t_l = 0;
			else if(t_l + _btn.outerWidth()/2 > _hold.outerWidth()) t_l = _hold.outerWidth() - _btn.outerWidth()/2;
			_btn.css('left', t_l);
		}
	});
}
/*--- drop function ---*/
function initDrop(){
	jQuery('div.resources-el').each(function(){
		var _hold = jQuery(this);
		var _btn = _hold.find('span.btn');
		_hold.mouseenter(function(){
			_hold.addClass('hover');
		}).mouseleave(function(){
			_hold.removeClass('hover');
		});
		_btn.click(function(){
			if(_hold.hasClass('actived')) _hold.removeClass('actived');
			else _hold.addClass('actived');
		});
		/*
		jQuery(document).bind('mousedown', function(e){
			if(_hold.hasClass('actived')){
				e = e || event;
				var t = e.target || e.srcElement;
				t = jQuery(t);
				if(t.parents().filter(_hold).length == 0 && t.get(0) != _hold.get(0)) _hold.removeClass('actived');
			}
		});
		*/
	});
}
/*---- clear inputs ---*/
function clearInputs(){
	jQuery('input:text, input:password, textarea').each(function(){
		var _el = jQuery(this);
		_el.data('val', _el.val());
		_el.bind('focus', function(){
			if(_el.val() == _el.data('val')) _el.val('');
		}).bind('blur', function(){
			if(_el.val() == '') _el.val(_el.data('val'));
		});
	});
}
/*--- hover for IE6 ---*/
function IE6Hover(h_list, h_class){
	if(jQuery.browser.msie && jQuery.browser.version < 7){
		if(!h_class) var h_class = 'hover';
		jQuery(h_list).mouseenter(function(){
			jQuery(this).addClass(h_class);
		}).mouseleave(function(){
			jQuery(this).removeClass(h_class);
		});
	}
}
// slideshow init
function initSlideShow() {
	jQuery('.slideshow').fadeGallery({
		slideElements:'.fade-gal > li',
		btnNext:'a.next',
		currentNum:'.cur-num',
		allNum:'.all-num',
		btnPrev:'a.prev',
		autoHeight: true
	});
}
// slideshow plugin
jQuery.fn.fadeGallery = function(_options){
	var _options = jQuery.extend({
		slideElements:'div.slideset > div',
		pagerLinks:'div.pager a',
		btnNext:'a.next',
		btnPrev:'a.prev',
		btnPlayPause:'a.play-pause',
		btnPlay:'a.play',
		btnPause:'a.pause',
		pausedClass:'paused',
		disabledClass: 'disabled',
		playClass:'playing',
		activeClass:'active',
		currentNum:false,
		allNum:false,
		startSlide:null,
		noCircle:false,
		pauseOnHover:true,
		autoRotation:false,
		autoHeight:false,
		onChange:false,
		switchTime:3000,
		duration:650,
		event:'click'
	},_options);

	return this.each(function(){
		// gallery options
		var _this = jQuery(this);
		var _slides = jQuery(_options.slideElements, _this);
		var _pagerLinks = jQuery(_options.pagerLinks, _this);
		var _btnPrev = jQuery(_options.btnPrev, _this);
		var _btnNext = jQuery(_options.btnNext, _this);
		var _btnPlayPause = jQuery(_options.btnPlayPause, _this);
		var _btnPause = jQuery(_options.btnPause, _this);
		var _btnPlay = jQuery(_options.btnPlay, _this);
		var _pauseOnHover = _options.pauseOnHover;
		var _autoRotation = _options.autoRotation;
		var _activeClass = _options.activeClass;
		var _disabledClass = _options.disabledClass;
		var _pausedClass = _options.pausedClass;
		var _playClass = _options.playClass;
		var _autoHeight = _options.autoHeight;
		var _duration = _options.duration;
		var _switchTime = _options.switchTime;
		var _controlEvent = _options.event;
		var _currentNum = (_options.currentNum ? jQuery(_options.currentNum, _this) : false);
		var _allNum = (_options.allNum ? jQuery(_options.allNum, _this) : false);
		var _startSlide = _options.startSlide;
		var _noCycle = _options.noCircle;
		var _onChange = _options.onChange;

		// gallery init
		var _hover = false;
		var _prevIndex = 0;
		var _currentIndex = 0;
		var _slideCount = _slides.length;
		var _timer;
		if(_slideCount < 2) return;

		_prevIndex = _slides.index(_slides.filter('.'+_activeClass));
		if(_prevIndex < 0) _prevIndex = _currentIndex = 0;
		else _currentIndex = _prevIndex;
		if(_startSlide != null) {
			if(_startSlide == 'random') _prevIndex = _currentIndex = Math.floor(Math.random()*_slideCount);
			else _prevIndex = _currentIndex = parseInt(_startSlide);
		}
		_slides.hide().eq(_currentIndex).show();
		if(_autoRotation) _this.removeClass(_pausedClass).addClass(_playClass);
		else _this.removeClass(_playClass).addClass(_pausedClass);

		// gallery control
		if(_btnPrev.length) {
			_btnPrev.bind(_controlEvent,function(){
				prevSlide();
				return false;
			});
		}
		if(_btnNext.length) {
			_btnNext.bind(_controlEvent,function(){
				nextSlide();
				return false;
			});
		}
		if(_pagerLinks.length) {
			_pagerLinks.each(function(_ind){
				jQuery(this).bind(_controlEvent,function(){
					if(_currentIndex != _ind) {
						_prevIndex = _currentIndex;
						_currentIndex = _ind;
						switchSlide();
					}
					return false;
				});
			});
		}

		// play pause section
		if(_btnPlayPause.length) {
			_btnPlayPause.bind(_controlEvent,function(){
				if(_this.hasClass(_pausedClass)) {
					_this.removeClass(_pausedClass).addClass(_playClass);
					_autoRotation = true;
					autoSlide();
				} else {
					_autoRotation = false;
					if(_timer) clearTimeout(_timer);
					_this.removeClass(_playClass).addClass(_pausedClass);
				}
				return false;
			});
		}
		if(_btnPlay.length) {
			_btnPlay.bind(_controlEvent,function(){
				_this.removeClass(_pausedClass).addClass(_playClass);
				_autoRotation = true;
				autoSlide();
				return false;
			});
		}
		if(_btnPause.length) {
			_btnPause.bind(_controlEvent,function(){
				_autoRotation = false;
				if(_timer) clearTimeout(_timer);
				_this.removeClass(_playClass).addClass(_pausedClass);
				return false;
			});
		}

		// gallery animation
		function prevSlide() {
			_prevIndex = _currentIndex;
			if(_currentIndex > 0) _currentIndex--;
			else {
				if(_noCycle) return;
				else _currentIndex = _slideCount-1;
			}
			switchSlide();
		}
		function nextSlide() {
			_prevIndex = _currentIndex;
			if(_currentIndex < _slideCount-1) _currentIndex++;
			else {
				if(_noCycle) return;
				else _currentIndex = 0;
			}
			switchSlide();
		}
		function refreshStatus() {
			if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentIndex).addClass(_activeClass);
			if(_currentNum) _currentNum.text(_currentIndex+1);
			if(_allNum) _allNum.text(_slideCount);
			_slides.eq(_prevIndex).removeClass(_activeClass);
			_slides.eq(_currentIndex).addClass(_activeClass);
			if(_noCycle) {
				if(_btnPrev.length) {
					if(_currentIndex == 0) _btnPrev.addClass(_disabledClass);
					else _btnPrev.removeClass(_disabledClass);
				}
				if(_btnNext.length) {
					if(_currentIndex == _slideCount-1) _btnNext.addClass(_disabledClass);
					else _btnNext.removeClass(_disabledClass);
				}
			}
			if(typeof _onChange === 'function') {
				_onChange(_this, _currentIndex);
			}
		}
		function switchSlide() {
			_slides.eq(_prevIndex).fadeOut(_duration);
			_slides.eq(_currentIndex).fadeIn(_duration);
			if(_autoHeight) _slides.eq(_currentIndex).parent().animate({height:_slides.eq(_currentIndex).outerHeight(true)},{duration:_duration,queue:false});
			refreshStatus();
			autoSlide();
		}

		// autoslide function
		function autoSlide() {
			if(!_autoRotation || _hover) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime+_duration);
		}
		if(_pauseOnHover) {
			_this.hover(function(){
				_hover = true;
				if(_timer) clearTimeout(_timer);
			},function(){
				_hover = false;
				autoSlide();
			});
		}
		refreshStatus();
		autoSlide();
	});
}
