$j = $; //jQuery.noConflict();
var scrollerstepwidth = 1;
var steps = 1;
jQuery.easing.def = 'easeOutCirc';


// extract coockies

a = document.cookie;
var lines = a.split("; ");

var cookies = new Array();
var parts = new Array();
var mouse_wheel_buffer = 0;
var page_id = 1;
var jump_steps = 1;


for (var i = 0; i <lines.length;i++)
{
	if (lines[i] == "")
		continue;
	
	parts = lines[i].split("=");
	//cookies[i] = new Array();
	cookies[parts[0]] = parts[1];
}



$j(document).ready(function(){
	// disable old events
	/*
	$j('li.Level0Item > ol').hide();

	$j('li.Level0Item').mouseover(function(){
		console.log('hover');
		$j('> ol', this).slideDown();
	}).mouseout(function(){
		$j('> ol', this).slideUp();
	});
	*/
	
	/*var divs = 
        $(unescape('%3Cdiv class="scroller" style=""%3E %3C/div%3E'));

	$('#lev1_balken').append(divs);
	steps = $('#scrollContainer .scrollContent').length;
	
	if (steps > 1)
	{
		var width = $('#scrollContainer').width();
		
		//var scrollbandwidth  = (steps*($('#scrollContainer .scrollContent').width()));
		/* var scrollbandwidth  = 0;
		
		
		
		$('#scrollContainer .scrollContent').each(function() {
			scrollbandwidth += $(this).width();
		});
		
		console.log(scrollbandwidth);
		//scrollbandwidth += 5;
		
		$('#scrollBand').css('width',scrollbandwidth+"px");
		
		//$("#scrollBand").css('width',scrollbandwidth+"px");			
		//$("#scrollBand").css('width',((steps*($('#scrollContainer .scrollContent').outerWidth())))+"px");
		
		steps_per_page = Math.ceil($('#scrollContainer').width()/$('#scrollContainer .scrollContent').eq(0).width());
		
		steps -= steps_per_page-1;w
		
		steps=4;
		
		scrollerstepwidth = width/steps;
		console.log("scrollpane width:"+width);
		console.log('scroller stepwidth: '+scrollerstepwidth);
		console.log ('steps: '+steps);
		console.log ('steps per page: '+steps_per_page);
		
		percent = width/(scrollbandwidth);
		

		
		scrollerwidth = width/steps;
		
		//console.log('scrollerwidth: '+scrollerwidth);
		
		//scrollerwidth = width*$('#scrollContainer')[0].offsetWidth/$('#scrollBand')[0].offsetWidth;
		
		scrollerstepwidth = scrollerstepwidth*percent;
		scrollerwidth = scrollerwidth*percent; */
		
		//scrollerstepwidth += 22;

		//console.log('scroller stepwidth: '+scrollerstepwidth);
		//console.log('scroller scrollerwidth: '+scrollerwidth);
		
		//console.log($('#scrollContainer')[0].offsetWidth/$('#scrollBand')[0].offsetWidth);
		
		//scrollerwidth2 = width/Math.round((scrollbandwidth-30)/width);
		
		
		//console.log(scrollerwidth2);
		//console.log($('#scrollBand')[0].offsetWidth);
		
		//if (scrollerwidth < 20)
		//	scrollerwidth = 20;
		/*$("#scrollBand").css('width',steps*$('#scrollContainer .scrollContent')[0].offsetWidth+"px");
		
		
		steps_per_page = Math.ceil($('#scrollContainer')[0].offsetWidth/$('#scrollContainer .scrollContent')[0].offsetWidth);
		steps -= steps_per_page-1;		
		scrollerstepwidth = width/steps;
		/* console.log(steps_per_page);
		console.log("scrollpane width:"+width);
		console.log('scroller stepwith: '+scrollerstepwidth);
		console.log ('steps: '+steps);
		console.log ('steps per page: '+steps_per_page); */
		
		/*scrollerwidth = width/steps;
		
		scrollerwidth2 = width/Math.round($('#scrollBand')[0].offsetWidth/$('#scrollContainer')[0].offsetWidth);		
		
		
		if (steps>1)
		{
			$('#lev1_balken .scroller').css('width',scrollerwidth+"px");
			$('#lev1_balken .scroller').css('cursor',"pointer");
			$('#lev1_balken').css('cursor',"pointer");
			
			$('#scrollContainer').mousewheel(function(objEvent, intDelta) {
				if (intDelta > 0)
				{
					//up 
					//scrollLeft();
					mouse_wheel_buffer--;
				}
				else
				{
					//scrollRight();
					mouse_wheel_buffer++;
				}
			});
			
			$('#lev1_balken .scroller').draggable({
				containment: $('#lev1_balken'), 
				axis: 'x',
				/* grid: [stepwidth,20], */
				/*drag: onScrollerDragging2,
				stop: onScrollerDragging
			});
			
			if (hide_scroller_arrows == undefined)
				var hide_scroller_arrows = false;
			
			$("#lev1_balken").bind('click',scrollBarClicked);
			
			if (!hide_scroller_arrows)
			{
				$('#scrollContainer').append('<a href="javascript: scrollLeft();" class="arrow_left"></a>');
				$('#scrollContainer .arrow_left').hover(function() {$('#scrollContainer .arrow_left').addClass('arrow_left_hover');},function() {$('#scrollContainer .arrow_left').removeClass('arrow_left_hover');});
				
				$('#scrollContainer').append('<a href="javascript: scrollRight();" class="arrow_right"></a>');
				$('#scrollContainer .arrow_right').hover(function() {$('#scrollContainer .arrow_right').addClass('arrow_right_hover');},function() {$('#scrollContainer .arrow_right').removeClass('arrow_right_hover');});
			}
			
		}
	}
	
	if (cookies["scroller_pos_"+page_id] && steps>1)
	{
		cur_scroller_pos = parseInt(cookies["scroller_pos_"+page_id]);
		restore_saved_scroller_pos();
	}*/
	
	
	if (TransMenu.isSupported())
	{
		function init_transmenu_subitems(transmenu, subitems)
		{
			subitems.each(function(i){
				var link = $j('> a', this);
				var item = transmenu.addItem(link.text(), link.attr('href'), link.attr('target'));

				var subitems = $j('> ol > li', this);
				if (subitems.length)
				{
					var subtransmenu = transmenu.addMenu(transmenu.items[i], i);
					init_transmenu_subitems(subtransmenu, subitems);
				}
			});
		}

		function hide_transmenu()
		{
			TransMenuSet.registry[0].hide();
		}

		var menuitems = $j('li.Level0Item').add('li.TopMenuItem').add('#MoilHeader');

		var ms = new TransMenuSet(TransMenu.direction.down, 1, 24, TransMenu.reference.topLeft);

		// generate transmenu from html
		menuitems.each(function(){
			var subitems = $j('> ol > li', this);
			
			var id =$(this).find("a").attr("id");
			
			if (!subitems.length)
			{
				this.onmouseover = hide_transmenu;
			}
			else
			{
				var link = $j('> a', this);
				var transmenu = ms.addMenu(link[0], 0);

				init_transmenu_subitems(transmenu, subitems);
				transmenu.onactivate = function(){
					$j("#"+id).css("background-color","#D85100");
					$j("#"+id).css("color","white");
				}
				transmenu.ondeactivate = function(){
					if ($j(".Level0ItemSelected").attr("id")!=id){
						$j("#"+id).css("background-color","");
						$j("#"+id).css("color","#969696");
					}
				}
			}
		});

		// remove menu from html
		menuitems.find('> ol').remove();

		/*
		menu.onactivate = function() {};
		menu.ondeactivate = function() {};
		*/

		// init transmenu
        var aMenuHtml = [];
        for (var i = 0, menu = null; menu = TransMenu.registry[i]; i++) {
                aMenuHtml[i] = menu.toString();
        }
        $j('body').append(aMenuHtml.join(""));

		TransMenu.initialize();
	}
	

	$('a.image-popup').lightBox();
});

var cur_scroller_pos = 0;

setInterval("move_wheel_scrolled();",500);

function showArrows() {
	if($('#scrollContainer .arrow_left').length>0) {
		$('#scrollContainer .arrow_left').addClass('arrow_left_hover');
		$('#scrollContainer .arrow_right').addClass('arrow_right_hover');
	} else{
		$('.arrow_left').addClass('arrow_left_hover');
		$('.arrow_right').addClass('arrow_right_hover');
	}
}

function hideArrows() {
	if($('#scrollContainer .arrow_left').length>0) {
		$('#scrollContainer .arrow_left').removeClass('arrow_left_hover');
		$('#scrollContainer .arrow_right').removeClass('arrow_right_hover');
	} else{
		$('.arrow_left').removeClass('arrow_left_hover');
		$('.arrow_right').removeClass('arrow_right_hover');
	}
}

function move_wheel_scrolled()
{
	//console.log(mouse_wheel_buffer);
	
	
	cur_scroller_pos += mouse_wheel_buffer;
	
	if (cur_scroller_pos > (steps-1))
		cur_scroller_pos = steps-1;
	
	if (cur_scroller_pos <= 0)
		cur_scroller_pos = 0;

	
	if (mouse_wheel_buffer)
	{
		onScrollDragged();
		moveScroller();
	}
	
	mouse_wheel_buffer = 0;
}

function restore_saved_scroller_pos()
{
	var scrollband_left = ($('#scrollContainer .scrollContent')[0].offsetWidth+22)*cur_scroller_pos*(-1);
	//var scrollband_left = ($('#scrollContainer .scrollContent')[0].offsetWidth)*cur_scroller_pos*(-1);
	
	//alert(scrollband_left);
	$('#scrollBand').css({marginLeft:scrollband_left+"px"});
	
	var margin_left = scrollerstepwidth*cur_scroller_pos;
	$('#lev1_balken .scroller').css({left:margin_left+"px"});
}

function onScrollerDragging2(event, ui)
{
	var new_pos = (ui.position.left)/scrollerstepwidth;
	
	
	/* if (new_pos > (steps-1))
		new_pos = (steps-1); */
	
//	new_pos = 2;
	var margin_left = ($('#scrollContainer .scrollContent').width())*new_pos*(-1);
	
	//margin_left += 30;
	//var margin_left = ($('#scrollContainer .scrollContent')[0].offsetWidth)*new_pos*(-1);
		
	$('#scrollBand').css({marginLeft:margin_left+"px"});
}

function onScrollerDragging(event, ui)
{
	new_pos = ui.position.left/scrollerstepwidth;
	
	/* if (new_pos > (steps-1))
		new_pos = (steps-1); */
	
	if (cur_scroller_pos != new_pos)
	{
		cur_scroller_pos = new_pos;
		onScrollDragged();
		//moveScroller();
	}

}

function scrollBarClicked(e)
{
	
	var scroll_container_pos = getAbsolutePosition($('#lev1_balken')[0]);
	var click_pos = getPos(e,'X')-scroll_container_pos.x;
	//e.clientX
	var scroller_pos = parseInt($('#lev1_balken .scroller').css('left').replace('px',''));
	var scroller_with = parseInt($('#lev1_balken .scroller').css('width').replace('px',''));
	
	
	if (isNaN(scroller_pos))
		scroller_pos = 0;
	
	if (click_pos > (scroller_pos + scroller_with))
	{
		scrollRight();
	}
	else if (click_pos < (scroller_pos))
	{
		scrollLeft();
	} 
	
}

function onScrollDragged()
{
	document.cookie="scroller_pos_"+page_id+"="+cur_scroller_pos;
	
	var margin_left = ($('#scrollContainer .scrollContent')[0].offsetWidth)*cur_scroller_pos*(-1);
	//var margin_left = ($('#scrollContainer .scrollContent')[0].offsetWidth)*cur_scroller_pos*(-1);
	$('#scrollBand').animate({ 
		marginLeft:margin_left+"px"
	},"slow","swing");
}

function moveScroller()
{
	document.cookie="scroller_pos_"+page_id+"="+cur_scroller_pos;
	
	var margin_left = scrollerstepwidth*cur_scroller_pos;
	$('#lev1_balken .scroller').animate({ 
		left:margin_left+"px"
	},"slow","swing");
}

function scrollRight()
{	
	cur_scroller_pos+=jump_steps;
	
	if ((cur_scroller_pos > (steps-1))) {
		while((cur_scroller_pos-jump_steps+1) == (steps-1)) {
			cur_scroller_pos--;
		}
	}	
	
	if (cur_scroller_pos > (steps-1))
		cur_scroller_pos = 0;

	onScrollDragged();
	moveScroller();
}

function scrollLeft()
{
	cur_scroller_pos-=jump_steps;
	
	if ((cur_scroller_pos < 0)) {
		while((cur_scroller_pos+jump_steps-1) == 0) {
			cur_scroller_pos++;
		}
	}	
	
	if (cur_scroller_pos < 0)
		cur_scroller_pos = (steps-1);
	
	onScrollDragged();
	moveScroller();
}


function getPos (event, c) {
	var p = c == 'X' ? 'Left' : 'Top';
	return event['page' + c] || (event['client' + c] + (document.documentElement['scroll' + p] || document.body['scroll' + p])) || 0;
};

function getAbsolutePosition(element) {
    var r = { x: element.offsetLeft, y: element.offsetTop };
    if (element.offsetParent) {
      var tmp = getAbsolutePosition(element.offsetParent);
      r.x += tmp.x;
      r.y += tmp.y;
    }
    return r;
};

function decryptCharcode(character) {
	character = character-2;
	
	return String.fromCharCode(character);
}

function link_toUncryptMail(str) {
	var len = str.length;
	var new_str = "mailto:";

	for(i=0; i<len; i++) {
		new_str = new_str+decryptCharcode(str.charCodeAt(i));
	}
	
	location.href=new_str;	
}
