mTID = null;
var globalHelp=true;
var helptimer=0;
var flagmouseover=false;
var gui = new GUI();

if(navigator.appName.indexOf('Microsoft') !=-1 ) {
	isIE = true;
} else {
	isIE = false
}
window.onresize=rinit;

function rinit() {
	mTID = setInterval('checkcontent()',1);
}

function checkcontent() {
	try {
		var k = getdom('maintbl');
		if(k) {
			if(isIE) {
				k.style.marginTop = (document.body.offsetHeight-k.offsetHeight-30)/2;
			} else {
				k.style.marginTop = (document.body.clientHeight-k.offsetHeight+30)/2;
			}
			clearInterval(mTID);
		}
	} catch(e) {};
}

function getdom(objid) {
	return (document.getElementById(objid));
}
function formsubmit(formname) {
	document.forms[formname].submit();
}
function removeAllChildren(tobj) {
	if(tobj.childNodes.length ) {
		while(tobj.childNodes.length) {
			tobj.removeChild(tobj.firstChild);
		}
	}
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent)	{
			curleft += obj.offsetLeft;
			if(!obj.offsetParent) {
				break;
			}
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent)	{
			curtop += obj.offsetTop;
			if(!obj.offsetParent) {
				break;
			}
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


/* the blur thing */

var blurOn = false;
var blurredObj = null;
var maskActive = false;

function showobject(objid) {
	
	if( blurOn) {
		return;
	}
	
	blurOn = true;
	blurredObj = objid;
	if(!isIE) {
		maskActive = true;
	}
	var tm = objid.substr(objid.length-1,objid.length);
	var adiv = document.createElement('div');
	document.body.appendChild(adiv);
	adiv.className = 'floater2active';
	adiv.id = 'mask'+tm;

	var q = getdom(objid);
	if( isIE ) {
		adiv.style.pixelTop = findPosY(q);
		adiv.style.pixelLeft = findPosX(q)+1;
	} else {
		adiv.style.top = findPosY(q)+1+'px';
		adiv.style.left = findPosX(q)+1+'px';
	}
	adiv.setAttribute('linked',q.childNodes[0].href);
	adiv.onmouseover = function() {
		maskActive = true;
	}
	adiv.onmouseout = function() {
		maskActive = false;
		removeAllMasks();
	}
	adiv.onclick = function() {
		document.location.href = this.getAttribute('linked');
	}
	// create the selected bag tag
	var itag = document.createElement('div');
	adiv.appendChild(itag);
	itag.className = 'floater2prodname';
	var itagvalue = document.createElement('p');
	itag.appendChild(itagvalue);
	itagvalue.className = 'twelve u';
	itag.appendChild(document.createTextNode(objalts[objid]));

		
	for(x=0; x < 6 ;x++) {
		q = getdom('object'+x);
		if(x != tm) {
			var ndiv = document.createElement('div');
			document.body.appendChild(ndiv);
			ndiv.className = 'floater2';
			ndiv.id = 'mask'+x;
			
			if( isIE ) {
				ndiv.style.pixelTop = findPosY(q);
				ndiv.style.pixelLeft = findPosX(q)+2;
			} else {
				ndiv.style.top = findPosY(q)+1+'px';
				ndiv.style.left = findPosX(q)+2+'px';
			}
		} //end if
		
	} // end for

	
}
function removeAllMasks() {
	if(maskActive) {
		return;
	}
	
	for(var i=0; i<6;i++) {
		try {
			q = getdom('mask'+i);
			q.parentNode.removeChild(q);
		} catch(e){ };
	}
	blurOn = false;
	blurredObj = null;
	maskActive = false;
}


function showAlt(objid) {
	
	if( blurOn) {
		return;
	}
	
	blurOn = true;
	blurredObj = objid;
	if(!isIE) {
		maskActive = true;
	}
	var tm = objid.substr(objid.length-1,objid.length);
	var adiv = document.createElement('div');
	document.body.appendChild(adiv);
	adiv.className = 'floater2active';
	adiv.id = 'mask'+tm;

	var q = getdom(objid);
	if( isIE ) {
		adiv.style.pixelTop = findPosY(q);
		adiv.style.pixelLeft = findPosX(q)+1;
	} else {
		adiv.style.top = findPosY(q)+1+'px';
		adiv.style.left = findPosX(q)+1+'px';
	}
	adiv.setAttribute('linked',q.childNodes[0].href);
	adiv.onmouseover = function() {
		maskActive = true;
	}
	adiv.onmouseout = function() {
		maskActive = false;
		removeAllMasks();
	}
	adiv.onclick = function() {
		document.location.href = this.getAttribute('linked');
	}
	// create the selected bag tag
	var itag = document.createElement('div');
	adiv.appendChild(itag);
	itag.onmouseover = function() {
		maskActive = true;
		return false;
	}
	itag.onmouseout = function() {
		maskActive = true;
		return false;
	}
	itag.className = 'floater2prodname';
	var itagvalue = document.createElement('p');
	itag.appendChild(itagvalue);
	itagvalue.className = 'twelve u';
	itag.appendChild(document.createTextNode(objalts[objid]));

/*
	for(x=0; x < 6 ;x++) {
		q = getdom('object'+x);
		if(x != tm) {
			var ndiv = document.createElement('div');
			document.body.appendChild(ndiv);
			ndiv.className = 'floater2';
			ndiv.id = 'mask'+x;
			
			if( isIE ) {
				ndiv.style.pixelTop = findPosY(q);
				ndiv.style.pixelLeft = findPosX(q)+2;
			} else {
				ndiv.style.top = findPosY(q)+1+'px';
				ndiv.style.left = findPosX(q)+2+'px';
			}
		} //end if
		
	} // end for
*/
	
}

function removeAlt() {
	if(maskActive) {
		return;
	}
	
	for(var i=0; i<6;i++) {
		try {
			q = getdom('mask'+i);
			q.parentNode.removeChild(q);
		} catch(e){ };
	}
	blurOn = false;
	blurredObj = null;
	maskActive = false;
}


function GUI() {
	this.setMozNode = function(nodetype, noderef) {
		noderef.type = nodetype;
		noderef.value = '';
		noderef.name = '';
		return noderef;
	}
	
	this.checkbox = function() {
		if(isIE) {
			var inode = document.createElement('<input type="checkbox" value="" name="">');
		} else {
			var inode = document.createElement('input');
			inode = this.setMozNode('checkbox',inode);
		}
		return inode;
	}
	
	this.button = function() {
		if(isIE) {
			var inode = document.createElement('<input type="button" value="" name="">');
		} else {
			var inode = document.createElement('input');
			inode = this.setMozNode('button',inode);
		}
		return inode;
	}
	
	this.hidden = function() {
		if(isIE) {
			var inode = document.createElement('<input type="hidden" value="" name="">');
		} else {
			var inode = document.createElement('input');
			inode = this.setMozNode('hidden',inode);
		}
		return inode;
	}
	
	this.text = function() {
		if(isIE) {
			var inode = document.createElement('<input type="text" value="" name="">');
		} else {
			var inode = document.createElement('input');
			inode = this.setMozNode('text',inode);
		}
		return inode;
	}
	
	this.select = function() {
		var inode = document.createElement('select');
		return inode;
  }
  
  this.radio = function() {
		if(isIE) {
			var inode = document.createElement('<input type="radio" value="" name="">');
		} else {
			var inode = document.createElement('input');
			inode = this.setMozNode('radio',inode);
		}
		return inode;
  }
  
} // end GUI


function showarticle(url) {
	window.open(url,'','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=900,height=732,screenX=100,screenY=50,top=50,left=100')
}
function showphoto(url) {
	window.open(url,'','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=900,height=770,screenX=100,screenY=50,top=50,left=100')
}

function selectLeather(objid, products_options_id, products_options_values_name) {
	var k = getdom('qtydiv');
	if (k.childNodes.length >= 5) {
		return;
	}
	var _obj = getdom(objid);
	var optid = parseInt(objid.substr(3, objid.length-1));
	
	// check if the leather already exists in the list
	if (k.childNodes.length > 0) {
		for(var x = 0; x < k.childNodes.length; x++) {
			var _tnode = k.childNodes[x].id;
			if (parseInt(_tnode.substr(2, _tnode.length-1)) == optid) {
				var _tsubnode = k.childNodes[x].childNodes[2];
				if (_tsubnode.selectedIndex < 9) {
					_tsubnode.options[_tsubnode.selectedIndex + 1].selected = true;
				}
				return;
			}
		}
	}
	
	var ndiv = document.createElement('div');
	k.appendChild(ndiv);
	ndiv.id = 'id' + optid;
	
	var productid_inode = gui.hidden();
	ndiv.appendChild(productid_inode);
	productid_inode.name = 'products_id[]';
	productid_inode.value = products_id + '{' + products_options_id + '}' + optid;
	
	var id_inode = gui.hidden();
	ndiv.appendChild(id_inode);
	id_inode.name = 'id[' + products_id + '{' + products_options_id + '}' + optid + '][' + products_options_id + ']';
	id_inode.value = optid;
	
	/*
	var check_inode = gui.radio();
	ndiv.appendChild(check_inode);
	check_inode.name= 'selected_bag';
	check_inode.className = 'check_inode';
	*/
	
	var inode = gui.select();
	ndiv.appendChild(inode);
	inode.name = 'cart_quantity[]';
	inode.setAttribute('optid', ndiv.id);
	inode.className = 'select_inode';
	
	for(var i = 0; i < 10 ; i++) {
		var nopt = document.createElement('option');
		inode.appendChild(nopt);
		nopt.value = i;
		nopt.text = i;
		if (i == 1) {
			nopt.selected = true;
		}
	}
	ndiv.appendChild(document.createTextNode('You have selected leather "' + products_options_values_name + '"'));
	
	inode.onchange = function() {
		if (this.options[this.selectedIndex].value == 0) {
			var _trdiv = getdom('id' + optid);
			_trdiv.parentNode.removeChild(_trdiv);
		}
	}
}

/* mouserover swatch */
function showhelp(elem, pid, pphoto, pname) {
	getdom(elem.id).className = 'swatch ton';
	var k = getdom("divhelp");
	var _offsetTop = 153;
	var _offsetLeft = -65;
	
	/*if (parseInt(_size(findPosX(elem), false)) > 800) {
		_offsetLeft = -155;
		k.style.backgroundImage='url(images/design/bubbleleft.png)';
	} else {
		k.style.backgroundImage='url(images/design/helper.png)';
	}*/
	
	if( isIE ) {
		k.style.pixelTop = _size(findPosY(elem) - _offsetTop, false);
		k.style.pixelLeft = _size(findPosX(elem) + _offsetLeft, false);
	} else {
		k.style.top = _size(findPosY(elem) - _offsetTop, true);
		k.style.left = _size(findPosX(elem) + _offsetLeft, true);
	}

	k.innerHTML = '<p>' + pname + '<br><img src="images/swatch/' + pphoto + '" width="150" height="113"><br>Click to select leather</p>';
	k.style.display="block";
	getdom(elem.id).onmouseout = function() {this.className = 'swatch';hidehelp();}
}
function hidehelp() {
		var k = getdom("divhelp");
		k.innerHTML="";
		k.style.display="none";
		return;
}

function _size(tsize, tflag) {
	var _tval = new String(tsize);
	if (tflag) {
		_tval += 'px';
	}
	return _tval;
}

