 $(document).ready(function() {
// Dashboard
    $.fn.checkboxToggle = function checkboxToggle(){
    	var elem = $(this).prev();
        elem.next().toggleClass('checked');
        elem.attr('checked', !elem.attr('checked'));
    }   
    
    $.fn.labelToggle = function labelToggle(){
    	var elem = $(this).prev().prev();
        elem.next().toggleClass('checked');
        oldHack = elem.attr('checked');
        globalHack = elem.attr;
        setTimeout("globalHack('checked', !oldHack);", 50);
    }
    
    $.fn.checkboxUncheck = function checkboxUncheck(){
    	var elem = $(this).prev();
        elem.next().removeClass('checked');
        elem.attr('checked', false);
    }    
    
    $.fn.checkboxCheck = function checkboxCheck(){
       	var elem = $(this).prev();
        elem.next().addClass('checked');
        elem.attr('checked', true);
    }
    
    var switcherText = 'schließen';
    $('#themenauswahl').hide();
    
    $('#themenswitcher').click((function() {
    	$('#themenauswahl').slideToggle();
    	var oldSwitcherText = $(this).html();
    	$(this).html(switcherText);
    	switcherText = oldSwitcherText;
    }));
    $('.themenblock input').hide().after('<div class="dashCheck"></div>');
    $('.themenblock input:checked').next().addClass('checked')
    
    
    
    
    
    $('#news.themenblock div.dashCheck:first').click(function() {
    	 $(this).checkboxToggle();    	 
    	 if ($(this).hasClass('checked')){
    	 	$('#news.themenblock div.dashCheck:gt(0)').checkboxCheck();
    	 }else{
    	 	$('#news.themenblock div.dashCheck:gt(0)').checkboxUncheck();
    	 }
    });
    
    
    $('#news.themenblock div.dashCheck:gt(0)').click(function() {
    	 $(this).checkboxToggle();    	 
    	 if (!$(this).hasClass('checked')){
    	 	$('#news.themenblock div.dashCheck:first').checkboxUncheck();
    	 }
    });
    
    $('#news.themenblock label:first').click(function() {
    	 $(this).labelToggle(); 
    	 if ($(this).prev().hasClass('checked')){
    	 	$('#news.themenblock div.dashCheck:gt(0)').checkboxCheck();
    	 }else{
    	 	$('#news.themenblock div.dashCheck:gt(0)').checkboxUncheck();
    	 }     
    });
    
    $('#news.themenblock label:gt(0)').click(function() {
    	$(this).labelToggle();
    	if (!$(this).prev().hasClass('checked')){
    	 	$('#news.themenblock div.dashCheck:first').checkboxUncheck();
    	 }      
    });
    
    
    
    $('#free.themenblock div.dashCheck').click(function() {
    	 $(this).checkboxToggle(); 
    });
    
    $('#free.themenblock label').click(function() {
    	$(this).labelToggle();    	 
    });   
    
    $('#free2.themenblock div.dashCheck').click(function() {
    	 $(this).checkboxToggle(); 
    });
    
    $('#free2.themenblock label').click(function() {
    	$(this).labelToggle();  
    }); 
});

// 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() {
		$('#submitBtn').parent().hide()
		$('#ajaxIndicator').css('display', 'block');
		var postData = $('#submitBtn').parent().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');
	});
}

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();
	});
}




    

