/**
 * @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($) {
	$.IT_SocialChannels = function(params) {
		var self = this;

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

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

	$.IT_SocialChannels.prototype = {
		init: function() {
			var self = this, btn = $('.it-form-field-social-channels a.btn');

			self.container = btn.closest('form');
			self.fields = $('#it-social-channels-modal > .form-horizontal');

			btn.unbind('click').bind('click', function() {
				// Setup modal to configure social channels
				$.IT_SocialChannels.modal = $('#it-social-channels-modal').dialog({
					modal: true,
					autoOpen: false,
					draggable: false,
					resizable: false,
					dialogClass: 'jsn-master',
					width: 800,
					height: 600,
					open: function() {
						// Append social channel configuration fields into modal so they can be edited
						$.IT_SocialChannels.modal.append(self.fields.removeClass('hidden'));
					},
					close: function() {
						$.IT_SocialChannels.modal.dialog('destroy');
						self.setup();
					}
				});

				// Show modal
				$.IT_SocialChannels.modal.dialog('open');
			});

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

			// Setup event handler to disable form submission when user press Enter while configuring social channels
			self.fields.find('input').keypress(function(event) {
				return event.keyCode != 13;
			});

			self.setup();
		},

		setup: function() {
			var	self = this,
				fields = $('.it-form-field-social-channels ul.it-items-list'),
				message = $('.it-form-field-social-channels p.it-social-channels-status'),
				initSortable = function() {
					fields.sortable({
						forceHelperSize: true,
						forcePlaceholderSize: true,
						placeholder: 'ui-state-highlight',
						axis: 'y',
						stop: function(event, ui) {
							ui.item.css({
								'position': '',
								'top': '',
								'left': ''
							});
						}
					});

					fields.disableSelection();
				};

			self.fields.find('input').each(function(i, e) {
				fields.each(function(i2, e2) {
					var cls = $(e).attr('id'), item = $(e2).find('li.' + cls);

					if ($(e).val() != '' && !item.length) {
						var name = $(e).attr('id').replace('it-social-channels-', '');

						$(e2).append(
							$('<li class="it-item ui-state-default ' + cls + '" />').append(
								$('<label class="checkbox-inline" for="' + $(e2).parent().attr('id') + '-' + name + '" />').html(
									'<input type="hidden" name="' + $(e2).parent().attr('data-name') + '[' + name + ']" value="0" />'
									+
									'<input id="' + $(e2).parent().attr('id') + '-' + name + '" type="checkbox" class="checkbox" onclick="jQuery(this).prev().val(this.checked ? 1 : 0);" />'
									+
									$(e).parent().prev().text()
								)
							)
						);
					} else if ($(e).val() == '' && item.length) {
						item.remove();
					}
				});
			});

			// Append social channel configuration fields into form so they can be saved
			self.container.append(self.fields.addClass('hidden'));

			// Show/hide elements
			if (fields.children('li.it-item').length) {
				message.addClass('hide');
				fields.parent().removeClass('hide');
			} else {
				message.removeClass('hide');
				fields.parent().addClass('hide');
			}

			initSortable();
		}
	};
})(jQuery);
