// 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);
	});
			
	$("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 ifChanged(prevValue,id,updateDiv){
	
	if($(id).val() !== prevValue)
		showUpdate('update' + updateDiv,updateDiv,'words');
}

function showUpdate(id,formId){
	$('div#' + id).html('<input type="button" value="Update" onclick="saveContent(\'' + formId + '\');" />');
	$('div#' + id).show();
}

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);
		}
		
}

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 setViewCart(){
		
	$.post('/admin/ajax/ajax.php', {a: 'setViewCart'},
		   function(data){
			   //alert(data);
		   });
	
	return false;
}


function addGuestEmail(){
//alert($('#email').val());
	if(checkEmail($("#email").val())){
	
		$.post('/admin/ajax/ajax.php', {email: $("#email").val(), a : "addGuestEmail"},
		function(data){
			
			if(data == '<p>You have already downloaded "Fruen"</p>'){
					$('#download-container').html(data);
			}
			else{
				$('#download-container').html('<p><a href="include/redirect.php?download=1&redirectUrl=http://www.sver.biz/vip/Sver - Fruen.zip" target="_blank">Download as WAV</a><br /><a href="include/redirect.php?download=1&redirectUrl=http://www.sver.biz/vip/Sver - Fruen mp3.zip" target="_blank">Download as MP3</a></p>');
				$('#download-container').prepend(data);
			}
		});	
	}
	else{
		$('#download-container').append('<p id="errorText" class="miniTextGrey">Not a valid email.</p>');
		$("#email").val('');
		setTimeout('$("#errorText").fadeOut(1000); $("#errorText").remove();',2000);
	}
}

function addDownloadToGuest(){
	//alert('hey!');
	
	$.post('/admin/ajax/ajax.php', {a: 'addDownloadToGuest'},
	   function(data){
		   alert(data);
	});
}
