$(document).ready(function() {
	
	// sidebar login 
	$("#sidebar-username").css({
		color: "#666"
	}).focus(function() {
		sidebar_username_value = $(this).val();
		$(this).val('').css({color: "#000"});
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).val(sidebar_username_value).css({color: "#666"});
		}
	});

	$("#sidebar-password").css({
		color: "#aaa"
	}).focus(function() {
		sidebar_password_value = $(this).val();
		$(this).val('').css({color: "#000"});
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).val(sidebar_password_value).css({color: "#666"});
		}
	});
	
	$(".newsheader").next(".newstext").hide();
	$(".newsheader_odd").next(".newstext").hide();
	
	$(".newsheader_first .newstoggle").toggle(
		function() {
			$(this).parent().next(".newstext").hide();
			$(this).parent().css("height", "38px");
			$(this).attr("src", "/gfx/news_aufklappen.png");
		},
		function() {
			$(this).parent().next(".newstext").show();
			$(this).parent().css("height", "52px");
			$(this).attr("src", "/gfx/news_zuklappen.png");
		}
		
	);
	
	$(".newsheader .newstoggle").toggle(
		function() {
			$(this).parent().next(".newstext").show();
			$(this).parent().css("height", "32px");
			$(this).parent().css("background-image", "url(/gfx/news_schmaler_bg_1.jpg)");
			$(this).attr("src", "/gfx/news_zuklappen.png");
		},
		function() {
			$(this).parent().next(".newstext").hide();
			$(this).parent().css("height", "18px");
			$(this).parent().css("background-image", "url(/gfx/news_schmaler_bg_1_zu.jpg)");
			$(this).attr("src", "/gfx/news_aufklappen.png");
		}
	);
	
	$(".newsheader_odd .newstoggle").toggle(
		function() {
			$(this).parent().next(".newstext").show();
			$(this).parent().css("height", "32px");
			$(this).parent().css("background-image", "url(/gfx/news_schmaler_bg_2.jpg)");
			$(this).attr("src", "/gfx/news_zuklappen.png");
		},
		function() {
			$(this).parent().next(".newstext").hide();
			$(this).parent().css("height", "18px");
			$(this).parent().css("background-image", "url(/gfx/news_schmaler_bg_2_zu.jpg)");
			$(this).attr("src", "/gfx/news_aufklappen.png");
		}
	);
	
	// featured
	var $featured_area = $('#featured');
	var $featured_item = $('#featured .featitems .featitem');
	var $slider_control = $('#featured .featitems');
	var $image_container = $('#featured .featimg');
	var ordernum;
	var pause_scroll = false;
			
	$image_container.css("background-color","#000000");
		


	$featured_area.mouseover(function(){
		pause_scroll = true;
	}).mouseout(function(){
		pause_scroll = false;
	});

	function gonext(this_element){
		$slider_control.find(".featitem.active").removeClass('active');
		this_element.addClass('active');
		ordernum = this_element.find("span.order").html();
		$('.featimg').cycle(ordernum - 1);
	} 
		
	$featured_item.mouseover(function() {
		gonext($(this)); 		
	})
		
	var auto_number;
	var interval;
	
	$featured_item.bind('autonext', function autonext(){
		if (!(pause_scroll)) gonext($(this)); 
		return false;
	});
	
	interval = setInterval(function () {
			auto_number = $slider_control.find("div.featitem.active span.order").html();
			if (auto_number == $featured_item.length) auto_number = 0;
			$featured_item.eq(auto_number).trigger('autonext');
		}, 4000);
	
	$('.featimg').cycle({
		timeout: 0,
		speed: 400,
		fx: 'fade'
	});
	
	
	var headerbilder = [
			'/gfx/glaeser_ani_1.jpg',
			'/gfx/glaeser_ani_2.jpg',
			'/gfx/glaeser_ani_1.jpg',
			'/gfx/glaeser_ani_3.jpg',
			'/gfx/glaeser_ani_1.jpg',
			'/gfx/glaeser_ani_4.jpg',
			'/gfx/glaeser_ani_1.jpg',
			'/gfx/glaeser_ani_5.jpg'
		];

	headerbilder.i = 0;


	function headershop() {
		$('#headershop').fadeTo(300,0.2,function() {
			$('#headershop').attr('src', headerbilder[headerbilder.i]);
		}).fadeTo(400,1);

		headerbilder.i++;
		if(headerbilder.i >= headerbilder.length)
			headerbilder.i = 0;
	}

	var t = setInterval(headershop, 700);
	
	
	Shadowbox.init({
		animate: 1,
		animateFade: 1,
		animSequence: 'sync',
		autoplayMovies: 1,
		continuous: 0,
		counterLimit: 10,
		counterType: 'default',
		displayCounter: 1,
		displayNav: 1,
		enableKeys: 1,
		fadeDuration: 0.35,
		flashParams: {bgcolor:"#000000",allowfullscreen:"true"},
		flashVars: {},
		flashVersion: '9.0.0',
		handleOversize: 'drag',
		handleUnsupported: 'link',
		initialHeight: 160,
		initialWidth: 320,
		modal: 0,
		onChange: function() {},
		onClose: function() {},
		onFinish: function() {},
		onOpen: function() {},
		overlayColor: '#000000',
		overlayOpacity: 0.5,
		resizeDuration: 0.4,
		showOverlay: 1,
		showMovieControls: 1,
		skipSetup: 0,
		slideshowDelay: 0,
		viewportPadding: 20,
		preserveAspectWhileResizing: 0
	});
	
	
	$(".newslist li.link").click(function() {
		var url = $(this).find(".title a").attr("href");
		if (url != undefined) {
			window.location.href = url;
		}
	});

	$("body").append('<div id="newsdim"></div>');
	
	$(".hide").hide();
	
	$(".toggleCommentFormBody").click(function() {
		if ($("#commentform").is(":visible")) {
			$("#commentform").slideUp();
		} else {
			$("#commentform").slideDown();
			$(this).slideUp();
		}
		return false;
	}); 
	
	$(".reward-link").live('click', function() {
		$(".reward").hide();
		$(".critique").hide();
		$(this).parent().parent().find(".reward").fadeIn();
		return false;
	}); 

	$(".critique-link").live('click', function() {
		$(".reward").hide();
		$(".critique").hide();
		$(this).parent().parent().find(".critique").fadeIn();
		return false;
	}); 
	
	$("form.rating-form").submit(function() {
		var error = false;
		$(this).find(".required").each(function(i) {
			if ($(this).val() == '') {				
				error = true;
			}
		});

		if (error == true) {
			alert("Bitten die erforderlichen Felder ausfüllen!"); 
			return false;
		} else {
			return true;
		}
		
	});
	
	$(".rating-star").mouseover(function() {
		var parts = $(this).attr("rel").split(","); 
		var rating = parts[0];
		var id = parts[1]; 
		var stardir = parts[2]; 
		if ($("#rating_set_"+id).val() == "1") {
			return;
		}
		for(i = 1; i <= rating; i++) {
			$('#rating_' + id + '_' + i).attr('src', '/gfx/ratings/'+stardir+'/rating_on.png');
		}
	}).mouseout(function() {
		var parts = $(this).attr("rel").split(","); 
		var id = parts[1]; 
		var stardir = parts[2]; 
		var curr = parts[3]; 
		if ($("#rating_set_"+id).val() == "1") {
			return;
		}
		for(i = 1; i <= 6; i++) {
			if (i <= curr) {
				$('#rating_' + id + '_' + i).attr('src', '/gfx/ratings/'+stardir+'/rating_on.png');
			} else {
				$('#rating_' + id + '_' + i).attr('src', '/gfx/ratings/'+stardir+'/rating_off.png');
			}
		}
	}).click(function() {
		var parts = $(this).attr("rel").split(","); 
		var rating = parts[0];
		var id = parts[1]; 
		var stardir = parts[2]; 
		if ($("#rating_set_"+id).val() == "1") {
			return;
		}
		$(this).parent().find(".critique-link").fadeOut();
		$(".reward").hide();
		$(".critique").hide();
		$(this).parent().parent().find(".reward").fadeIn();
		for(i = 1; i <= rating; i++) {
			$('#rating_' + id + '_' + i).attr('src', '/gfx/ratings/'+stardir+'/rating_on.png');
		}
		$("#rating_set_"+id).val("1"); 
		$.ajax({
			url: '/API/addRating/',
			data: { rating: rating, contact_id: id },
			dataType: 'json',
			type: "POST", 
			success: function(data) {
				if (data.success == true) {
					$("#rating_id_"+id).val(data.rating_id); 
				}
			}
			
		})
	}); 
	
	
	
	$(".poem-rating-star").mouseover(function() {
		var parts = $(this).attr("rel").split(","); 
		var rating = parts[0];
		var id = parts[1]; 
		var stardir = parts[2]; 
		if ($("#rating_set_"+id).val() == "1") {
			return;
		}
		for(i = 1; i <= rating; i++) {
			$('#rating_' + id + '_' + i).attr('src', '/gfx/ratings/'+stardir+'/rating_on.png');
		}
	}).mouseout(function() {
		var parts = $(this).attr("rel").split(","); 
		var id = parts[1]; 
		var stardir = parts[2]; 
		var curr = parts[3]; 
		if ($("#rating_set_"+id).val() == "1") {
			return;
		}
		for(i = 1; i <= 6; i++) {
			if (i <= curr) {
				$('#rating_' + id + '_' + i).attr('src', '/gfx/ratings/'+stardir+'/rating_on.png');
			} else {
				$('#rating_' + id + '_' + i).attr('src', '/gfx/ratings/'+stardir+'/rating_off.png');
			}
		}
	}).click(function() {
		var parts = $(this).attr("rel").split(","); 
		var rating = parts[0];
		var id = parts[1]; 
		var stardir = parts[2]; 
		if ($("#rating_set_"+id).val() == "1") {
			return;
		}
		for(i = 1; i <= rating; i++) {
			$('#rating_' + id + '_' + i).attr('src', '/gfx/ratings/'+stardir+'/rating_on.png');
		}
		$("#rating_set_"+id).val("1"); 
		$.ajax({
			url: '/API/addPoemRating/',
			data: { rating: rating, item_id: id },
			dataType: 'json',
			type: "POST", 
			success: function(data) {
				if (data.success == true) {
					// $("#rating_id_"+id).val(data.rating_id); 
				}
			}
			
		})
	}); 
});



