// votebox
function loadVotebox() {
	$('#votebox').load('/voting/vote_frontend.php?foo='+Math.random(), function() {
			$('#votebox').parent().show();
			setTimeout(mapFormEvent, 500);
		}
	);
}


var updateHtml;
function mapFormEvent() {
	$('#submitBtn').click(function() {
		$('#votebox #voteform').hide();
		$('#ajaxIndicator').css('display', 'block');
		var postData = $('#votebox #voteform').find('input').serialize() + '&send=Abstimmen';

		$.post('/voting/vote_frontend.php', postData, function(data, textStatus) {
			if(textStatus == 'success') {
				updateHtml = data;
				setTimeout('showVoteBox()', 1000);
			} else {
				$('#votebox').html('Es gab einen Verbindungsfehler');
			}
		}, 'html');
		return false;
	});
}

function showVoteBox() {
	$('#votebox').html(updateHtml);
	mapFormEvent();
}

// vote archive
function loadPagination(){
	$('#paginationLinks a').click(pager);
}

var page;
function pager() {
	var element = $(this);
	page = parseInt(element.html()) - 1;
	$('#paginationLinks a').removeClass('selected');
	element.addClass('selected');

	$('#voteArchive').html('');
	$('#ajaxIndicator2').show();
	setTimeout('loadPage()', 1000);
}

function loadPage() {
	$('#voteArchive').load('/voting/vote_frontend.php?pageNo='+page, function() {
		$('#ajaxIndicator2').hide();
	});
}

$(function() {
	// scroll pane
	$('.scroll-pane').jScrollPane();
	$('.scroll-pane-arrows').jScrollPane(
		{
			showArrows: true,
			horizontalGutter: 10,
			horizontalDragMinWidth: 60,
			horizontalDragMaxWidth: 60

		}
	);
	
	
	// homepage slidery	
	$('#slider-home .slider-navi li').mouseover( function() {
		var index = $(this).prevAll().length;
		showSliderItem(index, true);
		clearInterval(intervalSlider);
	});
	$('#slider-home .slider-navi li').mouseout( function() {
		makeSliderInterval();
	});
	makeSliderInterval();	
	
});
var activeSlider = 0;
var intervalSlider;

function makeSliderInterval() {
	intervalSlider = setInterval("automateSliderItem()", 7000);
}

function showSliderItem(i, manual) {
	if(activeSlider != i) {
		var duration = manual ? 500 : 2000;
		$('#slider-home #slider-content .item:visible').fadeOut(duration);
		$('#slider-home #slider-content .item:eq('+i+')').fadeIn(duration);
		activeSlider = i;
		$('#slider-home .slider-navi li.active').removeClass('active');
		$('#slider-home .slider-navi li:eq('+i+')').addClass('active');
	}
}

function automateSliderItem() {
	var show = activeSlider + 1;
	show = show % $('#slider-home #slider-content .item').length;
	showSliderItem(show, false);
}

function CheckStr(userInput, maxlength, lenCounter){
		StrLen = userInput.value.length;
		if (StrLen > maxlength ){
		userInput.value = userInput.value.substring(0,maxlength);
		StrLeft = 0;
		}else{
		StrLeft = maxlength - StrLen;
		}
		lenCounter = '#' + lenCounter;		
		$(lenCounter).val(StrLeft);
		
}


