/*!
 * This script has been created by AGENTURFUERKRANKEMEDIEN, Berlin 2o1o
 * http://agenturfuerkrankemedien.de/
 *
*/
(function() {

//sets the max size of elements
//taller elements will be folded
var maxheight = 155;
var maxheight_inner_foldable = 66;
var maxheight_news_box_foldable = 93;
//sets the cushion range
//(for elements that only slighty taller)
var cusionrange = 22;
//sets the elements per-page count
//for paginated pages
var perpage = 10;

//create dynamic paginator
var paginationElements = 0;
var paginationPages = 0;
var paginationCurrent = 0;

$(document).ready(function() {

//looks for foldable elements and re-wraps 'em
$( 'div[rel=unfoldable]' ).wrap( '<div class="foldable_element" />' );
//further processing
$( 'div.foldable_element' ).each( function(){
	//shortcuts for element
	var me = $(this);
	//check if element is large enough (minus cushion)
	if( me.height()-cusionrange > maxheight ) {
		//save the original height
		me.attr( 'rel', me.height());
		//check for profile_ontop class
		if( me.children().hasClass( 'profile_ontop' )) {
			//remove from children
			me.children().removeClass( 'profile_ontop' );
			//add to parent warp
			me.addClass( 'profile_ontop' );
		}
	} else {
		//exclude from the play
		me.removeClass( 'foldable_element' );
	}
});
//resize 'em all to default value
$( 'div.foldable_element' ).animate({ 'height': maxheight }, { duration: 'fast' });
//add open/close-button
$( 'div.foldable_element' ).append( '<div class="foldable_button"><img src="tl_files/site_elements/foldable-dummy.png" alt="weiterlesen.."></div>' );
//check for foldable_article
$( 'div.foldable_element' ).each( function(){
	//shortcuts for element
	var me = $(this);
	var children = me.children();
	if( children.hasClass( 'foldable_article' )) {
                //add extra class
		children.next().addClass( 'foldable_button_article' );
	}
});

//add button handler
$( 'div.foldable_button' ).click( function(){
	//shortcuts for element
	var me = $(this);
	var parent = me.parent();
	//remove hover classes
	me.removeClass( 'foldable_button_opened_hover' );
	me.removeClass( 'foldable_button_hover' );
	//still closed ?
	if( parent.height() == maxheight ) {
		//open
		parent.animate({ 'height': parent.attr( 'rel' ) }, { duration: 'fast' });
		//add css class for opened
		me.addClass( 'foldable_button_opened' );
	} else {
		//close
		parent.animate({ 'height': maxheight }, { duration: 'fast' });
		//remove css class for opened
		me.removeClass( 'foldable_button_opened' );
	}
});
$( 'div.foldable_button' ).hover(
	function (){ //over
		//shortcuts for element
		var me = $(this);
		//opened or closed ?
		if( me.hasClass( 'foldable_button_opened' )) {
			//opened
			me.addClass( 'foldable_button_opened_hover' );
		} else {
			//closed
			me.addClass( 'foldable_button_hover' );
		}
	},
	function (){ //out
		//shortcuts for element
		var me = $(this);
		//opened or closed ?
		if( me.hasClass( 'foldable_button_opened' )) {
			//opened
			me.removeClass( 'foldable_button_opened_hover' );
		} else {
			//closed
			me.removeClass( 'foldable_button_hover' );
		}
	}
);


//looks for inner foldable elements and re-wraps 'em
$( 'div[rel=inner_unfoldable]' ).wrap( '<div class="inner_foldable_element" />' );
//further processing
$( 'div.inner_foldable_element' ).each( function(){
	//shortcuts for element
	var me = $(this);
	//check if element is large enough (minus cushion)
	if( me.height()-cusionrange > maxheight_inner_foldable ) {
		//save the original height
		me.attr( 'rel', me.height());
	} else {
		//exclude from the play
		me.removeClass( 'inner_foldable_element' );
	}
});

//resize 'em all to default value
$( 'div.inner_foldable_element' ).animate({ 'height': maxheight_inner_foldable }, { duration: 'fast' });
//add open/close-button
$( 'div.inner_foldable_element' ).append( '<div class="inner_foldable_button">mehr</div>' );
//add button handler
$( 'div.inner_foldable_button' ).click( function(){
	//shortcuts for element
	var me = $(this);
	var parent = me.parent();
	//remove hover classes
	me.removeClass( 'inner_foldable_button_opened_hover' );
	me.removeClass( 'inner_foldable_button_hover' );
	//still closed ?
	if( parent.height() == maxheight_inner_foldable ) {
		//open
		parent.animate({ 'height': parent.attr( 'rel' ) }, { duration: 'fast' });
		//add css class for opened
		me.addClass( 'inner_foldable_button_opened' );
                me.html( 'schließen' );
	} else {
		//close
		parent.animate({ 'height': maxheight_inner_foldable }, { duration: 'fast' });
		//remove css class for opened
		me.removeClass( 'inner_foldable_button_opened' );
                me.html( 'mehr' );
	}
});
$( 'div.inner_foldable_button' ).hover(
	function (){ //over
		//shortcuts for element
		var me = $(this);
		//opened or closed ?
		if( me.hasClass( 'inner_foldable_button_opened' )) {
			//opened
			me.addClass( 'inner_foldable_button_opened_hover' );
		} else {
			//closed
			me.addClass( 'inner_foldable_button_hover' );
		}
	},
	function (){ //out
		//shortcuts for element
		var me = $(this);
		//opened or closed ?
		if( me.hasClass( 'inner_foldable_button_opened' )) {
			//opened
			me.removeClass( 'inner_foldable_button_opened_hover' );
		} else {
			//closed
			me.removeClass( 'inner_foldable_button_hover' );
		}
	}
);



//looks for news box foldable elements and re-wraps 'em
$( 'div[rel=news_box_unfoldable]' ).wrap( '<div class="news_box_foldable_element" />' );
//further processing
$( 'div.news_box_foldable_element' ).each( function(){
	//shortcuts for element
	var me = $(this);
	//check if element is large enough (minus cushion)
	if( me.height()-cusionrange > maxheight_news_box_foldable ) {
		//save the original height
		me.attr( 'rel', me.height());
	} else {
		//exclude from the play
		me.removeClass( 'news_box_foldable_element' );
	}
});

//resize 'em all to default value
$( 'div.news_box_foldable_element' ).animate({ 'height': maxheight_news_box_foldable }, { duration: 'fast' });
//add open/close-button
$( 'div.news_box_foldable_element' ).after( '<div class="news_box_foldable_button"><span>Mehrere News anzeigen</span></div>' );
//add button handler
$( 'div.news_box_foldable_button' ).click( function(){
	//shortcuts for element
	var me = $(this);
	//var parent = me.parent();
	var parent = $( 'div.news_box_foldable_element' );
	//remove hover classes
	me.removeClass( 'news_box_foldable_button_opened_hover' );
	me.removeClass( 'news_box_foldable_button_hover' );
	//still closed ?
	if( parent.height() == maxheight_news_box_foldable ) {
		//open
		parent.animate({ 'height': parent.attr( 'rel' ) }, { duration: 'fast' });
		//add css class for opened
		me.addClass( 'news_box_foldable_button_opened' );
		me.html( '<span>Weniger News anzeigen</span>' );
	} else {
		//close
		parent.animate({ 'height': maxheight_news_box_foldable }, { duration: 'fast' });
		//remove css class for opened
		me.removeClass( 'news_box_foldable_button_opened' );
		me.html( '<span>Mehrere News anzeigen</span>' );
	}
});
$( 'div.news_box_foldable_button' ).hover(
	function (){ //over
		//shortcuts for element
		var me = $(this);
		//opened or closed ?
		if( me.hasClass( 'news_box_foldable_button_opened' )) {
			//opened
			me.addClass( 'news_box_foldable_button_opened_hover' );
		} else {
			//closed
			me.addClass( 'news_box_foldable_button_hover' );
		}
	},
	function (){ //out
		//shortcuts for element
		var me = $(this);
		//opened or closed ?
		if( me.hasClass( 'news_box_foldable_button_opened' )) {
			//opened
			me.removeClass( 'news_box_foldable_button_opened_hover' );
		} else {
			//closed
			me.removeClass( 'news_box_foldable_button_hover' );
		}
	}
);


//looks for paginable elements
$( 'div[rel=pagination]' ).each( function(){
	//count 'em
	paginationElements++;
	//and add their number as title
	$(this).attr( 'title', 'pCount'+paginationElements );
});
//pagination needed?
if( paginationElements > 0 ) {
	//calculate number of pages
	var paginationPages = Math.ceil(paginationElements/perpage);
	//add wrapper for controls
	$( 'div#wrapper' ).prepend( '<div id="thePaginatorTop"></div>' );
	$( 'div#wrapper' ).append( '<div id="thePaginatorBottom"></div>' );
	//add controls
	for( var i=1; i<=paginationPages; i++ ) {
		var html = '<div title="'+i+'" rel="pSelect"><img src="tl_files/site_elements/paginator-icon-dummy.png" /></div>';
		$( 'div#thePaginatorTop' ).append( html );
		$( 'div#thePaginatorBottom' ).append( html );
	}
	//add wrappers for positioning
	$( 'div#thePaginatorTop' ).wrapInner( '<div class="pageCtrl" />' );
	$( 'div#thePaginatorBottom' ).wrapInner( '<div class="pageCtrl" />' );
	//add handler
	$( 'div.pageCtrl > div[rel=pSelect]' ).click( function(){
		//shortcuts for element
		var me = $(this);
		var pid = me.attr('title');
		//do pagination
		paginate( pid );
	});
	//add number
	$( 'div.pageCtrl' ).append( '<div class="number"><img src="tl_files/site_elements/paginator-icon-dummy.png" /></div>' );
	//start at first page
	paginate( 1 );
}

function paginate( pageNo ) {
	//act only if not current page
	if( paginationCurrent != pageNo ) {
		//save current
		paginationCurrent = pageNo;
		//update number
		$( 'div.number' ).attr( 'rel', 'number'+paginationCurrent );
		//reset (hide) all elements
		$( 'div[rel=pagination]' ).hide();
		//select current element range
		var range = [];
		for( var i=perpage*(pageNo-1)+1; i<=perpage*pageNo; i++ ) {
			range.push( 'pCount'+i );
		}
		//show the elements in range (with delay)
		$( 'div[rel=pagination]' ).each( function(){
			//shortcuts for element
			var me = $(this);
			var title = me.attr('title');
			//check if in range
			if( $.inArray( title, range ) != -1 ) {
				me.fadeIn( 'fast' );
			}
		});
		//alter page navigation
		$( 'div[rel=pSelect]' ).each( function(){
			//shortcuts for element
			var me = $(this);
			var pid = me.attr('title');
			//toggle classes
			if( pid == pageNo ){ me.addClass( 'active' ); }
			else{ me.removeClass( 'active' ); }
		});
	}
}
});
})();
function printfunction( lang ) {
	//add print control
	var html = '<div id="printControl" class="dontPrint"> </div>';
	$( 'div#wrapper' ).append( html );
	//change background image, if english
	if( lang == "en" ) {
		$( 'div#printControl' ).css( "background-position", "0px -17px" );
	}
	//add click event
	$( 'div#printControl' ).click( function(){
		window.print();
	});
}
