function isMobile() {
	if(
		navigator.userAgent.match(/Android/i) ||
		navigator.userAgent.match(/webOS/i) ||
		navigator.userAgent.match(/iPhone/i) ||
		navigator.userAgent.match(/iPad/i) ||
		navigator.userAgent.match(/iPod/i)
	) {
		return true;
	}
	return false;
}

function textareaResize(taEl) {
		var taObj	= jQuery(taEl);
		var lineHeight = parseInt( taObj.css('lineHeight') );

		if ( taEl.scrollHeight > taObj.height() ) {
			taObj.css('height', taEl.scrollHeight+'px');
		} else {
			var counter = 20;
			do {
				taObj.css('height', (taObj.height()-lineHeight)+'px');
				counter--;
			} while ( taEl.scrollHeight == taObj.height() && taObj.height() > 0 && counter > 0 );
			taObj.css('height', (taObj.height()+lineHeight)+'px');
		}
		
		if ( taObj.height() <= taObj.attr('rows')*lineHeight) {
			taObj.css('height', (taObj.attr('rows')*lineHeight)+'px');
		} else {
			taObj.css('height', (taObj.height()+lineHeight)+'px');
		}
	}

	function plusone(params) {
		dumplog(params);
	}
	
	
jQuery(document).ready(function() {
	if ( typeof window['gapi'] != 'undefined' ) {
		gapi.plusone.go();
	}
	
	if ( typeof initFacebook == 'function' ) {
		initFacebook();
	}
	
	if ( isMobile() != true ) {
		$("a.teaser.popup").fancybox({
			'width'				: 940,		
			'height'			: '85%',
			'centerOnScroll'	: true,
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'overlayOpacity'    : 0.8,
			'speedIn'           : 400,
			'speedOut'          : 400,
			'margin'            : 0,
			'padding'           : 0,
			'changeFade'		: 'slow',
			'type'				: 'iframe',
			'onStart'			: function() {
				var query_appended = false;
				
				if ( this.href.indexOf('?') == -1 ) {
					this.href += '?';
					query_appended = true;
				}
	
				if ( this.href.indexOf('popup=1') == -1 ) {
					if ( query_appended == true ) {
						this.href += 'popup=1';
					} else {
						this.href += '&amp;popup=1';
					}
				}
			}
		});
		
		$("a.fancybox").fancybox({
			type: 'iframe',
			width: 770,
			height: '90%',
			centerOnScroll: true,
			overlayOpacity: 0.7,
			overlayColor:	'0000000'
		});
	}
	
	jQuery('#scrollTop').unbind('click').bind('click', function() {
		jQuery('html,body,document').animate({scrollTop : 0},'slow');
		return false;
	});
	
	initSpecialContentToggle();
	
	jQuery('textarea').attr('onchange', 'textareaResize(this);').attr('onkeyup', 'textareaResize(this);');
	jQuery('textarea').each(function(){ textareaResize(this); });
	
	if ( typeof jQuery.fn.triplexProjekte == 'function' ) {
		jQuery('.referenzen').triplexProjekte();
	}

	if ( typeof jQuery.fn.triplexNewsIndex == 'function' ) {
		jQuery('.news_index.filter_list').triplexNewsIndex({'customEvents': {
																	'afterInit': function(params){ if (params.find('.active').length == 0 ) {params.find('li:eq(0) a').trigger('click');}},
																	'completeApplyFilter': function(params){
																		var mask = params.parents('#mask:eq(0)');
																		if ( mask.length > 0 ) {
																			var panels = params.parents('div[class^="panel_"]');
																			if ( panels.filter('.selected') == 0 ) {
																				mask.css('height', params.parents('div[class^="panel_"]:eq(0)').css('height') );
																			} else {
																				mask.css('height', params.parents('div[class^="panel_"]').filter('.selected').css('height') );
																			}
																		}
																	}
																}
		});
	}

	if ( typeof jQuery.fn.triplexContact == 'function' ) {
		jQuery('.form').triplexContact();
	}

	var promo = jQuery('.promo');
	if ( promo.length > 0 && typeof jQuery.fn.triplexPromoSlides == 'function') {
		promo.triplexPromoSlides();

		jQuery(window).keydown(function(event){
			if ( event.keyCode == 37 ) {
				promo.find('.left').trigger('click');
			} else if ( event.keyCode == 39 ) {
				promo.find('.right').trigger('click');
			}
		});

		promo.find('.arrow').unbind('click').bind('click', function(){
			var arrow = jQuery(this);
			
			var nav = promo.find('.nav');
			
			var active_elem = nav.find('li a.active');
			var new_index	= 0;
	
			var entries = nav.children('li');
	
			if ( active_elem.length > 0 ) {
				var active_index = active_elem.parent().index();
	
				if ( arrow.hasClass('left') ) {
					if ( active_index == 0 ) {
						new_index = entries.length - 1;
					} else {
						new_index = active_index - 1;
					}
				} else {
					if ( active_index != entries.length - 1 ) {
						new_index = active_index + 1;
					}
				}
			}
	
			var link = entries.eq(new_index).find('a');
			link.trigger('click');
			return false;
		});
	}
	
	//jQuery('.footerIcons').append('<div style="float: left; height: 20px; width: 67px;"><a href="http://twitter.com/share" class="twitter-share-button" data-count="none" data-lang="de">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div>');
	jQuery('.footerIcons').append('<div style="float: right; height: 20px; width: 67px; margin-left: 10px; overflow: hidden;"><a href="http://twitter.com/share" class="twitter-share-button" data-count="none" data-lang="de">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div>');
	jQuery('.footerIcons').append('<div style="float: right; height: 20px; width: 78px; margin-left: 10px; overflow: hidden;"><iframe src="http://www.facebook.com/plugins/like.php?api_key=113869198637480&channel_url=http%3A%2F%2Fstatic.ak.fbcdn.net%2Fconnect%2Fxd_proxy.php%3Fversion%3D2%23cb%3Df18854f94be8c64%26origin%3Dhttp%253A%252F%252Fdevelopers.facebook.com%252Ff4af34d873c8c4%26relation%3Dparent.parent%26transport%3Dpostmessage&font=verdana&href=http%3A%2F%2Fwww.triplex.de&layout=button_count&locale=en_US&node_type=link&sdk=joey&send=false&show_faces=false&width=300" scrolling="no" frameborder="0" style="border:none; overflow:hidden; height:21px;" allowTransparency="true"></iframe></div>');
	jQuery('.footerIcons').append('<div style="float: right; height: 20px; margin-left: 10px; overflow: hidden;"><g:plusone href="http://www.dgap.biz" callback="plusone" size="medium"></g:plusone><script type="text/javascript" src="https://apis.google.com/js/plusone.js">{lang: "de", parsetags: "explicit"}</script></div>');
});



/**
 * @author Alexander Farkas
 * v. 1.21
 */

(function($) {
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = jQuery.curCSS;
		jQuery.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}
	
	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};
	
	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}
	
	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');
			
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}
			
			start = toArray(start);
			
			fx.start = [start[0],start[2]];
			
			var end = toArray(fx.options.curAnim.backgroundPosition);
			fx.end = [end[0],end[2]];
			
			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery);


function initSpecialContentToggle (){
	jQuery('.special_content .toggle').unbind('click').bind('click', function(){
		animateSpecialContent( jQuery(this.parentNode) );
		return false;
	});
	
	/*
	jQuery('.special_content').unbind('mouseover').bind('mouseover', function(){
		animateSpecialContent( jQuery(this), tease = true );
		return false;
	}).unbind('mouseout').bind('mouseout', function(){
		animateSpecialContent( jQuery(this), tease = false );
		return false;
	});
	*/
}

function animateSpecialContent(contentEl, tease) {
	var duration		= 1000;
	var detailsEl		= contentEl.children('.details');
	var tease_height	= 50;
	
	var cur_height = (detailsEl.css('display') == 'none') ? 0 : detailsEl.height();
	var full_height = detailsEl.css('height', 'auto').height();
	detailsEl.css({'display': 'block', 'overflow': 'hidden'}).height(cur_height);
	
	if ( tease == true ) {
		if ( detailsEl.hasClass('animating') == false && contentEl.hasClass('open') == false ) {
			detailsEl.stop().addClass('tease').animate({'height': tease_height+'px', 'paddingTop': '10px'}, (tease_height - cur_height) * 2, 'linear' );
		}
		return true;
	}
	if ( tease == false ) {
		if ( detailsEl.hasClass('tease') == true ) {
			detailsEl.stop().animate({'height' : 0, 'paddingTop' : 0, 'paddingBottom': 0}, cur_height * 10, function() {
				detailsEl.css('display', 'none');
				detailsEl.removeClass('tease');
			} );
		}
		return true;
	}
	
	detailsEl.removeClass('tease');
	
	if (contentEl.hasClass('open')){
		detailsEl.stop().addClass('animating').css('height', '383px' ).animate({'height' : '0px', 'paddingTop' : 0, 'paddingBottom': 0}, duration, function() {
			detailsEl.css('display', 'none');
			detailsEl.removeClass('animating');
			contentEl.removeClass('open').addClass('closed');			
		});
	}
	else {
		detailsEl.stop().addClass('animating').animate({'height': full_height+'px', 'paddingTop': '35px', 'paddingBottom': '50px'}, duration, function() {
			detailsEl.removeClass('animating');
			contentEl.removeClass('closed').addClass('open');
		});
	}
}
