//init page
$(function(){
	initAutoscalingMenu();
	initCycleGallery();
});

//init autoscaling menu
function initAutoscalingMenu() {
	initAutoScalingNav({
		menuId: "nav",
		sideClasses: true
	});
}

function initCycleGallery(){
	var gallerys = $('.container');
	gallerys.each(function(){
		var gallery = $(this);
		var holder = $('.carousel-frame', gallery);
		var slider = $('>ul', holder);
		var slides = $('>li', slider);
		var btnNext = $('.btn-next1', gallery);
		var btnPrev = $('.btn-prev1', gallery);
		var switchSlide = 2000;
		var timer;
		
		var activeClass = 'active';
		var speed = 650;
		var autoHeight = false;
		
		slider.css({position:'raletive'});
		slides.css({position:'absolute',top:0,left:0});
		
		var curInd = 0;
		var oldInd = curInd;
		var maxOffset = holder.width();
		var prevOffset = 0;
		var maxHeight = 0;
		var totalSlides = slides.length-1;
		var maxLeft = 0;
		
		slides.removeClass(activeClass).eq(curInd).addClass(activeClass);
		slides.each(function(ind){
			var slide = $(this);
			slide.css('left',prevOffset);
			prevOffset += slide.outerWidth(true);
			maxLeft = prevOffset;
			if(autoHeight){
				if(slide.outerHeight(true) > maxHeight){
					maxHeight = slide.outerHeight(true);
				}
			}
		});
		if (autoHeight) {
			slider.height(maxHeight);
		}
		slider.hover(function(){
			clearTimeout(timer);
		},function(){
			autoRotation();
		});
		
		function goNextSlide(){
			clearTimeout(timer);
			oldInd = curInd;
			curInd++;
			if(curInd > totalSlides){
				curInd = 0;
			}
			slides.each(function(ind){
				var slide = $(this);
				slide.stop().animate({
					left: parseInt($(this).css('left')) - slides.eq(oldInd).outerWidth(true)
				}, {
					queue: false,
					duration: speed,
					complete: function(){
						if(parseInt(slide.css('left')) + slide.outerWidth(true) > maxLeft){
							maxLeft = parseInt(slide.css('left')) + slide.outerWidth(true);
						}
						slides.removeClass(activeClass).eq(curInd).addClass(activeClass);
						if(ind == oldInd){
							slide.hide().css('left',maxLeft-slide.outerWidth(true)).show();
						}
						autoRotation();
					}
				});
			});
		}
		function goPrevSlide(){
			clearTimeout(timer);
			slides.eq(curInd).hide().css('left',-slides.eq(curInd).outerWidth(true)).show();
			slides.each(function(ind){
				var slide = $(this);
				slide.animate({
					left: parseInt($(this).css('left')) + slides.eq(curInd).outerWidth(true)
				}, {
					queue: false,
					duration: speed,
					complete: function(){
						if(parseInt(slide.css('left')) + slide.outerWidth(true) > maxLeft){
							maxLeft = parseInt(slide.css('left')) + slide.outerWidth(true);
						}
						slides.removeClass(activeClass).eq(curInd).addClass(activeClass);
						autoRotation();
					}
				});
			});
		}
		function autoRotation(){
			if(switchSlide){
				clearTimeout(timer);
				timer = setTimeout(goNextSlide,switchSlide);
			}
		}
		
		btnNext.click(function(){
			if(!slides.is(':animated')){
				oldInd = curInd;
				curInd++;
				if(curInd > totalSlides){
					curInd = 0;
				}
				goNextSlide();
			}
			return false;
		});
		btnPrev.click(function(){
			if(!slides.is(':animated')){
				oldInd = curInd;
				curInd--;
				if(curInd < 0){
					curInd = totalSlides;
				}
				goPrevSlide();
			}
			return false;
		});
		autoRotation();
	});
}

//autoscaling navigation
function initAutoScalingNav(o) {
	if (!o.menuId) o.menuId = "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;
	if (!o.equalLinks) o.equalLinks = false;
	if (!o.flexible) o.flexible = false;
	var nav = document.getElementById(o.menuId);
	if(nav) {
		nav.className += " scaling-active";
		var lis = nav.getElementsByTagName("li");
		var asFl = [];
		var lisFl = [];
		var width = 0;
		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(width < t.offsetWidth) width = t.offsetWidth;
			}
			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(o.equalLinks && width * asFl.length < menuWidth) {
			for (var i=0; i<asFl.length; i++) {
				asFl[i].width = width;
			}
		}
		width = getItemsWidth(asFl);
		if(width < menuWidth) {
			var version = navigator.userAgent.toLowerCase();
			for (var i=0; getItemsWidth(asFl) < menuWidth; i++) {
				asFl[i].width++;
				if(!o.flexible) {
					asFl[i].style.width = asFl[i].width + "px";
				}
				if(i >= asFl.length-1) i=-1;
			}
			if(o.flexible) {
				for (var i=0; i<asFl.length; i++) {
					width = (asFl[i].width - o.spacing - o.constant/asFl.length)/menuWidth*100;
					if(i != asFl.length-1) {
						lisFl[i].style.width = width + "%";
					}
					else {
						if(navigator.appName.indexOf("Microsoft Internet Explorer") == -1 || version.indexOf("msie 8") != -1 || version.indexOf("msie 9") != -1)
							lisFl[i].style.width = width + "%";
					}
				}
			}
		}
		else if(o.minPaddings > 0) {
			for (var i=0; i<asFl.length; i++) {
				asFl[i].style.paddingLeft = o.minPaddings + "px";
				asFl[i].style.paddingRight = o.minPaddings + "px";
			}
		}
		if(o.sideClasses) {
			lisFl[0].className += " first-child";
			lisFl[0].getElementsByTagName(o.tag).item(0).className += " first-child-a";
			lisFl[lisFl.length-1].className += " last-child";
			lisFl[lisFl.length-1].getElementsByTagName(o.tag).item(0).className += " last-child-a";
		}
		nav.className += " scaling-ready";
	}
	function getItemsWidth(a) {
		var w = 0;
		for(var q=0; q<a.length; q++) {
			w += a[q].width;
		}
		return w;
	}
}


