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


			function enableHomeTabs() {
				tabs = $('.tabBox h6');
				tabs.click(switchTab);
				var activeTab = 0;
				interval = setInterval(function() {
					var elem = tabs.get(activeTab);
					switchTab(null, elem);
					activeTab = (activeTab + 1) % 3;
				}, 5000);
			}

			function switchTab(event, elem) {
				var elem = event ? $(this) : $(elem);
				var show = elem.attr('show');
				$('.tabBox h6.active').removeClass('active')
				$('.tabBox div.teaser').hide();
				var show = $('#'+show);
				show.show();
				elem.addClass('active');

				if(event) {
					clearInterval(interval);
				}
			}




// general
$(document).ready(function() {

	if(window.Node && Node.prototype && !Node.prototype.contains) {
		Node.prototype.contains = function (arg) {
			return !!(this.compareDocumentPosition(arg) & 16);
		};
	}
	window.onload = function() {
		var layer = document.getElementById('mediacentrumPopup');
		function hideLayer(e) {
			e = e || window.event;
			var toElement = e.relatedTarget || e.toElement || false;
			// falscher mouseout
			if(this.contains(toElement) || toElement == this) return;
			layer.style.display = 'none';
		}
		function showLayer() {
			layer.style.display = 'block';
			layer.onmouseout = hideLayer;
			return false;
		}
		document.getElementById('mediacentrumLink').onclick = showLayer;
	}

	var textarea = $('#seminar textarea[name=aufmerksam_sonstig]');
	textarea.hide().parent().prev().hide();
	$('#seminar select[name=aufmerksam]').change(function() {
		var option = $(this).find('option:selected').val();
		if(option == 1 || option == 2 || option == 7)
			textarea.show().parent().prev().show();
		else
			textarea.hide().parent().prev().hide();
	});

	$(function(){
	$('checkbox#nachrichten').click(function(){
			if ($this.attr('checked')=='checked'){
				$('[rel=nachrichten]').attr('checked','checked');
			}else {
				$('[rel=nachrichten]').removeAttr('checked');
			}
		})
});


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

    $('#automobile.themenblock div.dashCheck:first').click(function() {
    	 $(this).checkboxToggle();
    	 if ($(this).hasClass('checked')){
    	 	$('#automobile.themenblock div.dashCheck:gt(0)').checkboxCheck();
    	 }else{
    	 	$('#automobile.themenblock div.dashCheck:gt(0)').checkboxUncheck();
    	 }
    });


    $('#automobile.themenblock div.dashCheck:gt(0)').click(function() {
    	 $(this).checkboxToggle();
    	 if (!$(this).hasClass('checked')){
    	 	$('#automobile.themenblock div.dashCheck:first').checkboxUncheck();
    	 }
    });

    $('#automobile.themenblock label:first').click(function() {
    	 $(this).labelToggle();
    	 if ($(this).prev().hasClass('checked')){
    	 	$('#automobile.themenblock div.dashCheck:gt(0)').checkboxCheck();
    	 }else{
    	 	$('#automobile.themenblock div.dashCheck:gt(0)').checkboxUncheck();
    	 }
    });

    $('#automobile.themenblock label:gt(0)').click(function() {
    	$(this).labelToggle();
    	if (!$(this).prev().hasClass('checked')){
    	 	$('#automobile.themenblock div.dashCheck:first').checkboxUncheck();
    	 }
    });

    $('#career.themenblock div.dashCheck:first').click(function() {
    	 $(this).checkboxToggle();
    	 if ($(this).hasClass('checked')){
    	 	$('#career.themenblock div.dashCheck:gt(0)').checkboxCheck();
    	 }else{
    	 	$('#career.themenblock div.dashCheck:gt(0)').checkboxUncheck();
    	 }
    });


    $('#career.themenblock div.dashCheck:gt(0)').click(function() {
    	 $(this).checkboxToggle();
    	 if (!$(this).hasClass('checked')){
    	 	$('#career.themenblock div.dashCheck:first').checkboxUncheck();
    	 }
    });

    $('#career.themenblock label:first').click(function() {
    	 $(this).labelToggle();
    	 if ($(this).prev().hasClass('checked')){
    	 	$('#career.themenblock div.dashCheck:gt(0)').checkboxCheck();
    	 }else{
    	 	$('#career.themenblock div.dashCheck:gt(0)').checkboxUncheck();
    	 }
    });

    $('#career.themenblock label:gt(0)').click(function() {
    	$(this).labelToggle();
    	if (!$(this).prev().hasClass('checked')){
    	 	$('#career.themenblock div.dashCheck:first').checkboxUncheck();
    	 }
    });

    $('#free.themenblock div.dashCheck').click(function() {
    	 $(this).checkboxToggle();
    });

    $('#free.themenblock label').click(function() {
    	$(this).prev().checkboxToggle();
    });
});



// tabs
function makeTabBox(boxClass) {
	$('#'+boxClass+'reiter li a').click(function() {
		var elem = $(this);
		var template = elem.attr('template');
		var parameter = boxClass == 'stellenmarkt' ? 'page' : 'template';
		$('#'+boxClass+'box').load('/sixcms/list.php?'+parameter+'='+template);
		$('#'+boxClass+'reiter li a').removeClass('activetab');
		elem.addClass('activetab');
		return false;
	});
};


// poster with icons
function makePoster() {
	var oldActive = 0;
	$('#posterBox .numbers li').click(function() {
		var elem = $(this);
		var className = elem.attr('class');
		var number = parseInt(className.substr(className.length-1, 1));
		$('#posterBox .numbers li').removeClass('sprite-red'+oldActive);
		elem.addClass('sprite-red'+number);
		var layers = $('#posterBox .posterLayer');
		layers.fadeOut();
		layers.eq(number-1).fadeIn();
		oldActive = number;
	});
}


// slide box
var slideMove, autoDirection = -1, sliderInterval;
function makeSlider() {
	var elements = $('#previewContainer div');
	slideMove = elements.eq(0).outerWidth();
	$('#previewContainer').width(slideMove * elements.size());

	sliderInterval = setInterval(moveAutomatic, 3000);

	$('#sliderPrev').click(moveSliderBackwards);
	$('#sliderNext').click(moveSliderForwards);
}

function moveSlider(direction) {
	var container = $('#previewContainer');
	var left = parseInt(container.css('left'));
	var maxLeft = container.width() - slideMove * 3;

	if((Math.abs(left) < slideMove && direction == 1) || (Math.abs(left) >= maxLeft && direction == -1)) {
		autoDirection *= -1;
		return;
	}
	container.animate({
		'left': (left + slideMove * direction) + 'px'
	});
}

function moveSliderBackwards(){
	clearInterval(sliderInterval);
	moveSlider(1);
}

function moveSliderForwards(){
	clearInterval(sliderInterval);
	moveSlider(-1);
}

function moveAutomatic() {
	moveSlider(autoDirection);
}



