/**
 * Javascript library for the website finanzstandort.de
 *
 * @author	Markus Friedrich <markus.friedrich (at) omse.de>
 */
var ifd = {

	/**
	 * The height of the content area
	 * 
	 * @var	integer		contentHeight
	 */
	contentHeight: 0,
	
	/**
	 * Height that will be scrolled if the scrolling buttons are clicked
	 * 
	 * @var	integer		clickScrollHeight
	 */
	clickScrollHeight: 0,	

	/**
	 * The content area for scrolling
	 * 
	 * @var	object		scrollContent
	 */
	scrollContent: null,

	/**
	 * The container of the scrolling area
	 * 
	 * @var	object		scrollPane
	 */
	scrollPane: null,
	
	/**
	 * The step width for the scrollbar buttons
	 * 
	 * @var	integer		scrollStep
	 */
	scrollStep: 5,


	
	/**
	 * Initializes
	 * 
	 * @return	void
	 */
	init: function() {
			// hide news menu items
		if ($('ul.news-menu-level2').length) {
			$('ul.news-menu-level2').parent('li').addClass('news-menu-level1');
			$('ul.news-menu-level2').parent('li').addClass('act');
			$('.news-menu-level3').hide()
		}
		
			// scrolling
		if ($('#content_subpages').length || $('#content_gallery').length) {
			this.initScrollbar();
		}
		
		
	},
	
	
	
	/**
	 * Initializes the scrollbar
	 * 
	 * @return	void
	 */
	initScrollbar: function() {
		this.scrollContent = $('#scroll-content');
		
			// get elements and heights
		if ($('#content_subpages').length) {
			this.contentHeight = 557;
			this.scrollPane = $('#content_subpages');
			var class_scrollbarContainer = "scroll-bar-wrap-subpages";
		} else {
			this.contentHeight = 455;
			this.scrollPane = $('#content_gallery');	
			var class_scrollbarContainer = "scroll-bar-wrap-gallery";
		}
		
		
		if (this.contentHeight < this.scrollContent.height()) {
			
				// prepare content area
			this.scrollPane.height(this.contentHeight);
			this.scrollPane.css('overflow', 'hidden');
			this.scrollContent.css('width', (this.scrollPane.width() - 40) + 'px');
			this.scrollContent.css('float', 'left');
			this.scrollContent.css('position', 'relative');
			
				// add scroll bar
			this.scrollContent.after(
				'<div id="scroll-bar-wrap" class="' + class_scrollbarContainer + '">' +
					'<a class="btn-scroll" id="btn-scroll-up"></a>' +
				//		'<a class="btn-scroll" id="btn-scroll-up" onmousedown="scrollDivUp(\'btn-scroll-up\')" onmouseup="stopMe()"></a>' +
					'<div class="scroll-bar"></div>' +
					'<a class="btn-scroll" id="btn-scroll-down"></a>' +
				'</div>'
			);
			if ($('#content_gallery').length) {
				$('.ui-slider-vertical').css('height', '274');
			}			
			
				//build slider
			var scrollbar = $('.scroll-bar').slider({
				orientation: 'vertical',
				value: 100,
				slide:function(e, ui){
					if( ifd.scrollContent.height() > ifd.scrollPane.height()){
						ifd.scrollContent.css(
							'top',
							Math.round((100 - ui.value) / 100 * (ifd.scrollPane.height() - ifd.scrollContent.height())) + 'px'
						);
					} else {
						ifd.scrollContent.css('top', 0); 
					}
				}
			});			
			
				//scrolling function
			var activeUp = false;
			 $("#btn-scroll-up").mousedown(function(){
				if (!activeUp) {
					activeUp = !activeUp;
					startScrollingUp();
					$(this).everyTime(280, 'up', function(){
						startScrollingUp();
					});
				}
			}).mouseup(function(){
				if (activeUp) {
					activeUp = !activeUp;
					$(this).stopTime('up');
				}
			}).mouseout(function(){
				if (activeUp) {
					activeUp = !activeUp;
					$(this).stopTime('up');
				}
			});
			 
			var activeDown = false;
			$("#btn-scroll-down").mousedown(function(){
				if (!activeDown) {
					activeDown = !activeDown;
					startScrollingDown();
					$(this).everyTime(280, 'down', function(){
						startScrollingDown();
					});
				}
			}).mouseup(function(){
				if (activeDown) {
					activeDown = !activeDown;
					$(this).stopTime('down');
				}
			}).mouseout(function(){
				if (activeDown) {
					activeDown = !activeDown;
					$(this).stopTime('down');
				}
			});

				// init buttons
			this.clickScrollHeight = parseInt((ifd.scrollContent.height() - ifd.scrollPane.height()) * (this.scrollStep / 100));
			 
			function startScrollingUp(){
				if (parseInt(ifd.scrollContent.css('top')) < 0) {
					if (parseInt($('.ui-slider-handle').css('bottom')) + ifd.scrollStep < 100) {
						$('.ui-slider-handle').css('bottom', (parseInt($('.ui-slider-handle').css('bottom')) + ifd.scrollStep) + '%');
						ifd.scrollContent.css('top', (ifd.scrollContent.position().top + ifd.clickScrollHeight) + 'px');
					} else {
						$('.ui-slider-handle').css('bottom', '100%');
						ifd.scrollContent.css('top', '0px');
					}
				}
			};
			
			function startScrollingDown(){
				if (parseInt($('.ui-slider-handle').css('bottom')) > 0) {
					if (parseInt($('.ui-slider-handle').css('bottom')) > ifd.scrollStep) {
						$('.ui-slider-handle').css('bottom', (parseInt($('.ui-slider-handle').css('bottom')) - ifd.scrollStep) +  '%');
						ifd.scrollContent.css('top', (ifd.scrollContent.position().top - ifd.clickScrollHeight) + 'px');
					} else {
						$('.ui-slider-handle').css('bottom', '0%');
						ifd.scrollContent.css('top', ((ifd.scrollContent.height() - ifd.scrollPane.height()) * -1) + 'px');
					}
				}
			}
		} else {
			this.scrollContent.css('height' ,  this.contentHeight);
		}				
	},


	/**
	 * Prepares an update of thickbox content
	 * 
	 * @param	string		the new thickbox title
	 * @param	object		the body from the thickbox
	 * @return	void
	 */
	prepareThickboxUpdate: function(title, thickboxBody) {
		$('body').append('<div id="TB_load"><img src="' + imgLoader.src + '" /></div>');
		$('#TB_load').show();
		
		$('#TB_ajaxWindowTitle').html(title);
		thickboxBody.append('<div id="TB_overlay" class="TB_overlayBG"></div>');
	},

	
	/**
	 * Toggle submenu
	 * 
	 * @param	object		link: the clicked link tag
	 */
	toggleSubmenu: function(link) {
		var currentListElement = $(link).parent('li');
		
		if (currentListElement.hasClass('act')) {
			currentListElement.removeClass('act');
			
			if (currentListElement.parent().hasClass('news-menu-level2')) {
				$('ul.news-menu-level3').hide();
				$('ul.news-menu-level3 li').removeClass('act');
			} else if (currentListElement.hasClass('news-menu-level1')) {
				$('ul.news-menu-level2, ul.news-menu-level3').hide();
				$('ul.news-menu-level2 li, ul.news-menu-level3 li').removeClass('act');				
			}
			
		} else {
				// first level
			if (currentListElement.hasClass('news-menu-level1')) {
				$('ul.news-menu-level2').show();
				
				currentListElement.addClass('act');
			}
			
				// second level
			else if (currentListElement.parent().hasClass('news-menu-level2')) {
				$('ul.news-menu-level2 li').removeClass('act');
				$('ul.news-menu-level3 li').removeClass('act');
				$('ul.news-menu-level3').hide();
				
				currentListElement.addClass('act');
				currentListElement.children('ul').show();
			} 
			
				// third level
			else if (currentListElement.parent().hasClass('news-menu-level1')) {
				$('ul.news-menu-level3').hide();
				$('ul.news-menu-level3').removeClass('act');
				
				currentListElement.addClass('act');				
			}
			
		}
	}
	
	
	
};

	// initialize
$(document).ready(function() {
	ifd.init();
});