// ==================
// = add load event =
// ==================

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

// ==================
// = image swap out =
// ==================

function prepareSwapper(){
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("thumbs")) return false;
	var thumbs = document.getElementById("thumbs");
	var links = thumbs.getElementsByTagName("a");
	
	for (var i=0; i < links.length; i++) {
		links[i].onclick = function(){
			showPic(this);
			return showPic(this);
		}
	}
}

function showPic(whichpic) {
	if (!document.getElementById("productImg")) return false;
	var source = whichpic.getAttribute("href");
	var productImg = document.getElementById("productImg");
	productImg.setAttribute("src", source);
	return false;
}  

// =======================
// = reserve form button =
// ======================= 

// hide reserve button
function hideButton (){
	if (!document.getElementById("reserve")) return false;
	if (!document.getElementById("reserve_span")) return false;
	var reserveButton = document.getElementById("reserve");
	reserveButton.style.display = "none";
}

// insert javascript button
function addJavaButton (){
	if (!document.getElementById("reserve_span")) return false;
	var reserveSpan = document.getElementById("reserve_span");
	reserveSpan.innerHTML = "<a href=\"javascript:document.reserve_form.submit();\">BUY</a>";
}

addLoadEvent (prepareSwapper);
addLoadEvent (hideButton);
addLoadEvent (addJavaButton);