/**
 * @version    $Id$
 * @package    IT_Theme_Framework
 * @author     InnoThemes Team <support@innothemes.com>
 * @copyright  Copyright (C) 2012 InnoThemes.com. All Rights Reserved.
 * @license    GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
 *
 * Websites: http://www.innothemes.com
 */

(function($) {
	// Initialize inline radio image
	$.IT_RadioImageInline = function() {
		// Init inline radio image
		$('.it-form-field-radio-image-inline img').each(function(i, e) {
			$(e).unbind('click').bind('click', function(event) {
				event.preventDefault();

				// De-select previously selected radio image
				$(this).parent().parent().find('img.it-form-field-radio-image-selected').removeClass('it-form-field-radio-image-selected')
				.prev().find('input').removeAttr('checked');

				// Select clicked radio image
				$(this).addClass('it-form-field-radio-image-selected').prev().find('input').attr('checked', 'checked');

				// Trigger click and change events on the hidden radio input
				$(this).prev().find('input').trigger('click').trigger('change');
			});
		});
	};

	// Declare class to support modalized radio image
	$.IT_RadioImageModal = function(params) {
		var self = this;

		// Initialize parameters
		self.params = $.extend({}, params);

		// Initialize functionality
		self.init();
	};

	$.IT_RadioImageModal.prototype = {
		init: function() {
			var self = this;

			$('.it-form-field-radio-image-modal a.btn').unbind('click').bind('click', function(event) {
				event.preventDefault();

				if ($(event.target).hasClass('disabled')) {
					return;
				}

				// Setup radio image modal
				$.IT_RadioImageModal.modal = $('#' + $(this).parent().parent().parent().attr('id') + '-radio-image-modal').dialog({
					modal: true,
					autoOpen: false,
					draggable: false,
					resizable: false,
					dialogClass: 'jsn-master',
					width: 800,
					height: 600,
					open: function() {
						// Setup event handler to select image
						$.IT_RadioImageModal.modal.find('a.thumbnail').unbind('click').bind('click', function(event) {
							event.preventDefault();

							// Clear current highlighting
							$(this).parent().parent().find('a.thumbnail.it-form-field-radio-image-selected').removeClass('it-form-field-radio-image-selected');

							// Set highlighting for selected image
							$(this).addClass('it-form-field-radio-image-selected');

							// Parse data from href attribute
							var d = $(this).attr('href').split('::');

							// Store selected image's associated value
							self.select(d[0], d[1]);
						});
					},
					close: function() {
						$.IT_RadioImageModal.modal.dialog('destroy');
					}
				});

				$.IT_RadioImageModal.modal.dialog('open');
			});

			// Handle window resize event to update modal position
			$(window).resize(function() {
				if ($.IT_RadioImageModal.modal) {
					$.IT_RadioImageModal.modal.dialog('option', 'position', 'center');
				}
			});
		},

		select: function(id, val) {
			var f = $(id).find('a.btn').parent().prev();

			// Set field value
			f.val(val);
			f.prev().val(val);

			// Hide radio image modal
			$.IT_RadioImageModal.modal.dialog('close');

			// Trigger change event on updated field
			f.trigger('change');
		}
	};
})(jQuery);
