$(document).ready(function() {
	initLightBox();
	initGallery();
	initAccordion();
	initOpenClose();
});

function initGallery() {
	$('.gallery').Gallery({
		speed: 300,
		autoRotation: 7000,
		holder: '.mask',
		slider: 'ul',
		list: '>li'
	});
};

function initAccordion() {
	$('ul.accordion').Accordion({
		activeClass:'active',
		opener:'a.opener',
		slider:'div.slide',
		slideSpeed: 400,
		closed:true
	});
}

function initOpenClose() {
	setTimeout(function() {
		$('.info-block').OpenClose({
			activeClass:'active',
			opener:'.link, .close',
			slider:'.info-holder',
			slideblock: '.info',
			slideSpeed: 400
		});
	},1000)
	$('.gallery-holder .area').OpenClose({
		activeClass:'active',
		opener:'.link',
		slider:'.block-view-holder',
		slideblock: '.block-view',
		slideSpeed: 400
	});
}

jQuery.fn.OpenClose = function(_options) {
	var _options = jQuery.extend({
		activeClass:'active',
		opener:'.opener',
		slider:'.slide',
		slideSpeed: 400,
		animStart:false,
		animEnd:false,
		event:'click'
	},_options);
	return this.each(function() {
		var _holder = jQuery(this);
		var _slideSpeed = _options.slideSpeed;
		var _activeClass = _options.activeClass;
		var _opener = jQuery(_options.opener, _holder);
		var _slider = jQuery(_options.slider, _holder);
		var _block = jQuery(_options.slideblock, _slider);
		
		if(_slider.length) {
			var _h = _block.outerHeight(true);
			if(_holder.hasClass(_activeClass)) {_slider.height(_h);}
			else {_slider.height(0);}
		
			_opener.bind('click',function() {
				if(!_slider.is(':animated')) {
					if(_holder.hasClass(_activeClass)) {
						_holder.removeClass(_activeClass).find(_options.slider).animate({height:0}, _slideSpeed);
					}
					else{
						_holder.addClass(_activeClass).find(_options.slider).animate({height:_h}, {duration:_slideSpeed});
					}
				}
				return false;
			});
			$('body').click(function() {
				if(_holder.hasClass(_activeClass)) {
					_holder.removeClass(_activeClass).find(_options.slider).animate({height:0}, {duration:_slideSpeed, complete:function() {}});
				}
			})
		}
	});
}

jQuery.fn.Accordion = function(_options) {
	var _options = jQuery.extend({
		activeClass:'active',
		opener:'.opener',
		slider:'.slide',
		slideSpeed: 400,
		animStart:false,
		animEnd:false,
		event:'click',
		closed:false
	},_options);
	
	return this.each(function() {
		var _closed = _options.closed;
		jQuery(this).children().each(function() {
			var _holder = jQuery(this);
			var _slideSpeed = _options.slideSpeed;
			var _activeClass = _options.activeClass;
			var _opener = jQuery(_options.opener, _holder);
			var _slider = jQuery(_options.slider, _holder);
			var _animStart = _options.animStart;
			var _animEnd = _options.animEnd;
			var _event = _options.event;
			//console.log(_opener, _slider);
			if(_slider.length) {
				if(_holder.hasClass(_activeClass)) {_slider.show();}
				else { _slider.hide(); }
				_opener.bind(_event,function() {
					_holder.siblings().find('li').removeClass(_activeClass);
					if(!_slider.is(':animated')) {
						if(typeof _animStart === 'function') _animStart();
						if(!_holder.hasClass(_activeClass)) {
							_holder.siblings().removeClass(_activeClass).find(_options.slider).slideUp(_slideSpeed,function() {
								if(typeof _animEnd === 'function') _animEnd();
							});
							_holder.addClass(_activeClass);
							_slider.slideDown(_slideSpeed,function() {
								if(typeof _animEnd === 'function') _animEnd();
							});
						}else{
							if(_closed) {
								_holder.removeClass(_activeClass);
								_slider.slideUp(_slideSpeed,function() {
									if(typeof _animEnd === 'function') _animEnd();
								});
							}
						}
					}
					return false;
				});
			}
		});
	});
}

jQuery.fn.Gallery = function(_options) {
	// default options
	var _options = jQuery.extend({
		speed: 1200,
		autoRotation: 4000,
		holder: '.holder',
		slider: 'ul.slider',
		list: '>li',
		prev: '.prev, .link-prev, .btn-prev',
		next: '.next, .link-next, .btn-next',
		pager: 'ul.switcher',
		dynamicPagination: true,
		vertical: false,
		infinitive: true,
		stopOnHover: true,
		play: '.play',
		pause: '.pause'
	},_options);
	
	return this.each(function() {
		// options
		var _hold = jQuery(this);
		var _speed = _options.speed;
		var _autoRotation = _options.autoRotation;
		var _holder = _hold.find(_options.holder);
		var _slider = _holder.find(_options.slider);
		var _list = _slider.find(_options.list);
		var _prev = _hold.find(_options.prev);
		var _next = _hold.find(_options.next);
		var _pause = _hold.find(_options.pause);
		var _play = _hold.find(_options.play);
		var _vert = _options.vertical;
		var _inf = _options.infinitive;
		var _stop = _options.stopOnHover;
		var _f = true;
		var _p = _options.dynamicPagination;
		if (_vert) var _d = _list.eq(0).outerHeight(true);
		else var _d = _list.eq(0).outerWidth(true);

		/*--------ADDING SLIDES----------*/
		if (_vert) var _vis = Math.ceil(_holder.height()/_d);
		else var _vis = Math.ceil(_holder.width()/_d);
		if (_inf) {
			for	(var i=0; i < _vis; i++) {
				_list.eq(i).clone().appendTo(_slider);
			};
		}

		/*--------CREATING THUMBNAILS----------*/
		var _num = _hold.find(_options.pager);
		if(_p) {	_num.empty();
			_list.each(function(i) {
				$('<li><a href="#">'+(i+1)+'</a></li>').appendTo(_num);
			});	};
		var _thumb = _num.find('li');

		/*-------------------------------------*/

		var _a = _list.index(_list.filter('.active'));
		if(_a == -1) {_a = 0;
			_thumb.eq(_a).addClass('active');
			_list.eq(_a).addClass('active');
		}
		var _x=0, _new, _t;

		if (_f) { if (_autoRotation) Run(_a);}
		function Run(_a) {
			_t = setTimeout(function() {
				if (_inf) {
					if (_a < _list.length) {_a++
					}else {	_a = 1;	};
				}else{
					if (_a < _list.length-1) {_a++
					}else {	_a = 0;	};
				}
				Slide(_a);
			}, _autoRotation);
		};
		function Slide(_new) {
			_x = _new * _d;
			if (_inf) {if (_new == _list.length) {_new=0;}}
			_a = _new;
			_list.removeClass('active').eq(_new).addClass('active');
			_thumb.removeClass('active').eq(_new).addClass('active');
			if (_vert) {
				_slider.animate({top: -_x}, {queue:false, duration:_speed, easing:'linear', complete:function() {
					if (_inf) {if (_new == 0) {_slider.css({top:0});}}
				}});
			}else{
				_slider.animate({left: -_x}, {queue:false, duration:_speed, easing:'linear', complete:function() {
					if (_inf) {if (_new == 0) {_slider.css({left:0});}}
				}});
			}
			if(_t) clearTimeout(_t);
			if (_autoRotation) Run(_a);
		};
		if (_stop) {
			_holder.mouseenter(function() {		_f = false; if(_t) clearTimeout(_t);})
					.mouseleave(function() {		_f = true; if (_autoRotation) Run(_a);});
		}

		_thumb.click(function() {
			_a = _thumb.index($(this));
			Slide(_a);
			return false;
		});

		_next.click(function() {
			if (_inf) {
				if (_a < _list.length) {_a++
				}else {	_a = 1;	};
			}else{
				if (_a < _list.length-1) {_a++
				}else {	_a = 0;	};
			}
			Slide(_a);
			return false;
		});
		
		_prev.click(function() {
			if (_a>0) {_a--
			}else {
				if (_inf) {
					if (_vert) _slider.css({top:-_list.length*_d});
					else _slider.css({left:-_list.length*_d});
				}
				_a = _list.length - 1;
			};
			Slide(_a);
			return false;
		});
	});
};

function initLightBox() {
	var _speed = 300;
	var _ieVersion = 7;
	var _opener = $('a.logo-holder');
	var _lay = $('<div class="overlay"></div>').appendTo($('body'));
		if (jQuery.browser.msie && jQuery.browser.version < _ieVersion) {_lay.css({opacity:0.7}).hide();
		}else{_lay.css({opacity:0}).hide();	}
	
	_opener.click(function() {
		var _light = $($(this).attr('href')).appendTo($('body'));
		var _body = $('body').height(); 
		Position();
		if (jQuery.browser.msie && jQuery.browser.version < _ieVersion) {
			_light.show();
			_lay.show();
		}else{
			_light.css({opacity:0}).show().animate({opacity:1}, {queue:false, duration:_speed});
			_lay.show().animate({opacity:0.7}, {queue:false, duration:_speed});
		}

		_lay.click(function() {	Close();return false;	});
		_light.find('.link-close, .close, .btn-close').click(function() {	Close();return false;	});
		function Close() {
			if (jQuery.browser.msie && jQuery.browser.version < _ieVersion) {
				_light.hide();
				_lay.hide();
			}else{	
				_light.animate({opacity:0}, {queue:false, duration:_speed, complete:function() {$(this).hide();}});
				_lay.animate({opacity:0}, {queue:false, duration:_speed, complete:function() {$(this).hide();}});
			}
		}

		$(window).resize(function() {	Position();	});
		$(window).scroll(function() {	Position();	});
		
		function Position() {
			var _w = _light.outerWidth(true);
			var _h = _light.outerHeight(true);
			if (window.innerHeight) {var _wx = window.innerWidth; var _wy = window.innerHeight;}
			else{_wx = document.documentElement.clientWidth; _wy = document.documentElement.clientHeight;};
			if (_body < _wy) {	_lay.css({height:_wy})	}
			else{	_lay.css({height:_body});	};
			var _top = (_wy-_h)/2+$(document).scrollTop();
			if(_top + _h > _body) _top = _body - _h - 8;
			_light.css({left:(_wx-_w)/2, top:_top});
		};
		return false;
	});
};

