// page init
jQuery(function(){
	initZindex();
	initNavigation();
	initFontResize();
	hoverForIE6('ul.bubbles-list li', 'hover');
	initCycleGallery();
})

// hover for IE
function hoverForIE6(_list, _class) {
	var _hoverClass = 'hover';
	if(_class) _hoverClass = _class;
	if (jQuery.browser.msie && jQuery.browser.version < 7) {
		jQuery(_list).hover(function() {
			jQuery(this).addClass(_hoverClass);
		}, function() {
			jQuery(this).removeClass(_hoverClass);
		});
	}
}

// init cycle gallery
function initCycleGallery() {
	var _easing = 'easeInOutQuad';
	var _autoSlide = true;
	var _switchTime = 18000;
	var _speed = 2600;

	$('div.gallery').each(function(){
		// gallery options
		var _holder = $(this);
		var _btnLeft = _holder.find('.link-prev');
		var _btnRight = _holder.find('.link-next');
		var _slidesHolder = _holder.find('.frame-wrap');
		var _slidesHolderWidth = _slidesHolder.width();
		var _slider = _slidesHolder.children();
		var _slides = _slider.children();
		var _slidesCount = _slides.length;
		var _slideWidth = 300;
		var _currentIndex = 0;
		var _sumWidth = 0;
		var _animating;
		var _offset = 0;
		var _timer;

		// gallery init
		_slides.each(function(){_sumWidth+=$(this).outerWidth(true)});
		_slider.append(_slides.clone()).append(_slides.clone());
		_slider.css({marginLeft:-_sumWidth});
		_offset = -_sumWidth;

		// gallery control
		_btnLeft.click(function(){
			prevSlide();
			return false;
		});
		_btnRight.click(function(){
			nextSlide();
			return false;
		});

		// gallery animation
		function prevSlide() {
			if(_animating) return;
			_currentIndex--;
			switchSlide();
		}
		function nextSlide() {
			if(_animating) return;
			_currentIndex++;
			switchSlide();
		}
		function calcOffset(ind) {
			var _ofs = 0;
			_slides.each(function(num){
				if(num<ind) _ofs+= $(this).outerWidth(true);
			});
			return _ofs;
		}

		// anim func
		function switchSlide() {
			_animating = true;
			_offset = -_sumWidth-calcOffset(_currentIndex);

			_slider.animate({marginLeft:_offset},{duration:_speed, easing:_easing, queue:false,complete:function(){
				if(_currentIndex == _slidesCount || _currentIndex == -_slidesCount) {
					_currentIndex = 0;
					_slider.css({marginLeft:-_sumWidth});
					_offset = -_sumWidth;
				}
				_animating = false;
				refreshPopup();
			}});
			autoSlide();
		}
		function autoSlide() {
			if(!_autoSlide) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime);
		}

		// popup show
		var _popupHolder = _holder.find('.popup-holder .info-box-c');
		function getRightMostSlide() {
			var _gSlides = _slider.children();
			var _sumOffset = 0;
			var _searchInd = -1;
			_gSlides.each(function(ind){
				_sumOffset += $(this).outerWidth(true);
				if(_searchInd < 0 && (_sumOffset > Math.abs(_offset)+_slidesHolderWidth)) _searchInd = ind-1;
			});
			return _gSlides.eq(_searchInd);
		}
		function refreshPopup() {
			getRightMostSlide();
			_popupHolder.empty().append(getRightMostSlide().find('.slide-info').clone());
		}

		refreshPopup();
		autoSlide();
	});
}

// autoscaling menu
function initNavigation() {
	initAutoScalingNav({
		menuId: "main-nav",
		spacing: 2
	});
	initAutoScalingNav({
		menuId: "main-nav-2",
		sideClasses: true,
		tag: "a"
	});
	initAutoScalingNav({
		menuId: "main-nav-3",
		spacing: 12,
		constant: -10,
		sideClasses: true
	});
	initAutoScalingNav({
		menuId: "main-nav-4",
		sideClasses: true
	});
	initAutoScalingNav({
		menuId: "main-nav-5",
		sideClasses: true,
		tag: "a"
	});
}

// zIndex fix
function initZindex() {
	var sets = document.getElementsByTagName("ul");
	for (var i = 0; i < sets.length; i++)	{
		if (sets[i].className.indexOf("gallery-nav") != -1) {
			var lists = sets[i].getElementsByTagName("li");
			var list_len = lists.length;
			for (var j = 0; j < lists.length; j++) {
			 lists[j].style.zIndex = list_len;
			 list_len--;
			}
		}
	}
}

function initAutoScalingNav(o) {
	if (!o.menuId) o.menuId = "main-nav";
	if (!o.tag) o.tag = "a";
	if (!o.spacing) o.spacing = 0;
	if (!o.constant) o.constant = 0;
	if (!o.minPaddings) o.minPaddings = 0;
	if (!o.liHovering) o.liHovering = false;
	if (!o.sideClasses) o.sideClasses = false;
	var nav = document.getElementById(o.menuId);
	if(nav)
	{
		var lis = nav.getElementsByTagName("li");
		var asFl = [];
		var lisFl = [];
		for (var i=0, j=0; i<lis.length; i++)
		{
			if(lis[i].parentNode == nav)
			{
				var t = lis[i].getElementsByTagName(o.tag).item(0);
				asFl.push(t);
				asFl[j++].width = t.offsetWidth;
				lisFl.push(lis[i]);
			}
			if(o.liHovering)
			{
				lis[i].onmouseover = function()
				{
					this.className += " hover";
				}
				lis[i].onmouseout = function()
				{
					this.className = this.className.replace("hover", "");
				}
			}
		}
		var menuWidth = nav.clientWidth - asFl.length*o.spacing - o.constant;
		if(getItemsWidth(asFl) < menuWidth)
		{
			for (var i=0; getItemsWidth(asFl) < menuWidth; i++)
			{
				asFl[i].width++;
				if(i >= asFl.length-1) i=-1;
			}
			for (var i=0; i<asFl.length; i++)
			{
				asFl[i].style.width = asFl[i].width + "px";
			}
		}
		else if(o.minPaddings > 0)
		{
			for (var i=0; i<asFl.length; i++)
			{
				asFl[i].style.width = asFl[i].width + o.minPaddings*2 + "px";
			}
		}
	}
	function getItemsWidth(a)
	{
		var w = 0;
		for(var q=0; q<a.length; q++)
		{
			w += a[q].width;
		}
		return w;
	}
	if(o.sideClasses)
	{
		if(lisFl && lisFl.length) {
			lisFl[0].className += " first-child";
			lisFl[lisFl.length-1].className += " last-child";
		}
	}
}

/* Text changer - light version.
Let your text's font size customizable.
by Marco Rosella - http://www.centralscrutinizer.it/en/design/js-php/text-changer
v0.2 - May 18, 2006
*/
function initFontResize() {
	textChanger.init();
}
var textChanger = {
	defaultFS : 1.2,
	init: function() {
		var el = document.getElementsByTagName("body")[0];
		var sz = textChanger.getCookie();
		el.style.fontSize = sz ? sz + 'em' : textChanger.defaultFS + 'em';
		var incr = document.getElementById('increase');
		if(incr)
			incr.onclick = function(){textChanger.changeSize(1); return false;};
		var decr = document.getElementById('decrease');
		if(decr)
			decr.onclick = function(){textChanger.changeSize(-1); return false;};
		var reset = document.getElementById('reset');
		if(reset)
			reset.onclick = function(){textChanger.changeSize(0); return false;};
	},
	changeSize: function(val) {
		var el = document.getElementsByTagName("body")[0];
		var size = el.style.fontSize.substring(0, 4);
		var fSize = parseFloat(size, 10);
		if (val == 1)	{
			fSize += 0.1;
			if (fSize > textChanger.defaultFS*1.5) fSize = textChanger.defaultFS*1.5;
		}
		if (val == -1) {
			fSize -= 0.1;
			if (fSize < textChanger.defaultFS/1.5) fSize = textChanger.defaultFS/1.5;
		}
		if (val == 0) fSize = textChanger.defaultFS;
		el.style.fontSize = fSize.toFixed(2) + 'em';
		textChanger.updateCookie(fSize);
	},
	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));
	}
}

/* jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/ */
jQuery.easing['jswing'] = jQuery.easing['swing'];
jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		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;
	}
});