// mootools
var IrishHeart = new Class(
{
	initialize: function()
	{
		this.loadToolTips();
		this.loadShoppingCart();
		this.loadEventsOther();
	},

	loadToolTips: function()
	{
		var tips = $$('.event');
		var oktips = new Array();
		for(var i=0; i<tips.length; i++)
		{
			var id = tips[i].id.toString().replace(/event_/, "tooltip_");
			var title = $(id);
			if(title != null && title.value.toString().length > 0)
			{
				//tips[i].setProperty('title', title.value);
				tips[i].store('tip:title', '');
				tips[i].store('tip:text', title.value);
			}
		}
		var tips = new Tips('.event', {className:'tool-tip'});
	},
	
	loadShoppingCart: function()
	{
		if($chk($('cart_quantity')) && $chk($('shoppingcart')))
		{
			var quantities = $$('input.cart_quantity');
			var i = 0;
			
			for(i=0; i<quantities.length; i++)
			{
				quantities.addEvent('blur', function(event)
				{
					event = new Event(event);
					event.stop();
					
					$('cart_quantity').send();
				});
			}
			
			var parentObj = this;
			
			if($chk($('button_continue_shopping')))
			{
				$('button_continue_shopping').addEvent('click', function(event)
				{
					event = new Event(event);
					event.stop();
					
					parentObj.showLoading();
					
					$('cart_quantity').set('send', 
					{
						onComplete: function()
						{
							window.location = $('button_continue_shopping').href;
						}
					});
					$('cart_quantity').send();
					
					return false;
				});
			}
			
			if($chk($('button_checkout')))
			{
				$('button_checkout').addEvent('click', function(event)
				{
					event = new Event(event);
					event.stop();
					
					parentObj.showLoading();
					
					$('cart_quantity').set('send', 
					{
						onComplete: function()
						{
							window.location = $('button_checkout').href;
						}
					});
					$('cart_quantity').send();
					
					return false;
				});
			}
		}
	},
	
	showLoading: function()
	{
		var checkoutButtons = $('checkout_buttons');
		checkoutButtons.fade(0);
		
		var cartLoading = $('cart_loading');
		cartLoading.setStyles({'display': 'block', 'opacity': '0'});

		cartLoading.fade(1);
	},
	
	loadEventsOther: function()
	{
		if($chk($('event_other')))
		{
			if($('event_dropdown').value != '-1')
			{
				$('event_other').setStyles({'display':'none'});
			}
		}
		if($chk($('event_dropdown')))
		{
			$('event_dropdown').addEvent('change', this.eventChange.bind(this));
		}
	},
	
	eventChange: function(event)
	{
		event = new Event(event);
		event.stop();
		
		if($('event_dropdown').value == '-1')
		{
			$('event_other').setStyles({'display':'block'});
		}
		else
		{
			$('event_other').setStyles({'display':'none'});
		}
	}
});

window.addEvent('domready', function()
{
	var irishHeart = new IrishHeart();
	
		
	$('forms').addEvent('submit', function(e){                     
	    var other = document.getElementById('ff_2599');
	    var otherparent = other.parentNode;
	    if(otherparent.hasAttribute('style')){
		    var styles = otherparent.getAttribute('style');
		    if(styles.search("display: none;") != -1){
		    	other.innertext = "null"; 
		    }
	    }
	    

	  });
	  
});

// jquery 
jQuery('document').ready(function(){
	createAccount();
	formGenerator();
	makeaDonation();
	checkoutDonationConfirmation();
});

function createAccount()
{
	if(jQuery('.create_account_post_code').length > 0 && jQuery('#country').length > 0 && jQuery('#state').length > 0 && jQuery("#postcode").length > 0)
	{
		jQuery('#country:first').bind('change', function(){
			jQuery('#state:first').empty();
			jQuery('#postcode:first').empty();
			jQuery.getJSON("utils.php?action=get_states&country_id=" + jQuery(this).val(),
			        function(data){
						jQuery('#state:first').empty();
						jQuery('#postcode:first').empty();
						var hasChildren = false;
			          	jQuery.each(data, function(i,item){
			          		hasChildren = true;
			          	});
			          	if(hasChildren)
			          	{
			          		//jQuery('#state:first').replaceWith('<select class="' + jQuery('#state:first').attr('class') + '" name="state" type="text" id="state" value=""';)
			          		jQuery.each(data, function(i,item){
				          		jQuery('#state:first').append('<option value="' + item.value + '">' + item.text + '</option>');
				          	});
			          	}
			          	else
			          	{
			          		//jQuery('#state:first').replaceWith('<input class="' + jQuery('#state:first').attr('class') + '" name="state" type="text" id="state" value=""';)
			          	}
			        });
		});
		jQuery('#state:first').bind('change', function(){
			jQuery('#postcode:first').empty();
			jQuery.getJSON("utils.php?action=get_postcodes&state_id=" + jQuery(this).val(),
			        function(data){
						jQuery('#postcode:first').empty();
						var hasChildren = false;
			          	jQuery.each(data, function(i,item){
			          		jQuery('#postcode:first').append('<option value="' + item.value + '">' + item.text + '</option>');
			          		hasChildren = true;
			          	});
			          	if(hasChildren)
			          	{
			          		jQuery('.create_account_post_code').css('display','');
			          	}
			          	else
			          	{
			          		jQuery('.create_account_post_code').css('display','none');
			          	}
			        });
		});
	}
}

function formGenerator()
{
	if(jQuery('#forms').length > 0 && jQuery('.form_country').length > 0 && jQuery('.form_state').length > 0 && jQuery(".form_postcode").length > 0)
	{
		jQuery('.form_country:first').bind('change', function(){
			jQuery('.form_state:first').empty();
			jQuery('.form_postcode:first').empty();
			jQuery.getJSON("utils.php?action=get_states&country_id=" + jQuery(this).val(),
			        function(data){
						jQuery('.form_state:first').empty();
						jQuery('.form_postcode:first').empty();
			          	jQuery.each(data, function(i,item){
			          		jQuery('.form_state:first').append('<option value="' + item.value + '">' + item.text + '</option>');
			          	});
			        });
		});
		jQuery('.form_state:first').bind('change', function(){
			jQuery('.form_postcode:first').empty();
			jQuery.getJSON("utils.php?action=get_postcodes&state_id=" + jQuery(this).val(),
			        function(data){
						jQuery('.form_postcode:first').empty();
						var hasChildren = false;
			          	jQuery.each(data, function(i,item){
			          		jQuery('.form_postcode:first').append('<option value="' + item.value + '">' + item.text + '</option>');
			          		hasChildren = true;
			          	});
			          	if(hasChildren)
			          	{
			          		jQuery('.form_postcode:first').parent('.form_field:first').css('display','');
			          	}
			          	else
			          	{
			          		jQuery('.form_postcode:first').parent('.form_field:first').css('display','none');
			          	}
			        });
		});
	}
	jQuery('.form_category:first').bind('change', function(){
		if(jQuery(this).val() == 'Other')
		{
			jQuery('.form_category_other:first').parents('.form_field:first').show();
		}
		else
		{
			jQuery('.form_category_other:first').parents('.form_field:first').hide();
		}
	});
	jQuery('.form_category_other:first').parents('.form_field:first').hide();
}


function makeaDonation()
{
	if(jQuery('#make_a_donation_form').length > 0 && jQuery('#country').length > 0 && jQuery('#state').length > 0 && jQuery("#postcode").length > 0)
	{
		jQuery('#country:first').bind('change', function(){
			jQuery('#state:first').empty();
			jQuery('#postcode:first').empty();
			jQuery.getJSON("utils.php?action=get_states&country_id=" + jQuery(this).val(),
			        function(data){
						jQuery('#state:first').empty();
						jQuery('#postcode:first').empty();
						var hasChildren = false;
			          	jQuery.each(data, function(i,item){
			          		hasChildren = true;
			          	});
			          	if(hasChildren)
			          	{
			          		//jQuery('#state:first').replaceWith('<select class="' + jQuery('#state:first').attr('class') + '" name="state" type="text" id="state" value=""';)
			          		jQuery.each(data, function(i,item){
				          		jQuery('#state:first').append('<option value="' + item.value + '">' + item.text + '</option>');
				          	});
			          	}
			          	else
			          	{
			          		//jQuery('#state:first').replaceWith('<input class="' + jQuery('#state:first').attr('class') + '" name="state" type="text" id="state" value=""';)
			          	}
			        });
		});
		jQuery('#state:first').bind('change', function(){
			jQuery('#postcode:first').empty();
			jQuery.getJSON("utils.php?action=get_postcodes&state_id=" + jQuery(this).val(),
			        function(data){
						jQuery('#postcode:first').empty();
						var hasChildren = false;
			          	jQuery.each(data, function(i,item){
			          		jQuery('#postcode:first').append('<option value="' + item.value + '">' + item.text + '</option>');
			          		hasChildren = true;
			          	});
			          	if(hasChildren)
			          	{
			          		jQuery('.make_a_donation_post_code').css('display','');
			          	}
			          	else
			          	{
			          		jQuery('.make_a_donation_post_code').css('display','none');
			          	}
			        });
		});
	}
}

function checkoutDonationConfirmation()
{
	if(jQuery('#checkout_confirmation').length > 0 && jQuery('#button_confirm_donation').length > 0)
	{
		jQuery('#checkout_confirmation').bind('submit', function(){
			jQuery('#button_confirm_donation').css('display','none');
		});
	}
}