
// EDIT BLOB

function openBlob(id,table){
	$("div#editBlob" + id).draggable();
	$("div#editBlob" + id).disableSelection();
	
	$.post('/admin/finder/editBlobAjax/ajax.php', {a: 'loadBlobHtml',finderId:id,tableName:table},
		function(data){
			$("div#editBlob" + id).html(data);
	});
	
	// Display loading message
		/*	$("div#messageBoard").ajaxSend(function() {
				$("div#messageBoard").show();
				$("div#messageBoard").html('Working...');
			});
			
			// Loading Blob with html
			$("div#messageBoard").ajaxSuccess(function() {
				$("div#messageBoard").show();
				$("div#messageBoard").html('Done');
				$("div#messageBoard").fadeOut(3000);
			});*/
			
	$("div#editBlob" + id).show();
}

function loadContent(type,id){
	
	var save = $("div#editBlob" + id +" #submit2").html();
	
	// Loading with ajax
	$.post('/admin/finder/editBlobAjax/ajax.php', {a:type,finderId:id},
		function(data){
		$("div#editBlob" + id + " #blobContent").html(data);
	});
	
	// Display loading message
	$("div#editBlob" + id +" #submit2").ajaxSend(function() {
		$("div#editBlob" + id +" #submit2").html('Working...');
	});
	
	
	// Show loading DONE message
	$("div#editBlob" + id +" #submit2").ajaxSuccess(function() {	
		$("div#editBlob" + id +" #submit2").html('Changes Saved');
		$("div#editBlob" + id +" #submit2").html('<input type="button" id="blobUpdate" class="updateText" value="Save Changes" onclick="saveContent(\'finderInfo' + id + '\');" />');
	});
	
	//isLoadingContent(id);
	
	
}

function saveContent(tableId){
	
	$.post('/admin/edit.php', $("#" + tableId).serialize(),
		function(data){
		alert(data);
	});
}

function isLoadingContent(id){
	$("div#editBlob" + id + " #blobContent div").hide();
	$("div#editBlob" + id +" #loadingContent").show();
}
function isNotLoadingContent(id){
	$("div#editBlob" + id + " div#loadingContent").hide();
}

function closeBlob(id){
	$("div#editBlob" + id).hide();
}

//////////////////////////////////////////////////////////////////////////////////////////

function deleteFromTracks(member,id){
	
		var newMembers = '';

	var members = $('#tracks' + id).val();
	members = members.split(',');
	
	
	for(var i=0; i < members.length; i++){
		if(!(members[i] == member)){
			newMembers += members[i] + ',';
		}
	}
	
	// REMOVE LAST ,
	newMembers = newMembers.substring(0,newMembers.length - 1);
	
	if(newMembers != '')
		$('#tracks' + id).val(newMembers);
	else
		$('#tracks' + id).val(' ');
	
	
	member = member.replace(/:{2}/g,'\\:\\:');
	$('li#' + member).remove();
}

function deleteFromMembers(member,id){
	var newMembers = '';

	var members = $('#bandmembers' + id).val();
	members = members.split(',');
	
	
	for(var i=0; i < members.length; i++){
		if(!(members[i] == member)){
			newMembers += members[i] + ',';
		}
	}
	
	// REMOVE LAST ,
	newMembers = newMembers.substring(0,newMembers.length - 1);
	
	if(newMembers != '')
		$('#bandmembers' + id).val(newMembers);
	else
		$('#bandmembers' + id).val(' ');
	
	
	member = member.replace(/:{2}/g,'\\:\\:');
	$('li#' + member).remove();
}

function submitNewMember(id){		
		
		var bandmembers = document.getElementById('bandmembers' + id);
		var tracksUL = document.getElementById('sortable' + id);
		var noTracks = document.getElementById('noTracks' + id);
		
		var newTrack = document.getElementById('newTrackInput' + id);
		var newTrackNoMp3 = document.getElementById('newTrackNoMp3Input' + id);
		
	//	alert(newTrack.value + ' ' + newTrackNoMp3.value);
	
	if(newTrack.value != 'false' || newTrackNoMp3.value != ''){
			
			if(newTrack.value != 'false'){
				$.post('/admin/ajax/ajax.php', {a: 'addMember',parentId: id,finderId:newTrack.value},
					function(data){
					tracksUL.innerHTML += data;
				});
				if(bandmembers.value != '')
					bandmembers.value += ',' + newTrack.value;
				else
					bandmembers.value = newTrack.value;
			}
			else{
				if(bandmembers.value != '')
					bandmembers.value += ',' + '::' + newTrackNoMp3.value + '::';
				else
					bandmembers.value = '::' + newTrackNoMp3.value + '::';
				tracksUL.innerHTML += '<li class="trackListItem" id="' + '::' + newTrackNoMp3.value + '::' + '">' + newTrackNoMp3.value + '&nbsp;&nbsp;<a href="javascript:;" onclick="deleteFromMembers(\'' + '::' + newTrackNoMp3.value + '::' + '\',\'' + id + '\');"><img src="/img/delete.png" border="0" /></a></li>';
			}

		tracksUL.style.listStyleType = 'decimal';
		
		document.getElementById('newTrackInput' + id).selectedIndex = 0;
		newTrackNoMp3.value = '';
	}
}

function submitNewTrack(id){		
		
		var tracks = document.getElementById('tracks' + id);
		var tracksUL = document.getElementById('sortable' + id);
		var noTracks = document.getElementById('noTracks' + id);
		
		var newTrack = document.getElementById('newTrackInput' + id);
		var newTrackNoMp3 = document.getElementById('newTrackNoMp3Input' + id);
		
	//	alert(newTrack.value + ' ' + newTrackNoMp3.value);
	
	if(newTrack.value != 'false' || newTrackNoMp3.value != ''){
			if(newTrack.value != 'false'){
				$.post('/admin/ajax/ajax.php', {a: 'addTrack',parentId: id,finderId:newTrack.value},
					function(data){
					tracksUL.innerHTML += data;
				});
				tracks.value += ',' + newTrack.value;	
			}
			else{
				tracks.value += ',' + '::' + newTrackNoMp3.value + '::';
				tracksUL.innerHTML += '<li class="trackListItem" id="' + '::' + newTrackNoMp3.value + '::' + '">' + newTrackNoMp3.value + '&nbsp;&nbsp;<a href="javascript:;" onclick="deleteFromTracks(\'' + '::' + newTrackNoMp3.value + '::' + '\',\'' + id + '\');"><img src="/img/delete.png" border="0" /></a></li>';
			}
		
		
		tracksUL.style.listStyleType = 'decimal';
		
		document.getElementById('newTrackInput' + id).selectedIndex = 0;
		newTrackNoMp3.value = '';
	}
}

function getInfo(id){
	
	// Display loading message
		$("#info" + id).ajaxSend(function() {
			if($("#info" + id).html() == '')
				$("#info" + id).html('Working...');
		});
	
		$.get('/admin/ajax/getInfo.php', {liveId:id},
			function(info){
			$("#info" + id).html(nl2br(info,false));
		});
		
		
		
		replaceDiv('infoOn' + id,'infoOff' + id);
}

function addToMailinglist(){
	//alert($('#email').val());
	if(checkEmail($("#email").val())){
	
		$.post('/admin/ajax/ajax.php', {email: $("#email").val(), a : "addToMailinglist"},
				function(msg){
					$("#email").val('');
					alert(msg);
		});	
	}
	else{
		alert('Not a valid email.');
		$("#email").val('');
	}
}

function pagesOrder(){
	alert('hey');
	//alert($('#pagesOrder').val());
	
		$.post('/admin/ajax/ajax.php', {pagesOrder: $("input#pagesOrder").val(), a : "pagesOrder"},
				function(msg){
					$("input#save").hide();
					alert(msg);
		});
	
}

function moreBlogTitles(totalNumRows,m){

		$.post('/admin/ajax/ajax.php', {a: "moreBlogTitles", limitMax: $('#blogTitleLimitMax').val() * 1 + 5},
			function(info){
				//alert(info);
				if(totalNumRows < $('#blogTitleLimitMax').val()){
					var newM = $('#blogTitleLimitMax').val() * 1 + 5;
					$('#blogTitleLimitMax').val(newM);
					$("#blogTitles").html(info);
				}
				else{
					$("#blogTitles").html(info);
					$('#more').hide();
				}
			});		
}


/********************************/
// 			 SHOUTBOX			 //
/********************************/
function checkShoutbox(){

		$.post('/admin/ajax/ajax.php', {a: "checkShoutbox", },
			function(info){
				//alert(info);
					$("#shoutBox").prepend(info);
				
					//soundManager.play('pop');
				
			});		
}
function addToShoutbox(){
		
		if($("#shoutText").val() != ''){ // Checking that user has written somwthing
			if($("#shoutTitle").val() != ''){ // Checking that user has entered their name
				$("#addButton").html('Working...');
				$.post('/admin/ajax/ajax.php', $("#shoutbox").serialize(),
					function(info){
						//alert(info);
							$("#shoutBox").prepend(info);
							$("#shoutText").val('');
							$("#shoutTitle").val('');
							$("#addButton").html('<input type="button" value="Post" id="addButton" onclick="addToShoutbox();" style="display:block; margin-top:3px;" />')
					});
			}
			else{ // If user has't entered their name
			$("#addButton").prepend('<span id="errorText" style="font-size:9px;"><br />You need to write your name.</span>');
			setTimeout('$("#errorText").fadeOut(1000);',1000);
		}
		}
		else{ // If user has't written anything
			$("#addButton").prepend('<span id="errorText" style="font-size:9px;"><br />Please write something!</span>');
			setTimeout('$("#errorText").fadeOut(1000);',1000);
		}
		/*$("#addButton").ajaxSend(function(){
			$("#addButton").html('Working...');
		});
		
		$("#addButton").ajaxSuccess(function(){
			$("#addButton").html('<input type="button" value="Tyck Nåt!" id="addButton" onclick="addToShoutbox();" style="display:block; margin-top:3px;" />');
		});*/
}

function deleteFromShoutbox(id){
	$.post('/admin/ajax/ajax.php', {a: "deleteFromShoutbox", textId : id },
		function(error){
			if(error == '')
		    	alert(error);
			else
				$('#shoutBox div#shoutBoxItem' + id).remove();
		});
	
}

function working(){
	$("#olderLink").html('<div id="olderLink">Working...</div>');
}

function shoutboxGoOlder(limit,total){
	
	
	// Ta bort det som finns nu
		var olderLimit = limit + 10;
			
	// Hämtar äldre kommentarer
	
	
	$.post('/admin/ajax/ajax.php', {a: "goOlder", by : limit },
		function(info){
			//alert(info);
				$("#shoutBox").append(info);
				$("#shoutText").val('');
				$("#shoutTitle").val('');
				$("#addButton").html('<input type="button" value="Post" id="addButton" onclick="addToShoutbox();" style="display:block; margin-top:3px;" />')
				
				if(olderLimit >= total){
					$("#olderLink").remove();
				}
				else{
					$("#olderLink").html('<div id="olderLink"><a href="javascript:;" onmouseover="working();shoutboxGoOlder('+ olderLimit +','+ total +');" onclick="working(); shoutboxGoOlder('+ olderLimit +','+ total +');">Older comments...</a></div>');
				}
		});

		
}
