// JavaScript Document
var IE = document.all?true:false;
document.onmousemove = getMouseXY;
var tempX = 0;
var tempY = 0;
var pvw;
var pvc;

function _rollover(obj,r) { _assign(obj,obj.src,r); } function _assign(obj,s,r) { obj.onmouseover = function() { obj.src = r; }; obj.onmouseout = function() { obj.src = s; }; obj.border=0; obj.onload=''; }

function getMouseXY(e) {
	if (IE) { // grab the x-y pos.s if browser is IE
		if (document.documentElement)
		{
			tempX = event.clientX + document.documentElement.scrollLeft;
			tempY = event.clientY + document.documentElement.scrollTop;
		}
		else
		{
			tempX = event.clientX + document.body.scrollLeft;
			tempY = event.clientY + document.body.scrollTop;
		}
	}
	else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
	}
	if (tempX < 0){tempX = 0;}
	if (tempY < 0){tempY = 0;}
	return true;
}

function getRadioValue(objlist)
{
	var i;
	for(i=0;i<objlist.length;i++)
	{
	    if (objlist[i].checked) return objlist[i].value;
	}
	return undefined;
}

function findPos(obj) {
	return [tempX,tempY];
}


function make_drag(obj)
{
	new Draggable(obj, {starteffect:undefined, endeffect:undefined});
}

function make_dropable(obj)
{
	Droppables.add(obj,
	{
		accept:'dragable',
		onDrop: function(el)
		{
			new Effect.Shrink(el, { duration:0.5, afterFinish: function() { Element.remove(el); }});
			obj.src = '/images/trash_flush.gif';
		}
	});
}

function add_to_sandbox(img_id,url)
{
	//if (!confirm('Add to shopping cart?')) return;
	var pos2 = Position.cumulativeOffset($(img_id));
	var pos = Position.cumulativeOffset($('sandbox_bg'));

	var y = document.createElement('img');
	y.style.position='absolute';
	y.src = '/auto_crop.php?h=200&f='+url;
	y.style.left=pos2[0]+'px';
	y.style.top=pos2[1]+'px';
	document.body.appendChild(y);

	y.onload = function() {
	new Effect.Move(y,{duration:0.5, x:pos2[0], y:pos[1]+250,mode:'absolute',
	    afterUpdate:function() { Element.scrollTo(y); },
		afterFinish:function()
		{
		    //create_figure(url,pos2[0],y);
			$('sandbox_bg').appendChild(y);
			y.className = 'dragable';
			y.style.top = '250px';
			y.style.left = (pos2[0]-pos[0]) + 'px';
			make_drag(y);
		}
	});
	
	}
	return true;
}

function clear_sandbox()
{
	if (!confirm('Are you sure?')) return;
	
	replace_sandbox_bg('',0);
	var x = $('sandbox_bg').getElementsByTagName('img');
	for (i=0;i<x.length;)
	{
	    if (x[i].className=='dragable')
			Element.remove(x[i]);
	    else
	        i++;
	}
}

function show_l33t()
{
	var x = $('sandbox_bg').getElementsByTagName('img');
	var t = 'background: '+$('sandbox_bg').style.backgroundImage;
	for (i=0;i<x.length;i++)
	{
	    if (x[i].className=='dragable')
	    t += '\n['+i+'] left: '+x[i].style.left+' top: '+x[i].style.left+' src:'+x[i].src;
	}
	alert(t);
}

function replace_sandbox_bg(url)
{
	if (url=='')
		$('sandbox_bg').style.background = '';
	else
		$('sandbox_bg').style.background = "url('"+url+"') center bottom no-repeat";

	Element.scrollTo($('sandbox_bg'));
}

function pv(obj,pic,custom,subj,price, usprice)
{
	pvw = document.getElementById('preview');
	pvc = document.getElementById('preview_content');
	
	obj.className = 'mover';

	var pos = findPos(obj);
	var _x = (pos[0] - parseInt(pvw.style.width) - 50);
	if (_x < document.body.scrollLeft-5) _x = (pos[0] + 50);
	pvw.style.left = _x + 'px';

	var _y = (pos[1] - parseInt(pvw.style.height)/2);
	if (_y < document.body.scrollTop) _y = document.body.scrollTop;
	pvw.style.top = _y + 'px';

    var str = '<h3 style="margin:0;">' + subj;
    str += '<br>' + price + ' <sup>each';
    str += '<br><font color=#666666>' + '( approx. USD ' + usprice +' )</font>'; 
	if (custom > 1) str += ' set';
    str += '</sup></h3><img id=p1 src="/thumb.php?img='+pic+'&pad=1&h=360&w=280&cache=1">';
	str += '<br>Click the thumbnail to add to shopping cart.';

    pvc.innerHTML = str;
	pvw.style.display = '';
}

function spv()
{
	pvw = document.getElementById('preview');
	pvc = document.getElementById('preview_content');
	pvw.style.display = '';
}
function hpv(obj)
{
	pvw = document.getElementById('preview');
	pvc = document.getElementById('preview_content');
    obj.className = 'mout';
	pvw.style.display = 'none';
}

function load_tab(tab,t)
{
	for (i=0;i<10;i++)
	{
		if ($('t'+i)!=undefined)
		{
			$('t'+i).style.color = '#eee';
	    	$('t'+i).style.background = 'url(/images/tab0.jpg) left top no-repeat';
	    }
	}
    tab.style.color = '#fff';
    tab.style.background = 'url(/images/tab1.jpg) left top no-repeat';
	$('load').innerHTML = '<img src=/ui/clock.gif align=absmiddle> Loading...';
	new Ajax.Updater('load','load.php?t='+t+'&id='+tab.id);
}

function showtab(id)
{
	var d = document.getElementById(id);
	var alld = document.getElementsByTagName('div');
	var i;
	for (i=0;i<alld.length;i++)
	{
	    if (alld[i].className == 'mass')
	    {
	        alld[i].style.display = 'none';
	        document.getElementById('t'+alld[i].id).style.color = '#eee';
	        document.getElementById('t'+alld[i].id).style.background = 'url(/images/tab0.jpg) left top no-repeat';;
		}
	}
    d.style.display = '';
    document.getElementById('t'+id).style.color = '#fff';
    document.getElementById('t'+id).style.background = 'url(/images/tab1.jpg) left top no-repeat';
}

function submit_check()
{	var ff = $$('#photo');

	var j = 1;
	for(i=1;i<=ff.length*4;i=i+4)
	{
		if (eval("document.fp"+i))
		{

			if (eval("document.fp"+i+".uploaded.value == 0"))
			{
				alert('Please provide Front View photo for Figurine#'+j+'.');
				return false;
			}
			if (eval("document.fp"+(i+1)+".uploaded.value==0") || eval("document.fp"+(i+2)+".uploaded.value == 0") || eval("document.fp"+(i+3)+".uploaded.value == 0"))
			{
				if (!confirm('You did not provide all photos for Figurine#'+j+'. Providing more photos will greatly improve the similarity of the final product.\nClick OK if you want to continue.')) return false;
			}
		}
		j++;
	}
/*	
	if (document.fp1)
	{

		if (document.fp1.uploaded.value == 0)
		{
			alert('Please provide Front View photo for Figurine#1.');
			return false;
		}
		if (document.fp2.uploaded.value==0 || document.fp3.uploaded.value == 0 || document.fp4.uploaded.value == 0)
		{
			if (!confirm('You did not provide all photos for Figurine#1. Providing more photos will greatly improve the similarity of the final product.\nClick OK if you want to continue.')) return false;
		}
	}


	if (document.fp5)
	{
		  
		if (document.fp5.uploaded.value == 0)
		{
			alert('Please provide Front View photo for Figurine#2.');
			return false;
		}
		if (document.fp6.uploaded.value == 0 || document.fp7.uploaded.value == 0 || document.fp8.uploaded.value == 0)
		{
			if (!confirm('You did not provide all photos for Figurine#2. Providing more photos will greatly improve the similarity of the final product.\nClick OK if you want to continue.')) return false;
		}
	}

	if (document.fp9)
	{
		if (document.fp9.uploaded.value == 0)
		{
			alert('Please provide Front View photo for Figurine#3.');
			return false;
		}
		if (document.fp9.uploaded.value == 0 || document.fp10.uploaded.value == 0 || document.fp11.uploaded.value == 0)
		{
			if (!confirm('You did not provide all photos for Figurine#3. Providing more photos will greatly improve the similarity of the final product.\nClick OK if you want to continue.')) return false;
		}
	}

	if (document.fp13)
	{
		if (document.fp13.uploaded.value == 0)
		{
			alert('Please provide Front View photo for Figurine#4.');
			return false;
		}
		if (document.fp13.uploaded.value == 0 || document.fp14.uploaded.value == 0 || document.fp15.uploaded.value == 0)
		{
			if (!confirm('You did not provide all photos for Figurine#4. Providing more photos will greatly improve the similarity of the final product.\nClick OK if you want to continue.')) return false;
		}
	}
*/	return true;

}

function check_moq(obj, moq)
{
	if (parseInt(obj.value)<moq)
	{
		obj.value=moq;
		obj.focus();
		alert('Minimum order quantity is '+moq);
		return false;
	}
	return true;
}

function setc(formobj, value, obj)
{
	document.f1.elements[formobj].value = value;

	var div = document.getElementById('d'+formobj);
	var imgs = div.getElementsByTagName("img");
	for (i=0;i<imgs.length;i++)
	{
	    imgs[i].className = "def";
	}

	obj.className = "sel";
}

function check_jpg(v, c, pic)
{
	v = v.substring(v.length-3, v.length);
	v.toLowerCase();
	if (v != 'jpg' && v != 'JPG')
	{
		alert("The photo you selected is not a JPG file, please select a JPG image.");
		c.value = '';
		c.focus();
		return;
	}
	pic.src='ui/loading.gif';
	c.form.submit();
}

function show_customize(id)
{
	var mr = 'more' + id;
	if (document.getElementById(mr).style.display == 'none')
		document.getElementById(mr).style.display = '';
	else
		document.getElementById(mr).style.display = 'none';
}

function do_find()
{
	if ($('search_keywords').value=='') { alert('I won\'t do empty search ;)'); return; }

	for (i=0;i<10;i++)
	{
		if ($('t'+i)!=undefined)
		{
			$('t'+i).style.color = '#eee';
	    	$('t'+i).style.background = 'url(/images/tab0.jpg) left top no-repeat';
	    }
	}
	$('load').innerHTML = '<img src=/ui/clock.gif align=absmiddle> Loading...';
	//$('search_keywords').value = $('search_keywords').value.trim();
	new Ajax.Updater('load','load.php?'+Hash.toQueryString({ 'search':$('search_keywords').value }));
	
}
