$(function() {

	if(typeof console === "undefined") {
		console = { log: function() {}};
	}
	
	var $ = jQuery;
	
	var ZWC	=	{
	
		forms : function(){
		
			$("form a.submit, form a.login, p.submit a").live("click", function(){
				if (!$(this).hasClass("show-message"))
				{
					$(this).closest("form").submit();
					return false;				
				}
			});

			$('input').keydown(function(e){
				if (e.keyCode == 13) {
					$(this).parents('form').submit();
					return false;
				}
			});

			$("#forum_form").submit(function(){
				console.log("here");
				if($("select#categories option:selected").val() == ""){
					$('#forum-message').slideDown();
					$('body').addClass('message');
					$.scrollTo(0, 400);
					return false;
				}
			});

			if ($('.datepicker').length > 0)
			{
				$('.datepicker').datepicker({ dateFormat: 'yy.mm.dd' });
			}

		},
	
		messages : function(){
			$('a#login-link, #error-message a.login').live('click',function(){
				$('body').addClass('message');
				$('#login-message').slideDown();
				$.scrollTo(0, 400);
				return false;
			});
			$('a.show-message').live('click',function(){
				$('body').addClass('message');
				$('#login-message').slideUp();
				$('#error-message').slideDown();
				$.scrollTo(0, 400);
			});
			$('#error-message a.close').live('click',function(){
				$('#error-message').slideUp();
				$('body').removeClass('message');
				return false;
			});
			$('.inner a.close').live('click',function(){
				$('#login-message').slideUp();
				$('#error-message').slideUp();
				$('#veranstaltung-message').slideUp();
				$('body').removeClass('message');
				return false;
			});		
		},
		
		hovers : function(){
			$('#primary h1 a.pointer').hover(function() {
				$('#primary h1').addClass('over');
			}, function() {
				$('#primary h1').removeClass('over');
			});
			$('#main-slider a.pointer').hover(function() {
				$(this).parent().parent().addClass('over');
			}, function() {
				$(this).parent().parent().removeClass('over');
			});
			$('#members-slider a.pointer').hover(function() {
				$(this).parent().parent().addClass('over');
			}, function() {
				$(this).parent().parent().removeClass('over');
			});
			$('#accordion a.pointer').hover(function() {
				$(this).parent().parent().addClass('over');
			}, function() {
				$(this).parent().parent().removeClass('over');
			});		
			$('#member-box').hover(function() {
				$(this).addClass('over');
			}, function() {
				$(this).removeClass('over');
			});
		},
		
		typography : function(){
			$("#article #images + p:eq(0)").each(function() {
				var text = $.trim($(this).text());
				var first = $('<span>'+text.charAt(0)+'</span>').addClass('dropcap');
				$(this).html(text.substring(1)).prepend(first);
			});		
		},
		
		comments : function(){
			$('p.add-comment a').live('click',function(){
				$(this).parent().next('form').slideDown();
				return false;
			});
			$('#comments h2.hide').live('click',function(){
				$('#comments-content').slideUp();
				$(this).removeClass('hide');
				$(this).addClass('show');
				return false;
			});
			$('#comments h2.show').live('click',function(){
				$('#comments-content').slideDown();
				$(this).removeClass('show');
				$(this).addClass('hide');
				return false;
			});
			$('.post a.hide').live('click',function(){
					$(this).parent().parent().next('.replies-content').slideUp();
					$(this).removeClass('hide');
					$(this).addClass('show');
				return false;
			});
			$('.post a.show').live('click',function(){
				$(this).parent().parent().next('.replies-content').slideDown();
				$(this).removeClass('show');
				$(this).addClass('hide');
				return false;
			});		
		},
		
		paginate : function(){
			$(".ajax-pagination a").live("click", function(e){
				e.preventDefault();
				
				var link = $(this);

				$.ajax({
					type: "GET",
					url: $(this).attr("href"),
					data: '',
					success: function(data){
					
						var data = $(data);
					
						var direction = "";
						
						// Set Pagination
						$(".ajax-pagination").replaceWith($(".ajax-pagination", data));
						
						$(".ajax-pagination", data).remove();
						
						// Determine current position for non previous / next links
						if (!$(this).hasClass("prev") && !$(this).hasClass("next"))
						{
							var current = parseFloat($(link).closest("div").find("strong").text());
							var next	= parseFloat($(link).text());
							
							if (current > next)
							{
								direction = "prev";
							}
						}				

						// Animate new items
						if ($(link).hasClass("prev") || direction == "prev")
						{
							$(data).appendTo(".slideshow").css({"top" : "0", "left" : "-980px"});

							var first_item = $(".items").first();
							
							$(".items").animate({
								"left" : '+=980'
							}, 1000, function(){
								$(first_item).remove();
							});						
						}else{
							$(data).appendTo(".slideshow").css({"top" : "0", "left" : "980px"});

							var first_item = $(".items").first();
							
							$(".items").animate({
								"left" : '-=980'
							}, 1000, function(){
								$(first_item).remove();
							});
						}
										
					}
				});

			});
		},
		
		init : function(){
			ZWC.messages();
			ZWC.hovers();
			ZWC.typography();
			ZWC.comments();
			ZWC.forms();
			ZWC.paginate();
			
			if ($("#accordion").length > 0)
			{
				$("#accordion").accordion({autoHeight:true});
			}
		}
	
	};
	
	ZWC.init();
	

});
