(function(jquery) {
	function Gallery(el, options) {
		this._hold = jquery(el);
		this.initOptions(options);
		this.initialize();
	}
	
	jquery.fn.gallery = function(options) {
		return new Gallery(this.get(0), options);
	};
	
	Gallery.prototype = {
		initOptions: function(_obj){
			this.options = {
				duration: 2500,
				durationTwo: 50,
				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];
		},
		initialize: function(){
			var _bg = jquery('#promoHolder');
		
			var _this = this;
			var _hold = _this._hold;
			var _speed = _this.options.duration;
			var _speedTwo = _this.options.durationTwo;
			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(){
				
				// hide the image and text
				_list.eq(_last).animate({opacity:0}, {queue:false, duration: _speedTwo});
				// hide the background				
				_bg.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');
				}});
*/
				
				// remove active class from current list item
				_list.removeClass('active');
				// add active class to next list item
				_list.eq(_active).addClass('active');
				
				// find out the classes assigned to the new list item
				var classes = _list.eq(_active).attr("class");
				// strip out the active one from the list
				theClass = classes.replace(' active', '');
				// remove all classes from the background
				_bg.removeClass();
				// reveal the new background
				_bg.addClass(theClass).animate({opacity:1}, {queue:false, duration: _speed });
				// reveal the new image and text
				_list.eq(_active).animate({
					opacity:1
				}, {queue:false, duration: _speed, complete: function(){
					jquery(this).css('opacity','auto');
				}});
				
				if (_this.options.switcher) _switcher.removeClass('active').eq(_active).addClass('active');
				_last = _active;
			}
			
			// Function for "slide"
			function scrollEl(){
				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');
					
				} 
				
				
			}
			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(jquery(this));
				if(_t) clearTimeout(_t);
				if (!_this.options.effect) scrollEl();
				else fadeElement();
				//if (_timer) runTimer(); // turn of auto startup after clicking nav item
				return false;
			});
			if (_timer) runTimer();
		},
		stop: function(){
			alert(1);
			if (_t) clearTimeout(_t);
		},
		play: function(){
			if (_t) clearTimeout(_t);
			if (_timer) runTimer();
		}
	}
}(jQuery));

j(function(){
	initCufon();
	//$('#wrapper').myPopup();
	/*
$('div.gallery').gallery({
		duration: 500,
		autoRotation: 6000,
		listOfSlides: 'ul.gallery-list > li',
		switcher: 'ul.switcher > li',
		effect: 'fade'
	});
*/
});
function initCufon(){
	Cufon.replace('.gallery .mask .txt-box p', {fontFamily: '"gillsans"',hover: true});
	Cufon.replace('#content .title, #sidebar h3', {fontFamily: '"gillsans"',hover: true});
	Cufon.replace('.columns-holder .column h1, .columns-holder .column h2, .columns-holder .column h3', {fontFamily: '"gillsans"',hover: true});
	Cufon.replace('.category-widget h3, .btn-contact strong', {fontFamily: '"gillsans"',hover: true});
	Cufon.replace('.some-post .post-title h1', {fontFamily: '"gillsans"',hover: true});
	Cufon.replace('#respond h3', {fontFamily: '"gillsans"',hover: true});
	Cufon.replace('.popup popup-title', {fontFamily: '"gillsans"',hover: true});
	Cufon.replace('.gallery .txt-box-l, .gallery .txt-box-r', {fontFamily: '"gillsans"',hover: true});

}

jQuery.fn.myPopup = function(_options){
	/*
// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		linkOpenName: '.link-popup',
		linkCloseName: 'a.close, a.btn-close',
		divFader: 'fader',
		wrapper: '#wrapper'
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = _options.duration;
		var _IE = $.browser.msie;
		var links = _hold.find(_options.linkOpenName);
		var _fader = $('<div class="'+_options.divFader+'"></div>');
		var _select = $(_options.wrapper).find('select');
		var popup;
		$('body').append(_fader);
		_fader.css({
			position: 'absolute',
			top: '0px',
			left: '0px',
			zIndex: 999,
			background: 'black',
			opacity: 0.7
		});
		
		function init(_obj){
			popup = $(_obj);
			var btnClose = popup.find(_options.linkCloseName);
			var submitBtn = popup.find('.link-submit');
			var closeBtn = popup.find('.close-link');
			
			
			if (_IE) _select.css({visibility: 'hidden'});
			var w = $('body').width();
			var _w = $(_options.wrapper).width();
			if (_w > w) w =_w;
			var h = $(window).height();
			var _offset = $('html').scrollTop();
			
			var ret = _offset+(h/2) - popup.outerHeight(true)/2;
			if (ret < 0) ret = 0;
			var te = $(_options.wrapper).height();
			if ($(window).height() > te) te = $(window).height();
			
			popup.css({
				top: ret,
				left: w/2 - popup.outerWidth(true)/2
			}).hide();
			
			_fader.css({
				width: w,
				height: te
			}).fadeIn(300, function(){
				popup.fadeIn(300);
			});
			
			$(window).resize(function(){
				w = $('body').width();
				_w = $(_options.wrapper).width();
				if (_w > w) w =_w;
				popup.animate({
					left: w/2 - popup.outerWidth(true)/2
				}, {queue:false, duration: 300});
				_fader.css({
					width: w
				});
			});
			
			function closedPopup(opt1){
				popup.fadeOut(300, function(){
					popup.css({left: '-9999px'}).show();
					if (_IE) _select.css({visibility: 'visible'});
					submitBtn.unbind('click');
					$(window).unbind('resize');
					if (opt1) _fader.hide();
					else {
						if (submitBtn.attr('href')) init(submitBtn.attr('href'));
						else init(submitBtn.attr('title'));
					}
				});
			}
			
			/*
			btnClose.click(function(){
				closedPopup(true);
				return false;
			});
			
			submitBtn.click(function(){
				closedPopup();
				return false;
			})
		
			
			closeBtn.click(function(){
				closedPopup(true);
				return false;
			});
			/*
			_fader.click(function(){
				closedPopup(true);
				return false;
			});
			
		}
		links.click(function(){
			if ($(this).attr('href')) init($(this).attr('href'));
			else init($(this).attr('title'));
			return false;
		});
	});
*/
}