	var group_prefix = "group_";
	var option_prefix= "option_";
	var select_group_prefix = 'select_group_';
	var slave_select_prefix = 'select_slave_';
	var group_key, option_key;
	var groups = new Object;	
	var zoom_img_link;
	var zoom_link;
	
	function dyn_prod_init() {
		zoom_img_link = document.getElementById('main_img_zoom_link');
		zoom_link = document.getElementById("zoom_link");
		if (!use_lightbox) {
			zoom_img_link.removeAttribute("href");
			zoom_link.removeAttribute("href");
			zoom_link.onclick = function() { 
				open_image_window(zoom_image.src, zoom_image.width+40, zoom_image.height+40);
			}
			zoom_img_link.onclick = function() { 
				open_image_window(zoom_image.src, zoom_image.width+40, zoom_image.height+40);
			}
		}
	}
	function open_image_window(image, width, height) 
	{	
		var imageWindow = window.open(image, "mywindow", "scrollbars=no,menubar=no,height=" + height + ",width=" + width + ",resizeable=yes,toolbar=no,status=no");
		if (window.focus) {
			imageWindow.focus();
		}
		return false;		
	}
	
	function validate_form(form) {		
		var fgroups = form.fgroup_ids.value.split(",");	
		for (var i=0; i<fgroups.length;i++) {
			var sel_gid = select_group_prefix + fgroups[i];
			var sel = document.getElementById(sel_gid);			
			var choice_name = groups[group_prefix + fgroups[i]].name;		
							
			if (sel.options[sel.selectedIndex].value == 0) {								
				alert(groups[group_prefix + fgroups[i]].error);
				return false;
			}	 
		}		
		return true;
	}
	function update_image(new_main_img, new_zoom_img) {
		main_image = new_main_img.src;
		zoom_image = new_main_img.width;
		var main = document.getElementById('product_main_img');
		main.src=new_main_img.src;
		main.width=new_main_img.width;
		main.height=new_main_img.height;
		zoom_image = new Image(new_zoom_img.width,new_zoom_img.height);
		zoom_image.src = new_zoom_img.src;	
		if (use_lightbox) {			
			zoom_link.href=zoom_image.src;
			zoom_img_link.href=zoom_image.src;
		}
			 	
	}
	
	function set_slave_options(group_id, option_id) {
		var group_options = get_group_options(group_id, option_id);		
		var slave_select_div = document.getElementById("slave_select_holder_" + group_id);	
		if (!slave_select_div) return;
		
		slave_select_div.innerHTML = '';
		var label = document.createElement("label");			
		label.innerHTML = group_options.slave.name; 
		
		var select_box = document.createElement("select");		
		select_box.id = "slave_select_" + group_id;
		select_box.name = 'slave_group[]';
				
		slave_select_div.appendChild(label);
		slave_select_div.appendChild(select_box);
		var slave_option_ids = group_options.slave_option_ids;
		var sgroup_id = group_options['slave']['group_id'];
		var slave_options = group_options.slave.options;		
		for (var i=0; i<slave_option_ids.length;i++) {			
			var slave_option = slave_options[slave_option_ids[i]];			
			var option_text = slave_option.text;			 
			var option_value = String(sgroup_id) + '|' + String(slave_option_ids[i]);			
			select_box.options[i] = new Option(option_text, option_value);
			if (slave_option.option_on) {
				select_box.options[i].selected = true;
			}
		}
		
	}
	
	function update_slave_selects(group_id) {
		var parent_id = select_group_prefix + group_id;		
		var parent_select = document.getElementById(parent_id);
		var selected_value = parent_select.options[parent_select.selectedIndex].value;
		
		if (selected_value != 0) {
			if (parent_select) {
				var parts  = selected_value.split('|');
				var option_value = parts[1];
						
				var group_options =get_group_options(group_id, option_value);
								
				if (group_options && group_options.slave) {								
					set_slave_options(group_id, option_value);
				}		
			}
		}		
	}
	
	function get_group_options(group_id, option_id) {
		if (group_id && option_id) {
			return groups[group_prefix + group_id][option_prefix + option_id];
		}
	}
		
	function update_images_by_group_option(group_id, option_id) {
		var group_option =get_group_options(group_id, option_id); 	
		if (group_option) {
			update_image(group_option['main_image'], group_option['zoom_image']);
		}		
	}	
	function update_option_text(group_options){
		var el = document.getElementById('group_option_text');
		if (el) 
		{
			el.innerHTML = group_options.title;
		}
	}
	function update_option_dependencies(group_id, option_value) {
		//todo:add logic to set selected thumbnail with a className of selected
		var option_parts = option_value.split('|');
		if (option_parts.length > 1) {
			var option_value = option_parts[1];
		}
	
		var group_options =get_group_options(group_id, option_value);			
		if (!group_options) {
			return;
		}
		set_slave_options(group_id, option_value);
		if (group_options.num_images > 0) {
			update_option_text(group_options);					
		
			update_images_by_group_option(group_id, option_value);
		}
		
	}
	
	function update_dependancies_from_image_selection(group_id, option_id) {
			
		var selid = select_group_prefix + group_id;				
		var selectBox = document.getElementById(selid);		
		var options = selectBox.options;
		update_images_by_group_option(group_id, option_id);
		for (var i=0; i<options.length;i++) {
			var val  = options[i].value.split('|');
			if (val.length > 1) {			
				val = val[1];
			}		
			if (val == option_id) {
				selectBox.selectedIndex = i;
				break;
			}
		}
		update_slave_selects(group_id);
		var group_options =get_group_options(group_id, option_id);			
		if (!group_options) {
			return;
		}
		if (group_options.num_images > 0) {
			update_option_text(group_options);
		}
	}
	
	function set_on_options() {
		var elements = document.getElementsByName('option_group[]');
		for (var i=0; i<elements.length;i++){
			var el = elements[i];					
			var selected_index=0;
			var j;
			for (j=0; j<el.options.length; j++) {
				if (el.options[j] != '0') {
					var parts = el.options[j].value.split('|');
					var group_option = get_group_options(parts[0], parts[1]);
					if (group_option && group_option.option_on) {							
						//elements[i].selectedIndex = j;
						//update_option_dependencies(parts[0], parts[1]);
						break;
					}
				}
			}							
		}
	}					