// jQuery.noConflict();

jQuery(document).ready(function($) {
	
	window.jQuery = $;

	
	// jScrollPane
	$('.scrollable').jScrollPane({
		scrollbarWidth: 12,
		showArrows: true,
		arrowSize: 4
	});

	$('.hscrollable').jScrollHorizontalPane({
		scrollbarHeight: 12,
		showArrows: true,
		scrollbarMargin: 0,
		arrowSize: 50,
		wheelSpeed: 50
	});

	
	// image swap
	$('.leftbox > .banner').css({
		position: 'absolute',
		top: $('.main-content').height()-$('.leftbox > .banner').outerHeight()-'30'+'px'
	});
	
	$.swapImage('.swapImageDisjoint');
	
	// fancybox	
	
	
	$('a.fancybox').each(function() {
		$(this).fancybox({
			speedIn: 1,
			speedOut: 1,
			
			autoDimensions: false,
			autoScale: false,
			width : 1134,
			height: 630,
			margin: 0,
	
			titlePosition: 'inside',
			titleFormat: formatFancyboxTitle,
			
			showCloseButton: false,
			
			overlayShow: true,
			overlayColor: '#fff',
			overlayOpacity: 0.7,
			
			hideOnContentClick: false,
	
			onComplete: function() {
				$('.popup .scrollable').jScrollPaneRemove();
				$('.popup .scrollable').jScrollPane({
					scrollbarWidth: 12,
					showArrows: true,
					arrowSize: 4
				});
				
				$.swapImage('.swapImageDisjoint');
			}
		});
	});
	
 	// search box	
	$('#search').focus(function(evt) {
		$('#searchLabel').addClass(($(this).val() == '') ? 'partial' : 'hidden');
	});
	
	$('#search').blur(function(evt) {
		if ($(this).val() == '') {
			$('#searchLabel').removeClass('partial');
			$('#searchLabel').removeClass('hidden');
		}
	});
	
	$('#search').keypress(function(evt) {
		$('#searchLabel').addClass('hidden');
	});
	
	if ($('#search').val() != '') $('#searchLabel').addClass('hidden');
	
	// button hover for ie 6	
	$('button').hover(
		function() { $(this).addClass('hover'); },
		function() { $(this).removeClass('hover'); }
	);
	
	$('.headernav > li > div.submenu').hide().closest('li').mouseenter(function() {
		window.clearTimeout($(this).data('timer'));
		$(this).find('div.submenu').stop(true,true).fadeIn(355);
	}).mouseleave(function() {
		var _this = $(this);
		$(this).data('timer', window.setTimeout(function() {
			_this.find('div.submenu').stop(true,true).fadeOut(200);
		}, 100));
		
	});
	
	$('table').each(function() { $(this).find('tr:odd').addClass('even'); });
	
	// check for position: fixed
	var div = $('<div>', {css: {position: 'fixed', top: '10px', left: 0}}).appendTo(document.body);
	var supportFixed = div[0].offsetTop === 10;
	div.remove();
	
	// image sizing
	var background = $('img#background, div#background img');
	var imgRatio = background.metadata().width / background.metadata().height;
	
	if (!supportFixed && !background.attr('id')) {
		background.removeAttr('id').wrap($('<div>', {id: 'background'}));
	}
	
	var win = $(window);
	win.resize(function() {
		var width = win.width();
		var height = win.height();
		var ratio = width / height;
		
		if (ratio > imgRatio) {
			// window is wider than image -> image is 100% width of window
			var top = -(Math.round((width / imgRatio - height) / 2));
			background.css({width: '100%', height: 'auto', top: top, left: 0});
		} else {
			// image is wider than window -> image is 100% height of window
			var left = -(Math.round((height * ratio - width) / 2));
			background.css({width: 'auto', height: '100%', top: 0, left: left});
		}
	}).resize().resize();
	
});

function reinit() {
	var $ = window.jQuery;
	
	$.swapImage('.swapImageDisjoint');
	
	$('a.fancybox').each(function() {
		$(this).fancybox({
			speedIn: 1,
			speedOut: 1,
			
			autoDimensions: false,
			autoScale: false,
			width : 1134,
			height: 630,
			margin: 0,
	
			titlePosition: 'inside',
			titleFormat: formatFancyboxTitle,
			
			showCloseButton: false,
			
			overlayShow: true,
			overlayColor: '#fff',
			overlayOpacity: 0.7,
			
			hideOnContentClick: false,
	
			onComplete: function() {
				$('.popup .scrollable').jScrollPaneRemove();
				$('.popup .scrollable').jScrollPane({
					scrollbarWidth: 12,
					showArrows: true,
					arrowSize: 4
				});
				
				$.swapImage('.swapImageDisjoint');
			}
		});
	});
}
