var remote_updated = false;
var time_limit = 0;
function remote_observer() {
  Element.hide('new_image_form');
  new Ajax.Updater('product_images_cnt_tmp','/product/ajax_images',{onComplete:function() {
      if (!remote_updated) { /* get current data  first */
		 $('product_images_cnt_tmp2').innerHTML = $('product_images_cnt_tmp').innerHTML;
		 remote_updated = true;
	  }
      if ($('product_images_cnt_tmp').innerHTML != $('product_images_cnt_tmp2').innerHTML || time_limit > 5) { 
        $('product_images_cnt').innerHTML = $('product_images_cnt_tmp2').innerHTML = $('product_images_cnt_tmp').innerHTML;
	    Element.hide('working_new_image');
		remote_updated = false;
	    time_limit = 0;
        return true;
      } else {
        setTimeout('remote_observer()',1000);
		time_limit += 1;
      }
																						
																						}});
}

function swap_img(id_1,id_2) {
 $(id_1).src = $(id_2).src;	
 $(id_1).alt = $(id_2).alt;
}

function fix_heights(className) {
  var containers = document.getElementsByClassName(className);
  var max_height = 0;
  var image_cache = [];
  for(i=0;i<containers.length;i++) {
	if(max_height < parseInt(containers[i].offsetHeight)) max_height = parseInt(containers[i].offsetHeight);  
	//Cache images 
	//image_cache[i] = new Image();
	//image_cache[i].src = containers[i].getElementsByTagName('img')[0].src;
  }
  for(i=0;i<containers.length;i++) {
	containers[i].style.height = max_height + 'px';  
  }
}
function showAddressCnt(el) {
	var value = el.options[el.selectedIndex].value;
	if (value == 'home') {
	  Element.hide('work_address_cnt','other_address_cnt');
	  Element.show('home_address_cnt');
	} else if (value == 'work') {
	  Element.hide('home_address_cnt','other_address_cnt');
	  Element.show('work_address_cnt');
	} else if (value == 'other') {
	  Element.hide('home_address_cnt','work_address_cnt');
	  Element.show('other_address_cnt');
	}
}
function page_prev(container) {
  var containers = $(container).getElementsByTagName('div');
  for(i=0;i<containers.length;i++) {
	  if (containers[i].style.display != 'none') {
	    Element.hide(containers[i]);
	    if (i != 0 ) {
		  Element.show(containers[i-1]);
		} else {
		  Element.show(containers[containers.length-1]);
		}
		break;
	  }
	}
}
function page_next(container) {
	var containers = $(container).getElementsByTagName('div');
	for(i=0;i<containers.length;i++) {
	  if (containers[i].style.display != 'none') {
	    Element.hide(containers[i]);
	    if (containers[i+1] != null) {
		  Element.show(containers[i+1]);
		} else {
		  Element.show(containers[0]);
		}
		break;
	  }
	}
}
function page_first(container) {
	Element.show($(container).getElementsByTagName('div')[0]);
}
