$(function(){
	initGallery();
	$('#wrapper').myPopup();
	initSmooth();
});
function initSmooth(){
	var _link = $('.smooth-link');
	_link.each(function(){
		var link = $(this);
		var smooth = link.parent().find('> .hidden');
		link.mouseenter(function(){
			if(link.parents('div#sidebar').find('a.hidden').filter(':visible').length >= 1){
				$('div#sidebar a.hidden').hide();
				$('div#sidebar a.smooth-link').show();
			}
			if(!link.parent().hasClass('active')){
				if(link.parents('div#sidebar').length) link.hide();
				else link.fadeOut();
				smooth.fadeIn();
			}
		});
		smooth.mouseleave(function(){
			if(!link.parent().hasClass('active')){
				smooth.fadeOut();
				link.fadeIn();
			}
		});	
	});
}
function initGallery(){
	$('div.carousel').gallery({
		autoRotation: 5000,
		effect:'fade',
		listOfSlides:'div.carousel-holder > ul > li',
		switcher:'div.switcher > ul > li'
	});
	$('div.carousel2').gallery({
		autoRotation: 5000,
		listOfSlides:'div.carousel-frame > ul > li',
		effect: true
	});
	// $('div.carousel3').gallery({
		// autoRotation: 5000,
		// listOfSlides:'div.carousel-frame > ul > li',
		// effect: true,
		// IE: true
	// });
	$('div.g1').gallery({
		autoRotation: 5000,
		listOfSlides:'div.carousel-frame > ul > li',
		effect: true,
		switcher:'ul.gallery > li',
		IE: true
	});
	
	/***** CUSTOM ******/
	$('div.carousel').gallery({
		autoRotation: false,
		effect:'fade',
		listOfSlides:'div.carousel1-holder > ul > li',
		switcher:'div.switcher > ul > li'
	});
	$('div.carousel2').gallery({
		autoRotation: 5000,
		listOfSlides:'div.carousel1-frame > ul > li',
		effect: true
	});
	// $('div.carousel3').gallery({
		// autoRotation: 5000,
		// listOfSlides:'div.carousel-frame > ul > li',
		// effect: true,
		// IE: true
	// });
	$('div.g1').gallery({
		autoRotation: 5000,
		listOfSlides:'div.carousel1-frame > ul > li',
		effect: true,
		switcher:'ul.gallery > li',
		IE: true
	});
	/****** END *****/
}
(function($) {
	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');
				
				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 = $(window).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;
				})
				_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;
			});
		});
	}

	$.fn.gallery = function(options) { return new Gallery(this.get(0), options); };
	function Gallery(context, options) { this.init(context, options); };
	Gallery.prototype = {
		options:{},
		init: function (context, options){
			this.options = $.extend({
				infinite: false,								//true = infinite gallery
				duration: 700,									//duration of effect it 1000 = 1sec
				slideElement: 1,								//number of elements for a slide
				autoRotation: false,							//false = option is disabled; 1000 = 1sec
				effect: false,									//false = slide; true = fade
				listOfSlides: 'ul > li',						//elements galleries
				switcher: false,								//false = option is disabled; 'ul > li' = elements switcher
				disableBtn: false,								//false = option is disabled; 'hidden' = class adds an buttons "prev" and "next"
				nextBtn: 'a.link-next, a.btn-next, a.next',		//button "next"
				prevBtn: 'a.link-prev, a.btn-prev, a.prev',		//button "prev"
				circle: true,									//true = cyclic gallery; false = not cyclic gallery
				direction: false,								//false = horizontal; true = vertical
				event: 'click',									//event for the buttons and switcher
				IE: false,										//forced off effect it "fade" in IE
				autoHeight: false								//auto height on fade
			}, options || {});
			var _el = $(context).find(this.options.listOfSlides);
			if (this.options.effect) this.list = _el;
			else this.list = _el.parent();
			this.switcher = $(context).find(this.options.switcher);
			this.nextBtn = $(context).find(this.options.nextBtn);
			this.prevBtn = $(context).find(this.options.prevBtn);
			this.count = _el.index(_el.filter(':last'));
			
			if (this.options.switcher) this.active = this.switcher.index(this.switcher.filter('.active:eq(0)'));
			else this.active = _el.index(_el.filter('.active:eq(0)'));
			if (this.active < 0) this.active = 0;
			this.last = this.active;
			
			this.woh = _el.outerWidth(true);
			if (!this.options.direction) this.installDirections(this.list.parent().width());
			else {
				this.woh = _el.outerHeight(true);
				this.installDirections(this.list.parent().height());
			}
			
			if (!this.options.effect) {
				this.rew = this.count - this.wrapHolderW + 1;
				if (!this.options.direction) this.anim = '{marginLeft: -(this.woh * this.active)}';
				else this.anim = '{marginTop: -(this.woh * this.active)}';
				eval('this.list.css('+this.anim+')');
			}
			else {
				this.rew = this.count;
				this.list.css({opacity: 0}).removeClass('active').eq(this.active).addClass('active').css({opacity: 1}).css('opacity', 'auto');
				this.switcher.removeClass('active').eq(this.active).addClass('active');
				if(this.options.autoHeight) this.list.parent().css({height: this.list.eq(this.active).outerHeight()});
			}
			this.flag = true;
			if (this.options.infinite){
				this.count++;
				this.active += this.count;
				this.list.append(_el.clone());
				this.list.append(_el.clone());
				eval('this.list.css('+this.anim+')');
			}
			
			this.initEvent(this, this.nextBtn, true);
			this.initEvent(this, this.prevBtn, false);
			if (this.options.disableBtn) this.initDisableBtn();
			if (this.options.autoRotation) this.runTimer(this);
			if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
		},
		initDisableBtn: function(){
			this.prevBtn.removeClass('prev-'+this.options.disableBtn);
			this.nextBtn.removeClass('next-'+this.options.disableBtn);
			if (this.active == 0 || this.count+1 == this.wrapHolderW) this.prevBtn.addClass('prev-'+this.options.disableBtn);
			if (this.active == 0 && this.count == 1 || this.count+1 <= this.wrapHolderW) this.nextBtn.addClass('next-'+this.options.disableBtn);
			if (this.active == this.rew) this.nextBtn.addClass('next-'+this.options.disableBtn);
		},
		installDirections: function(temp){
			this.wrapHolderW = Math.ceil(temp / this.woh);
			if (((this.wrapHolderW - 1) * this.woh + this.woh / 2) > temp) this.wrapHolderW--;
		},
		fadeElement: function(){
			if ($.browser.msie && this.options.IE){
				this.list.eq(this.last).css({opacity:0});
				this.list.removeClass('active').eq(this.active).addClass('active').css({opacity:'auto'});
			}
			else{
				this.list.eq(this.last).animate({opacity:0}, {queue:false, duration: this.options.duration});
				this.list.removeClass('active').eq(this.active).addClass('active').animate({
					opacity:1
				}, {queue:false, duration: this.options.duration, complete: function(){
					$(this).css('opacity','auto');
				}});
			}
			if(this.options.autoHeight) this.list.parent().animate({height: this.list.eq(this.active).outerHeight()}, {queue:false, duration: this.options.duration});
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			this.last = this.active;
		},
		scrollElement: function($this){
			if (!$this.options.infinite) eval('$this.list.animate('+$this.anim+', {queue:false, duration: $this.options.duration});');
			else eval('$this.list.animate('+$this.anim+', $this.options.duration, function(){ $this.flag = true });');
			if ($this.options.switcher) $this.switcher.removeClass('active').eq($this.active / $this.options.slideElement).addClass('active');
		},
		runTimer: function($this){
			if($this._t) clearTimeout($this._t);
			$this._t = setInterval(function(){
				if ($this.options.infinite) $this.flag = false;
				$this.toPrepare($this, true);
			}, this.options.autoRotation);
		},
		initEventSwitcher: function($this, el){
			el.bind($this.options.event, function(){
				$this.active = $this.switcher.index($(this)) * $this.options.slideElement;
				if($this._t) clearTimeout($this._t);
				if ($this.options.disableBtn) $this.initDisableBtn();
				if (!$this.options.effect) $this.scrollElement($this);
				else $this.fadeElement();
				if ($this.options.autoRotation) $this.runTimer($this);
				return false;
			});
		},
		initEvent: function($this, addEventEl, dir){
			addEventEl.bind($this.options.event, function(){
				if ($this.flag){
					if ($this.options.infinite) $this.flag = false;
					if($this._t) clearTimeout($this._t);
					$this.toPrepare($this, dir);
					if ($this.options.autoRotation) $this.runTimer($this);
				}
				return false;
			});
		},
		toPrepare: function($this, side){
			if (!$this.options.infinite){
				if (($this.active == $this.rew) && $this.options.circle && side) $this.active = -$this.options.slideElement;
				if (($this.active == 0) && $this.options.circle && !side) $this.active = $this.rew + $this.options.slideElement;
				for (var i = 0; i < $this.options.slideElement; i++){
					if (side) { if ($this.active + 1 <= $this.rew) $this.active++; }
					else { if ($this.active - 1 >= 0) $this.active--; }
				};
			}
			else{
				if ($this.active >= $this.count + $this.count && side) $this.active -= $this.count;
				if ($this.active <= $this.count-1 && !side) $this.active += $this.count;
				eval('$this.list.css('+$this.anim+')');
				if (side) $this.active += $this.options.slideElement;
				else $this.active -= $this.options.slideElement;
			}
			if (this.options.disableBtn) this.initDisableBtn();
			if (!$this.options.effect) $this.scrollElement($this);
			else $this.fadeElement();
		},
		stop: function(){
			if (this._t) clearTimeout(this._t);
		},
		play: function(){
			if (this._t) clearTimeout(this._t);
			if (this.options.autoRotation) this.runTimer(this);
		}
	}
}(jQuery));
