var policyagree;
var newsletterabo;
$().ready( function() {
	if($("#content_mediasection").length||$("#takeaction").length){
		$(".addreply").click( function() {
			reload_captcha();
			$('#comment_reply_thankyou_with_nl').hide();
			$('#comment_reply_thankyou_without_nl').hide();
			$("#comment_send_form").show();
			$("#send_author_subject").val("Re: " + $(this).parent().parent().parent().find(".commentHeadline").text());
			$('#reply_commentID').val($(this).parent().parent().parent().find("a.commentId").attr("name"));
			$('#quoted div').text($(this).parent().parent().parent().find(".commentText").text());
			$('#mediasection_form_comments').show();
			$('#send_author_subject').focus();
			$('.nreply').show();
			$('.quoted').show();
			$('.ncom').hide();
		});
	
		$("#quoted").click( function() {
			$('#send_author_text').val("RE: " + $('#quoted div').text());
			return false;
		});
	
		$(".showreplies").click( function() {
			$('#replies' + $(this).parent().parent().parent().find("a.commentId").attr("id")).show();
			return false;
		});
	
		$(".addcomment").click( function() {
			reload_captcha();
			$('#comment_reply_thankyou_with_nl').hide();
			$('#comment_reply_thankyou_without_nl').hide();
			$("#comment_send_form").show();
			$('.quoted').hide();
			$("#send_author_subject").val("");
			$('#send_author_text').val("");
			$('#reply_commentID').val("");
			$('.nreply').hide();
			$('.ncom').show();
			$('#mediasection_form_comments').show();
			$('#send_author_subject').focus();
		});
	
		$(".seeallcomments,#showmorecomments,#seeallcomments").click( function() {
			$("#mediasection_short_comments").slideUp(500, function() {
				$("#mediasection_all_comments").slideDown(500, function() {
				});
			});
			return false;
		});
		$(".captcha").click( function() {
			reload_captcha();
			urchinTracker('/nopi/comments/captcha_failed/');
		});
		$("#comment_send_form").submit( function() {
			if ($("#policy_agree").attr("checked") == true) {
				var policyagree = 1;
			}
			if ($("#newsletter_abo").attr("checked") == true) {
				var newsletterabo = 1;
			}
	
			if ($("#reply_commentID").val() != "") {
				$.post("/en/comments/validate/index.html", {
					commentID :$('#reply_commentID').val(),
				    newsletter_abo :newsletterabo,
				    pageID :$("#inPageId").attr("rel"),
				    policy_agree :policyagree,
				    send_author_agree :$('.send_author_agree[checked]').val(),
				    send_author_email :$("#send_author_email").val(),
				    send_author_encodedurl :document.URL,
				    send_author_firstname :$("#send_author_firstname").val(),
				    send_author_lastname :$("#send_author_lastname").val(),
				    send_author_salutation :$("#send_author_salutation").val(),
				    send_author_subject :$("#send_author_subject").val(),
				    send_author_text :$("#send_author_text").val(),
				    send_author_verify :$("#send_author_verify").val(),
				    send_reply_x :'1'
				}, function(data) {
					if (data == 'reply_ok_with_nl') {
						$('#comment_send_form').hide();
						$('#comment_reply_thankyou_with_nl').show();
						reload_captcha();
					} else if (data == 'reply_ok_without_nl') {
						$('#comment_send_form').hide();
						$('#comment_reply_thankyou_without_nl').show();
						reload_captcha();
					} else {
						$("#comment_error").html(data);
						reload_captcha();
					}
				});
			} else {
				$.post("/en/comments/validate/index.html", {
				    send_author_firstname :$("#send_author_firstname").val(),
				    send_author_salutation :$("#send_author_salutation").val(),
				    send_author_verify :$("#send_author_verify").val(),
				    send_author_lastname :$("#send_author_lastname").val(),
				    send_author_subject :$("#send_author_subject").val(),
				    send_author_email :$("#send_author_email").val(),
				    send_author_text :$("#send_author_text").val(),
				    send_author_encodedurl :document.URL,
				    pageID :$("#inPageId").attr("rel"),
				    newsletter_abo :newsletterabo,
				    sent_x :"1",
				    policy_agree :policyagree,
				    send_author_agree :$('.send_author_agree[checked]').val()
				}, function(data) {
					if (data == 'ok_with_nl') {
						$('#comment_send_form').hide();
						$('#comment_thankyou_with_nl').show();
						reload_captcha();
					} else if (data == 'ok_without_nl') {
						$('#comment_send_form').hide();
						$('#comment_thankyou_without_nl').show();
						reload_captcha();
					} else {
						$("#comment_error").html(data);
						reload_captcha();
					}
				});
			}
			return false;
		});
	}
	
	// Copenhagen Special Menu
	if ($("#copMenu").length) {
		
		// handle layer menue
		$("#copAllTopics").click( function() {
			$("#copNextTopicContent").hide();
			$("#copAllTopicsContent").show();
		});
		$("#copNextTopic").click( function() {
			$("#copAllTopicsContent").hide();
			$("#copNextTopicContent").show();
		});
		

	}

});

// Post Comment Request

var http_request = false;

function reload_captcha() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 18;
	var randomstring = '';
	for ( var i = 0; i < string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum, rnum + 1);
	}
	var randomstring2 = '';
	for ( var i = 0; i < string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring2 += chars.substring(rnum, rnum + 1);
	}
	$(".captcha").attr('src', '/images/captcha/captcha.gif?randomstring2=' + randomstring);
}

// Add quote to reply form
function quote() {
	$('#reply_send_author_text').val($('#reply_original').val());
}

function set_salutation(value) {
	document.getElementById("send_author_salutation").value = value;
}

// Teaser Scroller & Funktionen
function FadeEmAll(me, activelist, gis) {
	$(".graphics").hide();
	$(".gallerypage").hide();
	$(".animationpage").hide();
	$(".flashpage").hide();
	$(".knowledgekits").hide();
	if (me == "all_media") {
		$(".graphics").show();
		$(".gallerypage").show();
		$(".animationpage").show();
		$(".flashpage").show();
		$(".knowledgekits").show();
	} else {
		if (me == 'graphics') {
			$(".graphics").show();
			$(".animationpage").show();
		} else {
			$("." + me).show();
		}
	}
	// addClass('visible')
	$(".video").removeClass('active');
	$(".allmedia").removeClass('active');
	$(".gallery").removeClass('active');
	$(".grafix").removeClass('active');
	$("." + activelist).addClass('active');
	$('#' + gis + ' .scroll-pane').jScrollPane();
}

function fadeIt(ting) {
	$(".teaser_box").fadeIn("slow");
}

function getPain() {
	$('.scroll-pane').jScrollPane();
}

function changeTab(hoi) {
	$("#info_box > div:visible").fadeOut("slow", function() {
		$("#" + hoi).fadeIn("slow");
	});
	$("#info_navi li").removeClass('active');
	$("#navi_" + hoi).addClass('active');
	setCookie('openNavi', hoi);
	setCookie('URL', window.location.pathname);
}

function setCookie(name, value) {
	document.cookie = name + "=" + value + "; path=/"
}

function setLBCookie(name, value){
	
	var endDate = new Date();
	var end = endDate.getTime() + (200 * 24 * 60 * 60 * 1000);
	endDate.setTime(end);

	document.cookie = name + "=" + escape(value) + "; expires=" + endDate.toGMTString();
}

function getCookie(Name) {
	// construct RE to search for target name/value pair
	var re = new RegExp(Name + "=[^;]*", "i");
	if (document.cookie.match(re)) {
		return document.cookie.match(re)[0].split("=")[1];
	}
	return null;
}

function direct(url) {
	window.location.href = url;
}

function checkPersist() {
	if (getCookie('openNavi') != "undefined") {
		if (getCookie('URL') == window.location.pathname) {
			return getCookie('openNavi');
		}
	}
}

function closeCopLayer() {
	$("#copAllTopicsContent").hide();
	$("#copNextTopicContent").hide();
}

// Infoboxen anzeigen
function hideLayers() {
	$('#info').hide();
	$('#share').hide();
	$('#rate').hide();
	$('#comments').hide();
	$('#related').hide();
	$("#info_navi li").removeClass("active");
}

// Info
function info() {
	hideLayers();
	$('div#info').show();
	$("#navi_info").addClass("active");
}

// Share
function share() {
	hideLayers();
	$('div#share').show();
	$("#navi_share").addClass("active");
}

// Rate
function rate() {
	hideLayers();
	$('div#rate').show();
	$("#navi_rate").addClass("active");
}

// Comments
function comments() {
	hideLayers();
	$('div#comments').show();
	$("#navi_comments").addClass("active");
}

// Related Article
function related() {
	hideLayers();
	$('div#related').show();
	$("#navi_related").addClass("active");
}