jQuery ( function () {
	/*initTextChanger ();*/
	initSlideBlock ();
	initGallery ();
	clearText();
	/*fontResizeEvent ();*/
})

$(document).ready(function() {
	$("#ldicredir input:text").val("");
});

function clearText(){
	document.getElementById('name').value='';
	document.getElementById('atlllt-atlllt').value='';
	document.getElementById('account').value='';
	document.getElementById('userid').value='';
	document.getElementById('password').value='';
	document.getElementById('edit-search-theme-form-1').value='';
}

// font resize event
/*function fontResizeEvent () {
	var _refresh = 1000;
	var _lastSize;
	var _body = $('body');
	var _text = $('<div class="font-resize-event">a</div>');
	_text.css({
		fontSize: '100em',
		position:'absolute',
		display:'block',
		top:-9999,
		left:-9999
	});

	_body.append(_text);
	_lastSize = _text.height();

	setInterval(function(){
		if(_text.height() != _lastSize) {
			_lastSize = _text.height();
			_body.trigger('fontResized');
		}
	},_refresh)
	_body.bind('fontResized',function(){
		initSlideBlock ()
	});
};*/

// initSlideBlock
function initSlideBlock () {
	var _buttonBlock = jQuery ( '#accesPortfolio' ),
		_slideBox = jQuery ( '> .text', _buttonBlock ),
		_w = _slideBox.outerWidth (),
		_t;
	
	_slideBox.css ({
		marginLeft: -_w
	})
	
	_buttonBlock.css ({
		marginTop: - ( _buttonBlock.height () / 2 )
	})
	
	_slideBox.unbind ( 'mouseenter' );
	_slideBox.unbind ( 'mouseleave' );
	
	_slideBox.bind ( 'mouseenter', function () {
		if ( _t ) clearTimeout ( _t );
		_slideBox.animate ({ marginLeft: 0 }, { queue: false, duration: 600 })
	}).bind ( 'mouseleave', function () {
		if ( _t ) clearTimeout ( _t );
		_t = setTimeout ( function () {
			_slideBox.animate ({ marginLeft: -_w }, { queue: false, duration: 600 })
		}, 800)
	})
	
	jQuery ( '.vSlide-area' ).each ( function () {
		var _holder = jQuery ( this ),
			_slideBox = jQuery ( '.slide', this ),
			_h = _slideBox.outerHeight (),
			_tt;
		
		_slideBox.css ({
			display: 'block',
			marginBottom: '-167px'
		})
		
		_holder.unbind ( 'mouseenter' );
		_holder.unbind ( 'mouseleave' );
		
		_holder.bind ( 'mouseenter', function () {
			if ( _tt ) clearTimeout ( _tt );
			_slideBox.animate ({ marginBottom: 0 }, { queue: false, duration: 600 });
		}).bind( 'mouseleave', function () {
			if ( _tt ) clearTimeout ( _tt );
			_tt = setTimeout ( function () {	 
				_slideBox.animate ({ marginBottom: '-167px' }, { queue: false, duration: 600 });
			}, 800);
		})
	})
}

// initTextChanger
/*
function initTextChanger () {
	textChanger.init ();
	var _btns = jQuery ( '#dSize, #ddSize, #dddSize' );
	
	_btns.each ( function () {
		var _btn = jQuery ( this );
		_btn.click ( function () {
			_btns.removeClass ( 'active' );
			_btn.addClass ( 'active' );
		})
	})
}

var textChanger = {
	defaultFS : '62.5',
	init: function() {
		var el = document.getElementsByTagName("body")[0];
		var sz = textChanger.getCookie();
		el.style.fontSize = sz ? sz + '%' : textChanger.defaultFS + '%';
		var dSize = document.getElementById('dSize');
		if(dSize)
			dSize.onclick = function(){textChanger.changeSize('62.5'); return false;};
		var ddSize = document.getElementById('ddSize');
		if(ddSize)
			ddSize.onclick = function(){textChanger.changeSize('78.1'); return false;};
		var dddSize = document.getElementById('dddSize');
		if(dddSize)
			dddSize.onclick = function(){textChanger.changeSize('93.7'); return false;};
		
		if ( sz ) {
			switch ( sz ) {
				case '62.5':
					if ( dSize ) dSize.className += ' active';
					if ( ddSize ) ddSize.className = ddSize.className.replace ( 'active', '' );
					if ( ddSize ) dddSize.className = dddSize.className.replace ( 'active', '' );
					break;
				case '78.1':
					if ( dSize ) dSize.className = dSize.className.replace ( 'active', '' );
					if ( ddSize ) ddSize.className += ' active';
					if ( ddSize ) dddSize.className = dddSize.className.replace ( 'active', '' );
					break;
				case '93.7':
					if ( dSize ) dSize.className = dSize.className.replace ( 'active', '' );
					if ( ddSize ) ddSize.className += ddSize.className.replace ( 'active', '' );
					if ( ddSize ) dddSize.className += ' active';
					break;
			}
		}
	},
	changeSize: function(val) {
		var el = document.getElementsByTagName("body")[0];
		el.style.fontSize = val + '%';
		textChanger.updateCookie(val);
	},
	updateCookie: function(vl){
		var today = new Date();
		var exp = new Date(today.getTime() + (365*24*60*60*1000));
		document.cookie = 'textChangerL=size=' + vl + ';' +'expires=' + exp.toGMTString() + ';' +'path=/';
	},
	getCookie: function()	{ 
		var cname = 'textChangerL=size=';
		var start = document.cookie.indexOf(cname);
		var len = start + cname.length;
		if ((!start) && (cname != document.cookie.substring(0,cname.length))) {return null;}
		if (start == -1) return null;
		var end = document.cookie.indexOf(";",len);
		if (end == -1) end = document.cookie.length;
		return unescape(document.cookie.substring(len, end));
	}
}*/

// initGallery
function initGallery () {
	jQuery ( '.slideshow' ).fadeGallery ({
		slideElements:'.slideset > li',
		pagerLinks:'.listing > li',
		pauseOnHover:true,
		autoRotation:true,
		switchTime:7000,
		duration:400
	})
}

// slideshow plugin
jQuery.fn.fadeGallery = function(_options){
	var _options = jQuery.extend({
		slideElements:'div.slideset > div',
		pagerLinks:'div.pager a',
		generatePagination:null,
		btnNext:'a.next',
		btnPrev:'a.prev',
		btnPlayPause:'a.play-pause',
		btnPlay:'a.play',
		btnPause:'a.pause',
		pausedClass:'paused',
		disabledPrevClass: 'prev-disabled',
		disabledNextClass: 'next-disabled',
		playClass:'playing',
		activeClass:'active',
		loadingClass:'ajax-loading',
		loadedClass:'slide-loaded',
		dynamicImageLoad:false,
		dynamicImageLoadAttr:'alt',
		currentNum:false,
		allNum:false,
		startSlide:null,
		noCircle:false,
		pauseOnHover:true,
		autoRotation:false,
		autoHeight:false,
		onBeforeFade:false,
		onAfterFade:false,
		onChange:false,
		disableWhileAnimating:false,
		stopAfterClick:false,
		switchTime:5000,
		duration:650,
		event:'click'
	},_options);

	return this.each(function(){
		// gallery options
		if(this.slideshowInit) return; else this.slideshowInit;
		var _this = jQuery(this);
		var _slides = jQuery(_options.slideElements, _this);
		var _pagerLinks = jQuery(_options.pagerLinks, _this);
		var _generatePagination = jQuery(_options.generatePagination, _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 _dynamicImageLoad = _options.dynamicImageLoad;
		var _dynamicImageLoadAttr = _options.dynamicImageLoadAttr;
		var _autoRotation = _options.autoRotation;
		var _activeClass = _options.activeClass;
		var _loadingClass = _options.loadingClass;
		var _loadedClass = _options.loadedClass;
		var _disabledNextClass = _options.disabledNextClass;
		var _disabledPrevClass = _options.disabledPrevClass;
		var _pausedClass = _options.pausedClass;
		var _playClass = _options.playClass;
		var _autoHeight = _options.autoHeight;
		var _stopAfterClick = _options.stopAfterClick;
		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;
		var _onBeforeFade = _options.onBeforeFade;
		var _onAfterFade = _options.onAfterFade;
		var _disableWhileAnimating = _options.disableWhileAnimating;

		// gallery init
		var _anim = false;
		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);
		if(_autoHeight) _slides.eq(_currentIndex).parent().css({height:_slides.eq(_currentIndex).outerHeight(true)});
		
		// gallery control
		if(_btnPrev.length) {
			_btnPrev.bind(_controlEvent,function(){
				if ( _stopAfterClick ) {
					_autoRotation = false;
					if(_timer) clearTimeout(_timer);
					_this.removeClass(_playClass).addClass(_pausedClass);
				}
				prevSlide();
				return false;
			});
		}
		if(_btnNext.length) {
			_btnNext.bind(_controlEvent,function(){
				if ( _stopAfterClick ) {
					_autoRotation = false;
					if(_timer) clearTimeout(_timer);
					_this.removeClass(_playClass).addClass(_pausedClass);
				}
				nextSlide();
				return false;
			});
		}
		if(_generatePagination.length){
			_generatePagination.empty();
			var _list = jQuery('<ul class="switcher" />').appendTo(_generatePagination);
			for(var i=0; i<_slideCount; i++) jQuery('<li><a href="#">'+(i+1)+'</a></li>').appendTo(_list);
			_pagerLinks = _list.children();
		}
		if(_pagerLinks.length) {
			_pagerLinks.each(function(_ind){
				jQuery(this).bind(_controlEvent,function(){
					if(_currentIndex != _ind) {
						if(_disableWhileAnimating && _anim) return;
						_prevIndex = _currentIndex;
						_currentIndex = _ind;
						if ( _stopAfterClick ) {
							_autoRotation = false;
							if(_timer) clearTimeout(_timer);
							_this.removeClass(_playClass).addClass(_pausedClass);
						}
						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;
			});
		}

		// dynamic image loading (swap from ATTRIBUTE)
		function loadSlide(slide) {
			if(!slide.hasClass(_loadingClass) && !slide.hasClass(_loadedClass)) {
				var images = slide.find(_dynamicImageLoad) // pass selector here
				var imagesCount = images.length;
				if(imagesCount) {
					slide.addClass(_loadingClass);
					images.each(function(){
						var img = this;
						img.onload = function(){
							img.loaded = true;
							img.onload = null;
							setTimeout(reCalc,_duration);
						}
						img.setAttribute('src', img.getAttribute(_dynamicImageLoadAttr));
						img.setAttribute(_dynamicImageLoadAttr,'');
					}).css({opacity:0});

					function reCalc() {
						var cnt = 0;
						images.each(function(){
							if(this.loaded) cnt++;
						});
						if(cnt == imagesCount) {
							slide.removeClass(_loadingClass);
							images.animate({opacity:1},{duration:_duration,complete:function(){
								if(jQuery.browser.msie && jQuery.browser.version < 9) jQuery(this).css({opacity:'auto'})
							}});
							slide.addClass(_loadedClass)
						}
					}
				}
			}
		}

		// gallery animation
		function prevSlide() {
			if(_disableWhileAnimating && _anim) return;
			_prevIndex = _currentIndex;
			if(_currentIndex > 0) _currentIndex--;
			else {
				if(_noCycle) return;
				else _currentIndex = _slideCount-1;
			}
			switchSlide();
		}
		function nextSlide() {
			if(_disableWhileAnimating && _anim) return;
			_prevIndex = _currentIndex;
			if(_currentIndex < _slideCount-1) _currentIndex++;
			else {
				if(_noCycle) return;
				else _currentIndex = 0;
			}
			switchSlide();
		}
		function refreshStatus() {
			if(_dynamicImageLoad) loadSlide(_slides.eq(_currentIndex));
			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(_disabledPrevClass);
					else _btnPrev.removeClass(_disabledPrevClass);
				}
				if(_btnNext.length) {
					if(_currentIndex == _slideCount-1) _btnNext.addClass(_disabledNextClass);
					else _btnNext.removeClass(_disabledNextClass);
				}
			}
			if(typeof _onChange === 'function') {
				_onChange(_this, _slides, _prevIndex, _currentIndex);
			}
		}
		function switchSlide() {
			_anim = true;
			if(typeof _onBeforeFade === 'function') _onBeforeFade(_this, _slides, _prevIndex, _currentIndex);
			_slides.eq(_prevIndex).fadeOut(_duration,function(){
				_anim = false;
			});
			_slides.eq(_currentIndex).fadeIn(_duration,function(){
				if(typeof _onAfterFade === 'function') _onAfterFade(_this, _slides, _prevIndex, _currentIndex);
			});
			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();
	});
}

