var addthis_config = {
	services_exclude: 'facebook,twitter',
	ui_cobrand: 'SYNAXON AG'
}

function loadResultCount() {
	var values = $('#service-finder').serialize();
	$('#service-finder-count').load($('#service-count').attr('action'), values);
}

function reloadDigitalLifeFeed() {
	if($('#spinner').length == 0) {
		$('body').append('<div id="spinner"></div>');
	}
	$('.digital-life-feed').each(function() {
		var el = $(this);
		var type = el.prev('.feedType').attr('type');
		$('#spinner').width(el.width()).height(el.height()).offset(el.offset()).show();
		el.load(window.location.href + (window.location.href.indexOf('?') == -1 ? '?' : '&') + 'type=' + type + ' .digital-life-feed-inner', function() {
			FB.init({status: true, cookie: true, xfbml: true});

			$('a.addthis_toolbox').each(function() {
				addthis.button(this)
			});

			$('#spinner').hide();
		});
	});
}

function checkDigitalLifeFeed() {
	/*
	var lastCheckDate = $.data(document.body, lastCheck);
	var lastCheck = Math.floor(nowDate.getTime() / 1000);
	
	var nowMinutes = nowDate.getMinutes();
	var now = Math.floor(nowDate.getTime() / 1000);
	*/
	var nowDate = new Date();
	var minutes = new Array(0, 15, 30, 45);
	/*
	if(!(lastCheck > 0)) {
		lastCheck = now - 30;
	}
	$.data(document.body, lastCheck, nowDate);
	*/
	
	if($.inArray(nowDate.getMinutes(), minutes) !== -1) {
		reloadDigitalLifeFeed();
		console.log('Feeds loaded');
	}
	/*
	$.getJSON('', {eID: 'checkDigitalLifeFeed', lastCheck: lastCheck}, function(response) {
		if(response.newItems) reloadDigitalLifeFeed();
	});
	*/
}

$(document).ready(function() {
	
	$('a.addthis_toolbox').each(function() {
		addthis.button(this)
	});

	$('#service-finder').addClass('with-js');
	$('#service-finder input:checkbox').click(function() {
		loadResultCount();
	});
	
	$('#service-finder-list-form select').change(function() {
		$('#service-finder-currentpage').val(1);
		$('#service-finder-list-form').submit();
	});
	
	$('.service-finder .pagerDB a').click(function() {
		$('#service-finder-currentpage').val($(this).html());
		$('#service-finder-list-form').submit();
		return false;
	});
	
	$('.digital-life-feed:not(.news-list) .newsbox_content').live('mouseover mouseout', function(event) {
		if (event.type == 'mouseover') {
			$(this).addClass('hover');
		} else {
			$(this).removeClass('hover');
		}
	});
	
	
	$('#service-finder div.business-type, #service-finder div.business-orientation, #service-finder div.interest').click(function() {
		var checkbox = $(this).toggleClass('active').find('input:checkbox')
		checkbox.attr('checked', !checkbox.attr('checked'));
		loadResultCount();
	});	
	
	/* Teaser Box */
	$('.akcentBox .akcentBox_middle').each(function() {
		if($('.csc-default:gt(0)', this).length > 1) {
			$('.csc-default:gt(0)', this).wrapAll('<div class="hidden-content" />')
			.parent().after('<div class="weiterlesen"><a href="#">weiterlesen</a></div>')
			$('.hidden-content', this).hide();
			$('.weiterlesen a', this).toggle(function() {
				$(this).html('schließen').parent().addClass('close').prev('.hidden-content').slideDown();
				return false;
			},function() {
				$(this).html('weiterlesen').parent().removeClass('close').prev('.hidden-content').slideUp();
				return false;
			});
		}
	});
	
	if($('.digital-life-feed').length != 0) {
		window.setInterval('checkDigitalLifeFeed()', 30000);
	}
	
	var height = $('#wrapper-content .left .list_position:first').height() - 27 - 32;
	$('.hover-menu-inner2').css('min-height', height);
	
	
	$('#wrapper-content .left .list_position:first ul.first > li:not(.custom)').each(function() {
		var el = $(this);
		el.hover(function() {
			el.addClass('hover');
			el.find('.hover-menu').offset({top: el.parents('.list_position').offset().top - 11, left: el.find('.hover-menu').offset().left});
		}, function() {
			el.removeClass('hover');
		});
		
		$('ul > li > a + ul', el).each(function() {
			$(this).prev('a').click(function() {
				$('ul > li > a.active', el).removeClass('active').next('ul').slideUp();
				$(this).next('ul').slideDown().end().addClass('active');
				return false;
			});
		});
	})
	
});
