function initSlideshow() {
	$('div.flash').slideShow({
		slideEl:'ul.slideshow > li',
		autoSlideShow:true,
		switchTime:4500,
		duration:850
	});
}

function initAccordions() {
	// accordion options
	var _slideSpeed = 300;
	var _activeClass = 'active';
	var _sliderClass = '.drop-content';
	var _collapseOthers = true;

	$('div.accordion-box').each(function(){
		var _holder = $(this);
		var _items = _holder.find('.drop-box');

		_items.each(function(){
			var _item = $(this);
			var _opener = _item.find(' > a');
			var _slider = _item.find(_sliderClass);

			_opener.click(function(){
				if(_item.hasClass(_activeClass)) {
					_slider.slideUp(_slideSpeed);
					_item.removeClass(_activeClass);
				} else {
					if(_collapseOthers) {
						_items.filter('.'+_activeClass).each(function(){
							$(this).removeClass(_activeClass);
							$(this).find(_sliderClass).slideUp(_slideSpeed);
						});
					}
					_slider.slideDown(_slideSpeed);
					_item.addClass(_activeClass);
				}
				return false;
			});
		});
	});
}

function showLightbox(_pid) {
	// defaults options
	var _lightboxContentBlock;
	var _faderOpacity = 0.8;
	var _faderBackground = '#000000';
	var _closeLink = 'a.close';
	var _href = _pid;
	var _isIE6 = false;
	if ($.browser.msie && $.browser.version == '6.0') _isIE6 = true;

	if (_pid && _pid.length > 0) {
		var _lightbox = _pid;
		if (!jQuery('div.lightbox-fader').length)
			_fader = $('body').append('<div class="lightbox-fader"></div>');

		if (_isIE6) {
			if (!jQuery('iframe.ie-fader').length) {
				_ieFader = $('body').append('<iframe class="ie-fader" src="about:blank" width="10" height="10" scrolling="no" frameborder="0"></iframe>');
				_ieFader = $('iframe.ie-fader')
				_ieFader.css({
					opacity:0,
					position:'absolute',
					visibility:'visible',
					top:0,
					left:0
				});
			}
		}

		_fader = jQuery('div.lightbox-fader');
		_lightbox.css({
			'zIndex':999,
			'visibility':'visible',
			'display':'none'
		});
		_fader.css({
			opacity:_faderOpacity,
			backgroundColor:_faderBackground,
			display:'none',
			position:'absolute',
			visibility:'visible',
			top:0,
			left:0,
			zIndex:998,
			textIndent: -9999
		}).text('$nbsp');

		_lightbox.find(_closeLink).click(function(){
			_lightbox.fadeOut(400, function(){
				_fader.fadeOut(300);
				if (_isIE6) _ieFader.hide();
			});
			return false;
		});

		_fader.click(function(){
			_lightbox.fadeOut(400, function(){
				_fader.fadeOut(300);
				if (_isIE6) _ieFader.hide();
			});
			return false;
		});

		_fader.fadeIn(300, function(){
			_lightbox.fadeIn(400);
			positionLightbox(_lightbox);
		});

		$(document).keydown(function (e) {
			if (!e) evt = window.event;
			if (e.keyCode == 27) {
				_lightbox.fadeOut(400, function(){
					_fader.fadeOut(300);
				});
			}
		});

		$(window).resize(function(){
			positionLightbox(_lightbox);
		});
		$(window).scroll(function(){
			positionLightbox(_lightbox);
		});

		return false;
	}

	function positionLightbox (_lbox) {

		var _height = 0;
		var _width = 0;
		var _minWidth = $('body > div:eq(0)').outerWidth();
		if (window.innerHeight) {
			_height = window.innerHeight;
			_width = window.innerWidth;
		} else {
			_height = document.documentElement.clientHeight;
			_width = document.documentElement.clientWidth;
		}
		var _thisHeight = _lbox.outerHeight();
		var _page = $('body');

		if (_lbox.length) {
			if (_height > _page.innerHeight()) _fader.css('height',_height);
			else _fader.css('height',_page.innerHeight());

			if (_width < _minWidth) _fader.css('width',_minWidth);
			else _fader.css('width','100%');

			if (_height > _thisHeight) {
				if (!window.innerHeight) {
					_lbox.css({
						position:'absolute',
						top: (document.documentElement.scrollTop + (_height - _thisHeight) / 2)+"px"
					});
				} else {
					_lbox.css({
						position:'fixed',
						top: ((_height - _lbox.outerHeight()) / 2)+"px"
					});
				}
			}
			else {
				_lbox.css({
					position:'absolute',
					top: 0
				});
				_fader.css({
					height:_thisHeight
				});
			}
			if (_width > _lbox.outerWidth()) _lbox.css({left:(_width - _lbox.outerWidth()) / 2 + "px"});
			else _lbox.css({position:'absolute',left: 0});

			if (_isIE6) {
				_ieFader.css({
					height:_fader.height(),
					width:_fader.width()
				});
			}
		}
	}

}

function initPopups() {
	// popup openers
	$('a.open-popup').click(function(){
		var _opener = $(this);
		var _target = $(_opener.attr('href'));
		if(_target.length) {
			// set tabs
			var _tabs = _target.find('.tabs-holder > div.tab-block');
			var _curIndex = parseInt(_opener.attr('rel'));
			var _tabCount =_tabs.length;
			var _btnPrev = _target.find('a.prev');
			var _btnNext = _target.find('a.next');
			_tabs.removeClass('active').hide().eq(_curIndex).show().addClass('active');
			if(_curIndex == 0) _btnPrev.addClass('disabled'); else _btnPrev.removeClass('disabled');
			if(_curIndex == _tabCount-1) _btnNext.addClass('disabled'); else _btnNext.removeClass('disabled');

			showLightbox(_target);
			return false;
		}
	});

	// lightbox tabs
	$('.lightbox').each(function(){
		var _holder = $(this);
		var _tabs = _holder.find('.tabs-holder > .tab-block');
		var _tabCount = _tabs.length;
		var _btnPrev = _holder.find('a.prev');
		var _btnNext = _holder.find('a.next');
		var _currentIndex = 0;

		_btnNext.click(function(){
			_currentIndex = _tabs.index(_tabs.filter('.active').eq(0));
			if(_currentIndex < _tabCount-1) {
				_currentIndex++;
				switchTab();
			}
			return false;
		});
		_btnPrev.click(function(){
			_currentIndex = _tabs.index(_tabs.filter('.active').eq(0));
			if(_currentIndex > 0) {
				_currentIndex--;
				switchTab();
			}
			return false;
		});

		function switchTab() {
			_tabs.removeClass('active').hide();
			_tabs.eq(_currentIndex).show().addClass('active');

			if(_currentIndex == 0) _btnPrev.addClass('disabled');
			else _btnPrev.removeClass('disabled');

			if(_currentIndex == _tabCount-1) _btnNext.addClass('disabled');
			else _btnNext.removeClass('disabled');
		}
	});
}

$(document).ready(function() {
	initAccordions();
	initSlideshow();
	initPopups();
});