function gallery(_obj) {
	// defaults options
	var _this = this;
	_this.options = {
		hold: 'div.gallery-slide',
		duration: 700,
		slideElement: 1,
		autoRotation: false,
		effect: false,
		listOfSlides: 'ul > li',
		switcher: false,
		disableBtn: false,
		nextBtn: 'a.link-next, a.btn-next, a.next',
		prevBtn: 'a.link-prev, a.btn-prev, a.prev',
		circle: true,
		direction: false
	};
	for(key in _obj) _this.options[key] = _obj[key];
	
	// Variables
	$(_this.options.hold).each(function(){
		var _hold = $(this);
		var _speed = _this.options.duration;
		var _timer = _this.options.autoRotation;
		var _el = _hold.find(_this.options.listOfSlides);
		if (_this.options.effect) var _list = _el;
		else var _list = _el.parent();
		var _switcher = _hold.find(_this.options.switcher);
		var _next = _hold.find(_this.options.nextBtn);
		var _prev = _hold.find(_this.options.prevBtn);
		var _count = _el.index(_el.filter(':last'));
		var _w = _el.outerWidth(true);
		var _h = _el.outerHeight(true);
		if (_timer) var _t;
		if (_this.options.switcher) var _active = _switcher.index(_switcher.filter('.active:eq(0)'));
		else var _active = _el.index(_el.filter('.active:eq(0)'));
		if (_active < 0) _active = 0;
		var _last = _active;
		
		// Installation directions
		if (!_this.options.direction) {
			var _wrapHolderW = Math.ceil(_list.parent().width() / _w);
			if (((_wrapHolderW - 1) * _w + _w / 2) > _list.parent().width()) _wrapHolderW--;
		}
		else{
			var _wrapHolderW = Math.ceil(_list.parent().height()/_h);
			if (((_wrapHolderW-1)*_h + _h/2) > _list.parent().height()) _wrapHolderW--;
		}
		
		// Setting "fade" or "slide" effect
		if (!_this.options.effect) var rew = _count - _wrapHolderW + 1;
		else var rew = _count;
		if (!_this.options.effect) {
			if (!_this.options.direction) _list.css({marginLeft: -(_w * _active)})
			else _list.css({marginTop: -(_h * _active)})
		}
		else {
			_list.css({
				opacity: 0
			}).removeClass('active').eq(_active).addClass('active').css({
				opacity: 1
			}).css('opacity', 'auto');
			_switcher.removeClass('active').eq(_active).addClass('active');
		}
		
		// Disable or enable buttons "prev next"
		if (_this.options.disableBtn) {
			if (_count < _wrapHolderW) _next.addClass(_this.options.disableBtn);
			_prev.addClass(_this.options.disableBtn);
		}
		
		// Function to "fade"
		function fadeElement(){
			if (_last != _active) {
				_list.eq(_last).animate({opacity:0}, {queue:false, duration: _speed});
				_list.removeClass('active').eq(_active).addClass('active').animate({
					opacity:1
				}, {queue:false, duration: _speed, complete: function(){
					$(this).css('opacity','auto');
				}});
				if (_this.options.switcher) _switcher.removeClass('active').eq(_active).addClass('active');
				_last = _active;
			}
		}
		
		// Function for "slide"
		function scrollEl(){
			if (_last != _active) {
				if (!_this.options.direction) _list.animate({marginLeft: -(_w * _active)}, {queue:false, duration: _speed})
				else _list.animate({marginTop: -(_h * _active)}, {queue:false, duration: _speed})
				if (_this.options.switcher) _switcher.removeClass('active').eq(_active).addClass('active');
				_last = _active;
			}
		}
		function toPrepare(){
			if ((_active == rew) && _this.options.circle) _active = -_this.options.slideElement;
			for (var i = 0; i < _this.options.slideElement; i++){
				_active++;
				if (_active > rew) {
					_active--;
					if (_this.options.disableBtn &&(_count > _wrapHolderW)) _next.addClass(_this.options.disableBtn);
				}
			};
			if (_active == rew) if (_this.options.disableBtn &&(_count > _wrapHolderW)) _next.addClass(_this.options.disableBtn);
			if (!_this.options.effect) scrollEl();
			else fadeElement();
		}
		function runTimer(){
			_t = setInterval(function(){
				toPrepare();
			}, _timer);
		}
		_next.click(function(){
			if(_t) clearTimeout(_t);
			if (_this.options.disableBtn &&(_count > _wrapHolderW)) _prev.removeClass(_this.options.disableBtn);
			toPrepare();
			if (_timer) runTimer();
			return false;
		});
		_prev.click(function(){
			if(_t) clearTimeout(_t);
			if (_this.options.disableBtn &&(_count > _wrapHolderW)) _next.removeClass(_this.options.disableBtn);
			if ((_active == 0) && _this.options.circle) _active = rew + _this.options.slideElement;
			for (var i = 0; i < _this.options.slideElement; i++){
				_active--;
				if (_active < 0) {
					_active++;
					if (_this.options.disableBtn &&(_count > _wrapHolderW)) _prev.addClass(_this.options.disableBtn);
				}
			};
			if (_active == 0) if (_this.options.disableBtn &&(_count > _wrapHolderW)) _prev.addClass(_this.options.disableBtn);
			if (!_this.options.effect) scrollEl();
			else fadeElement();
			if (_timer) runTimer();
			return false;
		});
		if (_this.options.switcher) _switcher.click(function(){
			_active = _switcher.index($(this));
			if(_t) clearTimeout(_t);
			if (!_this.options.effect) scrollEl();
			else fadeElement();
			if (_timer) runTimer();
			return false;
		});
		if (_timer) runTimer();
		
		_this.stop = function(){
			if (_t) clearTimeout(_t);
		}
		_this.play = function(){
			if (_t) clearTimeout(_t);
			if (_timer) runTimer();
		}
	});
}

$(document).ready(function(){
	var gal1 = new gallery({
		hold: 'div.gallery-section',
		autoRotation: 5000,
		duration: 800,
		circle: true,
		listOfSlides: 'ul > li',
		effect: 'fade'
	});

});

$(document).ready(function(){
	//global vars
	var searchBoxes = $("#s");
	var searchBox2 = $(".search #s");
	var searchBox2Default = "Search the site...";
	
	//Effects for both searchbox
	searchBoxes.focus(function(e){
		$(this).addClass("active");
	});
	searchBoxes.blur(function(e){
		$(this).removeClass("active");
	});
	
	//Searchbox2 show/hide default text if needed
	searchBox2.focus(function(){
		if($(this).attr("value") == searchBox2Default) $(this).attr("value", "");
	});
	searchBox2.blur(function(){
		if($(this).attr("value") == "") $(this).attr("value", searchBox2Default);
	});
});

$(document).ready(function(){
	//global vars
	var searchBoxes = $("#s");
	var searchBox2 = $("#mce-EMAIL");
	var searchBox2Default = "Your Email";
	
	//Effects for both searchbox
	searchBoxes.focus(function(e){
		$(this).addClass("active");
	});
	searchBoxes.blur(function(e){
		$(this).removeClass("active");
	});
	
	//Searchbox2 show/hide default text if needed
	searchBox2.focus(function(){
		if($(this).attr("value") == searchBox2Default) $(this).attr("value", "");
	});
	searchBox2.blur(function(){
		if($(this).attr("value") == "") $(this).attr("value", searchBox2Default);
	});
	
	
	function initPage() {
	initAutoScalingNav({
		menuId: "menu-primary",
		sideClasses: true,
		minPaddings: 20
	});
}
function initAutoScalingNav(o) {
	if (!o.menuId) o.menuId = "menu-primary";
	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 += " page-item-23";
			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;
	}
}
	
	
});
