//SET NOCONFLICT TO WORK WITH OTHER LIBRARIES
jQuery.noConflict();

//CUSTOM JQUERY FUNCTIONS
jQuery(document).ready(function(){
	
	// Set up Annual Reports dropdown, if it exists
	if (jQuery('#annualReportSelect').length != 0)
	{
		jQuery('#annualReportSelect').change(function()
		{
			if (jQuery('#annualReportSelect').val() != "")
			{
				window.location = jQuery('#annualReportSelect').val();
			}
		});
	}	

});

//clearInput function
jQuery.fn.clearInput = function(){
	return this.focus(function(){
		if(this.value == this.defaultValue){
			this.value = "";
		}
	}).blur(function(){
		if(!this.value.length){
			this.value = this.defaultValue;
		}
	});
};

//clearForm function
jQuery.fn.clearForm = function() {
	return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
			return jQuery(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};

// loadRecyclingData function: load data into page via JSON
function loadRecyclingData(id, lang)
{
	if (id > 0)
	{
		jQuery.getJSON(
			'/'+lang+'/recycling_data/' + id + '/',
			null,
			function(data, textStatus)
			{
				// Insert all location information
				jQuery('#contactInfo h4').html(data.title);
				jQuery('#regionLocation').html(data.location);
				jQuery('#regionAddress').html(data.address);
				jQuery('#regionCity').html(data.city);
				jQuery('#regionPostcode').html(data.postcode);
				jQuery('#regionPhone').html(data.phone);
				jQuery('#regionFax').html(data.fax);
				jQuery('#regionEmail').html(data.email);
				jQuery('#regionEmail').attr('href', 'mailto:' + data.email);
				jQuery('#regionEmail').attr('title', data.email);
				jQuery('#regionGeneralManager').html(data.general_manager);
				jQuery('#regionChairperson').html(data.chairperson);
				jQuery('#regionWeb').html(data.web);
				jQuery('#regionWeb').attr('href', 'http://' + data.web);
				jQuery('#regionWeb').attr('title', data.web);
				
				// Initialize the recycleTypes string and iterate through the various types to construct it
				var recycleTypes = '';
			
				for (var i in data.recycalables)
				{
				
					if (data.recycalables[i] != "")
					{
						recycleTypes += '<li class="' + i + '"><a href="#" title="' + data.recycalables[i][0] + '">' + data.recycalables[i][0] + '</a></li>';
					}
				}
				
				jQuery('#recycleTypes ul').html(recycleTypes);
				
				
				jQuery('#recycleTypes a').click(function(event)
				{
					event.preventDefault();
					
					jQuery('#recycleTypes a').removeClass('active');
					jQuery(this).addClass('active');
					jQuery('#directions h3').html(jQuery(this).attr('title'));
					jQuery('#recycleTypeInfo').html(data.recycalables[jQuery(this).parent().attr('class')][1]);
					Cufon.replace('#directions h3', { fontFamily: 'CalvertMTStd' });
					
				});
				
				jQuery('#recycleTypes a').first().click();
			}
		);
	
		return true;
	}
	else
	{
		return false;
	}
}
