function initLightbox() {
	// defaults options
	var _fadeSpeed = 500;
	var _linksSelector = 'a[rel*="lightbox"], a:has(img.small, img.medium)';
	var _lightboxID = '#simplelightbox';
	var _imageHolderID = '#simplelightbox-image';
	var _titleTextID = '#simplelightbox-title';
	var _frameHolder = '.popup-image';
	var _navigationBar = '.popup-link';
	var _faderBackground = '#212020';
	var _closeLink = 'a.close';
	var _btnPrev = 'a.prev';
	var _btnNext = 'a.next';
	var _faderOpacity = 0.95;
	var _centerPosition = true;
	var _circleSlide = true;
	var _lightboxHTML = ' \
		<div id="simplelightbox" class="popup" style="display:none;"> \
			<a class="close" href="#">Close</a> \
			<div class="load-wrapper"> \
				<div class="popup-image"> \
					<div id="simplelightbox-image" class="box-content"> \
					</div> \
				</div> \
			</div> \
			<div class="popup-link"> \
				<a class="next" href="#">next</a> \
				<a class="prev" href="#">prev</a> \
			</div> \
		</div>';


	// lightbox code
	var _isIE6 = (jQuery.browser.msie && jQuery.browser.version == '6.0' ? true : false);
	var _lightbox = jQuery(_lightboxHTML);
	_btnPrev = _lightbox.find(_btnPrev);
	_btnNext = _lightbox.find(_btnNext);

	if (!jQuery('div.lightbox-fader').length) _fader = jQuery('body').append('<div class="lightbox-fader"></div>');
	_fader = jQuery('div.lightbox-fader');
	jQuery('body').append(_lightbox);
	_imageHolderID = jQuery(_imageHolderID);
	_titleTextID = jQuery(_titleTextID);
	_frameHolder = _lightbox.find(_frameHolder);
	_navigationBar = _lightbox.find(_navigationBar).css({opacity:0});

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

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

	// lightbox opener code
	var _currentGroup;
	var _currentIndex;
	var _itemsCount;

	jQuery(_linksSelector).click(function(){
		var _opener = jQuery(this);
		_currentGroup = jQuery('a[rel="'+_opener.attr('rel')+'"]');
		_currentIndex = _currentGroup.index(jQuery(this));
		_itemsCount = _currentGroup.length;

		if(_itemsCount < 2){
			_btnPrev.hide();
			_btnNext.hide();
		} else {
			_btnPrev.show();
			_btnNext.show();
		}

		_fader.fadeIn(_fadeSpeed, function(){
			positionLightbox();
			var _img = new Image();
			_img.onload = function(){
				_imageHolderID.html('');
				_imageHolderID.append(_img);

				if(jQuery.browser.msie)
				setTimeout(function(){
					jQuery(_lightbox).css({width:jQuery(_img).width()+20});
				}, 50);

				positionLightbox();
				_lightbox.fadeIn(_fadeSpeed,function(){
					_navigationBar.animate({opacity:1},{duration:_fadeSpeed});
					positionLightbox();
				});
				if (_isIE6) _ieFader.show();
			};
			_img.src = _opener.attr('href');
			_img.alt = '';

			var _title = _opener.find('img').attr('title');
			if(_title.length) _titleTextID.html(_title);
		});
		positionLightbox();
		return false;
	});

	// lightbox navigation
	_btnPrev.click(function(){
		prevSlide();
		return false;
	});
	_btnNext.click(function(){
		nextSlide();
		return false;
	});

	function nextSlide() {
		if(_currentIndex < _itemsCount-1) {
			_currentIndex++;
			switchSlide();
		} else if(_circleSlide) {
			_currentIndex = 0;
			switchSlide();
		}
	}
	function prevSlide() {
		if(_currentIndex > 0) {
			_currentIndex--;
			switchSlide();
		} else if(_circleSlide) {
			_currentIndex = _itemsCount-1;
			switchSlide();
		}
	}
	function switchSlide() {
		_frameHolder.animate({opacity:0},{duration:_fadeSpeed,queue:true,complete:function(){
			if(jQuery.browser.msie) _frameHolder.css({opacity:'auto'});
			var _img = new Image();
			_img.onload = function(){
				_imageHolderID.html('');
				_imageHolderID.append(_img);

				if(jQuery.browser.msie)
				setTimeout(function(){
					jQuery(_lightbox).css({width:jQuery(_img).width()+20});
				}, 50);

				positionLightbox();
				_frameHolder.animate({opacity:1},{duration:_fadeSpeed,queue:true,complete:function(){
					if(jQuery.browser.msie) _frameHolder.css({opacity:'auto'});
					positionLightbox();
				}});
			};
			var _title = _currentGroup.eq(_currentIndex).find('img').attr('title');
			if(_title.length) _titleTextID.html(_title);

			_img.src = _currentGroup.eq(_currentIndex).attr('href');
			_img.alt = '';
		}});
	}

	// misc event listeners
	_lightbox.find(_closeLink).click(function(){
		hideLightbox();
		return false;
	});
	_fader.click(function(){
		hideLightbox();
		return false;
	});
	jQuery(document).keydown(function (e) {
		if (!e) evt = window.event;
		if (e.keyCode == 27) {
			hideLightbox();
		}
	});

	// lightbox hide function
	function hideLightbox() {
		_lightbox.fadeOut(_fadeSpeed, function(){
			_fader.fadeOut(_fadeSpeed);
			_navigationBar.css({opacity:0});
			if (_isIE6) _ieFader.hide();
		});
	}

	// lightbox positioning function
	function positionLightbox() {
		var _minWidth = jQuery('body > div:eq(1)').outerWidth();
		var _height = jQuery(window).height();
		var _width = jQuery(window).width();
		var _thisHeight = _lightbox.outerHeight();
		var _page = jQuery('body > div:eq(1)');

		if (_lightbox.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) {
					_lightbox.css({
						position:'absolute',
						top: (document.documentElement.scrollTop + (_height - _thisHeight) / 2)+"px"
					});
				} else {
					_lightbox.css({
						position:'fixed',
						top: ((_height - _lightbox.outerHeight()) / 2)+"px"
					});
				}
			}
			else {
				_lightbox.css({
					position:'absolute',
					top: 0
				});
				_fader.css({
					height:_thisHeight
				});
			}
			if (_width > _lightbox.outerWidth()) _lightbox.css({left:(_width - _lightbox.outerWidth()) / 2 + "px"});
			else _lightbox.css({position:'absolute',left: 0});

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

	jQuery(window).scroll(function(){
		positionLightbox();
	});
	jQuery(window).resize(function(){
		positionLightbox();
	});
}

function initPopups() {
	$('a.ajax-lightbox').simpleLightbox({
		faderOpacity: 0.7,
		faderBackground: '#000',
		closeLink:'a.close, a.cancel',
		href:true,
		onClick: null
	});
}

function initSlideShow() {
	$('div.content-image').slideShow({
		numElementLink:'> ul > li > a',
		slideEl:'> div > ul > li',
		autoSlideShow:false,
		switchTime:3000,
		duration:750
	});
}

function initAccordions(){
	$('ul.accordion').accordion({
		active: ".selected",
		autoHeight: false,
		header: ".opener",
		collapsible: false,
		event: "mouseover"
	});
	$('ul.accordion2').accordion({
		autoHeight: false,
		header: ".opener",
		collapsible: false,
		fillSpace: true,
		event: "click"
	});
}

function browserDetect() {
	var cssFix = function(){
		var u = navigator.userAgent.toLowerCase(),
		addClass = function(el,val){
		if(!el.className) {
			el.className = val;
		} else {
			var newCl = el.className;
			newCl+=(" "+val);
			el.className = newCl;

		}

	},
	is = function(t){return (u.indexOf(t)!=-1)};
	addClass(document.getElementsByTagName('html')[0],[
		(!(/opera|webtv/i.test(u))&&/msie (\d)/.test(u))?('ie ie'+RegExp.$1)
		:is('firefox/2')?'gecko ff2'
		:is('firefox/3')?'gecko ff3'
		:is('gecko/')?'gecko'
		:is('chrome/')?'chrome'
		:is('opera/9')?'opera opera9':/opera (\d)/.test(u)?'opera opera'+RegExp.$1
		:is('konqueror')?'konqueror'
		:is('applewebkit/')?'webkit safari'
		:is('mozilla/')?'gecko':'',
		(is('x11')||is('linux'))?' linux'
		:is('mac')?' mac'
		:is('win')?' win':''
	].join(" "));
}();
}

function ieHover(_list) {
	if ($.browser.msie && $.browser.version < 7) {
		$(_list).hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
	}
}

$(function() {
	browserDetect();
	initAccordions();
	initSlideShow();
	initPopups();
	initLightbox();
	ieHover('ul.nav > li, div.div-class, span, #btn-go, #nav > li');
	
	// Submenu dropdown
	$('.nav li#trade').hover(  
		//show its submenu
		function(){$('ul', this).slideDown(100);},
		//hide its submenu 
		function(){$('ul', this).slideUp(100);}
	);
	
	// Accordion product menu
	var $tab_scroll_ready = true;
	var $auto_close_timeout;
	
	// Expand only the active menu, which is determined by the class name
	//$('#tablist > li[class~=active]').addClass('expanded').find('ul:first').slideDown('medium');
	
	//var $selected_tab_index = {$accordion_open_index};
	$('#tablist > li').eq($selected_tab_index).addClass('expanded active').find('ul:first').slideDown('medium');

	// Toggle the selected menu's class and expand or collapse the menu
	$('#tablist > li > a').mouseover(accordion).click(accordion);
	$('#tablist a').mouseover(clear_accordion_timeout);
	$('#tablist a').mouseout(set_accordion_timeout);
	
	function clear_accordion_timeout()
	{
		clearTimeout( $auto_close_timeout );
	}
	
	function set_accordion_timeout()
	{
		$auto_close_timeout = setTimeout( close_accordion, 250 );
	}
	
	function close_accordion()
	{
		clear_accordion_timeout();
		
		// Hide any expanded, non-active
		$('#tablist > li[class~=expanded]').not('[class~=active]').removeClass('expanded').find('ul:first').slideUp('medium');
	}
	
	function accordion()
	{
		clear_accordion_timeout();
		
		var $parent = $(this).parent();
		
		if ( $tab_scroll_ready && $parent.hasClass('expanded') == false && $parent.hasClass('active') == false )
		{
			// Hide any expanded, non-active
			$('#tablist > li[class~=expanded]').not('[class~=active]').removeClass('expanded').find('ul:first').slideUp('medium');
			$parent.addClass('expanded');
			
			// Start expanding the following list if it exists
			var $next = $(this).next();
			
			if ( $next.length > 0 )
			{
				$tab_scroll_ready = false;
				$next.slideDown('medium',function(){$tab_scroll_ready=true;});
			}
			/*
			$tab_scroll_ready = false;
			$('#tablist > li[class~=expanded]').not('[class~=active]').removeClass('expanded').find('ul:first').slideUp('medium');
			$(this).parent().addClass('expanded');
			$(this).next().slideDown('medium',function(){$tab_scroll_ready=true;});
			*/
		}
	}
});
