/* upgrades collection interfaces to use javascript */
function upgradeCollectionInterface() {

	// collection switcher
	$('#collection-switcher-btn').addClass('hidden');
	$('#collection-switcher').change(function() {
		$('#global-load-message-wrapper').show();
		$('#collection-switcher option:selected').each(function() {
			var val = $(this).attr('value');
			$(collection_form).submit();
			//location.href = 'my-collections?CollectionID='+val;
		});
	});

	$('#edit-title-btn').bind('click', function (e) {
		e.preventDefault();
		$('#title-view').css('display', 'none');
		$('#title-write').css('display', 'block');
	});

	$('#edit-title-save-btn').bind('click', function(e) {
		e.preventDefault();
		var new_title = $('#collection-edit-title-input').attr('value');
		$.post('ajax/collection_actions.php?action=edit_title', {'new_title' : new_title}, function (response) {
			var objResponse = eval('('+response+')');

			if (objResponse.status == 'success') {
				$('#collection-title-text').html(new_title);
				$('#edit-title-success').show();

				$('#title-write').css('display', 'none');
				$('#title-view').css('display', 'block');

				var collection_browser_title = '';
				var collection_title = new_title;
				var collection_title_length = collection_title.length;

				var collection_count = $('input[name=collection_member_count]').val();

				if (collection_title_length > 12)
					collection_browser_title = collection_title.substring(0, 12)+'...';

				$('#collection-browser-title').html(collection_browser_title);

				$('#collection-option-'+objResponse.collection_id).html(collection_title + ' (' + collection_count + ' Items)');
				$('#results-info-collection-title').html(collection_title);

				$('#edit-title-success').fadeOut(3000);
			}
		});
	});

	$('#edit-title-cancel-btn').bind('click', function(e) {
		e.preventDefault();
		$('#title-write').css('display', 'none');
		$('#title-view').css('display', 'block');
	});

	$('#max_to_show').bind('change', function () {
		$('#global-load-message-wrapper').show();
		$(collection_form).submit();
	});

	$('.collection-type-counts').bind('change', function () {
		var type_id = $(this).attr('value');
		$('#global-load-message-wrapper').show();
		$(collection_form).submit();
	});
}

$(function()
{
	// Handle the add to cart button
	// Display the proper dialog and info
	var cart_button = $('.collection-item-add_to_cart');

	if( cart_button.length )
	{
		cart_button
		.bind('click', function(e)
		{
			if( !$('div.pricing-tool').length )
				$('body').append('<div id="dialog" class="pricing-tool" style="display: none;"/>');

			// Find all checked items and determine the type (cartable or not)
			// and display a dialog with thumbs and title and explain each type
			// and the process for purchasing each
			var checked_boxes = $('input[type=checkbox]:checked');
			var item_data = new Object();
			//var last_ind = 0;
			checked_boxes.each(function(ind)
			{
				item_data['item_'+ $(this).val()] = $(this).val();
				//last_ind = (ind + 1);
			});

			//Now add any existing Non-Cartables to the item_data array
			if( window['stored_non_cartables'] != undefined )
			{
				$(stored_non_cartables)
				.each(function(ind)
				{
					item_data['item_'+ stored_non_cartables[ind]] = stored_non_cartables[ind];
				});
			}

			item_data['snt'] = true;

			// Add the items to the dialog and you are done
			$(document)
			.itemDialog.show('Add to Cart Verification Tool', 'Please wait...', true);

			// Send an AJAX request to get the items' data
			params = {snt:true, url:'ajax/custom_request_items.php', data:item_data};
			$(document).AjaxRequester(params);
			return false;
		});
	}
});
