jQuery.noConflict();
jQuery(function(){
	initAutoScalingNav({
		menuId: "nav",
		tag: "a",
		spacing: 2,
		constant: 0,
		sideClasses: "true"
	});
	initGallery({
		holder:'.g1-hold',
		list:'ul.g1',
		autoRotation:7000
	});
});

function initGallery(option){
	var hold = jQuery(option.holder);
	var duration = option.autoRotation;
	var activeSlide = option.activeSlide || 0;
	var step = option.step || 1;
	var disableBtn = option.disableBtn || false;
	var switcher = option.switcher || false;
	var event = option.event || 'click';
	var onStart = option.onStart;
	var onChange = option.onChange;
	hold.each(function(){
		var _this = jQuery(this);
		var list = _this.find(option.list),
			count = list.children().length,
			w = list.children().outerWidth() * step,
			_t,
			a = 0,
			r = a,
			f = true;

		var holdW = list.parent().width();
		var visEl = Math.ceil(holdW/w);
		
		if(count <= visEl) f = false;
		
		list.css({marginLeft:-w*a});
		
		if(typeof onStart === 'function') onStart(list, a);
		
		if(option.prev && option.next){
			var prev = _this.find(option.prev).attr('rel', 'prev').click(animateSlide);
			var next = _this.find(option.next).attr('rel', 'next').click(animateSlide);
		}
		if(option.switcher){
			switcher = _this.find(switcher);
			switcher.eq(r).removeClass('active');
			switcher.eq(a).addClass('active');
			switcher.bind(event, function(){
				var ind = switcher.index(jQuery(this))+activeSlide;
				animateSlide(ind);
				return false;
			});
		}

		if(disableBtn){
			if(a == 0 && count > 1) {
				prev.addClass('disabled');
				next.removeClass('disabled');
			}
			if(a == count-1 && count > 1) {
				prev.removeClass('disabled');
				next.addClass('disabled');
			}
			if(count == 1 || count <= visEl) {
				prev.addClass('disabled');
				next.addClass('disabled');
			}
		}
		
		if(option.autoRotation && f) runTimer();
		if(option.effect == 'fade') {
			list.children().css('opacity', 0);
			list.children().eq(a).css('opacity', 1).addClass('active');
		}
		if(option.stopOnHover && _t){
			list.mouseenter(function(){
				clearTimeout(_t);
			}).mouseleave(runTimer);
		}
		function runTimer(){
			_t = setTimeout(function(){
				animateSlide('next');
			}, duration);
		}
		function animateSlide(e){
			if(f){
				r = a;
				if(typeof e == 'string' && e == 'next') a++;
				else if(typeof e == 'number') a=e;
				else{
					if(e.target.rel == 'next') a++;
					else if(e.target.rel == 'prev') a--;
				}
				if(_t) clearTimeout(_t);
				if(a == count) a=0;
				else if(a == -1) a=count-1;
				
				list.children().eq(r).removeClass('active');
				list.children().eq(a).addClass('active');
				
				if(option.switcher){
					switcher.eq(r).removeClass('active');
					switcher.eq(a).addClass('active');
				}
				
				if(disableBtn){
					if(a == 0) {
						prev.addClass('disabled');
						next.removeClass('disabled');
					}
					else if(a + visEl == count) {
						prev.removeClass('disabled');
						next.addClass('disabled');
					}
					else {
						prev.removeClass('disabled');
						next.removeClass('disabled');
					}
				}
				
				if(option.effect == 'fade'){
					list.children().eq(r).animate({opacity:0}, {queue:false, duration:700});
					list.children().eq(a).animate({opacity:1}, {queue:false, duration:700, complete:function(){
						if(option.autoRotation) runTimer();
					}});
				}
				else{
					if(a + visEl > count) a = 0;
					
					list.animate({marginLeft:-w*a}, {queue:false, duration:700, complete:function(){
						if(option.autoRotation) runTimer();
						if(typeof onChange === 'function') onChange(list, a);
					}});
				}
			}
			return false;
		}
	});
};

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;
	}
}
