function getXmlHttp() {
  xmlHttp = null;

  if (window.XMLHttpRequest) {
    xmlHttp = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
      xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
    }
  } else {
    xmlHttp = new XMLHttpRequest();
  }

  return xmlHttp;
}


function shadeBg(bgColor, opacity) {
  var flash = document.getElementsByTagName('embed');

  if (document.getElementById("bgShade")) {
    var bodyRef = document.getElementsByTagName("body").item(0);
    var div = document.getElementById("bgShade");
    bodyRef.removeChild(div);
    
    for (i = 0, n = flash.length; i < n; i++) {
      flash[i].style.display = 'none';
    }
  } else {
    var text = " ";
    var bodyRef = document.getElementsByTagName("body").item(0);

    bgDiv = document.createElement("DIV");
    divText = document.createTextNode(text);
    bgDiv.appendChild(divText);
    bgDiv.setAttribute("id", "bgShade");
    bodyRef.appendChild(bgDiv);

    div = document.getElementById("bgShade");
    div.style.backgroundColor = bgColor;
    div.style.zIndex = 100;

    if (opacity.indexOf(".") == -1) {
      div.style.opacity = "0." + opacity;
      div.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=' + opacity  + ')';
    } else {
      div.style.opacity = opacity;
      div.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=' + opacity * 100 + ')';
    }

    var h = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
    var winH = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;

    if (window.innerHeight && window.scrollMaxY) {
	yWithScroll = window.innerHeight + window.scrollMaxY;
	xWithScroll = window.innerWidth + window.scrollMaxX;
    } else if (document.body.scrollHeight > document.body.offsetHeight) {
	yWithScroll = document.body.scrollHeight;
	xWithScroll = document.body.scrollWidth;
    } else {
	yWithScroll = document.body.offsetHeight;
	xWithScroll = document.body.offsetWidth;
    }

    div.style.position = "absolute";
    div.style.left = 0;
    div.style.top = 0;
    div.style.width = bodyRef.offsetWidth + 'px';
    div.style.height = (winH + h) + 'px';
    div.style.height = yWithScroll + 'px';
  
    for (i = 0, n = flash.length; i < n; i++) {
      flash[i].style.display = 'none';
    }
  }
}

function openBox(boxid) {
  if (boxid == '' || !boxid) {
	var boxid = 1;
  }

  var bodyRef = document.getElementsByTagName("body").item(0);

  shadeBg('#000000', '70');

  var selects = document.getElementsByTagName('select');
  for (i = 0, n = selects.length; i < n; i++) {
    selects[i].style.display = 'none';
  }

  var main = document.createElement("DIV");
  main.setAttribute("id", "breakbox");
  main.style.width = 705 + 'px';
  main.style.zIndex = 150;
  main.style.position = 'absolute';
  main.style.left = ((bodyRef.offsetWidth / 2) - 365) + 'px';
  main.style.top = 100 + 'px';
  main.style.border = '1px solid #000000';
  main.style.backgroundColor = '#FFFFFF';
  main.style.padding = '5px 5px 5px 20px';
  main.style.overflow = 'auto';

  var close = document.createElement("DIV");
  close.setAttribute("id", "closebreak");
  close.innerHTML = '<input type="reset" value="   " style="background: url(\'/images/close.gif\'); border: 0; width: 23px; height: 23px; cursor: pointer" onClick="closeBox();">';
  close.setAttribute('style', 'float: right');

  var browser = navigator.appName;
  if (browser.indexOf('Microsoft') != -1) {
    close.style.styleFloat = 'right';
  }

  main.appendChild(close);

  if (boxid == 1) {
    var text = 'Alternate Views';
  } else {
    var text = 'Notify Me Form';
  }
  var header = document.createElement('DIV');
  header.innerHTML = '<span class="header" style="font-size: 22px; color: #fec7de; padding-left: 25px;">' + text + '</span><br /><img src="/images/underline.gif" style="position: relative; top: -5px;" />';
  header.style.padding = '7px 0 10px 0';
  main.appendChild(header);


  var inner = document.createElement("DIV");
  inner.setAttribute("id", "bbinner");
  inner.style.backgroundColor = '#FFFFFF';
  inner.style.padding = '1px';


	var xmlHttp = getXmlHttp();
	var xmlresponse = '';
	if (boxid == 1) {
		var file = '/viewerimg/imageviewer.php?mv_arg=' + document.getElementById('mainsku').value + '&dealer=' + document.getElementById('isdealer').value + '&sid=' + Math.random();
		xmlHttp.open('GET', file, true);
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				xmlresponse = xmlHttp.responseText;
  				inner.innerHTML = '<div id="flowplayerholder" style="text-align: center;">' + xmlresponse + '</div>';
			}
		}
		xmlHttp.send(null);
	} else if (boxid == 2) {
		var outstock = document.getElementById('outstock_values').value;
		var outstockoptions = '';
		var split = outstock.split('|~|');
		for (i = 0, n = split.length; i < n; i++) {
			var chunk = split[i].split('|!|');
			if (chunk.length == 2) {
				outstockoptions += '<option value="' + chunk[0] + '">' + chunk[1] + '</option>';
			}
		}
		inner.style.textAlign = 'center';
		inner.innerHTML = '<div class="bodytext" style="text-align: left;"><form action="/shop/restock_notify.html" method="post" onSubmit="return validateNotify(this);"><input type="hidden" name="return_url" value="' + document.getElementById('mainsku').value + '" />Product: <select name="mv_arg"><option value="" SELECTED>--Select One--</option>' + outstockoptions + '</select><br /><br />E-Mail Address: <input type="text" name="email" class="coutbox" /><br /><br /><input name="Submit" type="submit" class="blank_button" value="Submit"></form></div>';
	}

  inner.innerHTML += '<div style="clear: both;"></div>';

  main.appendChild(inner);
  bodyRef.appendChild(main);
}

function closeBox() {
  var bodyRef = document.getElementsByTagName("body").item(0);
  var box = document.getElementById("breakbox");
  bodyRef.removeChild(box);

  var selects = document.getElementsByTagName('select');
  for (i = 0, n = selects.length; i < n; i++) {
    selects[i].style.display = 'inline';
  }

  shadeBg();

}


var imagePos = 0;
var imageHeight = null;
var rotateInterval = null;
$('#prod-image').ready(function () { startRotate(); });
function switchImage( img ) {
   var largeImage = img.getAttribute('large');
   var mainImage  = document.getElementById('prod-image');
   mainImage.style.backgroundImage = 'url(' + largeImage + ')';
   mainImage.style.backgroundPosition = '0 0';

   imagePos = 0;
   imageHeight = null;

   startRotate();
   return false;
}
function startRotate( interval ) {
   if ( !interval ) var interval = 4000;
   clearInterval( rotateInterval );
   rotateInterval = setInterval( 'rotateImage()', interval );
}
function rotateImage() {
   var mainImage = document.getElementById('prod-image');
   if (!imageHeight) {
      var background = mainImage.style.backgroundImage;
      $.get('/fpcgi-bin/imageRotator.php', { image: background.substring( 4, background.indexOf(')') ) }, function( data ) {
         imageHeight = data;
      });
   }
   
   imagePos += 316;
   if ( imagePos >= imageHeight ) {
      mainImage.style.backgroundPosition = '0 0';
      imagePos = 0;
   } else {
      document.getElementById('prod-image').style.backgroundPosition = '0 -' + imagePos + 'px';
   }
}
    
function updateLarge(img) {
  alert(img);
  var lg = document.getElementById('large_image');
  lg.src = '/images/' + img;
  return false;
}

function validateNotify(form) {
	if (form.mv_arg.value == '') {
		alert('Please select the item from the drop down you would like to be notified of.');
		return false;
	} else if (!form.email.value.match(/^.+@.+\..{2,3}$/i)) {
		alert('Please enter a valid e-mail address we can contact you at.');
		return false;
	}

	return true;
}
