
$(document).ready(function () {	
	//alert('js');
	
	//--- Accordeon ---// 
	if( $(".accordeon").length > 0 ){Accordeon.init();}
	if( $(".press_accordeon").length > 0 ){Pressaccordeon.init();}
	//--- Voting ---// 
	if( $("#form_voting").length > 0 ){Voting.init();}
		
	//--- LIGHTBOX ---// 
	if($(".lightbox").length > 0) {$("a.lightbox").lightbox();} 
			
	//---EXTERNAL---//	
	$("a").each(function(){
	 	if(
                  $(this).attr("href") &&
	 	  $(this).attr("href") != "" &&	
		  $(this).attr("href").indexOf(window.location.hostname) < 0 &&
		  $(this).attr("href").indexOf("http://") >= 0 	
		  ||  $(this).attr("rel") == "external"  
	 	 ){
	 		$(this).attr("target", "_blank");
	 	 }
	 });		 
	 //---STARTSCREEN---//
	 if( $("#intro").length > 0 ){
		Intro.init();
	}	
	 

});

Intro = {
	init: function(){
		 var dimension= Util.getWindowSize();		
		var page_width= dimension[0];
		var page_height= dimension[1];		
		var intro_height = $("#intro").height();
		var intro_width = $("#intro").width();		
		
		$("#intro").css("top", ((page_height - intro_height)/2)+'px' ).css("left", ((page_width - intro_width)/2)+'px');

		 window.setTimeout(function() {
			$('#intro').fadeOut(500, function(){
				window.location ="/de/vineus/zum_voting";
			});
		 }, 3000);

	},
	setIntro: function(){
 		var dimension= Util.getWindowSize();
		
		var page_width= dimension[0];
		var page_height= dimension[1];
		
		var intro_height = $("#intro").height();
		var intro_width = $("#intro").width();		
		$("#intro").css("top", ((page_height - intro_height)/2)+'px' ).css("left", ((page_width - intro_width)/2)+'px');
	
	}
}

Voting = {
	init: function(){
		$("#form_voting .inactiveCategory").hide();
		$("#votingOverview").hide();
		$("#contactForm").hide();
		Voting.addListener();		
	},
	showNextButton: function(){
		$(".activeCategory .voting_error").hide();
		$(".activeCategory .btn_voting_next").show();
	},
	showNextCategory: function(id){
		var old_id = $(".activeCategory").attr('id');
		$("#"+old_id).hide().removeClass('activeCategory');
		$("#"+id).removeClass('inactiveCategory').addClass('activeCategory').show();
		
		//alert(".overview_spaceholder li."+id);
		$("#votingOverview li."+id).addClass('active visited'),
		$("#votingOverview li."+old_id).removeClass('active');
		Voting.addListener();
				
	},
	showContactForm: function(){
		$("#votingForm").hide();
		$("#contactForm").show();
	},
	addListener: function(){
		$(".activeCategory .overview_spaceholder").append($("#votingOverview").html());
		$(".activeCategory input").click(function(){	
			if($("#form_voting.expert").length > 0){
				var pid = "#"+$(this).parent().parent().attr("id");
				if( $(pid+" input:checked").length > 1 ){
					alert("ACHTUNG! Sie haben für einen Kandidaten mehrfach gestimmt. Dies ist ungültig. Bitte einen anderen Kandidaten wählen.");
				}else{
					if( $(".activeCategory input:checked").length == 3 ){
						if( $(".activeCategory .btn_voting_next").is(':hidden') === true ){
							Voting.showNextButton();
						}				
					}					
				}				
			}else{
				if( $(".activeCategory .btn_voting_next").is(':hidden') === true ){
						Voting.showNextButton();
				}
			}
									
		});
	}
}


//--------------------------
//----------       Accordeon
//--------------------------
Accordeon= {
	init: function() {	
		var teaserlength=320;	
		$(".accordeon").each(function(i){
				var teasertext = $(this).children('.acc_content').text().substring(0, teaserlength);	
				if( teasertext.length >= teaserlength  ){
					$(this).children('.acc_teaser').children('.paragraph').html( teasertext+'...' );	
				}else{
					$(this).removeClass('accordeon');
					$(this).children('.acc_teaser').hide();
					$(this).children('.acc_content').show();
					$(this).children('.acc_content').children('.acc_btn_close').hide();					
				}
						
			}
		);		
		
		$(".accordeon").click(function(){					 
				if( $(this).hasClass('active_accordeon') ){							
					var textheight = $(this).children('.acc_teaser').height();							
					$(this).children('.acc_content').fadeOut(500);		
					$(this).animate({
						height: textheight + 'px'
						 }, 500, 'swing', function() {
							$(this).children('.acc_teaser').fadeIn(500);
							$(this).css('height', 'auto').removeClass('active_accordeon');						
		
				    });										 
						
				 }else{		
 						var textheight = $(this).children('.acc_content').css('display','block').height();	
 						$(this).children('.acc_content').css('display','none');				
						$(this).children('.acc_teaser').fadeOut(500);		
						$(this).animate({
								height: textheight + 'px'
								 }, 500, 'swing', function() {
								 		$(this).css('height', 'auto').addClass('active_accordeon');							
										$(this).children('.acc_content').fadeIn(500);
												
						 });
						
						
					
			 	 }
		});
		
	
	}
}
Pressaccordeon= {
	init: function() {	
		$(".press_accordeon").each(function(i){
				$(this).css('height', $(this).children('.acc_head').height()+'px');					
			}
		);				
		$(".press_accordeon").click(function(){					 
				if( $(this).hasClass('active_accordeon') ){															 
						var textheight = $(this).children('.acc_head').height();	
						$(this).animate({
								height: textheight + 'px'
								 }, 500, 'swing', function() {					
									$(this).removeClass('active_accordeon');													
						 });
				 }else{				 		
 						var textheight = $(this).children('.acc_content').height()+$(this).children('.acc_head').height();	
						$(this).animate({
								height: textheight + 'px'
								 }, 500, 'swing', function() {
								 		$(this).css('height', 'auto').addClass('active_accordeon');							
																						
						 });
			 	 }
		});
		
	
	}
}

Util= {
	getWindowSize: function(){
	
		var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight) {
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else if(document.body) { 
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;
		if (self.innerHeight) {
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth;
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) {
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		if(yScroll < windowHeight) {
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}

		if(xScroll < windowWidth) {
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}
		var arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
		
	},  validateForm: function(id) {
		var result= true;				
		$("#"+id + " input, #"+id + " textarea, #"+id + " input.checkbox, #"+id + " select").each(function() {
			if($(this).attr("class") && $(this).attr("class").match("required")) {
			    $(this).val($(this).val().replace(/^\s+|\s+$/g, ''));
				var itemresult= Util.validate($(this).attr("id"));
				result= result && itemresult;
			}
		});		
		if(!result){
			$('#error .message').css('visibility', 'visible');
		}	
		else{
			if(confirm('Das Formular absenden?'))
				result = result && true;
			else
				result = result && false;			
		}
		return result;			
	},
	validate: function(id) {
		var result= true;
		var obj= $("#"+id);		
		var commands= $("#"+id).attr("class").split(" ");
		for(var i=0; i<commands.length; i++) {
			var command= commands[i].replace(/[0-9]/g, "");			
			switch(command) {
				case("minlength"):
					var length= parseInt(commands[i].replace(/minlength/g, ""));
					result= result && (obj.val().length>=length);
					break;
				case("maxlength"):
					var length= parseInt(commands[i]);
					result= result && (obj.val().length<=length);
					break;
				case("email"):
					result = result && ( obj.get(0).value.match(/\S@\S.\S{2,}/)!=null );
					break;
				case("checkbox"):
					if( $("#"+id+":checked").length <= 0 ){
						result = result && false;
						obj.parent().children("label").addClass("error");
					}else{
						obj.parent().children("label").removeClass("error");
					}			
					break;
				case("select"):
					result = result && !( $("#"+id).val() == '---' );					
					break;
				default:
					result= result && (obj.val().length!=0);
					break;
			}
		}
						
		if(!result) {
			obj.addClass("error");			
		} else {
			obj.removeClass("error");
		}
		return result;
	}
}

DynamicForm = {
	accinit: function(){
		$("#dynamicform fieldset.accordion").css('height', $("fieldset.accordion .form_legend").height()+'px');
		
		$("#dynamicform fieldset.accordion .form_legend").click(function(){	
			height=0;
			$(this).parent().children('.row_box').each(function(){
				//height += parseInt($(this).height())+parseInt($(this).css('margin-bottom'));
			});
			if( $(this).parent().hasClass('active') ){				
				$(this).parent().css('height',  $(this).height()+'px');				
				$(this).parent().removeClass('active');		
			}else{
				$(this).parent().css('height', 'auto');
				$(this).parent().addClass('active');
			}		
		});				
	}
}





