var pictureChanger_position;
var pictureChanger_id;
var pictureChanger_nChildren;
var pictureChanger_fadeTime = 1000;

$(document).ready(function() {
	$('.contentBox').each(function() {
		$(this).append('<div class="cornerTopLeft"></div><div class="cornerTopRight"></div><div class="cornerBottomLeft"></div><div class="cornerBottomRight"></div>');
	});
	
	$('#header, .contentBox:not(:has(#menu))').pngFix();
	
	$('.mediumBeschriftung').each(function() {
		$(this).parent().prev('a').attr('title', $(this).text());
	});
	
	// FancyBox starten
	$("div.text a[rel='lytebox[group]']").fancybox();
	$("a[rel='fancybox']").fancybox();
	
	$('ul.navigation li:not(:has(a.aktiv))').hover(
		function() {
			$(this).children('.x1').css('background', 'url(bilder/menuLeftActive.png) no-repeat');
			$(this).children('.x2').css('background', 'url(bilder/menuRightActive.png) no-repeat');
			$(this).children('a').css('background', 'url(bilder/menuMidActive.png) repeat-x');
			$(this).toggleClass('active');
			$(this).pngFix();
		},
		function() {
			$(this).children('.x1').css('background', 'url(bilder/menuLeft.png) no-repeat');
			$(this).children('.x2').css('background', 'url(bilder/menuRight.png) no-repeat');
			$(this).children('a').css('background', 'url(bilder/menuMid.png) repeat-x');
			$(this).toggleClass('active');
			$(this).pngFix();
		}
	);
	
	var workErrors = function(info) {
		//console.log ('showError');
		var errors = info.errors;
		var posx = 0;
		$.each(errors, function(name, id) {
			//console.log('name: ' + name + ' info: ' + id + ' type ' + id.length);
			if (id.length) {
				$('label[for=' + name + ']').before("<div class='message'>" + info.errorMessages[name][id] + "</div>").parent().addClass('error');
				if (!posx) { var offset = $('label[for=' + name + ']').parents('.type-text').offset(); posx = offset.top; }
			}
		});
		window.scroll(0,(posx - 50));
		alert('Es sind noch fehlerhafte Eingaben vorhanden!');
	};
	
	var removeErrors = function() {
		//console.log('remove');
		$('.type-text').removeClass('error').find('div.message').remove();
	};
	
	$('#umfrage').submit(function() {
		if ($('#umfrage #ident').val() == '') {
			$('#umfrage #ident').val('test');
			$('#umfrage').ajaxSubmit({dataType: 'json', iframe: false, beforeSubmit:  function() {
			},  
		        success:       function(data) {
				removeErrors();
				if (data === true) {
					$('#umfrage #ident').val('success');
					$('#umfrage').submit();
				} else {
					if (data == "doubleuser") {
						alert('Sie haben bereits Abgestimmt!');
					} else if (data == "timeout") {
						alert('Von Ihrem Rechner wurde bereits abgestimmt. Bitte versuchen Sie es später noch einmal.');
					} else {
						workErrors(data);						
					}
					$('#umfrage #ident').val('');
				}
			//console.log('wrong');
			return false;
			}});
		}
		//console.log('right');
		if ($('#umfrage #ident').val() == 'success') return true;
		else return false;
	});
	
	$('.button').parent().css({float: 'left', padding: '5px 12px', background: '#DE7F25', borderRadius: '6px', color: 'white'}).hover(function() {
		$(this).css('textDecoration', 'none');
	}).parent().css('overflow', 'hidden');
	
	
	
});

$(document).ready(function(){
	$.fn.pictureChanger = function(speed)
	{
		pictureChanger_position = 0;
		pictureChanger_id = '#' + this.attr('id');
		pictureChanger_nChildren = $(pictureChanger_id).children('div').length;
		
		if (pictureChanger_nChildren > 1) {
			$(document).everyTime(speed, function() {
				if(pictureChanger_position >= pictureChanger_nChildren - 1) pictureChanger_position = pictureChanger_nChildren - 1;
				$(pictureChanger_id + ' > div:eq(' + pictureChanger_position + ')').fadeOut(pictureChanger_fadeTime);
				pictureChanger_position++;
				if(pictureChanger_position > pictureChanger_nChildren - 1) pictureChanger_position = 0;
				elm = $(pictureChanger_id + ' > div:eq(' + pictureChanger_position + ')');
				elm.fadeIn(pictureChanger_fadeTime);
			});
		}
	}
	$('#pictureChanger').pictureChanger(5000);
});
