$(document).ready(function(){
	$.fx.speeds._default = 1000;
	$(function() {
		$("#reservationsTabs").tabs();
	});
	$("#form01").validate();
	$('#dialog').dialog({
		autoOpen: false,
		show: 'blind',
		modal: true,
		hide: 'explode'
	});
	
	//
	
	// hide on load
	$("div.hideshow03").hide();
	$("div#forgotPass").hide();
	$("div#eventMedia").hide();
	
	$(function() {
		$('#datepicker').datepicker({
			numberOfMonths: 2,
			dateFormat: 'yy-mm-dd',
			
		});

		$('#datepicker2').datepicker({
			numberOfMonths: 2,
			dateFormat: 'yy-mm-dd',
			
		});
		$('#addDatepicker').datepicker({
			numberOfMonths: 2,
			dateFormat: 'yy-mm-dd',
			
		});
		
	});
	
	$("[id^=sendRe]").live( 'click', function(){
		
		var action = 'sendText';
		var theID = $(this).parents("tr:first").attr('id');
		var companyID = $("#companyID").html();
		$.ajax({
			type: 'POST',
			data: 'func=closeReservation' + '& resID=' + theID + '& companyID=' + companyID,
			url: 'process.php',
			success: function(result) {
			var res = $.evalJSON(result);
			console.log(res);
			if(res.success == 1){
				window.location.reload();
				$(this).parents("tr:first").remove();
			}
				
			console.log(result);
			
		},
		error: function(event) {
			
			
			alert(event);
			console.log(event);
			alert('ok, check it out');
			
		}
		
		
		});
		
		
	})
	
	$("[id^=updater]").live('click', function(){
		
		var theID = $(this).parents("tr:first").attr('id');
		var theDay = $(this).parents("tr:first").children("td:first").children('input').val();
		var theName = $(this).parents("tr:first").children("td:gt(0)").children('input').val();
		var thePhone = $(this).parents("tr:first").children("td:gt(1)").children('input').val();
		var totalParty = $(this).parents("tr:first").children("td:gt(2)").children('input').val();
		
		$.ajax({
			type: 'POST',
			url: 'process.php',
			data: 'func=updateReservation' + '& resID=' + theID + '& day=' + theDay + '& name=' + theName + '& party=' + totalParty + '& phone=' + thePhone,
			success: function(result){
			$(this).parents("tr").fadeOut();
			
			window.location.reload();
			
			},
			error: function(result){
			
				console.log(result);
				
			}
			
		});
		
		
	})
	$("[id^=dater]").live('click', function(){
		$(this).datepicker({
			
			formatDate: 'yy-mm-dd',
			
	    	duration: '',

	        showTime: true,

	        constrainInput: false

	     });

		
		
	})
	$("[id^=closer]").live('click', function(){
		
		$(this).parents("tr").fadeOut(1000);
		
	})
	$("[id^=editRe]").live( 'click', function(){

		var action = 'editReservation';
		var theID = $(this).parents("tr:first").attr('id');
		var theDay = $(this).parents("tr:first").children("td:first").html();
		var theName = $(this).parents("tr:first").children("td:gt(0)").html();
		var thePhone = $(this).parents("tr:first").children("td:gt(1)").html();
		var totalParty = $(this).parents("tr:first").children("td:gt(2)").html();
		
		$(this).parents("tr:first").after('<tr id="' + theID + '"><td><input id="dater" type="text" value="' + theDay + '"/></td><td><input type="text" value="' + theName + '"/> ' + 
										  '</td><td><input type="text" value="' + thePhone + '"/></td><td><input type="text" value="' + totalParty + '"/></td>' + 
										  '<td><button id="updater" value="' + theID + '">Update</button><button id="closer">X</button></td></tr>');
		
	})
	$("[id^=delRe]").live( 'click', function(){
			
		
		var theID = $(this).parents("tr:first").attr('id');
		$.ajax({
			type: 'POST',
			data: 'func=deleteReservation' + '& resID=' + theID,
			url: 'process.php',
			success: function(result) {
			var res = $.evalJSON(result);
		
			if(res.success == 1){
				window.location.reload();
				
			}
				
		},
		error: function(event) {
			
		}
	
		})	
	});
	
	
	$("#defButton").click(function(){
		
		var companyID = $("#companyID").html();
		var defText = $("#defText").val();
		
				$.ajax({
					type: 'POST',
					data: 'func=updateDef' + '& companyID=' + companyID + '& defText=' + defText,
					url: 'process.php',
				success: function(result) {
					window.location.reload();
				},
				error: function(event) {
					
				}
				
				});
	
	});
	$("#resButton").click(function(){
		
		var companyID = $("#companyID").html();
		var gName = $("#gName").val();
		var addlNotes = $("#addlNotes").val();
		var gPhone = $("#gPhone").val();
		var day = $('#addDatepicker').val();
		var minute = $('select#minute option:selected').val();
		var hour = $('select#hour  option:selected').val();
		var ap = $('select#ampm  option:selected').val();
		
				$.ajax({
					type: 'POST',
					data: 'func=addReservation' + '& companyID=' + companyID + '& gName=' + gName + '& gPhone=' + gPhone + '& addlNotes=' + addlNotes +  '& d=' + day + '& m=' + minute + '& h=' + hour + '& ap=' + ap,
					url: 'process.php',
				success: function(result) {
					window.location.reload();
				},
				error: function(event) {
					
				}
				
				});
	
	});
	// hide/show on click
	$("a.hideshow03").click(function(){
		$("div.hideshow03").slideToggle("slow");
	});
	$("a#addImage").click(function(){
		$("div#eventMedia").slideToggle("slow");
	});
	$("a#forgot").click(function(){
		$('#resetError').fadeOut('slow');
		$('#resetSuccess').fadeOut('slow');
		$('#loginError').fadeOut('slow');
		$("div#forgotPass").slideToggle("fast");
		$("div#login").slideToggle("fast");
	})
	$('form#quickMessage').submit(function(){
		var message = $('#theMessage').attr('value');
		var callback = $('#rb1').attr('value');
		$.ajax({
			type: "POST",
			url: "includes/quickMessage.php",
			data: "message=" + message +"& callback=" + callback,
			success: function(event, jsonObj, testing) {
				var result = $.evalJSON(event);
				if (result.success == 1){
					
					$('#dialog').dialog('open');
					$('#theMessage').attr('value', '');
					} else {
					
	
					
				}	
		},
		error: function(event) {
			
			
			alert(event.responseText);
			
		}
		});
		
		return false;
		
	});
	$("form#loginform").submit(function(){
		var username = $('#username').attr('value');
		var password = $('#password').attr('value');
		$('#loginError').fadeOut('fast');
		$.ajax({
			type: "POST",
			url: "checkLogin.php",
			data: "username="+ username +"& password=" + password,
		
			success: function(event, jsonObj, testing) {
			
				var result = $.evalJSON(event);
		
				if (result.success == 1){
				
					var redir = result.url;
				
					window.location = redir;
					
				} else {
					
					$('#loginError').fadeIn('slow');
					$('#loginError').html('<p class=error>' + result.message + '</p>');
					
				}
				
			
		},
		
			error: function(event) {
        	
		
		}
			
		
		});
		
		return false;
				
	});
	

	//revcover password
	
	$("form#recoverPass").submit(function(){
		var email = $('#email').attr('value');
		$('#resetSuccess').fadeOut('fast');
		$('#resetError').fadeOut('fast');
		$.ajax({
			type: "POST",
			url: "recoverPass.php",
			data: "email="+ email,
			
			success: function(event, jsonObj, testing) {
			
				var result = $.evalJSON(event);
			
				if (result.success == 1){
					
					$('#resetSuccess').fadeIn('slow');
					$('#resetSuccess').html('<p class=success>' + result.message + '</p>');
					
					
				} else {
			
					$('#resetError').fadeIn('slow');
					$('#resetError').html('<p class=error>' + result.message + '</p>');
					
					
					
				}
				
			
		},
		
			error: function(event) {
        	alert(event.responseText);
		
		}
			
		
		});
		
		return false;
				
	});
	
	
	// success demo, just for show...
	// remove these lines
	$(".success").hide();
	$(".success").fadeIn("slow");
	$(".error").hide();
	$(".error").fadeIn("slow");
	$("#loggingin").hide();
	
	
	$(".hideSuccess").click(function(){
		$(".success").fadeOut("slow");
	});
	$(".hideError").click(function(){
		$(".error").fadeOut("slow");
	});
	
	$(function() {
		$("#sortable").sortable({
			placeholder: 'ui-state-highlight'
		});
		$("#sortable").disableSelection();
	});

	$("#accordion").accordion();

	$("a[rel^='prettyPhoto']").prettyPhoto();

	
});
