/**
 * Navigation
 */
$(function(){
	$('#nav > li').hide().parent().addClass('folded').wrap('<div id="navsensitivity" />').append(
		$("<span>").attr('class', 'navtoggle').text('Navigation')
	).parent().hover(
		function (){
			$('#nav > li').show().parent().animate({height:'46px',top:'140px',marginLeft:'-330px'}).find('.navtoggle').hide();
		},
		function (){
			//if(isNotIE6()){
				$('#nav > li').hide().parent().animate({height:'20px',top:'180px',marginLeft:'-330px'}).find('.navtoggle').show();
			//}
		}
	);
});

/**
 * Main Content
 */
$(function(){
	// Content Sliding
	$('#content_left .ce-wrap').wrapAll('<div class="ces-wrap clearfix" />').parent().parent().append(
		$('<div>').attr('class', 'arrowleft').click(function (){
			if(cml() < 0	){
				$('#content_left .ces-wrap').animate({
					marginLeft:'+=460px'
				});
			}else{
				$('#content_left .ces-wrap').animate({
					marginLeft:(-(($('#content_left .ces-wrap').children().size()-1)*460))+'px'
				});
			}
		})
	).append(
		$('<div>').attr('class', 'arrowright').click(function (){
			if(cml() > - ($('#content_left .ces-wrap').children().size()-1)*460){
				$('#content_left .ces-wrap').animate({
					marginLeft:'-=460px'
				});
			}else{
				$('#content_left .ces-wrap').animate({
					marginLeft:'0'
				});
			}
		})
	);
	// Content More Link
	$('#content_left .ce-wrap').each(function (){
		$(this).append(
			$('<div>').attr('class', 'morelink').append(
				$('<a>').attr('href', $(this).find('.stdheader a').attr('href')).text('mehr lesen')
			)
		);
	});
});

function cml(){
	return parseInt($('#content_left .ces-wrap').css('marginLeft'))
}

function getActiveContentItem(){
	return Math.floor(-(parseInt($('#content_left .ces-wrap').css('marginLeft'))/460))
}

function getActiveNewsItem(){
	if($('.news-list-item').height() < 150){
		var active = Math.floor(-(parseInt($('.news-slider').css('marginTop'))/132))
	}else{
		var active = Math.floor(-(parseInt($('.news-slider').css('marginTop'))/388))
	}
	return active;
}

function isNotIE6(){
	var notIE6 = true;
	jQuery.each(jQuery.browser, function(i, val) {
	   if(i=="msie")
		   notIE6 = false;
	 });
	//alert(jQuery.browser.version);
	return notIE6;
}

/**
 * News Content
 */
$(function(){
	// News Sliding
	$('.news-list-container').wrapInner('<div class="news-slider" />').parent().parent().addClass('clearfix').prepend(
		$('<div>').attr('class', 'newsarrowwrap').append(
			$('<div>').attr('class', 'newsarrowtop').click(function(){
				if(parseInt($('.news-slider').css('marginTop')) < 0	){
					$('.news-slider').animate({
						'marginTop': (parseInt($('.news-slider').css('marginTop'))+132)+'px'
					})
				}else{
					$('.news-slider').animate({
						'marginTop': (-(($('.news-slider').children().size()-3)*132))+'px'
					})
				}
			})
		).append(	
			$('<div>').attr('class', 'newsarrowbottom').click(function(){
				if(parseInt($('.news-slider').css('marginTop')) > - (($('.news-slider').children().size())-3)*132	){
					$('.news-slider').animate({
						'marginTop': (parseInt($('.news-slider').css('marginTop'))-132)+'px'
					})
				}else{
					$('.news-slider').animate({
						'marginTop': 0
					})
				}
			})
		)
	).find('.ce-wrap').css({'float':'right','width':'150px'});
	$('.news-content').hide();
	$('.news-list-item').css({height:132,overflow:'hidden'}).parent().append(
		$('<div>').attr('class', 'morelink').append(
			$('<a>').text('mehr lesen')
		)
		
	).append(
		$('<div>').attr('class', 'closenews').append(
			$('<a>').text('schließen')
		).hide()
	);
	
	$('.news-list-container .morelink').click(function(){
		if($('.news-list-item').height() < 150){
			$('#subcontent').animate({
				marginTop: '-221px'
			});
			$('.news-slider').animate({
				'marginTop': -(getActiveNewsItem()*388)
			})
			
			$('.news-list-item').height(388);
			$('.news-content').show();
			$('#subcontent .fheight').animate({
				height: '+=221px',
				backgroundColor: '#e0007c',
				color: '#fff'
			});
			$('.newsarrowwrap > div').animate({
				height: 0
			});
			$('.newsarrowwrap').animate({
				backgroundColor: '#e0007c'
			});
			$('.news-list-container .morelink').animate({
				top: '-244px'
			});
			$('.news-list-container .morelink').css({
				'background-image': 'none'
			});
			$('#subcontent .stdheader').hide();
			$('.news-list-container').parent().parent().css(
				'background-image', 'url(../../img/news_slided_bg.gif)'
			);
			$('.closenews').show();
		}
	});
	$('.news-list-container .closenews').click(function(){
		$('#subcontent').animate({
			marginTop: 0
		});
		$('.news-slider').animate({
			'marginTop': -(getActiveNewsItem()*132)
		});
		$('.news-list-item').height(132);
		$('.news-content').hide();
		$('#subcontent .fheight').animate({
			height: '177px',
			backgroundColor: '#fff',
			color: '#4b4b4b'
		});
		$('.newsarrowwrap > div').animate({
			height: '23px'
		});
		$('.newsarrowwrap').animate({
			backgroundColor: '#e0007c'
		});
		$('.news-list-container .morelink').animate({
			top: '155px'
		});
		$('.news-list-container .morelink').css({
			'background-image': 'url(../../img/morelink_bg.gif)'
		});
		$('#subcontent .stdheader').show();
		$('.news-list-container').parent().parent().css(
			'background-image', 'url(../../img/content_bg.png)'
		);
		$('.closenews').hide();
	});
});

/**
 * Scrollable Content Area
 */
$(function(){
	if($().jScrollPane) {
		$('.maintemplate #content .clip').jScrollPane({showArrows:true,scrollbarWidth:20, scrollbarMargin:0, arrowSize:23, animateTo: true});
	}
});

/**
 * Misc
 */
$(function(){
	$('.fheight, .newsarrowwrap').css({height:'177px',overflow:'hidden'});
	$('.newsarrowbottom').css('marginTop',($('.newsarrowwrap').parent().height()-$('.newsarrowtop').height()-$('.newsarrowbottom').height())+'px');
});
