/* gm_gprint.js <?php
#   --------------------------------------------------------------
#   gm_gprint.js 2015-01-08 gm
#   Gambio GmbH
#   http://www.gambio.de
#   Copyright (c) 2015 Gambio GmbH
#   Released under the GNU General Public License (Version 2)
#   [http://www.gnu.org/licenses/gpl-2.0.html]
#   --------------------------------------------------------------
?>*/

<?php
include_once(get_usermod(DIR_FS_CATALOG . 'gm/javascript/jquery/plugins/ajaxfileupload/ajaxfileupload.js'));
?>

var c_mode = encodeURIComponent('<?php echo gm_prepare_string($this->v_data_array['GET']['mode']); ?>');

if(typeof(gm_session_id) == 'undefined')
{
	var gm_session_id = '<?php echo gm_prepare_string($this->v_data_array['GET']['XTCsid']); ?>';
}
gm_session_id = encodeURIComponent(gm_session_id);


var t_current_page = '<?php echo gm_prepare_string($this->v_data_array['GET']['current_page']); ?>';

var coo_surfaces_manager = null;
var coo_gprint_configuration = null;
var coo_cart_wishlist_manager = null;

<?php
include_once(get_usermod(DIR_FS_CATALOG . 'gm/javascript/gm_gprint_functions.js'));
include_once(get_usermod(DIR_FS_CATALOG . 'gm/javascript/GMGPrintConfiguration.js'));
include_once(get_usermod(DIR_FS_CATALOG . 'gm/javascript/GMGPrintSurfacesManager.js'));
include_once(get_usermod(DIR_FS_CATALOG . 'gm/javascript/GMGPrintSurfaces.js'));
include_once(get_usermod(DIR_FS_CATALOG . 'gm/javascript/GMGPrintElements.js'));
include_once(get_usermod(DIR_FS_CATALOG . 'gm/javascript/GMGPrintCartWishlistManager.js'));
?>

$(document).ready(function()
{
	coo_gprint_configuration = new GMGPrintConfiguration();
	coo_gprint_configuration.set_languages_id(<?php echo $_SESSION['languages_id'] ?>);
	coo_cart_wishlist_manager = new GMGPrintCartWishlistManager();
	window.coo_cart_wishlist_manager = coo_cart_wishlist_manager;
	
	<?php
	if(!empty($this->v_data_array['GET']['id']))
	{
		echo 'coo_surfaces_manager = new GMGPrintSurfacesManager(' . (int)$this->v_data_array['GET']['id'] . ', coo_gprint_configuration);';
		echo 'coo_surfaces_manager.load_surfaces_group(' . (int)$this->v_data_array['GET']['id'] . ', \'' . addslashes($this->v_data_array['GET']['product']) . '\');';
		echo '$(\'#details_cart_part\').show();';
	}
	?>
		
	if(t_current_page == 'ProductInfo' && $('#gm_gprint').attr('id') == 'gm_gprint')
	{
		$('#gm_wishlist_link').click(function()
		{
			$('#gm_gprint input[type="text"], #gm_gprint textarea').each(function()
			{
				var t_value = $(this).val();
				t_value = t_value.replace('€', '&euro;');
				$(this).val(t_value);
			});
			
			var t_product = '';
			
			var t_input_name = '';
			
			$('.gm_attr_calc_input').each(function()
			{
				t_input_name = $(this).attr('name');
			
				if(t_input_name.indexOf('id[') != -1)
				{
					if($(this).attr('type') == 'radio' && $(this).prop('checked') == true)
					{
						t_product += '{' + gm_gprint_clear_number(t_input_name) + '}' + $(this).val();
					}
					else if($(this).attr('type') != 'radio')
					{
						t_product += '{' + gm_gprint_clear_number(t_input_name) + '}' + $(this).val();
					}
				}
			});
			
			t_product_copy = t_product;
			t_input_name = $('#gm_gprint_random').attr('name');
			
			var t_product_random = '';
			t_product_random = '{' + gm_gprint_clear_number(t_input_name) + '}' + $('#gm_gprint_random').val();
			
			var t_opened_product = '<?php echo gm_prepare_string($this->v_data_array['GET']['product']); ?>';
						
			if((t_opened_product != '' && (t_opened_product.indexOf(t_product) == -1 || t_opened_product.indexOf(t_product_random) == -1)) || t_opened_product.substr(0, 4) == 'cart')
			{
				var t_random_number = Math.random() + '';
				t_random_number = t_random_number.substr(2, 6);
				
				if(t_random_number.substr(0, 1) == '0')
				{
					t_random_number = '1' + t_random_number.substr(1, 5);
				}
				$('#gm_gprint_random').attr('name', 'id[' + t_random_number + ']');
				
				var delete_file_id = '';
				var t_old_product = t_opened_product.replace(/wishlist_/g, '');
				t_old_product = t_old_product.replace(/cart_/g, '');
				
				var t_start = t_old_product.indexOf(t_product_random);
				var t_end = t_start+t_product_random.length;
				
				var t_new_product_part_1 = t_old_product.substring(0, t_start);
				var t_new_product_part_2 = t_old_product.substring(t_end);
				
				var t_new_product = t_new_product_part_1 + '{' + t_random_number + '}' + $('#gm_gprint_random').val() + t_new_product_part_2;

				var t_source = 'wishlist';
				if(t_opened_product.substr(0, 4) == 'cart')
				{
					t_source = 'cart';
				}
				
				$('.delete_file').each(function()
				{
					delete_file_id = $(this).attr('id');
					delete_file_id = delete_file_id.replace(/delete_file_/g, '');
					coo_cart_wishlist_manager.copy_file(delete_file_id, t_old_product, t_new_product, 'wishlist', t_source);
				});
			}
			
			coo_cart_wishlist_manager.get_customers_data(coo_surfaces_manager, 'wishlist');
		});
		
		$('#cart_quantity').submit(function()
		{
			return false;
		});

		$('#cart_button').click(function()
		{
			$('#gm_gprint input[type="text"], #gm_gprint textarea').each(function()
			{
				var t_value = $(this).val();
				t_value = t_value.replace('€', '&euro;');
				$(this).val(t_value);
			});
			
			var t_product = '';
			
			var t_input_name = '';
			
			$('.gm_attr_calc_input').each(function()
			{
				t_input_name = $(this).attr('name');
				
				if(t_input_name.indexOf('id[') != -1)
				{
					if($(this).attr('type') == 'radio' && $(this).prop('checked') == true)
					{
						t_product += '{' + gm_gprint_clear_number(t_input_name) + '}' + $(this).val();
					}
					else if($(this).attr('type') != 'radio')
					{
						t_product += '{' + gm_gprint_clear_number(t_input_name) + '}' + $(this).val();
					}
				}
			});
			
			t_product_copy = t_product;
			t_input_name = $('#gm_gprint_random').attr('name');

			var t_product_random = '';
			t_product_random = '{' + gm_gprint_clear_number(t_input_name) + '}' + $('#gm_gprint_random').val();
			
			var t_opened_product = '<?php echo gm_prepare_string($this->v_data_array['GET']['product']); ?>';
						
			if((t_opened_product != '' && (t_opened_product.indexOf(t_product) == -1 || t_opened_product.indexOf(t_product_random) == -1)) || t_opened_product.substr(0, 8) == 'wishlist')
			{
				var t_random_number = Math.random() + '';
				t_random_number = t_random_number.substr(2, 6);
				
				if(t_random_number.substr(0, 1) == '0')
				{
					t_random_number = '1' + t_random_number.substr(1, 5);
				}

				$('#gm_gprint_random').attr('name', 'id[' + t_random_number + ']');
				
				var delete_file_id = '';
				var t_old_product = t_opened_product.replace(/cart_/g, '');
				t_old_product = t_old_product.replace(/wishlist_/g, '');
				
				var t_start = t_old_product.indexOf(t_product_random);
				var t_end = t_start+t_product_random.length;
				
				var t_new_product_part_1 = t_old_product.substring(0, t_start);
				var t_new_product_part_2 = t_old_product.substring(t_end);
				
				var t_new_product = t_new_product_part_1 + '{' + t_random_number + '}' + $('#gm_gprint_random').val() + t_new_product_part_2;

				var t_source = 'cart';
				if(t_opened_product.substr(0, 8) == 'wishlist')
				{
					t_source = 'wishlist';
				}
				
				$('.delete_file').each(function()
				{
					delete_file_id = $(this).attr('id');
					delete_file_id = delete_file_id.replace(/delete_file_/g, '');
					coo_cart_wishlist_manager.copy_file(delete_file_id, t_old_product, t_new_product, 'cart', t_source);
				});
			}
			
			if(typeof(coo_dropdowns_listener) != 'undefined')
			{
				coo_dropdowns_listener.check_combi_status();
			}
			
			coo_gm_qty_ckeck = new GMOrderQuantityChecker();
			var t_gm_qty_check = coo_gm_qty_ckeck.check();

			if(t_gm_qty_check)
			{
				coo_cart_wishlist_manager.get_customers_data(coo_surfaces_manager, 'cart');
			}

			return false;
		});
	}
	else if(t_current_page == 'Cart')
	{
		$('#cart_quantity').submit(function(){
			return coo_cart_wishlist_manager.update_cart();
		});
	}
	else if(t_current_page == 'Wishlist' && $('#gm_update_wishlist').attr('id') == 'gm_update_wishlist' && $('#gm_wishlist_to_cart').attr('id') == 'gm_wishlist_to_cart')
	{
		var t_wishlist_delete_href = $('#gm_update_wishlist').attr('href');
		t_wishlist_delete_href = t_wishlist_delete_href.replace(/:/g, ':coo_cart_wishlist_manager.update_wishlist();');
		$('#gm_update_wishlist').attr('href', t_wishlist_delete_href);
		
		var t_wishlist_add_href = $('#gm_wishlist_to_cart').attr('href');
		t_wishlist_add_href = t_wishlist_add_href.replace(/:/g, ':coo_cart_wishlist_manager.wishlist_to_cart();');
		$('#gm_wishlist_to_cart').attr('href', t_wishlist_add_href);
	}

    if(typeof(coo_qty_input_resizer) == 'object')
	{
        coo_qty_input_resizer.init_binds();
    }

	$('.gm_gprint_surface').on('click', '*', function () {
		$(this).parent().find('*').removeClass('selected-customizer-element');
		$('#' + $(this).prop('id').replace('copy_', '')).addClass('selected-customizer-element');
	});
});

