var filenames = Array();
var confirmation;

function fileSize(bytes){
	var size = "";
	if (bytes === 0) { 
		size = "-";
	}else { 
		if (bytes > 1024) { 
			size = Math.round(bytes/1024) + " KB";
		}else { 
			if (bytes > 1024*1024) { 
				size = Math.round(bytes / ((1024*1024)*100)/100) + " MB";
			}else { 
				if (bytes > 1024*1024*1024) {
						size = Math.round(bytes / ((1024*1024)*100)/100) + " GB";			
				} else {
					size = bytes + " bytes";	
				} 
			}
		}
	}
return size;
}

function refreshPage(){
	window.location.reload(true);	
}

function deleteItem(class_, table, itemtodelete){
	$(".delete_"+class_).click(function(e){
		var deleteID=$(this).attr('id').substr(4,10);
		if(confirm("Are you sure you want to delete this "+itemtodelete)){
			$("#"+class_+"_box_"+deleteID).fadeOut();
			$.post("/scripts/delete_item.php", {id: deleteID, table: table}, function(data){
				//alert(data);
				if(data=="refresh"){
					refreshPage();
				}
			});
		}
	});
}
function buildLoading(){
	$("#fileprogress").html("");
	var contains = "";
	for(var i=0;i<filenames.length;i++){
		contains = $("#fileprogress").html();
		$("#fileprogress").html(contains+filenames[i][0]+": "+filenames[i][1]+"<br/>");	
	}
}
function updateNote(class_,status){
	$(class_).click(function(e){
		var id=$(this).attr('id').substr(4,10);
		//alert("status: "+status+" id:"+id);
		$.post("/scripts/update_note.php", {id: id, status: status}, function(data){
			refreshPage();
		});
	});
}

function sendMessage(msg){
	$.post("/scripts/add_msg.php", "msg="+msg, function(data){
//				alert(data);
refreshPage();
	});
}

//function_call,vars,file_path,selectedFileArray[i].name,selectedFileArray[i].size,editedDate

function addPhoto(filename){
	$.post("/scripts/add_photo.php", "filename="+filename, function(data){
		//after photo is uploaded it calls this page, this is where next upload needs to be called
		$('#flash_upload').calledFromFlash("hi");
	});
}

function addMultiPhoto(filename,vars){
	$.post("/scripts/add_multi_photo.php", "filename="+filename, function(data){
		//after photo is uploaded it calls this page, this is where next upload needs to be called
		//$('#flash_upload').calledFromFlash("hi");
		if(vars=='true'){
			refreshPage();
		}
	});
}

function loadingFile(percent,filename){
	var found = -1;
	for(var i=0;i<filenames.length;i++){
		if(filenames[i][0]==filename){
			found = i;
		}
	}
	if(found==-1){
		filenames.push([filename,percent]);	
	}else{
		filenames[found][1]=percent;	
	}
	buildLoading();
}
	
function addFile(filenum,vars){
	alert('filename');
	//if(vars=='true'){
		//refreshPage();
	//}
	/*
	$("#filelist").show();
	var linkTo = root+path+"/"+filename;
	var ext = filename.split(".");
	ext = ext[ext.length-1];
	size = fileSize(filesize);
	$("#filelist").html($("#filelist").html()+"<div class='file_full' id='image_"+filenum+"' title='"+linkTo+"'><a class='file_link' href='"+linkTo+"'><div class='filename'><img src='"+root+"includes/admin/icons/"+ext+".png' /> "+filename+" </div><div class='filesize'> "+size+" </div><div class='lastmodified'></div></a><div class='clear'></div></div>");
	*/
}

function confirmation(){
	confirmation=confirm("I confirm that all information I entered into this form is entirely correct and understand that after pressing OK I won't be able to come back and make any changes.");
	if (confirmation){
		return true;
	} else {
		return false ;
	}									 
}

//__________________________________________________________________________________________________________________________________________________________
//																																			JQUERY FUNCTIONS

$(document).ready(function() {	

	$.ajaxSetup({cache: false}); 

    $(".flat").blur(function () {
		$(this).removeClass('flat_focus');
		$(this).addClass('flat_unfocus');
    });
	
	$(".flat").focus(function () {					  
		$(this).removeClass('flat_unfocus');
		$(this).addClass('flat_focus');
    });		
	
	$(".display_img").click(function () {				
		var displayID=$(this).attr('id').substr(4,10);
		$.post("/scripts/display_image.php", {id: displayID}, function(data){
			//alert(data);
		});
    });
	
	$(".cms_icons").hide();
	$("table tr").mouseleave(function(){
		$(".cms_icons").hide();
	});
	$("table tr").mouseenter(function(){
		$(".cms_icons").hide();
		var showbox=("#"+$(this).attr('id'));
		$(showbox+" .cms_icons").show();
	});
	$(".clear_on_focus").focus(function() {
        if (this.value == "Enter Text") {
            this.value = "";
        }
    }).blur(function() {
		if (this.value != "Enter Text"){
			this.value = "Enter Text";
		}
	});
	
//__________________________________________________________________________________________________________________________________________________________
//																																				AVAILABILITY
	var availability = ["m","t","w","h","f","s","u"];
	for(var i=0;i<availability.length;i++){
		var letter = availability[i];
		var turnto = true;
		$("#"+letter+"a").attr('letter',letter);
		$("#"+letter+"a").click(function(){
			if($(this).is(":checked")){
				turnto = true;
			}else{
				turnto = false;
			}
			for(var j=1;j<13;j++){	
				$("#"+$(this).attr('letter')+j).attr('checked',turnto);
			}
		});
		for(var j=1;j<13;j++){
			$("#"+letter+j).attr('letter',letter);		
			$("#"+letter+j).click(function(){
				if(!$(this).is(":checked")){
					$("#"+$(this).attr('letter')+"a").attr('checked',false);
				}
				onCount = 0;
				for(var k=1;k<13;k++){
					if($("#"+$(this).attr('letter')+k).is(":checked")){
						onCount++;
					}
				}
				if(onCount==12){
					$("#"+$(this).attr('letter')+"a").attr('checked',true);	
				}
			});
		}
	}

//__________________________________________________________________________________________________________________________________________________________
//																																			  EVENT CALENDAR	
	$(".eventmonthselect").click(function(){
		window.location = $(this).attr('value');
	});
	
	$(".eventCol").mouseenter(function(){
		$(".eventCol").removeClass("eventhover");
		$(this).addClass("eventhover");
	}).mouseleave(function(){
		$(this).removeClass("eventhover");
	});

//__________________________________________________________________________________________________________________________________________________________
//																																				 SMS MESSAGE
	var maxChars = 160;
	$("#message_input").keyup(function(e) {
		if(this.value.length>maxChars-1){
			this.value = this.value.substr(0,160);	
		}
		$("#message_size").html(maxChars-this.value.length);
	});

//__________________________________________________________________________________________________________________________________________________________
//																																		 FLASH UPLOAD BUTTON
	$("#flash_upload_button").mouseenter(function(e){
		$(".file_upload_link").removeClass("link_normal");
		$(".file_upload_link").addClass("link_hover");
    }).mouseleave(function(e){
		$(".file_upload_link").addClass("link_normal");
		$(".file_upload_link").removeClass("link_hover");
    });

//__________________________________________________________________________________________________________________________________________________________
//																																				ACCREDITATION
	$(".quizradio").click(function(){
		var creditid = $('#quiz').attr('name').substr(3,10);
		var id=$(this).attr('id').substr(3,10);	
		$("#answer_"+id).css("display","block");
		if($("#answer_"+id+" img").hasClass("incorrect")){
			// alert('That answer is incorrect.\nPlease try again.');
		}else{
			var showid=$("#answer_"+id).attr('name').substr(3,10);
			var totalq=$("#answer_"+id+" img").attr('name').substr(3,10);
			$("#quizquestion_"+showid).removeClass('hidden');
			if(parseInt(showid,10)==(parseInt(totalq,10)+1)){
				$.post("/scripts/quiz_completed.php", "creditid="+creditid, function(data){
					//alert(data);									   
				});
			}			
		}
	});
	
	$(".creditacheived").click(function(){
										
		var creditid=$(this).attr('id').substr(3,10);	
		$.post("/scripts/quiz_completed.php", "creditid="+creditid, function(data){
//			alert(data);
			if(confirm("Please confirm you have acheived this qualification")){
				refreshPage();
			}
		});
										
	});

//__________________________________________________________________________________________________________________________________________________________
//																																					CONTACTS	
	$(".add_msg").click(function(e){
		var id=this.id.substr(4,10);
		$.post("/scripts/add_contact.php", {id: id}, function(data){
			alert("Details has been added to your contacts module.");
		});
	});

//__________________________________________________________________________________________________________________________________________________________
//																																				FILE MANAGER
	
	if(Number($(".file_full").size())===0){
		$("#filelist").hide();
	}
	var removedFiles = $(".file_full").size();
	$(".delete_image").click(function(){
		var vars = this.id.split("%?%");
		//alert(vars[0] + "     " + vars[1] + "     " +vars[2] + "     " +vars[3]+ "     ");
		msg = "Are you sure you want to delete the file '"+vars[2]+"'?";
		if(vars[3]=="Folder"){
		msg = "Are you sure you want to delete folder '"+vars[2]+"' and it's contents?";
		}
		if(confirm(msg)){
			$("#"+vars[0]).fadeOut();
			$.post("/scripts/delete_file.php", "path=/"+vars[1]+"/&file="+vars[2]+"&ext="+vars[3],function(data){
				//alert(data);
				removedFiles--;
				if(removedFiles===0){
					refreshPage();
				}														
			});
		}
	});
	
	$(".renamefile").click(function(){
		var vars = this.id.split("%?%");
		var name = prompt("Rename file or folder",vars[2]);
		if(name){
			names = name.split(" ");
			name = "";
			for(var i=0;i<names.length;i++){
				name +=	names[i];
				if(i<names.length-1){
					name += "-";
				}
			}
			$.post("/scripts/rename_file.php", "path="+vars[1]+"&oldname="+vars[2]+"&newname="+name,function(data){
				 //alert(data);	
				 refreshPage();
			});
		}
	});
	
	$(".newfolder").click(function(){	
		var path=$(this).attr('id').substr(4,30)+"/";
		var name = prompt("Please select a name for your new folder ","New-Folder");
		if(name){
			$.post("/scripts/create_folder.php", "path="+path+"&name="+name,function(data){
				 //alert(data);	
				 refreshPage();
			});	
		}
	});
	
//__________________________________________________________________________________________________________________________________________________________
//																																				NOTIFICATIONS	
	$(".replynote").click(function(){
		var noteid=$(this).attr('id').substr(4,10);
		var msg = prompt("Reply to notification","");
		if(msg){
			$.post("/scripts/send_note.php", "noteid="+noteid+"&msg="+msg,function(data){
				 //alert(data);	
				 refreshPage();
			});
		}
	});

	$(".addnewnote").click(function(){
		var msg = prompt("Add item to your notification list","");
		if(msg){
			$.post("/scripts/add_note.php", "admin=no&msg="+msg,function(data){
				 //alert(data);	
				 refreshPage();
			});
		}
	});
	
	$(".addadminnote").click(function(){
		var msg = prompt("Send notification to admin","");
		if(msg){
			$.post("/scripts/add_note.php", "admin=yes&msg="+msg,function(data){
				 //alert(data);	
				 refreshPage();
			});
		}
	});	


//__________________________________________________________________________________________________________________________________________________________
//																																				PHOTO GALLERY
	$(".delete_photo").click(function(){
		var vars = this.id.split("%?%");
		if(confirm("Are you sure you want to delete "+vars[2]+"?")){
			$("#"+vars[0]).fadeOut();
			$.post("/scripts/delete_photo.php", "path="+vars[1]+"&image="+vars[2],function(data){
				alert("removed "+vars[2]);
			});
		}
	});	
//__________________________________________________________________________________________________________________________________________________________
//																																		   DATABASE DELETION

	deleteItem("svc","pages","Page?");
	deleteItem("acc","accreditations","Accreditation?");	
	deleteItem("cnt","contacts","Person?");
	deleteItem("img","gallery","Image?");	
	deleteItem("tes","testimonials","Testimonial?");
	deleteItem("not","notifications","Notification?");
	deleteItem("msg","messages","Enquiry?");	
	deleteItem("tme","timesheets","Timesheet?");
	deleteItem("qiz","quiz","Quiz Question?");
	deleteItem("bkk","bookings","Booking? \nNOTE: this can not be reversed.");
	
	updateNote(".flag_note",1);//pinned
	//updateNote(".flag_note",2);//overdue
	//updateNote(".flag_note",3);//gettingclose
	updateNote(".unflag_note",4);//standard
	updateNote(".complete_note",5);//completed
	
	
	
	$(".delete_table").click(function(e){
		var row=$(this).parent().parent().parent().attr('id').split("_");
		var id=row[2];
		var table=row[1];
		if(confirm("Are you sure you want to delete this row?")){
			$("#table_"+table+"_"+id).fadeOut();
			$.post("/scripts/delete_item.php", {id: id, table: table}, function(data){
				//alert(data);
				if(data=="refresh"){
					refreshPage();
				}
			});
		}
	});
	
	
	
});
