Build = {

	changeStep: function(step) {
		try {
			$('step').value = step;
			$('workshop').submit();
		} catch (e) {};
	},

	setArtworkType: function(artwork_type_id) {
		try {
			if (artwork_type_id == 'mirror') {
				$('artwork_theme_id').value = '';
			}
			$('workshop').submit();

		} catch (e) {};
	},

	setStartFrom: function(step) {
		try {
			window.location = window.location.pathname +'?step='+step;
		} catch (e) {};
	},

	resetPresets: function(build_step) {
		try {
			$('reset_presets').disabled = false;
			$('reset_presets').value = 't';
			Build.current.process(build_step);
		} catch (e) {};
	},

	setPresetCategoryId: function(val, build_step) {
		try {
			if (val.length > 0) {
				$('private').value = 'false';
				if ($('artwork_type').value == 'my_artwork') {
					$('artwork_type').value = '';
					$('part_artwork').value = '';
					$('part_artwork_user').value = '';
				}
			} else {
				$('private').value = 'true';
			}
			//Build.current.process(build_step);
		} catch (e) {};
	},

	removePreset: function(val) {
		try {
			$('remove_preset_id').value = val;
			$('workshop').submit();
		} catch (e) {};
	},

	setArtworkUserDelete: function(iid) {
		try {
			if (confirm("Are you sure you would like to delete?")) {
				$('part_artwork_user_delete').value = iid;
				if ($('part_artwork_user').value == iid) {
					$('part_artwork_user').value = '';
				}
				Build.current.process('artwork');
			}
		} catch (e) {};
	},

	current: {

		workshop_preset_id: 0,

		show: function(panel) {
			try {
				panels = ['artwork','matte','frame','finish'];
				panels.each(function(p) {
					$('build_' + p).hide();
					$('panel_select_'+p).removeClassName('workshop_panel_selected');
				});
				$('build_' + panel).show();
				$('panel_select_' + panel).addClassName('workshop_panel_selected');

				$('build_step').value = panel;

				var old_panel = this.panel;

				return old_panel;
			} catch (e) {};
		},

		process: function(panel) {
			try {

				$('build_step').value = panel;

				$('workshop').submit();

			} catch (e) {};
		},

		popupdiv: function(element_id, show) {
			if (show) {
				$(element_id).show();
			} else {
				$(element_id).hide();
			}
		},

		refreshUserUpload: function(img_id) {
			try {
				$('artwork_user_upload_finish').value = 'true';
				Build.current.setPart('artwork_user', img_id);
				Build.current.process('artwork');
			} catch (e) {};
		},

		setPart: function(part_type, val) {
			try {
				$('part_'+part_type).value = val;
				switch(part_type) {
					case 'artwork':
						$('part_artwork_user').value = '';
						if ($('artwork_type').value == 'mirror') {
							$('part_matte').value = '';
							$('part_matte_2').value = '';
							$('part_glass').value = 'none';
						}
						Build.current.process('artwork');
						break;

					case 'artwork_user':
						$('part_artwork').value = '';
						Build.current.process('artwork');
						break;

					case 'matte':
						Build.current.process('matte');
						break;

					case 'matte_2':
						Build.current.process('matte');
						break;

					case 'frame':
						Build.current.process('frame');
						break;

					case 'glass':
						$('part_glass_none').value = '';
						Build.current.process('finish');
						break;
				}


			} catch (e) {};
		},

		setMatteOption: function(val) {
			try {
				switch (val) {
					case 'one':
						$('part_matte_2').value = '';
						$('offset').value = '';
						break;
					case 'two':
						break;
					case 'none':
						$('part_matte').value = '';
						$('part_matte_2').value = '';
						$('pad_left').value = '0';
						$('pad_right').value = '0';
						$('pad_top').value = '0';
						$('pad_bot').value = '0';
						$('offset').value = '0';
						break;
				}
				Build.current.process('matte');
			} catch (e) {};
		},

		setWorkshopPresetId: function(val) {
			try {
				$('workshop_preset_id').value = val;
				$('template_'+val).addClassName('workshop_build_sel');
				if (this.workshop_preset_id > 0) {
					$('template_'+this.workshop_preset_id).removeClassName('workshop_build_sel');
				}
				this.workshop_preset_id = val;
				$('btn_continue').disabled = false;
			} catch (e) {};
		},

		showDesc: function(part_type, val) {
			try {
				if ($(part_type+'_desc_link_'+val).innerHTML == 'more') {
					$(part_type+'_desc_link_'+val).innerHTML = 'less';
					$(part_type+'_desc_'+val).show();
				} else {
					$(part_type+'_desc_link_'+val).innerHTML = 'more';
					$(part_type+'_desc_'+val).hide();
				}
			} catch (e) {};
		},

		adjustAllPadding: function(new_pad, max_pad_width, max_pad_height) {
			try {
				var adjusted = false;
				var req_pad_width = parseFloat(new_pad) * 2;
				if (req_pad_width > max_pad_width) {
					// Our required padding exceeds the max so let's set at closest possible to max
					var pad_side = max_pad_width / 2;
					pad_side = parseInt(pad_side);
					// because our selections are in increments of five we need a value that is divisible by 5
					for (var i = pad_side; i <= pad_side; i--) {
						if (i % 5 == 0) {
							pad_side = i;
							break;
						}
						if (i == 0) {
							pad_side = 0;
							break;
						}
						if (i < parseInt($('pad_side').options[1].value)) {
							pad_side = 0;
							break;
						}
					}
					$('pad_side').value = pad_side;
					$('pad_left').value = pad_side;
					$('pad_right').value = pad_side;
					adjusted = true;
				} else {
					$('pad_side').value = new_pad;
					$('pad_left').value = new_pad;
					$('pad_right').value = new_pad;
				}

				var req_pad_height = parseFloat(new_pad) * 2;
				if (req_pad_height > max_pad_height) {
					// Our required padding exceeds the max so let's set at closest possible to max
					var pad_vert = max_pad_height / 2;
					pad_vert = parseInt(pad_vert);
					// because our selections are in increments of five we need a value that is divisible by 5
					for (var i = pad_vert; i <= pad_vert; i--) {
						if (i % 5 == 0) {
							pad_vert = i;
							break;
						}
						if (i == 0) {
							pad_vert = 0;
							break;
						}
					}
					$('pad_top').value = pad_vert;
					$('pad_bot').value = pad_vert;
					adjusted = true;
				} else {
					$('pad_top').value = new_pad;
					$('pad_bot').value = new_pad;
				}

				if (adjusted) {
					//alert('Your padding has been automatically adjusted due to size restrictions of the selected mat board.');
				}
				$('pad_all').value = '';
				Build.current.process('matte');
			} catch (e) {};
		},

		adjustPaddingOptionsOffset: function(new_offset, old_offset, max_pad_width, max_pad_height) {
			try {
				var adjusted = false;
				var req_pad_width = (parseInt($('pad_side').value) * 2) + (parseInt(new_offset) * 2);
				var new_max_pad_width = max_pad_width + (old_offset * 2);
				if (req_pad_width > new_max_pad_width) {
					// take away our required offset from the max to get our new padding
					var pad_side = new_max_pad_width - (parseInt(new_offset) * 2);
					pad_side = pad_side / 2;
					pad_side = parseInt(pad_side);
					// because our selections are in increments of five we need a value that is divisible by 5
					for (var i = pad_side; i <= pad_side; i--) {
						if (i % 5 == 0) {
							pad_side = i;
							break;
						}
					}
					// select at least the minimum
					if (pad_side < parseInt($('pad_side').options[1].value)) {
						pad_side = parseInt($('pad_side').options[1].value);
					}

					var curr_pad_side = parseInt($('pad_side').value);
					if (curr_pad_side != pad_side) {
						adjusted = true;
					}
					$('pad_side').value = pad_side;
					$('pad_left').value = pad_side;
					$('pad_right').value = pad_side;
				}

				var req_pad_height = (parseInt($('pad_top').value) + parseInt($('pad_bot').value)) + (parseInt(new_offset) * 2);
				var new_max_pad_height = max_pad_height + (old_offset * 2);
				if (req_pad_height > new_max_pad_height) {
					// take away our required offset from the max to get our new padding
					var pad_vert = new_max_pad_height - (parseInt(new_offset) * 2);
					pad_vert = pad_vert / 2;
					pad_vert = parseInt(pad_vert);
					// because our selections are in increments of five we need a value that is divisible by 5
					for (var i = pad_vert; i <= pad_vert; i--) {
						if (i % 5 == 0) {
							pad_vert = i;
							break;
						}
					}
					var pad_top = pad_vert;
					var pad_bot = pad_vert;
					// set at least the minimum
					if (pad_top < parseInt($('pad_top').options[1].value)) {
						pad_top = parseInt($('pad_top').options[1].value);
					}
					// set at least the minimum
					if (pad_bot < parseInt($('pad_bot').options[1].value)) {
						pad_bot = parseInt($('pad_bot').options[1].value);
					}

					var curr_pad_top = parseInt($('pad_top').value);
					if (curr_pad_top != pad_top) {
						adjusted = true;
					}
					$('pad_top').value = pad_top;

					var curr_pad_bot = parseInt($('pad_bot').value);
					if (curr_pad_bot != pad_bot) {
						adjusted = true;
					}
					$('pad_bot').value = pad_bot;

				}

				if (adjusted) {
					//alert('Your padding has been automatically adjusted due to size restrictions of the selected mat board.');
				}
				Build.current.process('matte');
			} catch (e) {};
		},

		adjustPaddingOptions: function(pad, element1, element2, max_pad) {
			try {
				if (element1 == 'pad_left' && element2 == 'pad_right') {
					$(element1).value = pad;
					$(element2).value = pad;
				} else {
					var diff = max_pad - pad;
					/* check if difference is less then element2, if so adjust element 2 */
					if (diff < parseFloat($(element2).value)) {
						$(element1).value = pad;
						// because our selections are in increments of five we need a difference that is divisible by 5
						for (var i = diff; i <= diff; i--) {
							if (i % 5 == 0) {
								diff = i;
								break;
							}
							if (i == 0) {
								diff = 0;
								break;
							}
						}
						$(element2).value = diff;
						//alert('Your padding has been automatically adjusted due to size restrictions of the selected mat board.');
					}
				}
				Build.current.process('matte');
			} catch (e) {};
		},

		setFrame: function(val, rebate) {
			try {
				var curr_frame_rebate = parseFloat($('frame_rebate').value);
				$('frame_rebate').value = rebate;

				// we need to adjust our padding in case it is at max size
				if (rebate > curr_frame_rebate) {
					var rebate_diff = rebate - curr_frame_rebate;
					var pad_left = parseInt($('pad_left').value) - rebate_diff;
					var pad_right = parseInt($('pad_right').value) - rebate_diff;
					var pad_top = parseInt($('pad_top').value) - rebate_diff;
					var pad_bot = parseInt($('pad_bot').value) - rebate_diff;

					// because our selections are in increments of five we need a value that is divisible by 5
					for (var i = pad_left; i <= pad_left; i--) {if (i % 5 == 0) {pad_left = i;break;}}
					for (var i = pad_right; i <= pad_right; i--) {if (i % 5 == 0) {pad_right = i;break;}}
					for (var i = pad_top; i <= pad_top; i--) {if (i % 5 == 0) {pad_top = i;break;}}
					for (var i = pad_bot; i <= pad_bot; i--) {if (i % 5 == 0) {pad_bot = i;break;}}

					// now adjust the options
					$('pad_left').value = pad_left;
					$('pad_right').value = pad_right;
					$('pad_top').value = pad_top;
					$('pad_bot').value = pad_bot;
					//alert('Your mat board padding has been automatically adjusted to allow for the selected frame.');
				}
				Build.current.setPart('frame', val);
			} catch (e) {}
		},

		setPage: function(obj, pid) {
			try {
				var hash = obj.hash;
				var next = hash.substring(7);
				$(pid).value = next;
			} catch (e) {}
		},

		setArtworkOption: function(artwork_option) {
			try {
				/* if we currently have mirror and our artwork option
					 is not purchase then we want to select something else and mirror will
					 not be available
				*/
				if (artwork_option != 'purchase') {
					if ($('artwork_type').value == 'mirror') {
						$('artwork_type').value = $('artwork_type').options[0].value;
					}
				}
				if (artwork_option == 'purchase_artwork') {
					$('part_frame').value = '';
					$('part_glass').value = '';
					$('part_matte').value= '';
					$('part_matte_2').value = '';
					$('frame_rebate').value = '';
					$('pad_left').value = '';
					$('pad_right').value = '';
					$('pad_top').value = '';
					$('pad_bot').value = '';
					$('offset').value = '';
				}

				Build.current.process('artwork');
			} catch (e) {}
		},

		setArtworkType: function(artwork_type) {
			try {
				if (artwork_type == 'my_artwork' || artwork_type == 'mirror') {
					$('artwork_theme').selectedIndex = '';
					if (artwork_type == 'mirror') {
						$('quality').value = 'standard';
					}
				}
				Build.current.process('artwork');
			} catch (e) {}
		},

		setArtworkTheme: function(artwork_theme) {
			try {
				Build.current.process('artwork');
			} catch (e) {}
		},

		setArtworkArtist: function(artwork_artist) {
			try {
				Build.current.process('artwork');
			} catch (e) {}
		},

		setNoGlass: function() {
			try {
				$('part_glass_none').value = '1';
				$('part_glass').value = '';
				Build.current.process('finish');
			} catch (e) {}
		},

		submitButton: function(btn) {
			try {
				$(btn).click();
			} catch (e) {}
		},

		nextStep: function(current_step, mirror) {
			try {
				switch (current_step) {
					case 'artwork':
						if (mirror) {
							Build.current.process('frame');
						} else {
							Build.current.process('matte');
						}
						break;
					case 'matte':
						Build.current.process('frame');
						break;
					case 'frame':
						Build.current.process('finish');
						break;
				}
			} catch (e) {}
		}

	}
}


