$(document).ready(function () {
	
	$('#resources_list h3:first').each(function(){
		$(this).next().next().find('li a').each(function(){
			
			if ($(this).html() != 'e-mail') { 
				var name = $(this).html();
				var website = $(this).attr('href');
				//$.post('/resources/add', {name:name, website:website, id:2});
				//alert(name + ' ' + website);
			} else {
				var email = $(this).attr('href');
				var name = $(this).prev().html(); 
			//	$.post('/resources/add', {name:name, email:email});
				//alert(name + ' ' + email);
			}
			
		});
	});
	
	$('form input:checkbox').click(function(){
		if ($("input:checkbox:checked").length > 3) {
			alert("You may only choose up to three categories");
			return false;
		}
	});
	
	if ($('p.error').size() > 0){
		$('div.input').show();	
	}
	
	if ($('#tags_help').size() > 0) {
		
		$('a#tags_help').mouseover(function(){
			show_tags_help($(this));
		});
				
		$('a#tags_help').mouseout(function(){
			hide_tags_help();
		});
		
	}
	
	$.localScroll();
	
	if ($('#admin').size() > 0){

		$('select.event_photo').change(function(){
			var id = $(this).attr('event_id');
			var file = $(this).val();
			
			$.post('/events/associate_photo/'+id, {file_name:file}, function(){
				alert('Image has been associated with this event');
			});
		});
		
		$('a.feature').click(function(){
			var id = $(this).attr('rel');
			var me = $(this);
			var confirm = window.confirm('Are you sure you want to feature this event?');
			if (confirm) {
				$.post('/events/feature/'+id, {}, function(){
					me.html('&#x2714;');
				});
			}
			
		});

		$('a.delete').click(function(){
			var id = $(this).attr('rel');
			var name = $(this).attr('title');
			var controller = $(this).attr('controller');
			var me = $(this);
			var table = $(this).closest('table');
			var confirm = window.confirm('Are you sure you want to delete "'+name+'"?');
			if (confirm) {
				me.html('<img src="/public/images/loading.gif"/>');
				$.post('/'+controller+'/delete_data/'+id, {}, function(){
					me.closest('tr').remove();
					table.find('tr').removeClass('alt');
					table.find('tr:odd').addClass('alt');
				});
			}
			
		});
		
		$('a.edit_hero').click(function(){
			$(this).closest('tr').next().show();
			$(this).closest('tr').hide();
		});

		$('a.delete_hero').click(function(){
			var id = $(this).attr('rel');
			var name = $(this).attr('title');
			var me = $(this);
			var confirm = window.confirm('Are you sure you want to delete "'+name+'"?');
			if (confirm) {
				me.html('<img src="/public/images/loading.gif"/>');
				$.post('/admin/delete_hero/'+id, {}, function(){
					me.closest('tr').remove();
				});
			}
		});
		
	}
	
	if ($('#np_search').size() > 0) {
		setup_autocomplete_np();
	}

	if ($('#resources_search').size() > 0) {
		setup_autocomplete_resources();
	}
	
	if ($('#calendar').size() > 0) {
		
		setup_autocomplete();
		
		$('.input_date').datepicker({
			dateFormat: 'yy-mm-dd'
		});

		$('.input_time').ptTimeSelect();

		var d = new Date();
		var y = d.getFullYear();
		var m = d.getMonth();
	
		$('#calendar').fullCalendar({
			timeFormat: 'ga',
			events: '/calendar/events/',
			eventRender: function(calEvent, element){set_qTip(calEvent, element)},
			eventClick: function(){return false}
			//eventMouseover: function(calEvent){eventPopup(calEvent)},


/*
			events: [
				{
					id: 4,
					title: "Click for Facebook",
					start: new Date(y, m, 27, 16),
					end: new Date(y, m, 29),
					url: "http://facebook.com/"
				}
			]
*/
		});
	
	}

   $('<div id="qtip-blanket">')
      .css({
         position: 'absolute',
         top: $(document).scrollTop(), // Use document scrollTop so it's on-screen even if the window is scrolled
         left: 0,
         height: $(document).height(), // Span the full document height...
         width: '100%', // ...and full width

         opacity: 0.7, // Make it slightly transparent
         backgroundColor: 'black',
         zIndex: 5000  // Make sure the zIndex is below 6000 to keep it below tooltips!
      })
      .appendTo(document.body) // Append to the document body
      .hide(); // Hide it initially

	
});

function show_tags_help(a) {
	a.after('<div id="tags_help_box"><p>Tags are keywords you wish to associate with your organization. For example, an <strong>Animal Shelter</strong> might use these tags:</p><p><strong>dogs, cats, pets, animals</strong> </p></div>');
	$('#tags_help_box').fadeIn();
}

function hide_tags_help(a) {
	$('#tags_help_box').fadeOut();
}

function eventPopup(calEvent, element) {
	//alert(calEvent.description);
	element.after('<div id="event_info" class="hide"><p>'+calEvent.description+'</p></div>');
	$('#event_info').fadeIn();	
	return false;
}

function set_qTip(calEvent, element) {
   $(element).qtip(
   {
      content: {
         title: {
            text: calEvent.title,
            button: 'Close'
         },
         text:  '<strong>Organization: </strong>'+calEvent.organization+'<br/>'+
         		'<strong>Date: </strong>'+calEvent.day+'<br/>'+
         		'<strong>Time: </strong>'+calEvent.start_hour+' - '+calEvent.end_hour+'<br/>'+
      			'<strong>Price</strong>: '+calEvent.ticket_price+'<br/>'+
      			'<strong>Description</strong>: '+calEvent.description+'<br/>'+
      			'<strong>Website: </strong><a href="'+calEvent.url+'" target="_blank">'+calEvent.url+'</a>'
      },
      position: {
         target: $(document.body), // Position it via the document body...
         corner: 'center' // ...at the center of the viewport
      },
      show: {
         when: 'click', // Show it on click
         solo: true // And hide all other tooltips
      },
      hide: false,
      style: {
         width: { max: 450 },
         padding: '14px',
         border: {
            width: 5,
            radius: 10,
            color: '#666666'
         },
         name: 'light'
      },
      api: {
         beforeShow: function()
         {
            // Fade in the modal "blanket" using the defined show speed
            $('#qtip-blanket').fadeIn(this.options.show.effect.length);
         },
         beforeHide: function()
         {
            // Fade out the modal "blanket" using the defined hide speed
            $('#qtip-blanket').fadeOut(this.options.hide.effect.length);
         }
      }
   
   });
   	
}

function validate_checkboxes() {

		if ($("input:checkbox:checked").length == 0) {
			alert("You must choose at least one category");
			return false;
		}
return true;

}

function google_search_setup(){
	var query = $('div.site_search form input[name="query"]').val();
	$('div.site_search form input[name="query"]').val('');
	$('div.site_search form input[name="q"]').val('site:chicagononprofit.org '+query);
}

function setup_autocomplete() {
		$("input.organization").autocomplete(base_url+"calendar/get_non_profits", {
			minChars: 1,
			max: 25,
			autoFill: false,
			mustMatch: false,
			//highlight: true,
			scrollHeight: 220,
			selectFirst: false
		});
}

function setup_autocomplete_np() {
		$("#np_search").autocomplete(base_url+"calendar/get_non_profits", {
			minChars: 1,
			max: 25,
			autoFill: false,
			mustMatch: false,
			matchContain: true,
			//highlight: true,
			scrollHeight: 220,
			selectFirst: false
		});
}

function setup_autocomplete_resources() {
		$("#resources_search").autocomplete(base_url+"resources/get_resources", {
			minChars: 1,
			max: 25,
			autoFill: false,
			mustMatch: false,
			//highlight: true,
			scrollHeight: 220,
			selectFirst: false
		});
}

function delete_data(id, controller) {
		

}