// Choose Print Type Class
// -
var ChoosePrintType = {
	
	init: function() {
		
		var tags = $$('#print_genre input.radio');
		tags.forEach( function (t) {
			t.onclick = function() {
					ChoosePrintType.toggle(t);
				}
		});
		
	},
	
	toggle: function(t) {
		
		var c = $(t).getProperty('class');
		c = c.replace(/radio/, '');
		c = c.trim();
		id = c.replace(/print_type_/, '');
		
		$('print_type_1').setStyle('display', 'none');
		$('print_type_2').setStyle('display', 'none');
		$('print_type_3').setStyle('display', 'none');
		
		$('print_type_preview_1').setStyle('display', 'none');
		$('print_type_preview_2').setStyle('display', 'none');
		$('print_type_preview_3').setStyle('display', 'none');
		
		$(c).setStyle('display', 'block');
		$('print_type_preview_'+id).setStyle('display', 'block');
		
		switch(id) {
			
			case '1':
			case '3':
				
				// Check and rebuild Selects
				this.checkSelects(c);
				
				$('p_t_p_treatment_1').innerHTML = default_treatment[0];
				$('p_t_p_print_pos_1').innerHTML = default_print_positions[0];
				$('p_t_p_treatment_3').innerHTML = default_treatment[0];
				$('p_t_p_print_pos_3').innerHTML = default_print_positions[0];
				
				// If is Text, change Film Type
				if (id == '3') {
				
					// Rebuild Select
					$('OrderItemFilmType').length = 0;
					for (a=0;a<text_film_print.length;a++) {
						var option = new Element('option', {
								'value': text_film_print[a]
							});
						option.innerHTML = text_film_print[a];
						$('OrderItemFilmType').adopt(option);
					}
				
					// Change Text
					$('p_t_p_film_print_3').innerHTML = $('OrderItemFilmType').options[0].value;
					
					// Check for empty text & Active Buy Button
					if ($('CustomizationText').getProperty('value') == '') {
						$('buy_btn').setStyle('display', 'none');
						$('buy_btn_disabled').setStyle('display', 'block');
						$('text_warning').setStyle('display', 'block');
					}
					else
					{
						$('buy_btn').setStyle('display', 'block');
						$('buy_btn_disabled').setStyle('display', 'none');
						$('text_warning').setStyle('display', 'none');
					}
					
				}
				else
				{
					
					// Rebuild Select
					$('OrderItemFilmType').length = 0;
					for (a=0;a<default_film_print.length;a++) {
						var option = new Element('option', {
								'value': default_film_print[a]
							});
						option.innerHTML = default_film_print[a];
						$('OrderItemFilmType').adopt(option);
					}

					// Change Text
					$('p_t_p_film_print_1').innerHTML = $('OrderItemFilmType').options[0].value;
					$('p_t_p_film_print_2').innerHTML = $('OrderItemFilmType').options[0].value;
					$('p_t_p_film_print_3').innerHTML = $('OrderItemFilmType').options[0].value;
					
					// Disable Button if the Treatment is equal to Ricamo
					if ($('OrderItemTreatment').options[$('OrderItemTreatment').selectedIndex].value == 'Ricamo')
					{
						$('buy_btn').setStyle('display', 'none');
						$('buy_btn_disabled').setStyle('display', 'block');
					}
					else
					{
						$('buy_btn').setStyle('display', 'block');
						$('buy_btn_disabled').setStyle('display', 'none');
					}
				
				}
				
				if ($('OrderItemTreatment').options[$('OrderItemTreatment').selectedIndex].value == 'Stampa Transfer') {
					// Show Select
					$('filmPrintType').setStyle('display', 'block');
				}
				else
				{
					// Hide Select
					$('filmPrintType').setStyle('display', 'none');
					
					$('p_t_p_film_print_1').innerHTML = 'Non Necessario';
					$('p_t_p_film_print_2').innerHTML = 'Non Necessario';
					$('p_t_p_film_print_3').innerHTML = 'Non Necessario';
				}
				
			break;
			
			// Only for Image from Catalog
			case '2':
				
				// Set Default Data for the image selected
				var tag = $$('#print_type_2 .choose_image a.selected');
				tag.forEach( function (t) {
					var elm = $(t).getProperty('class');
					id = elm.replace(/anchor/, '');
					id = id.replace(/selected/, '');
					id = id.trim();
					
					// Change Treatment and Print Position
					var temp_print_position = eval('print_positions_'+id);
					$('OrderItemPrintPosition').options.length = 0;
					for (a=0;a<temp_print_position.length;a++) {
						
						for (b=0;b<default_print_positions.length;b++) {

							if (default_print_positions[b] == temp_print_position[a]) {
								var option = new Element('option', {
										'value': temp_print_position[a]
									});
								option.innerHTML = temp_print_position[a];
								$('OrderItemPrintPosition').adopt(option);
							}

						}
						
						
					}

					var temp_treatment = eval('treatment_'+id);
					$('OrderItemTreatment').options.length = 0;
					for (a=0;a<temp_treatment.length;a++) {
						
						for (b=0;b<default_treatment.length;b++) {

							if (default_treatment[b] == temp_treatment[a]) {
								var option = new Element('option', {
										'value': temp_treatment[a]
									});
								option.innerHTML = temp_treatment[a];
								$('OrderItemTreatment').adopt(option);
							}

						}
						
					}

					$('p_t_p_treatment_2').innerHTML = temp_print_position[0];
					$('p_t_p_print_pos_2').innerHTML = temp_treatment[0];
					
				});
				
				if ($('OrderItemTreatment').options[$('OrderItemTreatment').selectedIndex].value == 'Stampa Transfer') {
					// Rebuild Select
					$('OrderItemFilmType').length = 0;
					for (a=0;a<default_film_print.length;a++) {
						var option = new Element('option', {
								'value': default_film_print[a]
							});
						option.innerHTML = default_film_print[a];
						$('OrderItemFilmType').adopt(option);
					}
				
					// Change Text
					$('p_t_p_film_print_1').innerHTML = $('OrderItemFilmType').options[0].value;
					$('p_t_p_film_print_2').innerHTML = $('OrderItemFilmType').options[0].value;
					$('p_t_p_film_print_3').innerHTML = $('OrderItemFilmType').options[0].value;
					
					// Show Select
					$('filmPrintType').setStyle('display', 'block');
				}
				
				// Active Buy Button
				$('buy_btn').setStyle('display', 'block');
				$('buy_btn_disabled').setStyle('display', 'none');
				
			break;
			
		}
	},
	
	checkSelects: function(c) {
		
		$('OrderItemPrintPosition').options.length = 0;
		for (a=0;a<default_print_positions.length;a++) {
			var option = new Element('option', {
					'value': default_print_positions[a]
				});
			option.innerHTML = default_print_positions[a];
			$('OrderItemPrintPosition').adopt(option);
		}
		
		$('OrderItemTreatment').options.length = 0;
		for (a=0;a<default_treatment.length;a++) {
			var option = new Element('option', {
					'value': default_treatment[a]
				});
			option.innerHTML = default_treatment[a];
			$('OrderItemTreatment').adopt(option);
		}
		
	}
	
}

// Select Color Class
// -
var SelectColor = {
	
	init: function() {
		
		var tags = $$('#product_detail a.color');
		
		tags.forEach( function (t) {
			t.onclick = function() {
					SelectColor.chooseThis(t);
					return false;
				}
		});
		
	},
	
	chooseThis: function(t) {
		
		// Remove Selected Class
		var tags = $$('#product_detail a.color');
		tags.forEach( function (tag) {
			tag.removeClass('selected');
		});
		
		var elm = $(t).getProperty('rel');
		id = elm.replace(/color_/, '');
		
		$('color_'+id).setProperty('checked', 'checked');
		$('color_item_'+id).addClass('selected');
		
		// Change Color Text in Preview
		$('p_t_p_color_1').innerHTML = $('color_'+id).getProperty('value');
		$('p_t_p_color_2').innerHTML = $('color_'+id).getProperty('value');
		$('p_t_p_color_3').innerHTML = $('color_'+id).getProperty('value');
		
		// Change Big Image
		$('productImage').setProperty('src', eval('product_photo_'+id));
	}
	
}

// Choose Image form Catalog
//
var ChooseImage = {
	
	init: function() {
		
		var tags = $$('#print_type_2 .choose_image a');
		tags.forEach(function(t) {
			t.onclick = function() {
				ChooseImage.chooseThis(t);
				return false;
			}
		});
		
	},
	
	chooseThis: function(tag) {
		
		// Get ID
		var elm = $(tag).getProperty('class');
		id = elm.replace(/anchor/, '');
		id = id.replace(/selected/, '');
		id = id.trim();
		
		// De-Check all Radios
		var tags = $$('#print_type_2 .choose_image input');
		tags.forEach(function(t) {
			$(t).setProperty('checked', '');
		});
		
		// De-Select all Images
		var tags = $$('#print_type_2 .choose_image a');
		tags.forEach(function(t) {
			$(t).removeClass('selected');
		});
		
		// Select this Image
		$(tag).addClass('selected');
		$('image'+id).setProperty('checked', 'checked');
		
		// Change Preview Image
		$('preview_image').setProperty('src', '/'+eval('preview_image_'+id));
		$('preview_image_anchor').setProperty('href', '/'+eval('preview_image_'+id));
		
		// Change Treatment and Print Position
		var temp_print_position = eval('print_positions_'+id);
		$('OrderItemPrintPosition').options.length = 0;
		for (a=0;a<temp_print_position.length;a++) {
			
			for (b=0;b<default_print_positions.length;b++) {
				
				if (default_print_positions[b] == temp_print_position[a]) {
					var option = new Element('option', {
							'value': temp_print_position[a]
						});
					option.innerHTML = temp_print_position[a];
					$('OrderItemPrintPosition').adopt(option);
				}
				
			}
			
		}
		
		var temp_treatment = eval('treatment_'+id);
		$('OrderItemTreatment').options.length = 0;
		for (a=0;a<temp_treatment.length;a++) {
			
			for (b=0;b<default_treatment.length;b++) {
				
				if (default_treatment[b] == temp_treatment[a]) {
					var option = new Element('option', {
							'value': temp_treatment[a]
						});
					option.innerHTML = temp_treatment[a];
					$('OrderItemTreatment').adopt(option);
				}
				
			}
			
		}
		
		$('p_t_p_treatment_2').innerHTML = temp_print_position[0];
		$('p_t_p_print_pos_2').innerHTML = temp_treatment[0];
		
		// Change value for $('CustomizationImageId') input
		$('CustomizationImageId').setProperty('value', $('image'+id).getProperty('value'));
		
		// Control for Ricamo Type
		if ($('OrderItemTreatment').options[$('OrderItemTreatment').selectedIndex].value == 'Ricamo')
		{
			$('p_t_p_film_print_1').innerHTML = 'Non Necessario';
			$('p_t_p_film_print_2').innerHTML = 'Non Necessario';
			$('p_t_p_film_print_3').innerHTML = 'Non Necessario';
			
			$('filmPrintType').setStyle('display', 'none');
		} else {
			$('p_t_p_film_print_1').innerHTML = $('OrderItemFilmType').options[$('OrderItemFilmType').selectedIndex].value;
			$('p_t_p_film_print_2').innerHTML = $('OrderItemFilmType').options[$('OrderItemFilmType').selectedIndex].value;
			$('p_t_p_film_print_3').innerHTML = $('OrderItemFilmType').options[$('OrderItemFilmType').selectedIndex].value;
			
			$('filmPrintType').setStyle('display', 'block');
		}
	}
	
}

// ProductPage functions
//
ProductPage = {
	
	cat_image_current_page: 1,
	cat_image_item_x_page: 12,
	cat_image_tot_images: 0,
	cat_image_tot_pages: 0,
	galleryRequest: null,
	
	init: function() {
		// Classes
		window.addEvent('domready', ChooseImage.init);
		window.addEvent('domready', SelectColor.init);
		window.addEvent('domready', ChoosePrintType.init);
		
		// Text Example
		$('getTextExample').addEvent('click', function() {
			
			// Preloading
			$('textExample').setProperty('src', '/img/site/loading.gif');
			
			// Generate Text Example
			var textRequest = new Request({
				url: '/products/saveTextInSession',
				data: 'data[Text]='+$('CustomizationText').value
			});
			textRequest.send();
			textRequest.onSuccess = function(responseText) {
				var currentTime = new Date();
				var font = $('CustomizationFont').options[$('CustomizationFont').selectedIndex].value;
				
				var backcolor = 'ffffff';
				var textcolor = '000000';
				
				switch($('CustomizationFontColor').options[$('CustomizationFontColor').selectedIndex].value) {
					case 'Bianco':
						backcolor = '000000';
						textcolor = 'ffffff';
					break;
					case 'Nero':
						backcolor = 'ffffff';
						textcolor = '000000';
					break;
					
					case 'Verde':
						backcolor = 'ffffff';
						textcolor = '0f9b00';
					break;
					case 'Rosa':
						backcolor = '000000';
						textcolor = 'ffbea7';
					break;
					case 'Giallo':
						backcolor = '000000';
						textcolor = 'fff600';
					break;
					case 'Rosso':
						backcolor = 'ffffff';
						textcolor = 'ff0000';
					break;
					case 'Blue':
						backcolor = 'ffffff';
						textcolor = '0054ff';
					break;
					case 'Arancio':
						backcolor = 'ffffff';
						textcolor = 'ff5a00';
					break;
					case 'Oro':
						backcolor = '000000';
						textcolor = '927016';
					break;
					case 'Argento':
						backcolor = '000000';
						textcolor = 'd1d1d1';
					break;
				}
				
				$('textExample').setProperty('src', '/products/create_text/'+font+'/'+textcolor+'/'+backcolor+'/'+currentTime);
			}
			
			return false;
		});
		
		// Text Customization
		if ($('CustomizationText'))
		{
			$('CustomizationText').addEvent('keyup', function() {
				$('p_t_p_text').innerHTML = $('CustomizationText').getProperty('value');
				
				// If is empty, Disable Buy Button
				if ($('CustomizationText').getProperty('value') == '')
				{
					$('text_warning').setStyle('display', 'block');
					$('buy_btn').setStyle('display', 'none');
					$('buy_btn_disabled').setStyle('display', 'block');
				}
				else
				{
					$('text_warning').setStyle('display', 'none');
					$('buy_btn').setStyle('display', 'block');
					$('buy_btn_disabled').setStyle('display', 'none');
				}
			});
		}
		
		// Font Customization
		if ($('CustomizationFont'))
		{
			$('CustomizationFont').addEvent('change', function() {
				$('p_t_p_font_3').innerHTML = $('CustomizationFont').options[$('CustomizationFont').selectedIndex].innerHTML;
			});
		}
		
		// Font Customization Color
		if ($('CustomizationFontColor'))
		{
			$('CustomizationFontColor').addEvent('change', function() {
				$('p_t_p_font_color_3').innerHTML = $('CustomizationFontColor').options[$('CustomizationFontColor').selectedIndex].innerHTML;
			});
		}
		
		// Treatment
		$('OrderItemTreatment').addEvent('change', function() {
			$('p_t_p_treatment_1').innerHTML = $('OrderItemTreatment').options[$('OrderItemTreatment').selectedIndex].value;
			$('p_t_p_treatment_2').innerHTML = $('OrderItemTreatment').options[$('OrderItemTreatment').selectedIndex].value;
			$('p_t_p_treatment_3').innerHTML = $('OrderItemTreatment').options[$('OrderItemTreatment').selectedIndex].value;
			
			if ($('OrderItemTreatment').options[$('OrderItemTreatment').selectedIndex].value != 'Stampa Transfer') {
				$('p_t_p_film_print_1').innerHTML = 'Non Necessario';
				$('p_t_p_film_print_2').innerHTML = 'Non Necessario';
				$('p_t_p_film_print_3').innerHTML = 'Non Necessario';
				
				$('filmPrintType').setStyle('display', 'none');
			} else {
				
				$('filmPrintType').setStyle('display', 'block');
				
				$('p_t_p_film_print_1').innerHTML = $('OrderItemFilmType').options[$('OrderItemFilmType').selectedIndex].value;
				$('p_t_p_film_print_2').innerHTML = $('OrderItemFilmType').options[$('OrderItemFilmType').selectedIndex].value;
				$('p_t_p_film_print_3').innerHTML = $('OrderItemFilmType').options[$('OrderItemFilmType').selectedIndex].value;
			}
			
			if ($('OrderItemTreatment').options[$('OrderItemTreatment').selectedIndex].value == 'Ricamo') {
				$('warning_no_print').setStyle('display', 'block');
			} else {
				$('warning_no_print').setStyle('display', 'none');
			}
			
			// Buy Button
			if ($('OrderItemTreatment').options[$('OrderItemTreatment').selectedIndex].value == 'Ricamo' &&
				$('OrderItemPrintGenrePesonalImage').checked)
			{
				$('buy_btn').setStyle('display', 'none');
				$('buy_btn_disabled').setStyle('display', 'block');
			}
			else
			{
				$('buy_btn').setStyle('display', 'block');
				$('buy_btn_disabled').setStyle('display', 'none');
			}
			
		});
		
		// Print Position
		$('OrderItemPrintPosition').addEvent('change', function() {
			$('p_t_p_print_pos_1').innerHTML = $('OrderItemPrintPosition').options[$('OrderItemPrintPosition').selectedIndex].value;
			$('p_t_p_print_pos_2').innerHTML = $('OrderItemPrintPosition').options[$('OrderItemPrintPosition').selectedIndex].value;
			$('p_t_p_print_pos_3').innerHTML = $('OrderItemPrintPosition').options[$('OrderItemPrintPosition').selectedIndex].value;
		});
		
		// Sizes
		$('OrderItemSize').addEvent('change', function() {
			$('p_t_p_size_1').innerHTML = $('OrderItemSize').options[$('OrderItemSize').selectedIndex].value;
			$('p_t_p_size_2').innerHTML = $('OrderItemSize').options[$('OrderItemSize').selectedIndex].value;
			$('p_t_p_size_3').innerHTML = $('OrderItemSize').options[$('OrderItemSize').selectedIndex].value;
		});
		
		// Film Type
		$('OrderItemFilmType').addEvent('change', function() {
			$('p_t_p_film_print_1').innerHTML = $('OrderItemFilmType').options[$('OrderItemFilmType').selectedIndex].value;
			$('p_t_p_film_print_2').innerHTML = $('OrderItemFilmType').options[$('OrderItemFilmType').selectedIndex].value;
			$('p_t_p_film_print_3').innerHTML = $('OrderItemFilmType').options[$('OrderItemFilmType').selectedIndex].value;
		});
		
		// Quantity
		$('OrderItemQty').addEvent('keyup', function() {
			$('p_t_p_qty_1').innerHTML = $('OrderItemQty').value;
			$('p_t_p_qty_2').innerHTML = $('OrderItemQty').value;
			$('p_t_p_qty_3').innerHTML = $('OrderItemQty').value;
		});
		
		// Images Pagination
		ProductPage.setImagePagination();
	},
	
	setImagePagination: function() {
		ProductPage.cat_image_tot_pages = Math.ceil(ProductPage.cat_image_tot_images/ProductPage.cat_image_item_x_page);
		
		if (ProductPage.cat_image_tot_pages > 1)
		{
			ProductPage.resetNavigationEvents();
		}
	},
	
	goNext: function() {
		ProductPage.removeNavigationEvents();
		ProductPage.cat_image_current_page++;
		ProductPage.performData();
	},
	
	goPrev: function() {
		ProductPage.removeNavigationEvents();
		ProductPage.cat_image_current_page--;
		ProductPage.performData();
	},
	
	performData: function() {
		
		// Overlay
		$('overlay_images').setStyle('display', 'block');
		
		var url = '/products/get_images/'+ProductPage.cat_image_product_slug+'/page:'+ProductPage.cat_image_current_page;
		
		ProductPage.galleryRequest = new Request({url: url});
		ProductPage.galleryRequest.send();
		ProductPage.galleryRequest.onSuccess = function( responseText ) {
			
			// Workaround for the Javascript Running after Ajax Loading
			// Replace the 'scripta' string with the correct 'string' in the <script> tag
			// Now I can insert the correct code in my XHTML with the corrected <script> tag
			// and I can evaluate this script to run the Class
			responseText = responseText.replace(/scripta/g, 'script')

			$('gallery_images_content').empty();
			$('gallery_images_content').set('html', responseText);
			responseText.stripScripts(true);
			ChooseImage.init();
			
			$('overlay_images').setStyle('display', 'none');
		}
		
		ProductPage.resetNavigationEvents();
	},
	
	resetNavigationEvents: function() {
		
		// Check for Limits
		if (ProductPage.cat_image_current_page+1 <= ProductPage.cat_image_tot_pages )
		{
			$('next').addEvent('click', function() {
				ProductPage.goNext();
				return false;
			});
		}
		
		if (ProductPage.cat_image_current_page-1 > 0 )
		{
			$('prev').addEvent('click', function() {
				ProductPage.goPrev();
				return false;
			});
		}
		
	},
	
	removeNavigationEvents: function() {
		$('prev').removeEvents();
		$('next').removeEvents();
		
		$('prev').addEvent('click', function() {
			return false;
		});
		$('next').addEvent('click', function() {
			return false;
		});
	}
}
window.addEvent('domready', ProductPage.init);