$(document).ready(function(){
	$('div.add-container div.list').gallSlide({
		duration: 700,
		autoSlide: 4000
	});
	$('div.slider-frame').boxSlider({
		duration: 700,
		autoSlide: 4000
	});
	initMap();
	initTooltip();
	mapTooltip();
	initLightbox();
})

function initMap(){
	var _duration = 600;
	var _mapOpener = $('a.map-opener');
	var _listOpener = $('a.list-opener');
	var _map = $('div.map');
	var _mapW = _map.outerWidth();
	var _list = $('div.index-slider div.list');
	var _listW = _list.outerWidth();
	_list.width(0);
	_listOpener.click(function(){
			_map.animate({marginLeft: -_mapW}, {queue:false, duration:_duration, complete:function(){
			}});
			_list.animate({width: _listW}, {queue:false, duration:_duration, complete:function(){
			}});
			$('div.gallery-controls').fadeIn();
		return false;
	});
	_mapOpener.click(function(){
		_map.animate({marginLeft: 0}, {queue:false, duration:_duration, complete:function(){
		}});
		_list.animate({width: 0}, {queue:false, duration:_duration, complete:function(){
		}});
		$('div.gallery-controls').fadeOut();
		return false;
	});
}
function mapTooltip(){
	$('div.map .popup-hover, div.bg div.tab-content .popup-hover, div.gallery-controls .popup-hover').each(function(){
		var _btn = $(this).children('a');
		var _popup = $(this).children('div.popup');
		$(this).mouseenter(function(){
			$(this).addClass('active');
			_popup.fadeIn();
		}).mouseleave(function(){
			_popup.stop().hide().css('opacity', 1);
			$(this).removeClass('active');
		});
	});
}
function initTooltip(){
	$('.tooltip-hover').each(function(){
		var _btn = $(this).children('a');
		var _popup = $(this).children('div.popup');
		$(this).mouseenter(function(){
			_popup.fadeIn();
		}).mouseleave(function(){
			_popup.stop().hide().css('opacity', 1);
		});
	});
}
jQuery.fn.gallSlide = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		autoSlide: 5000
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = _options.duration;
		var _timer = _options.autoSlide;
		var _wrap = _hold.find('ul.list-slider');
		var _el = _hold.find('ul.list-slider > li.slide-item');
		var _next = $('div.controls-holder').find('a.btn-next');
		var _prev = $('div.controls-holder').find('a.btn-prev');
		var _count = _el.length-1;
		var _w = _el.outerWidth();
		var _wrapHolderW = Math.ceil(_wrap.parent().width()/_w);
		var _active = 0;
		function scrollEl(){
			_wrap.eq(0).animate({
				marginLeft: -(_w * _active) + "px"
			}, {queue:false, duration: _speed});
		}
		_next.click(function(){
			_active++;
			if (_active > (_count - _wrapHolderW + 1)) _active = 0;
			scrollEl();
			return false;
		});
		_prev.click(function(){
			_active--;
			if (_active < 0) _active = _count - _wrapHolderW + 1;
			scrollEl();
			return false;
		});
	});
}
jQuery.fn.boxSlider = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		autoSlide: 5000
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = _options.duration;
		var _timer = _options.autoSlide;
		var _wrap = _hold.find('ul.slider');
		var _el = _hold.find('ul.slider > li.slide-block');
		var _btn = $('div.slide-navigation ul a');
		var _count = _el.length-1;
		var _w = _el.outerWidth();
		var _wrapHolderW = Math.ceil(_wrap.parent().width()/_w);
		var _active = 0;
		function scrollEl(){
			if(_active == 1){
				$('div.gallery-controls').fadeIn();
			}
			else{
				$('div.gallery-controls').hide();
			}
			_wrap.eq(0).animate({
				marginLeft: -(_w * _active) + "px"
			}, {queue:false, duration: _speed});
		}
		_btn.click(function(){
			_active = _btn.index($(this));
			if (_active > (_count - _wrapHolderW + 1)) _active = 0;
			scrollEl();
			return false;
		});
	});
}
function initLightbox(){
	if($('a.lightbox-opener').length != 0){
		$('a.lightbox-opener').fancybox({
		'titleShow': false,
		'overlayColor' : '#000',
		'overlayOpacity' : 0.6,
		'scrolling' : 'no'
	});
		
	var _closer = $('div.light-box').find('a.close');
	_closer.click($.fancybox.close);
	_closer.click(function(){
		return false;
	});
}
}

