// JavaScript Document

function initSF(){
	$("ul.sf-menu").superfish(
								{
									pathClass : 'current',
									pathLevels : 3
								}
							  ); 
}


function load_section(section_name, id_rec){
	if(id_rec == undefined){
		id_rec = '';
	}else{
		id_rec = id_rec;
	}
	
	if(section_name == 'admin_log_in'){
		url = 'ajax/admin_log_in.php';
	}else if(section_name == 'administrators'){
		url = 'ajax/admin_admin.php';
	}
	
	$.get(
		  url,
		  {
			id: id_rec  
		  },
		  function success(data){
			  faceboxMsg(data);
		  }
	);
	
}

function section_action(section, form_id, action){
	 
	 if(section == 'administrators'){
			url = 'admin_admin.php'; 
	 }
	
	 $.ajax({
	   type: "POST",
	   url: 'ajax/' + url,
	   data: $('#' + form_id).formSerialize() + '&action_do=' + action,
	   cache: false,
	   dataType: 'json',
	   beforeSend : function(){
		   admin_loader(true);
	   },
	   complete : function(){
		   admin_loader(false);
	   },
	   success: function(msg){
			if(msg.success == false){
				alert(msg.msg);
			}else{
				admin_loader(false);
			}
	   }
 });
}

function admin_logIn(){
	 $.ajax({
	   type: "POST",
	   url: 'ajax/admin_log_in.php',
	   data: $('#log_admin').formSerialize() + '&action_do=log_in',
	   cache: false,
	   dataType: 'json',
	   beforeSend : function(){
		   admin_loader(true);
	   },
	   complete : function(){
		   admin_loader(false);
	   },
	   success: function(msg){
			if(msg.success == false){
				admin_loader(false);
				alert('Wrong username or password. Please try again.');
				window.location.href = 'index.php?log_in=true'
			}else{
				admin_loader(false);
				window.location.href = 'index.php'
			}
	   }
 });
}



function faceboxMsg(msg){
	setTimeout(function() { 
		$.fn.nyroModalManual({
				bgColor: '#999999',
				content: msg
			});
	}, 1000);
}

function admin_logOut(){
	 $.ajax({
	   type: "POST",
	   url: 'ajax/admin_log_in.php',
	   data: 'action_do=log_out',
	   cache: false,
	   dataType: 'json',
	   beforeSend : function(){
		   admin_loader(true);
	   },
	   complete : function(){
		   admin_loader(false);
	   },
	   success: function(msg){
			if(msg.success == true){
				alert('You have been logged out...');
				window.location.href = 'index.php';
			}
	   }
	 });
}

function admin_list_sections(tab, set, to_display, search_query){
	admin_loader(true);
	
	if(to_display == undefined){
		to_display = 10;
	}

	if(search_query == undefined){
		search_query = '';
	}


	$.get(
		  'ajax/admin_list.php',
		  {
			t  : tab,
			msgset : set,
			items_to_display : to_display,
			q : search_query
		  },
		  function success(data){
			  $.fn.nyroModalManual({
				bgColor: '#999999',
				content: data
			  });
			  
			  $("#list_items").sortable();
			  //alert($("#list_items").html());
			  
			  admin_loader(false);
		  });
}

function admin_loader(display){

	if(display == true){
		$.blockUI({ css: { 
				msg: 'Loading...',
				border: 'none', 
				padding: '15px', 
				backgroundColor: '#000', 
				'-webkit-border-radius': '10px', 
				'-moz-border-radius': '10px', 
				opacity: .5, 
				color: '#fff' 
			} }); 	
	}else{		
			setTimeout(function() { 
				
				$.unblockUI({ 
					onUnblock: function(){ 
						
					} 
				}); 
			}, 1000);
	}
}

function enableJedit(){
	$(".autogrow").editable("ajax/admin_edits.php", { 
		indicator : "Loading...",
		type   : 'autogrow',
		submitdata: { action_do : 'edit' },
		select : false,
		submit : 'EDIT',
		cancel : 'CANCEL',
		onblur : 'ignore',
		cssclass : "editable",
		callback:function(){
			alert('Content edited correctly');
		},
		autogrow : {
		   lineHeight : 21,
		   minHeight  : 42
		}
		
	});
}

function enableJeditPublish(){
	$(".jedit_publish").editable("ajax/admin_edits.php", { 
		indicator : "Loading...",
		type   : 'select',
		data : "{'1':'Published', '0':'Unpublished'}",
		submitdata: { action_do : 'edit' },
		select : false,
		submit : 'EDIT',
		cancel : 'CANCEL',
		onblur : 'ignore'
	});
}



function enableJeditGalleries(galleriesObj){
	$(".jedit_galleries").editable("ajax/admin_edits.php", { 
		indicator : "Loading...",
		type   : 'select',
		data : galleriesObj,
		submitdata: { action_do : 'edit' },
		select : false,
		submit : 'EDIT',
		cancel : 'CANCEL',
		onblur : 'ignore'
	});
}


function recordUploaderQuick(form, file_f, redirect, redirect_var, do_confirmation, custom_success_msg){
	
	if(do_confirmation != undefined && do_confirmation == true){
	
		if(!confirm('Would you like to add this record?')){
			return;
		}
	
	}
	
	
		formData = 'action_do=add&' + $('#' + form).formSerialize(); 		

		$.ajax({
		   type: "POST",
		   url: 'ajax/admin_edits.php',
		   data: formData,
		   cache: false,
		   dataType: 'json',
		   beforeSend : function(){
			   admin_loader(true);
		   },
		   complete : function(){
			   admin_loader(false);
		   },
		   success : function(msg){
					if(msg.success == false){
						
						alert(msg.msg);
						
					}else{
						if(custom_success_msg != undefined){
							alert(custom_success_msg);
						}else{
							alert(msg.msg);
						}
						
						if(redirect_var == true){
							window.location.href = redirect + msg.id;
						}else{
							window.location.href = redirect
						}
						
						
					}	
		   }
		});		
		
	
}

function recordUploader(main_action /* add, preview_image*/, file_f /*comma separated*/){
	
	if(file_f == undefined){
		file_f = '';
	}
	
	if(confirm('Would you like to add this record?')){
		
		$('.hidden_field').each(function(i){
			$('.hidden_field_form_' + i).val($(this).html());
		});
				
		$('#add_form').ajaxForm({
				dataType : 'json',
				data:{
					action_do : main_action,
					file_fields : file_f
				},
				url:'ajax/admin_edits.php',
				
				success: function(data) {
					
					if(data.success == false){
						alert(data.msg);
					}else{
						alert(data.msg);
						window.location.reload(true);
					}	
									
				}
		});
	}
}

function recordDeleter(id_f, redirect){
	if(!confirm('Would you like to delete this record? This action is undoable.')){
		admin_loader(false);
		return;
	}
	

	$.ajax({
	   type: "POST",
	   url: 'ajax/admin_edits.php',
	   data: {
			  action_do : 'delete_record',
			  id : id_f
	   },
	   cache: false,
	   dataType: 'json',
	   beforeSend : function(){
		   admin_loader(true);
	   },
	   complete : function(){
		   admin_loader(false);
	   },
	   success : function(msg){
			 if(msg.success == true){
				alert(msg.msg);
				
				if(redirect == undefined){
					$('#rec_' + msg.id).remove();
				}else{
					window.location.href = redirect;	
				}
			 }else{
				alert(msg.msg);
			 }
	   }
	});
	
}

function deleteImage(file_f, image_file, id, img_size_array, field){
	
	if(!confirm('Would you like to delete this image?')){	
			return;
	}
	
	if(field != undefined){
		ff = ', \''+field+'\'';
		field = field + '_';		
	}else{
		field = '';
		ff = '';	
	}
	
	
	$.ajax({
	   type: "POST",
	   url: 'ajax/admin_edits.php',
	   data: {
	   		action_do : 'delete_image',
			file_fields : file_f,
			file : image_file
	   },
	   cache: false,
	   dataType: 'json',
	   beforeSend : function(){
		   admin_loader(true);
	   },
	   complete : function(){
		   admin_loader(false);
	   },
	   success: function(msg){
			if(msg.success == true){
				alert(msg.msg);
			
				document.getElementById('img_' + field + id).src = 'pics/not_uploaded.jpg';
				
				img_arr = '[' + img_size_array[0] + ',' + img_size_array[1] + ']';
				$('#img_option_'  + field + id).html('<form id="img_form_' + field + id +'" enctype="multipart/form-data" method="post">'+
												'<input name="'+file_f+'" type="file" />' +
												'<input type="submit" name="add_record" value="Upload Image" onclick="uploadImage(\''+file_f+'\', \''+id+'\', '+img_arr+' '+ff+')" />'+
											'</form>');
			}else{
				alert(msg.msg);
			}
	   }
	 });
}

function uploadImage(file_f, id, img_size_array, field){
		
	admin_loader(true);
	
	if(field != undefined){
		ff = ', \''+field+'\'';
		field = field + '_';
	}else{
		ff = '';	
		field = '';
	}
	
	//alert('img_picture_' + field + id);
	
	$('#img_form_'  + field + id).ajaxForm({
			dataType : 'json',
			data:{
				action_do : 'add_image',
				file_fields : file_f
			},
			url:'ajax/admin_edits.php',
			
			success: function(data) {
				if(data.success == true){
					alert(data.msg);
					
					document.getElementById('img_' + field + id).src = 'phpThumb/phpThumb.php?src=../pics/'+data.file+'&w='+img_size_array[0]+'&h=' + img_size_array[1];
					img_arr = '[' + img_size_array[0] + ',' + img_size_array[1] + ']';
					
					$('#img_option_' + field + id).html('<img src="pics/icons/no_16.png" width="16" height="16" style="padding-right:0px" /> <span class="form_option_label" ><a href="javascript:;" onclick="deleteImage(\''+file_f+'\', \''+data.file+'\', \''+id+'\', '+img_arr+' '+ff+')">Delete Image?</a></span>');
				}else{
					alert(data.msg);
				}
				admin_loader(false);			
			}
	});
}



function callEditor(obj){
	
	$.post(
		  'ajax/admin_editor.php',
		  {
			  id : $(obj).attr('id'),
			  value : $(obj).html()
		  },
		  function success(data){
			  $.fn.nyroModalManual({
					bgColor: '#999999',
					content: data,
					endFillContent : enableTinyMce
				});			  
		  }
	);
}


function saveEditorContent(rid){
	admin_loader(true);
	$.post(
		  'ajax/admin_edits.php',
		  {
			  action_do : 'edit',
			  id : rid,
			  value : $('#admin_editor').html()
		  },
		  function success(data){
			  admin_loader(false);
			  //$('#'+rid).html(data);
			  
			  alert('Content edited');
			  location.reload(true);
		  }
	);
}

function enableTinyMce(){
		$('#admin_editor').tinymce({
			// Location of TinyMCE script
			script_url : 'js/tinymce_3_2_5_jquery/tinymce/jscripts/tiny_mce/tiny_mce.js',

			// General options
			theme : "advanced",
			plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

			// Theme options
			// Theme options
			theme_advanced_buttons1 : "newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull",
			theme_advanced_buttons2 : "cut,copy,paste,pasteword,|,search,replace,|,bullist,numlist,|,undo,redo,|,link,unlink,anchor,cleanup",
			theme_advanced_buttons3 : "fullscreen",
			//theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
			//theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
			//theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
			//theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,

			// Example content CSS (should be your site CSS)
			content_css : CSS_PATH + "editor.css",

			// Drop lists for link/image/media/template dialogs
			template_external_list_url : "lists/template_list.js",
			external_link_list_url : "lists/link_list.js",
			external_image_list_url : "lists/image_list.js",
			media_external_list_url : "lists/media_list.js",

			// Replace values for the template plugin
			template_replace_values : {
				username : "Some User",
				staffid : "991234"
			}
	});
}

function toggleEditableAreas(){
	$('.editable_area').each(function(i){
		//alert('OK'  + $(this).css('border') );
		if( $(this).css('border') == '3px dotted rgb(153, 0, 0)' || $(this).css('border') == '#900 3px dotted'){
			$(this).css('border', '0px dotted #900');	
		}else{
			$(this).css('border', '3px dotted #900');	
		}
	});
}

function toggle_area(area_id){
	if( $('#' + area_id).css('display') == 'block'){
		$('#' + area_id).hide(500);
	}else{
		$('#' + area_id).show(500);
	}
}

function toggle_areas(area_class){
	$('.' + area_class).each(function(i){
		//alert($(this).css('display'));
		if( $(this).css('display') == 'none'){
			$(this).css('display', 'block');
		}else{
			$(this).css('display', 'none');
		}
	});
}
