$(function(){
	initOpen();
	var gal1 = $('div.carousel').gallery({
		duration: 500,
		listOfSlides: 'div.slides > div.mask > ul > li',
		nextBtn: 'div.slides a.next',
		prevBtn: 'div.slides a.prev',
		IE: true
	});
	var gal_popup1 = $('div#popup').gallery({
		duration: 500,
		autoRotation: false,
		listOfSlides: 'div.slideshow ul.slides > li'
	});
	var gal_popup2 = $('div#popup02').gallery({
		duration: 500,
		autoRotation: false,
		listOfSlides: 'div.slideshow ul > li'
	});
	$('ul.gallery a').click(function(){
		var ind = $('ul.gallery a').index($(this));
		var w = $('div#popup div.slideshow ul.slides > li').width();
		$('div#popup div.slideshow ul.slides').css({marginLeft:-w*ind});
		gal_popup1.active = ind;
		return false;
	});
	$('ul.team > li > a').click(function(){
		var ind = $('ul.team > li > a').index($(this));
		var w = 472;
		$('div#popup02 div.slideshow ul').css({marginLeft:-w*ind});
		gal_popup2.active = ind;
		return false;
	});
	var gal2 = $('div.boards-gallery').gallery({
		duration: 500,
		listOfSlides: 'div.slider > div.slider-holder > ul > li',
		nextBtn: 'ul.control li.next a',
		prevBtn: 'ul.control li.prev a',
		slideElement: 1
	});
	var gal3 = $('div.carousel-box').gallery({
		duration: 500,
		listOfSlides: 'div.carousel-holder > ul > li',
		nextBtn: 'a.next',
		prevBtn: 'a.prev',
		slideElement: 1
	});
	InitGallery();
	ScrollWidth();
	initToolTip();
	$('body').myPopup();
	initProductsList();
});

function initProductsList(){
	$('ul.products').each(function(){
		var _holder = $(this);
		var _els = _holder.children();
		var _elW = _els.eq(0).innerWidth();
		function setElements(){
			var _elsNum = Math.floor(_holder.width()/_elW);
			_els.each(function(ind){
				if (ind > _elsNum-1) $(this).hide();
				else $(this).show();
			})
		}
		setElements();
		$(window).resize(setElements);
	})
}

function initOpen(){
	$('#sidebar ul.accordion > li').each(function(){
		var _this = $(this);
		var link = _this.find('div.opener > a');
		var box = _this.find('div.slider');
		var w = box.outerHeight(true);
		box.height(0);
		
		link.click(function(){
			if($(this).hasClass('active')){
				$(this).removeClass('active');
				box.animate({height:0},500);
			}
			else{
				$(this).addClass('active');
				box.animate({height:w},500);
			}
			return false;
		});
	});
}
function initToolTip(){
	var _time;
	$('ul.tooltips-hold > li').each(function(){
		var _this = $(this);
		var w = _this.width()/2;
		var tooltip = _this.find('div.tooltip');
		var w1 = tooltip.width()/2;

		$('body').append(tooltip.clone());
		tooltip.remove();
		
		var ind = $('.text-scroll ul.tooltips-hold > li').index(_this);
		
		_this.mouseenter(function(){
			var t = _this.offset().top;
			var l = _this.offset().left;
			$('div.tooltip').eq(ind).css({left:(w-w1)+l, top:t}).show();
		}).mouseleave(function(){
			_time = setTimeout(function(){
				$('div.tooltip').eq(ind).hide();
			}, 100);
		});
		$('div.tooltip').mouseenter(function(){
			if(_time) clearTimeout(_time);
		}).mouseleave(function(){
			$('div.tooltip').hide();
		});
	});
}
function ScrollWidth(){
	var _block = $('.text-scroll ul');
	var _els = _block.find('li');
	var _width = 0;
	_els.each(function(){
		_width +=$(this).outerWidth() + 4;
	});
	_block.width(_width);
}
function InitGallery(){
	var _speed = 800,
		_duration = 7000;	
	var _hold = $('.visual-block');
	var _list =_hold.find('.mask ul>li');
	var _a = _list.index(_list.filter('.active:eq(0)'));
	if(_a == -1) {_a = 0;_list.eq(_a).addClass('active')}
	var _i, _old = _a, _t;
/*--------CREATING THUMBNAILS----------*/
	var _num = _hold.find('.switcher ul').empty();
	_list.each(function(i){
		$('<li><a href="#">'+(i+1)+'</a></li>').appendTo(_num);
	});
/*-------------------------------------------------*/
	var _thumb = _num.find('li');
	_thumb.eq(_a).addClass('active');
	if (jQuery.browser.msie && jQuery.browser.version < 10){		_list.hide().eq(_a).show();
	} else{		_list.css({opacity:0}).eq(_a).css({opacity:1});	}
	Run(_a);
	function Run(_a){	
		_t = setTimeout(function(){
			_a++; if (_a >= _list.length){_a=0}
			ChangeFade(_a);
		}, _duration);
	};
	function ChangeFade(_new){
		if(jQuery.browser.msie && jQuery.browser.version < 10){
			_list.eq(_old).removeClass('active').hide();
			_list.eq(_new).addClass('active').show();
		}else{
			_list.eq(_old).removeClass('active').animate({opacity:0}, {queue:false, duration:_speed});
			_list.eq(_new).addClass('active').animate({opacity:1}, {queue:false, duration:_speed});
		}
		_thumb.eq(_old).removeClass('active');
		_thumb.eq(_new).addClass('active');
		_old=_new;_a=_new;
		if(_t) clearTimeout(_t);
		Run(_new);
	};
	_thumb.click(function(){
		_i = _thumb.index($(this));
		ChangeFade(_i);
		_a = _i;
		return false;
	});
};
;(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: 'body'
	},_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: '#000000',
			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 - 693/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').split('#')[1]));
			}
			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({
				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,
				event: 'click',
				IE: false,
				after: function(){}
			}, options || {});
			this.context = $(context);
			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.list.css({marginLeft: -(this.woh * this.active)});
				else this.list.css({marginTop: -(this.woh * this.active)});
			}
			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.disableBtn) {
				if (this.count < this.wrapHolderW) this.nextBtn.addClass(this.options.disableBtn);
				if (this.active == 0) this.prevBtn.addClass(this.options.disableBtn);
			}
			
			this.initEvent(this, this.nextBtn, this.prevBtn, true);
			this.initEvent(this, this.prevBtn, this.nextBtn, false);
			
			if (this.options.autoRotation) this.runTimer(this);
			
			if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
		},
		installDirections: function(temp){
			this.wrapHolderW = Math.ceil(temp / this.woh);
			if (((this.wrapHolderW - 1) * this.woh + this.woh / 2) > temp) this.wrapHolderWwrapHolderW--;
		},
		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.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			this.last = this.active;
		},
		scrollElement: function(){
			if (!this.options.direction) this.list.animate({marginLeft: -(this.woh * this.active)}, {queue:false, duration: this.options.duration});
			else this.list.animate({marginTop: -(this.woh * this.active)}, {queue:false, duration: this.options.duration});
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			this.options.after();
		},
		runTimer: function($this){
			if($this._t) clearTimeout($this._t);
			$this._t = setInterval(function(){
				$this.toPrepare($this, true);
			}, this.options.autoRotation);
		},
		initEventSwitcher: function($this, el){
			el.bind($this.options.event, function(){
				$this.active = $this.switcher.index($(this));
				if($this._t) clearTimeout($this._t);
				if (!$this.options.effect) $this.scrollElement();
				else $this.fadeElement();
				if ($this.options.autoRotation) $this.runTimer($this);
				return false;
			});
		},
		initEvent: function($this, addEventEl, addDisClass, dir){
			addEventEl.bind($this.options.event, function(){
				if($this._t) clearTimeout($this._t);
				if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) addDisClass.removeClass($this.options.disableBtn);
				$this.toPrepare($this, dir);
				if ($this.options.autoRotation) $this.runTimer($this);
				return false;
			});
		},
		toPrepare: function($this, side){
			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) {
						if ($this.options.disableBtn && ($this.count > $this.wrapHolderW)) $this.nextBtn.addClass($this.options.disableBtn);
					}
					else $this.active++;
				}
				else{
					if ($this.active - 1 < 0) {
						if ($this.options.disableBtn && ($this.count > $this.wrapHolderW)) $this.prevBtn.addClass($this.options.disableBtn);
					}
					else $this.active--;
				}
			};
			if ($this.active == $this.rew && side) if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) $this.nextBtn.addClass($this.options.disableBtn);
			if ($this.active == 0 && !side) if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) $this.prevBtn.addClass($this.options.disableBtn);
			if (!$this.options.effect) $this.scrollElement();
			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);
