var undefined;
function getObj (elementName){
	if ( document.getElementById != undefined ) {
		return document.getElementById(elementName);
	} else {
		if ( document.all != undefined ) {
			return document.all[elementName]; 
		} else if ( document.layers != undefined ) {
			return document.layers[elementName];
		}
	}
}

function doCart( btn ){
	var action = getObj("cart_action");
	action.value = btn;
	getObj("form").submit();
}
function doQCart( btn ){
	var action = getObj("q_cart_action");
	action.value = btn;
	getObj("q_form").submit();
}

function selectall(all){
	var ary = document.getElementsByTagName("input");
	var limit = ary.length;

	for(var x = 0; x < limit; x++){
		var obj = ary[x];
		if(obj.type=="checkbox"){
			obj.checked = all;
		}
	}
}

function clearCart(btn){
	if(confirm("This action will empty your Shopping Cart. This action can NOT be undone.")){
		doCart( btn );
	}
}

function selectAddAlso(partId){
	var check = getObj("check_"+partId);
	check.checked = !check.checked;
}

function selectRadio(partId){
	var check = getObj("check_"+partId);
	check.checked = true;
}

function addSelected(check, id){
	var label1 = _.$("label_"+id+"-1");
	var label2 = _.$("row_"+id+"-2");
	var bg = (check.checked)? "url(/images/select-glow.png) repeat-x bottom" : "" ;
	label1.style.background = bg;
	label2.style.background = bg;
}

function showOptDetails( optId ){
	var detail = _.$("opt-detail-"+optId);
	var arrow = _.$("opt-arw-"+optId);
//	alert(detail.style.display);
	if(detail.style.display == 'block'){
		detail.style.display = 'none';
		arrow.src = "/images/arrow_right.png";
	}else{
		detail.style.display = 'block';		
		arrow.src = "/images/arrow_down.png";
	}
}

var unitPrice = 0.0
function updatePrice(checked,val){
	var price = _.$("priceHtml");
	unitPrice += (checked)? val : (-1*val);
	price.innerHTML = unitPrice.toFixed(2);
}

function commercial_hoverChair( newChair ){
	hoverChair( newChair );
}

function hoverChair( newChair ){
	var title = getObj("furnitureTitle");
	var price = getObj("furniturePrice");
	title.innerHTML = chairData[newChair].name;
	price.innerHTML = chairData[newChair].price;
}

function selectChair( newChair ){
	window.location.href = homeSite + 'products/' + newChair;
}

function str_repeat(i, m) { for (var o = []; m > 0; o[--m] = i); return(o.join('')); }

function sprintf () {
  var i = 0, a, f = arguments[i++], o = [], m, p, c, x;
  while (f) {
    if (m = /^[^\x25]+/.exec(f)) o.push(m[0]);
    else if (m = /^\x25{2}/.exec(f)) o.push('%');
    else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
      if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) throw("Too few arguments.");
      if (/[^s]/.test(m[7]) && (typeof(a) != 'number'))
        throw("Expecting number but found " + typeof(a));
      switch (m[7]) {
        case 'b': a = a.toString(2); break;
        case 'c': a = String.fromCharCode(a); break;
        case 'd': a = parseInt(a); break;
        case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
        case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
        case 'o': a = a.toString(8); break;
        case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
        case 'u': a = Math.abs(a); break;
        case 'x': a = a.toString(16); break;
        case 'X': a = a.toString(16).toUpperCase(); break;
      }
      a = (/[def]/.test(m[7]) && m[2] && a > 0 ? '+' + a : a);
      c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
      x = m[5] - String(a).length;
      p = m[5] ? str_repeat(c, x) : '';
      o.push(m[4] ? a + p : p + a);
    }
    else throw ("Huh ?!");
    f = f.substring(m[0].length);
  }
  return o.join('');
}

function insertImage(imgStr,obj,size,currX,currY,cellW,cellH,imgId){
	if(obj.innerHTML.length > 8) return '';
	var src = imgStr;
//	src = sprintf(src,size,currX,currY,cellW,cellH,imgId);
	src = sprintf(src,imgId,"-1",size,currX,currY,cellW,cellH);
	obj.innerHTML = '<img src="'+src+'" />';
	return src;
}

function thumbMoveListener(e){
	e = _.fixEventObject( e );
	
	loadPoints( e.offsetX , e.offsetY );
}

function loadPoints(mx,my){
	
	var magX = mx * ratio;
	var magY = my * ratio;

	var currCol = Math.floor(magX / cellW);
	var currRow = Math.floor(magY / cellH);

	var colX1 = (currCol * cellW);
	var colX2 = colX1 + cellW;
	
	var magX1 = (magX - (magW/2));
	var magX2 = (magX + (magW/2));
	
	var colY1 = (currRow * cellH);
	var colY2 = colY1 + cellH;
	
	var magY1 = (magY - (magH/2));
	var magY2 = (magY + (magH/2));
	
	setPosition(mx,my);
	
	loadPostion(currCol,currRow);
	
	if(colX1 > magX1){
		loadPostion(currCol -1 ,currRow);
	}
	
	if(colX2 < magX2){
		
		loadPostion(currCol + 1 ,currRow);
	}
	
	if(colY1 > magY1){
		loadPostion(currCol ,currRow - 1);
	}
	
	if(colY2 < magY2){
		loadPostion(currCol ,currRow + 1);
	}
	
	if(colY1 > magY1 && colX1 > magX1){
		loadPostion(currCol -1 ,currRow - 1);
	}
	
	if(colY2 < magY2 && colY2 < magY2){
		loadPostion(currCol + 1 ,currRow + 1);
	}
	
	if(colX1 > magX1 && colX2 < magX2){
		loadPostion(currCol -1 ,currRow + 1);
	}
	
	if(colX2 < magX2 && colY1 > magY1){
		
		loadPostion(currCol + 1 ,currRow - 1);
	}
	
}

function loadPostion(currCol,currRow){
	var currCell = _.$( sprintf(idStr,currCol,currRow));
	if(currCell == null) return;
	insertImage(	imgStr,
			currCell,
			fullSize,
			Math.ceil(currCol*cellW),
			Math.ceil(currRow*cellH),
			Math.ceil(cellW),
			Math.ceil(cellH),
			imgId
		);
}

function setPosition(mx,my){
	
	var diffH = Math.floor( magH / 2 );
	var diffW = Math.floor( magW / 2 );
	
	var glass = _.$("glass");
	
	var myfloor = Math.floor( (my || 0) * ratio );
	var mxfloor = Math.floor( (mx || 0) * ratio );
	
	glass.style.top = Math.min( ( (myfloor - diffH) * -1 ), 0 ) +"px";
	glass.style.left = Math.min( ( (mxfloor - diffW) * -1 ), 0 ) +"px";
}

function center(){	
	loadPoints(Math.floor(fullW/ratio/2) - 1,Math.floor(fullH/ratio/2));
}

function showMagGlass(){
	var chairPreview = _.$("chairPreview");
	chairPreview.style.display = "none";
	
	var magnifier = _.$("magnifier");
	magnifier.style.display = "block";
}

function hideMagGlass(){
	var magnifier = _.$("magnifier");
	magnifier.style.display = "none";
	var chairPreview = _.$("chairPreview");
	chairPreview.style.display = "block";
}

var CMS = function(){
	return {currInput : null};
}();

var popup = null;

function getValue(obj,winUrl){
popup = window.open(winUrl+"?select",'selWin','width=700,height=600,resizable=1,scrollbars=1,toolbar=0,status=1');
popup.parent.currInput = obj;

}

function setValue(value){
CMS.currInput.value = value;
CMS.currInput = null;
}

function selectValue(value){
parent.currInput.value = value;
window.close();
}

