/**
* @version $Id: wishlist.js 47937 2011-04-28 20:49:27Z keith $
*/

jQuery(document).ready(function () {
	jQuery("#private_reg").click(function () {
		if(jQuery(this).is(":checked")) {
			jQuery("#group_info").hide("slow");
		}
	});
	jQuery(".select_action").change(function () {
		if (jQuery(this).is(":checked")) {
			jQuery(".bulk_add, .select_action").each(function () {
				jQuery(this).attr("checked", "checked");
			});
		} else {
			jQuery(".bulk_add, .select_action").each(function () {
				jQuery(this).removeAttr("checked");
			});
		}
	});
	jQuery("#public_reg").click(function () {
		if(jQuery(this).is(":checked")) {
			jQuery("#group_info").show("slow");
		}
	});
	jQuery("#share").click(function () {
		jQuery("#email_area").toggle("slow");
	});
	jQuery("#bulk_add").click(function () {
		jQuery("#masform :input[name='action']").val("add_selected_wishes_to_cart");
		jQuery("form[name='display']").submit();
	});

	/**
	* Wishlist/Gift Registry
	*/
	jQuery('#wishlist-list-table a.btn2-m').click(function () {
		var qty = jQuery(jQuery(this).parent().parent().children().get(4)).first().val();
		jQuery(location).attr('href', jQuery(this).attr('href') + '&qty='+qty);
	});
	jQuery('a.update-wish').click(function () {
		jQuery('form[name="masform"] input[name="action"]').val('update');
		jQuery('form[name="masform"]').submit();
		return false;
	});
	jQuery('a.move-wish').click(function () {
		var obj = this;
		jQuery.ajax({
			url: smarty_vars.secure_html_url,
			data: 'page=wishlists&template=move_wish&except='
				+ jQuery('form[name="masform"] input[name="id"]').val(),
			type: 'POST',
			dataType: 'html',
			success: function(o) {
				divinity.box.open(o, this);
				jQuery('#wish-move').live('click', function () {
					jQuery(location).attr(
						'href',
						smarty_vars.rel_html_url
							+ 'index.php?page=wishlists&action=move&id='
							+ jQuery('#group').val()
							+ '&wish_ids[0]=' + obj.hash.substr(1)
					);
				});
			}
		});
		return false;
	});

	jQuery('a.add-wish-to-cart').click(function () {
		var url = jQuery(this).attr('href');
		var data = parse_url(url);
		//console.log(jQuery('form[name="list_items"] input[name="wish[qty][' + data.params.id + ']"]').val());
		jQuery(location).attr(
			'href',
			url + '&qty='
			+ jQuery('form[name="list_items"] input[name="wish[qty][' + data.params.id + ']"]').val()
		);
		return false;
	});

	/**
	* Add to Wishlist/Gift Registry
	*/
	jQuery('#add-to-wishlist').click(function () {
		if (jQuery(this).is('.notlogged')) {
			divinity.box.error(
				'<p>You are not logged in, click <a href="'
					+ smarty_vars.secure_html_url + 'index.php?page=login&return_url=' + smarty_vars.return_url
					+ '">here</a> to log in.</p>',
				'Please Log in!'
			);
		} else if (jQuery(this).is('.onelist')) {
			jQuery(location).attr(
				'href',
				smarty_vars.rel_html_url
				+ 'index.php?page=wishlists&action=add&id='
				+ jQuery('form[name="display"] input[name="id"]').val()
				+ '&qty=' + jQuery('form[name="display"] input[name="qty"]').val()
			);
		} else {
			jQuery.ajax({
				url: smarty_vars.rel_html_url,
				data: 'page=wishlists&return_url=' + smarty_vars.request_uri,
				type: 'POST',
				dataType: 'html',
				success: function(o) {
					divinity.box.open(o, {title:'Add To Wishlist / Gift Registry'});
				}
			});
		}
	});
	jQuery('#wishlist').live('click', function () {
		jQuery(location).attr(
			'href',
			smarty_vars.rel_html_url
				+ 'index.php?page=wishlists&action=add&id='
				+ jQuery('form[name="display"] input[name="id"]').val()
				+ '&qty=' + jQuery('form[name="display"] input[name="qty"]').val()
				+ '&group=' + jQuery('#groups').val()
		);
	});

	/**
	* Create gift registry
	*/
	jQuery('form[name="masform"] input[name="coregistrant[first_name]"]').change(function () {
		if (jQuery(this).val()) {
			jQuery('#shipto_before_coregistrant').removeAttr('disabled');
			jQuery('#shipto_after_coregistrant').removeAttr('disabled');
		} else {
			jQuery('#shipto_before_coregistrant').removeAttr('checked');
			jQuery('#shipto_before_coregistrant').attr('disabled', 'disabled');
			jQuery('#shipto_after_coregistrant').removeAttr('checked');
			jQuery('#shipto_after_coregistrant').attr('disabled', 'disabled');
		}
	});

	jQuery('#coregistrant_same').change(function () {
		if (jQuery(this).is(':checked')) {
			jQuery('#coregistrant_address input, #coregistrant_address select').attr('disabled', 'disabled');
			jQuery('#coregistrant_address').hide();
		} else {
			jQuery('#coregistrant_address input, #coregistrant_address select').removeAttr('disabled');
			jQuery('#coregistrant_address').show();
		}
	});

	jQuery('form[name="masform"] input[name="shipto_before[alias]"]').change(function () {
		if (jQuery(this).val() == 'other') {
			jQuery('#shipto_before_address').show();
		} else {
			jQuery('#shipto_before_address').hide();
		}
	});

	jQuery('#addresses-registrant,#addresses-coregistrant,#addresses-shipto_before').change(function () {
		var type = jQuery(this).attr('id').substr(10);
		var offset = type.length + 1;
		var prefill = function (o) {
			jQuery('input[name^="'+type+'["], select[name^="'+type+'["]').each(function () {
				var field = jQuery(this).attr('name').substring(offset, jQuery(this).attr('name').length - 1);
				if (typeof(addresses[o.val()][field]) != 'undefined') {
					jQuery(this).val(addresses[o.val()][field]).trigger('change');
				}
			});
		}
		var clear = function (o) {
			for (var f in addresses[o.children()[1].value]) {
				jQuery('input[name="'+o.attr('id').substring(10)+'['+f+']"], select[name="'+o.attr('id').substring(10)+'['+f+']"]').val('');
			}
		}
		if (jQuery(this).val().length) {
			prefill(jQuery(this));
		} else {
			clear(jQuery(this));
		}
	});

	/**
	* Search gift registry
	*
	*/
	jQuery('form[name="gift_registry_search"]').submit(function () {
		if (jQuery('form[name="gift_registry_search"] select[name="registrant[state_id]"]').val() === '') {
			divinity.box.open('Please select a State!', {title: 'Error!'});
			return false;
		}
	});

});
