YAHOO.namespace("jolkona");
YAHOO.namespace("jolkona.container");

YAHOO.jolkona.waitDuration = 0;
YAHOO.jolkona.dataTimer = null;

// Bind function to DOM ready
$(document).ready(function(){
	$('input.toggleAll').bind('click', function() {
		$(this).parents('ul').find('input[type="checkbox"]').attr('checked', $(this).get(0).checked ? 'checked' : '');
	});
	$('input.toggleChildren').bind('click', function() {
		$(this).parents('li').find('input[type="checkbox"]').attr('checked', $(this).get(0).checked ? 'checked' : '');
	});	
	$('#projectFilter input[type="checkbox"]').bind('click', function(){
		// Check the "all" checkbox when all checkboxes in the group are checked and uncheck the "all" box when one or more checkboxes in the group are not checked
		if ($(this).parents('ul').find('input[type="checkbox"]:not(input.toggleAll):checked').length == $(this).parents('ul').find('input[type="checkbox"]:not(input.toggleAll)').length) {
			$(this).parents('ul').find('input.toggleAll').attr('checked', 'checked');
		} else {
			$(this).parents('ul').find('input.toggleAll').attr('checked', '');
		}
		loadDataWait();
	});
	$('a.expandCollapse').bind('click', function() {
		if ($(this).hasClass('iconExpand')) {
			$(this).removeClass('iconExpand');
			$(this).addClass('iconCollapse');
			$(this).parents('li').find('ul').slideDown();
		} else {
			$(this).removeClass('iconCollapse');
			$(this).addClass('iconExpand');
			$(this).parents('li').find('ul').slideUp();
		}
	});		
});

function loadDataWait() {

	// Reset timer
	clearTimeout(YAHOO.jolkona.dataTimer);
	
	// Give the user a few seconds to make changes before loading data
	YAHOO.jolkona.dataTimer = setTimeout(loadData, YAHOO.jolkona.waitDuration);
	
}

function loadData() {

	// Show loading screen
	YAHOO.jolkona.container.wait.show();

	// Force a half-second delay to prevent the loading screen from rapidly appearing and
	// then disappearing when the remote data is cached or loaded fast
	setTimeout(function(){
		var url = baseUrl + '?' + $('#projectFilter').serialize() + '&nocache=' + Math.random();
		
		// If we're on the project detail page, submit the filter form rather than loading the projects via AJAX
		// since the Google map does not load correctly for some reason
		if ($('#projectFilter input[name="isProjectDetailPage"]').val() == '1') {
			$('#projectFilter').get(0).submit();
			return false;
		}
		
		// Load content
		//$('#projectListWrapper').load(url, null, function(){
		$('#content').load(url, null, function(){
			// Switch to sidebar layout (in case the project list is being loaded from a dual sidebar layout page such as the project detail page)
			$('div.dualSidebarLayout').removeClass('dualSidebarLayout').addClass('sidebarLayout');
			// Re-bind scroll follow to project map
			$('#projectMap').scrollFollow({offset: 20, container: "body"});
			// Hide loading screen
			YAHOO.jolkona.container.wait.hide();
		});
	},500);
}

YAHOO.util.Event.onDOMReady(function() {
	if (!YAHOO.jolkona.container.wait) {
		// Initialize the temporary Panel to display while waiting for external content to load
		YAHOO.jolkona.container.wait = 
				new YAHOO.widget.Panel("wait",  
												{ width: "240px", 
												  fixedcenter: true, 
												  close: false, 
												  draggable: false, 
												  zindex:4,
												  modal: true,
												  visible: false
												} 
											);
		YAHOO.jolkona.container.wait.setHeader("Updating projects list...");
		YAHOO.jolkona.container.wait.setBody("<img src=\"http://us.i1.yimg.com/us.yimg.com/i/us/per/gr/gp/rel_interstitial_loading.gif\"/>");
		YAHOO.jolkona.container.wait.render(document.body);
	}
});

function initSlider() {
	
	var sliderTimer = null;
	
	// Sliders have a range of 165 pixels
	var range = 165;
		
	// Set up a function to convert the min and max values into something useful
	var convertMin = function (val) {
		return Math.round(val * (sliderMax / range));
	};
	var convertMax = function (val) {
		return Math.round(val * (sliderMax / range));
		//return Math.round(val * (sliderMax / range) + (((range - val) / range) * 200));
	};		

	// Setup sliders when DOM is ready
	YAHOO.util.Event.onDOMReady(function () {
		
		// Create min slider
		var minSlider = YAHOO.widget.Slider.getHorizSlider("minDonationAmountSlider", "minSliderThumb", 0, 165);
		// Create max slider
		var maxSlider = YAHOO.widget.Slider.getHorizSlider("maxDonationAmountSlider", "maxSliderThumb", 0, 165);
		
		// Initalize sliders
		minSlider.setValue(sliderMinInit, true, true, true);
		maxSlider.setValue(sliderMaxInit, true, true, true);
		
		// Custom function to update the text fields, the converted value
		// report and the slider's title attribute
		var updateUI = function () {
			min = minSlider.getValue();
			max = maxSlider.getValue();
			YAHOO.util.Dom.get("donationAmountFrom").innerHTML = convertMin(min);
			YAHOO.util.Dom.get("donationAmountTo").innerHTML   = convertMax(max);
			YAHOO.util.Dom.get("donationAmountMin").value   = convertMin(min);
			YAHOO.util.Dom.get("donationAmountMax").value   = convertMax(max);
			//if (min > max) maxSlider.setValue(min, true, true, true);
			//if (max < min) minSlider.setValue(max, true, true, true);
		};
				
		// Subscribe to slider events
		minSlider.subscribe('change', updateUI);
		minSlider.subscribe('slideEnd', function(){
			// Pause for a short while to give the user a chance to make more changes
			sliderTimer = setTimeout(loadData, 1000);
		});
		minSlider.subscribe('slideStart', function(){
			// Cancel timer
			clearTimeout(sliderTimer);
		});
		maxSlider.subscribe('change', updateUI);
		maxSlider.subscribe('slideEnd', function(){
			// Pause for a short while to give the user a chance to make more changes
			sliderTimer = setTimeout(loadData, 1000);
		});
		maxSlider.subscribe('slideStart', function(){
			// Cancel timer
			clearTimeout(sliderTimer);
		});		
				
		// Attach the sliders to the YAHOO.jolkona namespace for public probing
		YAHOO.jolkona.minSlider = minSlider;		
		YAHOO.jolkona.maxSlider = maxSlider;		
		
				
	});
	
	/*
	var sliderTimer = null;
	
	// Slider has a range of 165 pixels
	var range = 165;

	// No ticks for this example
	var tickSize = 0;

	// We'll set a minimum distance the thumbs can be from one another
	var minThumbDistance = 1;

	// Initial values for the thumbs
	var initValues = [sliderMinInit,sliderMaxInit];

	// Set up a function to convert the min and max values into something useful
	var convert = function (val) {
		return Math.round(val * (sliderMax/range));
	};

	// Setup slider when DOM is ready
	YAHOO.util.Event.onDOMReady(function () {
		var sliderElement = YAHOO.util.Dom.get("donationAmountSlider");

		// Create the DualSlider
		var slider = YAHOO.widget.Slider.getHorizDualSlider(sliderElement,
			"donationAmountSliderMin", "donationAmountSliderMax",
			range, tickSize, initValues);

		slider.minRange = minThumbDistance;
        
		// Custom function to update the text fields, the converted value
		// report and the slider's title attribute
		var updateUI = function () {
			YAHOO.util.Dom.get("donationAmountFrom").innerHTML = convert(slider.minVal);
			YAHOO.util.Dom.get("donationAmountTo").innerHTML   = convert(slider.maxVal);
			YAHOO.util.Dom.get("donationAmountMin").value   = convert(slider.minVal);
			YAHOO.util.Dom.get("donationAmountMax").value   = convert(slider.maxVal);
		};

		// Subscribe to the dual thumb slider's change and ready events to
		// report the state.
		slider.subscribe('ready', updateUI);
		slider.subscribe('change', updateUI);
		slider.subscribe('slideEnd', function(){
			// Pause for a short while to give the user a chance to make more changes
			sliderTimer = setTimeout(loadData, 1000);
		});
		slider.subscribe('slideStart', function(){
			// Cancel timer
			clearTimeout(sliderTimer);
		});			
		
		// Attach the slider to the YAHOO.jolkona namespace for public probing
		YAHOO.jolkona.slider = slider;		
	});
	*/
};
initSlider();