/**
 * @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($) {
	$(window).load(function() {
		// Delay 1 more second to ensure all cient-side initialization is completed
		setTimeout(function() {
			var	form = $('.it-options form'),
				profileField = form.find('#profile');
	
			if (profileField.length) {
				// Handle settings change event
				form.find('input, select, textarea').change(function() {
					$(this).attr('id') == 'profile' || (form[0].__changed__ = true);
				});

				// Handle profile change action
				profileField.change(function(event) {
					if (form[0].__changed__ && confirm('Do you want to save changes for the current profile before switching to the ' + $(profileField[0].options[profileField[0].selectedIndex]).text() + ' profile?')) {
						// Reset referer
						var referer = form.find('input[name="_wp_http_referer"]');
						referer.val(referer.val().replace(/&settings-profile=[^&]+/, '') + '&settings-profile=' + profileField.val());
	
						// Submit the form
						form.submit();
					} else {
						window.location.href = window.location.href.replace(/&settings-profile=[^&]+/, '') + '&settings-profile=' + profileField.val();
					}
				});
			}
		}, 1000);
	});
})(jQuery);
