//Vars//

$(document).ready(function(){ 
	var emailError = false;
	var userError = false;
	var passError = false;
	
	var error = "Poke Callum and tell him to fix this";
	
	/////////////////////
	//Buttons
	///////////////////// 
	
	/////////////////////
	//Input
	/////////////////////
	$(".inputText").focusin(function(){
		$(this).css("border",'1px solid #00CDE5');
	});
	$("#userLog").focusin(function(){
		$(this).css("border",'1px solid #00CDE5');
	});
	$("#userLog").focusout(function(){
		$(this).css("border",'1px solid #000000');
	});
	
	
	
	/////////////////////
	//Dialog Boxes
	/////////////////////
	$("#AddUser").dialog({
		autoOpen:false,
		width	:762,
		height	:400,
		title	:"Add User",
		modal	:true,
		close	:function() {
    				window.location.reload();
 				}		
		
	});
	$("#EditUser").dialog({
		autoOpen:false,
		width	:762,
		height	:500,
		title	:"Edit User",
		modal	:true,
		close	:function() {
    				window.location.reload();
 				}
		
	});
	$("#DeleteUser").dialog({
		autoOpen:false,
		width	:762,
		height	:500,
		title	:"Delete User",
		modal	:true,
		close	:function() {
    				window.location.reload();
 				}
		
	});
	$("#logout").dialog({
		autoOpen:false,
		width	:300,
		height	:170,
		title	:"Logout",
		modal	:true,
		resize	:false,
		buttons: { 
			"Cancel": function() { 
				$(this).dialog("close"); 
			},
			"Ok": function() { 
				$.post("/classes/users/logout.php?act=logout");						
				$(this).dialog("close"); 
				window.location.reload();
			}			
		}	
	});	
	
	
	$("#MenuAdmin").dialog({
		autoOpen:false,
		width	:762,
		height	:500,
		title	:"Menu Editor",
		modal	:true,
		close	:function() {
    				window.location.reload();
 				}
		
	});
	
	$("#AddPage").dialog({
		autoOpen:false,
		width	:762,
		height	:650,
		title	:"Add Page",
		modal	:true,
		resizable: false,
		close	:function() {
    				window.location.reload();
 				}
		
	});
	$("#EditPage").dialog({
		autoOpen:false,
		width	:762,
		height	:650,
		title	:"Edit Page",
		modal	:true,
		resizable: false,
		close	:function() {
    				window.location.reload();
 				}
		
	});
	$("#AddNews").dialog({
		autoOpen:false,
		width	:762,
		height	:680,
		title	:"Add News",
		modal	:true,
		resizable: false,
		close	:function() {
    				window.location.reload();
 				}
		
	});
	$("#EditNews").dialog({
		autoOpen:false,
		width	:762,
		height	:680,
		title	:"Edit News",
		modal	:true,
		resizable: false,
		close	:function() {
    				window.location.reload();
 				}
		
	});
	$("#DeleteNews").dialog({
		autoOpen:false,
		width	:300,
		height	:170,
		title	:"Logout",
		modal	:true,
		resize	:false,
		buttons: { 
			"Cancel": function() { 
				$(this).dialog("close"); 
			},
			"Ok": function() { 
				$.post("/classes/news/addNews.php?act=delete&id="+id);						
				$(this).dialog("close"); 
				window.location.reload();
				window.location = "/classes/news/news.php";
			}			
		}	
		
	});
	
	$("#imageManager").dialog({
		autoOpen:false,
		width	:800,
		height	:460,
		title	:"Image Manager",
		modal	:true,
		resizable: false	
		
	});
	
	$("#barManager").dialog({
		autoOpen:false,
		width	:762,
		height	:400,
		title	:"Bar Manager",
		modal	:true,
		resizable: false,
		close	:function() {
    				window.location.reload();
 				}	
		
	});
	$("#adManager").dialog({
		autoOpen:false,
		width	:762,
		height	:600,
		title	:"Ad Manager",
		modal	:true,
		resizable: false,
		close	:function() {
    				window.location.reload();
 				}	
		
	});
	
		
	/////////////////////
	//Admin Bar Functions
	/////////////////////	
		
	$(".userBtn").mouseenter(function(){
		$(".userMenu").slideDown("fast");
		
	});	
	$(".userMenu").mouseleave(function(){
		$(".userMenu").slideUp("slow");
		
	});	
	
	$(".pageBtn").mouseenter(function(){
		$(".pageMenu").slideDown("fast");
		
	});	
	$(".pageMenu").mouseleave(function(){
		$(".pageMenu").slideUp("slow");
		
	});	
	$(".newsBtn").mouseenter(function(){
		$(".newsMenu").slideDown("fast");
		
	});	
	$(".newsMenu").mouseleave(function(){
		$(".newsMenu").slideUp("slow");
	});	
	
	/////////////////////////
	//   User
	/////////////////////////
	
	$(".addUser").click(function(){
		$("#AddUser").html('<iframe src="/classes/users/addUser.php" frameborder="0" width="100%" height="100%">Your browser does not support this CMS Please upgrade</iframe>');
		$("#AddUser").dialog("open");
	});
	
	$(".editUser").click(function(){	
		$("#EditUser").html('<iframe src="/classes/users/editUser.php" frameborder="0" width="100%" height="100%">Your browser does not support this CMS Please upgrade</iframe>');	
		$("#EditUser").dialog("open");
	});
	
	$(".deleteUser").click(function(){
		$("#DeleteUser").html('<iframe src="/classes/users/deleteUser.php" frameborder="0" width="100%" height="100%">Your browser does not support this CMS Please upgrade</iframe>');		
		$("#DeleteUser").dialog("open");
	});
	$(".logoutBtn").click(function(){
		$("#logout").dialog("open");
	});
	
	$(".menuBtn").click(function(){
		$("#MenuAdmin").html('<iframe src="/classes/menu/changeMenu.php" frameborder="0" width="100%" height="100%">Your browser does not support this CMS Please upgrade</iframe>');
		$("#MenuAdmin").dialog("open");
	});	
	
	/////////////////////////
	//   PAGE
	/////////////////////////
	
	$(".addPage").click(function(){
		$("#AddPage").html('<iframe src="/classes/page/addPage.php" frameborder="0" width="100%" height="100%">Your browser does not support this CMS Please upgrade</iframe>');
		$("#AddPage").dialog("open");
	});
	
	$(".editPage").click(function(){
		$("#EditPage").html('<iframe src="/classes/page/addPage.php?act=edit&id='+id+'" frameborder="0" width="100%" height="100%"></iframe>');		
		$("#EditPage").dialog("open");
	});
	$(".adBtn").click(function(){
		$("#adManager").html('<iframe src="/classes/page/adManager.php?id='+id+'" frameborder="0" width="100%" height="100%"></iframe>');		
		$("#adManager").dialog("open");
	});
	$(".deletePage").click(function(){		
		alert(error);
	});
	
	/////////////////////////
	//   NEWS
	/////////////////////////
	
	$(".addNews").click(function(){
		$("#AddNews").html('<iframe src="/classes/news/addNews.php" frameborder="0" width="100%" height="100%">Your browser does not support this CMS Please upgrade</iframe>');
		$("#AddNews").dialog("open");
	});
	
	
	$(".editNews").click(function(){
		$("#EditNews").html('<iframe src="/classes/news/addNews.php?act=edit&id='+id+'" frameborder="0" width="100%" height="100%"></iframe>');				
		$("#EditNews").dialog("open");
	});
	$(".deleteNews").click(function(){
		$("#DeleteNews").html('Are you sure you wish to delete this article?');
		$("#DeleteNews").dialog("open");
	});
	
	$(".uploadBtn").click(function(){		
		$("#uploadPdf").dialog("open");
	});
	$(".imgBtn").click(function(){
		$("#imageManager").html('<iframe src="/classes/imageLib/imgLib.php" frameborder="0" width="100%" height="100%">Your browser does not support this CMS Please upgrade</iframe>');		
		$("#imageManager").dialog("open");
	});
	$(".barBtn").click(function(){
		$("#barManager").html('<iframe src="/classes/imageLib/barAdmin.php?id='+id+'" frameborder="0" width="100%" height="100%">Your browser does not support this CMS Please upgrade</iframe>');		
		$("#barManager").dialog("open");
	});
	
	
	/////////////////////
	//Validation/Forms
	/////////////////////
	$("#passwordC").focusout(function(){		
		checkPasswords();
	});
	
	$("#password").focusout(function(){
		checkPasswords();
	});	
	$("#username").focusout(function(){
		checkUser();
	});
	$("#email").focusout(function(){
		checkEmail();
	});
	
	$("#username").keyup(function(){
		checkUser();
	});
	
	
	$("#userAdd").submit(function(){
		checkPasswords();
		checkUser();
		checkEmail();
		if(emailError == true || userError == true || passError == true){		
			return false;
		} else{
			return true
		}
	});
	
	$("#userEdit").submit(function()
		{								
		checkEmail();			
		if(emailError == true){		
			return false;
		} else{
			return true;
		}
				
	});		
	
	
	function checkEmail(){
		//$('div:hidden').fadeIn("slow");
		var email 		= $('#email').val();
		var emailAt 	= email.indexOf("@");
		var emailDot 	= email.lastIndexOf(".");
		if(emailAt == -1 || emailDot == -1){
			$('#emailError').html('Please Enter a Valid Email');
			$('#emailError').css("color",'#953427');
			$('#email').css("border",'1px solid #ff0000');
			emailError = true;
		}
		else if(emailAt>emailDot)
		{
			$('#emailError').html('Please Enter a Valid Email');
			$('#emailError').css("color",'#953427');
			$('#email').css("border",'1px solid #ff0000');
			emailError = true;			
		} 		
		else if(emailDot - emailAt == 1)
		{
			$('#emailError').html('Please Enter a Valid Email');
			$('#emailError').css("color",'#953427');
			$('#email').css("border",'1px solid #ff0000');
			emailError = true;			
		}		
		else
		{
			$('#emailError').html('');
			$('#email').css("border",'1px solid #00ff00');		
			emailError = false;
			$('div:hidden').fadeOut("slow");
		}
	}
		
	      
	function checkUser(){
		$('div:hidden').fadeIn("slow");
		$('#userError').html('Checking User...');
		var username = $('#username').val();
		if(username == ""){
			$('#userError').html('Please Enter a Username');
			$('#username').css("border",'1px solid #ff0000');
			userError = false;
		}else {
			$.post("checkuser.php", { username: username },  
				function(result){               
					if(result == 1){
						$('#userError').html('');
						$('#username').css("border",'1px solid #00ff00'); 					
						userError = false; 
					}else{ 
						$('#userError').html(username + ' is Not Available');
						$('#username').css("border",'1px solid #ff0000');						
						userError = true;   
					}  
			});
		}
	}
	
	function checkPasswords(){
		//$('div:hidden').fadeIn("slow");		
		var passwordVal = $("#password").val();
		var checkVal = $("#passwordC").val();
		if (passwordVal == '') 
		{
			$("#passError").html("Please Enter a Password");
			$("#password").css("border",'1px solid #ff0000');
			$("#passwordC").css("border",'1px solid #ff0000');								
			passError = true;
		} 
		else if (checkVal == '')
		{
			$("#passError").html("Please Re-Enter Your Password");
			$("#password").css("border",'1px solid #ff0000');
			$("#passwordC").css("border",'1px solid #ff0000');						
			hasError = true;
		} 
		else if (passwordVal != checkVal ) 
		{
			$("#passError").html("Passwords do not match");
			$("#password").css("border",'1px solid #ff0000');
			$("#passwordC").css("border",'1px solid #ff0000');			
			passError = true;
		}
		else
		{
			passError = false;
			$("#password").css("border",'1px solid #00ff00');
			$("#passwordC").css("border",'1px solid #00ff00');	
			$("#passError").html("");	
		}
	}
	
	
	
	/////////////////////
	//Contact
	/////////////////////
    
	$("#commentForm").submit(function(){		
		$.post("/classes/contact/email.php", $("#commentForm").serialize(),function(result){               
					if(result !=1){
						alert("Your Message Has Not Been Sent");
						
					}else{ 
						alert("Your Message Has Been Sent");  
					}  
			});
			
		return false;
		
	});	

	/////////////////////
	//Image Lib
	/////////////////////
	$("#imageTabs").tabs();
//	$("#DeleteImage").dialog({
//		autoOpen:false,
//		width	:300,
//		height	:170,
//		title	:"Delete",
//		modal	:true,
//		resize	:false,
//		buttons: { 
//			"Cancel": function() { 
//				$(this).dialog("close"); 
//			},
//			"Ok": function() { 
//				$.post("/classes/imageLib/barAdmin.php?act=delete&id="+id);					
//				$(this).dialog("close"); 
//				window.location.reload();					
//			}			
//		}	
//	});				
});

function submitImage(img,id){
	if(id == null){	
		window.opener.document.getElementById("imgbar").value = img;
		window.close();
	}else{
		window.opener.document.getElementById(id).value = img;	
		window.close();
	}			
}
function refreshPage(){
	window.location.reload();
	
};
