var watchdogTimer = 0;

function startWatchdog(limit, callback) {
    watchdogTimer = 0;
    return (setInterval(function() {
                watchdogTimer ++;
                if (watchdogTimer > limit) {
                    callback();
                }
            }, 1000));
}

function stopWatchdog(id) {
    clearInterval(id);
}

function resetWatchdog() {
    watchdogTimer = 0;
}
function showModal(){
	 $("#hiddenModalContent").slideDown();
	 $("#hiddenModalContent").animate({"opacity": 1},6000);
	 $("#hiddenModalContent").fadeOut(2000);


}
function rendercal(u,n) {
	$('fieldset#avail table').fadeOut();
	if (n == "" || n == null) { n = 0;}
	$.ajax({
		type:"GET",
		url:"/availcalendar/"+u+"/"+n,
		success: function (m) {
			$('fieldset#avail').html(m);
			$('fieldset#avail table').fadeIn();

		} 
	});
}

jQuery.obuCompactor = function ( list, listLink, item ) {
	$(list).find(listLink).addClass("link")
		.hover( function () {
			$(this).addClass(" linkOver");
			}, function () {
			$(this).removeClass("linkOver");
		}).toggle( function () {
			$(this).addClass(" linkOn").next(item).slideDown("fast");
			}, function () {
			$(this).removeClass("linkOn").next(item).slideUp("fast");
	});
}


$(window).load(function() {
    $(window).mousemove(resetWatchdog);
    $(window).keypress(resetWatchdog);
});

$(function() {
    jQuery.obuCompactor( "#faqList", "h3", "p" );
    if ($('#faqList').length > 0 && location.hash) {
        $(location.hash).click();
    }
});

$(document).ready(function() {
	$('#pickup_date').datepicker({
		showOn: 'both',
   	buttonImageOnly: true,
   	buttonImage: '/public/images/mini-calendar.gif'
   	 });
	
	$('#dropoff_date').datepicker({
		showOn: 'both',
   	buttonImageOnly: true,
   	buttonImage: '/public/images/mini-calendar.gif'
   	 });;
   $("#tabs").tabs();
   $("#mtabs").tabs();

   
   $('form p:even').addClass('even');
   $('form p:odd').addClass('odd');
   
   $('table.hlist tr:even').addClass('even');
   $('table.hlist tr:odd').addClass('odd');

	$('#dialog').dialog({ 
      autoOpen: false,
    	modal: true,
    	position: 'center',
    	maxWidth: 400
    });
    
});

     function rejectUnit(unit_id) {
   	
   	$('#dialog').dialog('option', 'title', 'Reject Unit #'+unit_id);
		$('#dialog').html('Are you sure you wish to Reject this unit?<br/><strong>Reason:</strong><br/><textarea id="reason" style="width:80%"></textarea>');
		
   	$('#dialog').dialog('option', 'buttons', { 
			"Cancel": function() { $(this).dialog("close"); },
			"Reject Unit": function() { 
				var reason = $('#dialog textarea#reason').val();
				$.ajax({
					url:"/admin/rejectunit/"+unit_id,
					type: "POST",
					data:"reason="+reason,
   				success: function(msg){		
   					$('#dialog').dialog("close");		
   					$('tr#unit'+unit_id).fadeOut();
					}
				}); 
		}
   		
 		});
		$('#dialog').dialog('open');
   }
   
   
     function deleteUser(user_id,role_id) {
   	
   	$('#dialog').dialog('option', 'title', 'Delete User #'+user_id);
		$('#dialog').html('Are you sure you wish to Delete this User?');
		
   	$('#dialog').dialog('option', 'buttons', { 
			"Cancel": function() { $(this).dialog("close"); },
			"Delete User": function() { 
				$.ajax({
					url:"/admin/deleteuser/"+user_id,
					type: "POST",
					data: "role_id="+role_id,
   				success: function(msg){		
   					$('#dialog').dialog("close");		
   					$('tr#owner'+user_id).fadeOut();
					}
				}); 
		}
   		
 		});
		$('#dialog').dialog('open');
   }
   
   
     function cancelReservation(res_id,amount) {
   	
   	$('#dialog').dialog('option', 'title', 'Delete User #'+res_id);
		$('#dialog').html('Are you sure you wish to Delete this Reservation?');
		
   	$('#dialog').dialog('option', 'buttons', { 
			"Cancel": function() { $(this).dialog("close"); },
			"Yes": function() { 
				$.ajax({
					url:"/admin/cancelReservation/"+res_id,
					type: "POST",
					data: "amount="+amount,
   				success: function(msg){		
   					$('#dialog').dialog("close");		
   					$('tr#res'+res_id).fadeOut();
					}
				}); 
		}
   		
 		});
		$('#dialog').dialog('open');
   }

   
   

   function deleteUnit(unit_id,owner_login_id) {
   	$('#dialog').dialog('option', 'buttons', { 
			"Cancel": function() { $(this).dialog("close"); },
			"Delete Unit": function() { 
				$.ajax({
					url:"/admin/deleteunit/"+unit_id+"/"+owner_login_id,
					type: "POST",
   				success: function(msg){		
   					$('#dialog').dialog("close");		
   					$('tr#unit'+unit_id).fadeOut();
					}
				}); 
		}
   		
 		});
		$('#dialog').dialog('option', 'title', 'Delete Unit #'+unit_id);
		$('#dialog').html('Are you sure you wish to Delete this unit?');
		$('#dialog').dialog('open');

   
   }
   
   
   function deleteUnitOwner(unit_id,owner_login_id) {
   	$('#dialog').dialog('option', 'buttons', { 
			"Cancel": function() { $(this).dialog("close"); },
			"Delete Unit": function() { 
				location.href = "/myunits/deleteunit/"+unit_id+"/"+owner_login_id;
			}
   		
 		});
		$('#dialog').dialog('option', 'title', 'Delete Unit #'+unit_id);
		$('#dialog').html('Are you sure you wish to Delete this unit?');
		$('#dialog').dialog('open');

   
   }


	function approveUnit(unit_id) {
		$.ajax({
			url:"/admin/approveunit/"+unit_id,
			type: "POST",
   			success: function(msg){				
   			 $('tr#unit'+unit_id).fadeOut();

			}
		});
	}

 
