var noUpdate = false;

var persistmenu="yes" //"yes" or "no". Make sure each SPAN content contains an incrementing ID starting at 1 (id="sub1", id="sub2", etc)
var persisttype="sitewide" //enter "sitewide" for menu to persist across site, "local" for this page only

if (document.getElementById){ //DynamicDrive.com change
document.write('<style type="text/css">\n')
document.write('.submenu{display: none;}\n')
document.write('</style>\n')
}

function SwitchMenu(obj){
	if(document.getElementById){
	var el = document.getElementById(obj);
	var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
		if(el.style.display != "block"){ //DynamicDrive.com change
			for (var i=0; i<ar.length; i++){
				if (ar[i].className=="submenu") //DynamicDrive.com change
				ar[i].style.display = "none";
			}
			el.style.display = "block";
		}else{
			el.style.display = "none";
		}
	}
}

function get_cookie(Name) { 
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { 
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}


function savemenustate(){
var inc=1, blockid=""
while (document.getElementById("sub"+inc)){
if (document.getElementById("sub"+inc).style.display=="block"){
blockid="sub"+inc
break
}
inc++
}
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
document.cookie=cookiename+"="+cookievalue
}

if (persistmenu=="yes" && document.getElementById)
window.onunload=savemenustate

function links() {
	newWin = window.open('/us/us_link.php', 'newWin', 'status=0, menubar=0, height=400, width=400, scrollbars=yes');
	newWin.focus();
}

function showHidden(){
	$("#hiddenLinks").toggle();	
}

function add_url(){

	if($("#addicon").attr("src") == '/gfx/001_05.png'){
		$("#addicon").attr("src", "/gfx/001_01.png");
		$("#urlcontainer").slideUp();
		$("#linkStatus").val('http://');
	} else {
		
		$("#addicon").attr("src", "/gfx/001_05.png");
		$("#urlcontainer").slideDown();
	}
}

function openRadio(){
	newWin = window.open('/radio/radio.php', 'SMRadio', 'status=0, menubar=0, height=380, width=400, scrollbars=no');
	newWin.focus();
}

function showblog(id){
	$(".blogpost").hide();
	$("#blog_"+id).show();
}

function scrollNewsfeed(direction){
	if(noUpdate) {
		return;
	}
	$("#updating_newsfeed").show();
	var offset = $("#newsfeed_offset").val();
	offset = offset*1;
	var hfp = window.location.href.split('?');
	var params = hfp[1];
	params += '&salt='+Math.random();

	
	if(direction == 'previous'){
		var new_offset = offset+15;
	}
	if(direction == 'next'){
		var new_offset = offset-15;
		if(new_offset <0 ){
			new_offset = 0;
		}
	}
	
	if(direction == 'current'){
		var new_offset = offset;
	}
	
	$("#newsfeed_offset").val(new_offset);
	
			var posts = new Array();
			$.get('/us/us_newsfeed_ajax.php?'+params, 'offset='+new_offset, function(data, textStatus) {
				$(data).find('.newsfeed_item').each(function() {
					posts.push($(this).attr('id'));
					if($('#newsfeed_html #'+$(this).attr('id')).length == 0) {
						$('#newsfeed_html').prepend($(this));
					}
//					else {
//						$('#newsfeed_dump').html('').prepend($(this));
//						if($('#newsfeed_dump #'+$(this).attr('id')).html() != $('#newsfeed_html #'+$(this).attr('id')).html()) {
//							$('#newsfeed_html #'+$(this).attr('id')).replaceWith($(this));
//						}
//					}
				});
//					$("#newsfeed_html").html(msg);
				$('#newsfeed_html .newsfeed_item').each(function() {
					var found = false;
					for(var i = 0; i < posts.length; i++) {
						if(posts[i] == $(this).attr('id')) {
							found = true;
						}
					}
					if(!found) {
						$(this).remove();
					}
				});
				$("#updating_newsfeed").hide();
			
			}, 'html');

		
		

	
}


function writeComment(id){
	if($("writecomment"+id).style.display!='none') {
            Effect.SlideUp("writecomment"+id);
        } else {
            Effect.SlideDown("writecomment"+id);
    } 
}
function showComments(id){
	if($("status"+id).style.display!='none') {
            Effect.SlideUp("status"+id);
        } else {
            Effect.SlideDown("status"+id);
    } 
}
function admintools(){
	$("#admintoolsbox").toggle();
}

function popupImg(sImgName, iImgWidth, iImgHeight) {

	newWin=window.open('/jb/popup_image.php?img=' + sImgName + '&iw=' + iImgWidth + '&ih=' + iImgHeight + '&sw=' + screen.width + '&sh=' + screen.height, 'SMCIMAGE', 'toolbar=no,scrollbars=no');
}

function toggleStatus(){

	if($('#onlystatus').is(':checked')){
		$(".newsitem-notstatus").hide();
		$(".newsitem-extrastatus").show();
	} else {
		$(".newsitem-notstatus").show();
		$(".newsitem-extrastatus").hide();
	}
}

function likeNewsFeed(id){

		$.ajax({
   type: "GET",
   url: "/status/socket.php",
   data: "query=like&stream_id="+id,
   dataType: 'json',
   success: function(msg){
   	
   		$("#likelink_"+msg.id).html(msg.message);

   }
 });

}

function prepareStatus(){
	if($('#statusMsg').val() == 'Hvad laver du lige nu?'){
		$('#statusMsg').val('');
		$("#character-count").val('255 tegn tilbage');
		$("#status-tips").slideDown();
	}
}

function fixStatus(){
	if($('#statusMsg').val() == ''){
		$('#statusMsg').val('Hvad laver du lige nu?');
		$("#character-count").val('255 tegn tilbage');
		$("#status-tips").slideUp();
	}
}
function postStatus(){
	$('#sendbutton').attr('disable','disable');
		var message = $('#statusMsg').val();
var linkstatus = $('#linkStatus').val();
	$.ajax({
   type: "GET",
   url: "/status/socket.php",
   data: "query=update&linkstatus="+linkstatus+"&msg="+message,
   success: function(msg){
     $('#statusMsg').val('Hvad laver du lige nu?');
//     $("#statustable"). html(msg);
//     $("#statustable"). slideDown();
     		$("#addicon").attr("src", "/gfx/001_01.png");
		$("#urlcontainer").slideUp();
		$("#status-tips").slideUp();
		$("#linkStatus").val('http://');
		scrollNewsfeed('next');
   }
 });
	

}
function postStatusOnProfile(){
	var message;
	message = $('statusMsg').value;
	new Ajax.Request('/status/socket.php?query=update&msg='+message, {
			method: 'get', 
			onSuccess: function(transport) {
				$('statusMsg').value = 'Hvad laver du lige nu?';
				$('stmsg').innerHTML = message+' for 1 sekund siden';
				
			}
		});	
}
function remove_feed(id){
	
	var answer = confirm("Vil du slette denne opdatering?");
	
	if(answer){
		$("#newsfeed_item-"+id).fadeOut();
				$.ajax({
   type: "GET",
   url: "/status/socket.php",
   data: "query=remove&stream_id="+id,
   dataType: 'json',
   success: function(msg){

   }
 });
		
	}
}

function remove_comment(id){
	
	var answer = confirm("Vil du slette denne kommentar?");
	
	if(answer){
		$("#comment_"+id).fadeOut();
				$.ajax({
   type: "GET",
   url: "/status/socket.php",
   data: "query=remove_comment&comment_id="+id,
   dataType: 'json',
   success: function(msg){
   		
  

   }
 });
		
	}
}


function showCommentNewsfeed(id){
	
	if($("#comments_"+id).css("display") == 'block'){
		$("#comments_"+id).slideUp();
	} else {
		$("#comments_"+id).slideDown();
	}

}

function update_draft(){
	var id = $('#draft_select').val();
	
	if(id != 0){
	
	var where_to= confirm("Er du sikker på du vil indsætte skabelonen? Du overskriver eventuelt eksisterende indhold?");
 if (where_to== true)
 {
var message = $("#"+id+"_message").val();
		var subject = $("#"+id+"_subject").val();
		$('input[name="form_subject"]').val(subject);
		tinyMCE.get('form_message').setContent(message, {format : 'raw'});
 }
 
		
	} else {
		alert("Du skal vælge en skabelon før du kan indsætte den.");
	}
}

function delete_draft(){
	var id = $('#draft_select').val();
	
	if(id != 0){
	
	var where_to= confirm("Er du sikker på du vil slette skabelonen?");
 if (where_to== true){
	
	
	$.ajax({
		type: "POST",
		url: "/us/ajax_removedraft.php",
		data: "id="+id,
		success: function(msg){
			$("#draft_select option[value='"+id+"']").remove();
		}
	});

 }
 
		
	} else {
		alert("Du skal vælge en skabelon før du kan slette den.");
	}
}

function savedraft(){

	var message = tinyMCE.get('form_message').getContent();
	var subject = $('input[name="form_subject"]').val();
	var to_id = $('input[name="form_to"]').val();

	$("#btnsavedraft").val("Gemmer skabelon..");
	$.ajax({
		type: "POST",
		url: "/us/ajax_savedraft.php",
		data: "message="+message+"&subject="+subject+"&to_id="+to_id,
		success: function(msg){
			alert("Din skabelon er gemt. Du kan nu anvende den i andre breve.");
			$("#btnsavedraft").val("Gem som skabelon");
		}
	});
}
function toggle_wrapper(wrapper_class){
	$("."+wrapper_class).toggle();
}
function showadmins(){
	$(".adminlist").toggle();
}
function submitNewComment(){

	var message = $('#comment_text').val();
	var comment_id = $('#comment_id').val();
	
	$.ajax({
		type: "POST",
		url: "/us/ajax_submitcomment.php",
		data: "comment_id="+comment_id+"&message="+message,
		success: function(msg){
			var comment_id = $('#comment_id').val();
			$.ajax({
				type: "GET",
				url: "/us/ajax_getcomments.php",
				data: "id="+comment_id,
				success: function(msg){
					
					var comment_id = $('#comment_id').val();
					$("#comments_"+comment_id).html(msg);
					showCommentNewsfeed(comment_id);
				}
			});
			$('#writeComment').jqmHide();
		}
	});
}

function submit_message(){

	var message = $('#message').val();

	$.ajax({
   type: "POST",
   url: "/us/ajax_submitmessage.php",
   data: "message="+message,
   success: function(msg){
     $('#messages').html(msg);
     $('#message').val('Klik her for at skrive en besked..');
     $("#messsage_counter").html('255 tegn tilbage.');
   }
 });
	

}

function closeLikeWindow(){

	$('#wholikes').jqmHide(); 
}
function closeCommentWindow(){

	$('#writeComment').jqmHide(); 
}

function addCommentNewsfeed(id){
	
	if($('#newsfeed_item-'+id+' .newsfeed_content .newsfeed_time a:first').attr('href').replace('/us/us_show.php?id=','') == $('#writeComment').attr('data-whoami') || $('#writeComment').hasClass('ismember')) {
		$('#writeComment .ismember').show();
		$('#writeComment .notmember').hide();
	}
	else{
		$('#writeComment .ismember').hide();
		$('#writeComment .notmember').show();
	}
	$("#comment_id").val(id);
	$('#writeComment').jqmShow(); 
}

function showLikes(id){
	
		$.ajax({
   type: "GET",
   url: "/im/socket.php",
   data: "query=likes&id="+id,
   success: function(msg){
     $("#wholikes_content").html(msg);
   }
 });
	


	
	$('#wholikes').jqmShow(); 
}

$(document).ready(function(){

	if($('#writeComment').hasClass('ismember')) {
		$('#writeComment div.ismember').show();
		$('#writeComment div.notmember').hide();
	}
	else if($('#writeComment').hasClass('notmember')) {
		$('#writeComment div.ismember').hide();
		$('#writeComment div.notmember').show();
	}

/**
 * Character Counter for inputs and text areas
 */
	$('.word_count').keyup(function(){
		// get new length of characters
		var new_length = $('.word_count').val().length;
		var max_char = 255;
		
		var remaining_char = max_char-new_length;
		// get new length of words
		//var new_length = $(this).val().split(/\b[\s,\.-:;]*/).length;
		// update
		$("#messsage_counter").html( remaining_char + ' tegn tilbage.');
	});

	$('#statusMsg').keyup(function(){
		// get new length of characters
		var new_length = $('#statusMsg').val().length;
		var max_char = 255;
		
		var remaining_char = max_char-new_length;
		// get new length of words
		//var new_length = $(this).val().split(/\b[\s,\.-:;]*/).length;
		// update
		$("#character-count").html( remaining_char + ' tegn tilbage.');
	});

	$(".newphotoalbumswithtitle[title]").tooltip();


	setInterval( "scrollNewsfeed('current')", 420000 );

   
   $.PeriodicalUpdater({
      url : '/im/socket.php?query=status',
      minTimeout: 3000, 
      maxTimeout: 25000
   },
   
   
   function(data){

      $('#messages').html(data);
   });
  
	
$("img.newphotoalbums").hover(
function() {
$(this).stop().animate({"opacity": "1"},  "fast");
},
function() {
$(this).stop().animate({"opacity": "0.6"},  "fast");
});



 
 	$('#writeComment').jqm({
    overlay: 60, /* 0-100 (int) : 0 is off/transparent, 100 is opaque */
    overlayClass: 'blackOverlay'});
  
 	$('#wholikes').jqm({
    overlay: 60, /* 0-100 (int) : 0 is off/transparent, 100 is opaque */
    overlayClass: 'blackOverlay'});
  
 
   
   
   if (persistmenu=="yes"){
		var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
		var cookievalue=get_cookie(cookiename)
		if (cookievalue!="") document.getElementById(cookievalue).style.display="block"
	}
   
})
function chat(){
	var imwin = window.open('http://www.bdsmworld.org/chat2/?lang=da&user='+window.nick, 'login', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=750,height=650,left=380,top=120');
	imwin.focus();

}

