// main page init function
$(function() {
	//initOptionsMenu();
	//initSlideShow();
	initLogoGallery();
});


// init scroll gallery
function initLogoGallery() {
	var _activeClass = 'active';
	var _switchTime = 5000;
	var _slideSpeed = 750;
	var _autoSlide = true;

	$('div.logo-gallery').each(function(){
		var _holder = $(this);
		var _btnPrev = _holder.find('.btn-prev');
		var _btnNext = _holder.find('.btn-next');
		var _descriptions = _holder.find('ul.description li');
		var _slidesHolder = _holder.find('.logos-holder');
		var _slider = _slidesHolder.find('>ul.logos');
		var _slideItems = _slider.find('>li');
		var _slideCount = _slideItems.length;
		var _slideWidth = _slideItems.eq(0).outerWidth(true);
		var _diff = _slideWidth - _slideItems.eq(0).width();
		var _sumWidth = _slideWidth*_slideCount;
		var _currentIndex = 0;
		var _offset = 0;
		var _animating = false;
		var _timer;

		// gallery control
		_slider.append(_slideItems.clone()).append(_slideItems.clone());
		_btnPrev.click(function(){
			prevSlide();
			return false;
		});
		_btnNext.click(function(){
			nextSlide();
			return false;
		});

		// switch description function
		function switchDescriptions(_imm) {
			if(_imm) var _slideSpeed=0;
			_descriptions.filter(':visible').fadeOut(_slideSpeed);
			_descriptions.eq((_currentIndex<_slideCount-1 ? _currentIndex+1 : 0)).fadeIn(_slideSpeed);
		}

		// gallery animation
		function prevSlide() {
			if(_animating) return;
			_currentIndex--;
			switchLogos();

			if(_timer) clearTimeout(_timer);
			if(_autoSlide) _timer = setTimeout(function(){nextSlide()},_switchTime);
		}
		function nextSlide() {
			if(_animating) return;
			_currentIndex++;
			switchLogos();

			if(_timer) clearTimeout(_timer);
			if(_autoSlide) _timer = setTimeout(function(){nextSlide()},_switchTime);
		}
		function calcOffset() {
			_offset = -_sumWidth -_currentIndex*_slideWidth - (_slideWidth/2+_diff/2);
		}
		function switchLogos(_imm) {
			if(_imm) var _slideSpeed=0;
			_animating = true;
			calcOffset();
			_slider.animate({marginLeft:_offset},{duration:_slideSpeed, queue:false, easing:'easeInExpo', complete:function(){
				if(_currentIndex > _slideCount-1) {
					_currentIndex = 0;
					calcOffset();
					_slider.css({marginLeft:_offset});
				}
				if(_currentIndex < 0) {
					_currentIndex = _slideCount-1;
					calcOffset();
					_slider.css({marginLeft:_offset});
				}
				switchDescriptions(_imm);
				_animating = false;
			}});
		}
		switchLogos(true);

		// autoslide
		_holder.hover(function(){
			if(_timer) clearTimeout(_timer);
		},function(){
			if(_autoSlide) _timer = setTimeout(function(){nextSlide()},_switchTime);
		});
		if(_autoSlide) _timer = setTimeout(function(){nextSlide()},_switchTime);

	});
}




// cookies service functions
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.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 ) );
}
function setCookie(name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}
function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

/* slideshow plugin */
jQuery.fn.slideShow = function(_options){
	var _options = jQuery.extend({
		slideEl:'div.img-hold img',
		linkNext:'a.next',
		linkPrev:'a.prev',
		linkPause:'a.pause',
		numElementLink:'div.nav li a',
		duration:500,
		autoSlideShow:false,
		autoHeight:false,
		switchTime:3000,
		event:'click',
		currentEl:'#footer span.cur',
		allEl:'#footer span.all'
	},_options);

	return this.each(function(){
		var _THIS = jQuery(this);
		var _slideEl = $(_options.slideEl, _THIS);
		var _elTagName = _options.slideEl;
		var _linkNext = $(_options.linkNext, _THIS).length ? $(_options.linkNext, _THIS) : false;
		var _linkPrev = $(_options.linkPrev, _THIS).length ? $(_options.linkPrev, _THIS) : false;
		var _linkPause = $(_options.linkPause, _THIS).length ? $(_options.linkPause, _THIS) : false;
		var _numElementLink = $(_options.numElementLink, _THIS).length ? $(_options.numElementLink, _THIS) : false;
		var _numElement = $(_options.numElementLink, _THIS).length ? _numElementLink.parent() : false;
		var _duration = _options.duration;
		var _switchTime = _options.switchTime;
		var _numElActive, _n, _timer = false, _hover = false;

		if (!_slideEl.filter('.active').length)
			_slideEl.eq(0).addClass('active');
		$(_slideEl).not(".active").css('display','none');

		if ($(_numElement).length) {activeNumEl()}

		if (_options.autoSlideShow) {
			_timer = setTimeout(function(){nextEl()},_switchTime);
			contentHover();
		}
		if (_linkNext) {
			$(_linkNext).click(function(){
				if (_timer) {
					clearTimeout(_timer);
					_timer = setTimeout(function(){nextEl()},_switchTime);
				}
				if ($(_slideEl).filter(".active").next().is(_elTagName)) {
					$(_slideEl).filter(".active").next().addClass("next");
				} else {
					$(_slideEl).eq(0).addClass("next");
				}
				fadeElement();
				activeNumEl();
				return false;
			});
		}
		if (_linkPrev) {
			$(_linkPrev).click(function(){
				if (_timer) {
					clearTimeout(_timer);
					_timer = setTimeout(function(){nextEl()},_switchTime);
				}
				if ($(_slideEl).filter(".active").prev().is(_elTagName)) {
					$(_slideEl).filter(".active").prev().addClass("next");
				} else {
					$(_slideEl).filter(":last-child").addClass("next");
				}
				fadeElement();
				activeNumEl();
				return false;
			});
		}
		if (_linkPause) {
			if (!_options.autoSlideShow) {
				_linkPause.addClass('play');
				_options.autoSlideShow = true;
				if (!_hover) contentHover();
			}
			$(_linkPause).click(function(){
				if (!_linkPause.is('.play')) {
					clearTimeout(_timer);
					_linkPause.addClass('play');
				} else {
					_linkPause.removeClass('play');
					_timer = setTimeout(function(){nextEl()},_switchTime);
				}
				return false;
			});
		}
		if (_numElementLink) {
			$(_numElementLink).bind(_options.event, function(){
				if (_timer) {
					clearTimeout(_timer);
					_timer = setTimeout(function(){nextEl()},_switchTime);
				}
				_n = _numElement.index($(this).parent());
				if (!$(_slideEl).eq(_n).hasClass("active")){
					$(_slideEl).eq(_n).addClass("next");
					fadeElement();
					activeNumEl();
				}
				return false;
			});
		}
		if (_options.currentEl || _options.allEl) {
			_THIS.all = _slideEl.length;
			$(_options.allEl, _THIS).html(_THIS.all);
			function currentNum() {
				var _index = _slideEl.index(_slideEl.filter('.active')) + 1;
				$(_options.currentEl, _THIS).html(_index);
			}
			currentNum();
		}
		function contentHover() {
			_hover = true;
			_slideEl.hover(function() {
				if (_timer) clearTimeout(_timer);
			}, function(){
				if (_linkPause && !_linkPause.is('.play'))
					_timer = setTimeout(function(){nextEl()},_switchTime);
				else if (!_linkPause)
					_timer = setTimeout(function(){nextEl()},_switchTime);
			});
		}
		function nextEl(){
			if ($(_slideEl).filter(".active").next().is(_elTagName)){
				$(_slideEl).filter(".active").next().addClass("next");
			} else {
				$(_slideEl).eq(0).addClass("next");
			}
			fadeElement();
			activeNumEl();
			_timer = setTimeout(function(){nextEl()},_switchTime);
		};
		function activeNumEl() {
			$(_numElement).removeClass("active");
			_numElActive = $(_slideEl).index($(_slideEl).filter(".active")[0]);
			$(_numElement).eq(_numElActive).addClass("active");
		};
		function fadeElement(){
			$(_slideEl).filter(".active").fadeOut(_duration).removeClass("active");
			$(_slideEl).filter(".next").fadeIn(_duration).addClass("active").removeClass("next");
			if (_options.autoHeight) $(_slideEl).filter(".active").eq(0).parent().animate({height:$(_slideEl).filter(".active").eq(0).height()},{duration:_duration});
			if (_options.currentEl || _options.allEl) currentNum();
		};
	});
}


/* jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/ */
// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});
